1 #include "filesys/file.h"
3 #include "filesys/inode.h"
4 #include "threads/malloc.h"
9 struct inode *inode; /* File's inode. */
10 off_t pos; /* Current position. */
11 bool deny_write; /* Has file_deny_write() been called? */
14 /* Opens a file for the given INODE, of which it takes ownership,
15 and returns the new file. Returns a null pointer if an
16 allocation fails or if INODE is null. */
18 file_open (struct inode *inode)
20 struct file *file = calloc (1, sizeof *file);
21 if (inode != NULL && file != NULL)
25 file->deny_write = false;
36 /* Opens and returns a new file for the same inode as FILE.
37 Returns a null pointer if unsuccessful. */
39 file_reopen (struct file *file)
41 return file_open (inode_reopen (file->inode));
46 file_close (struct file *file)
50 file_allow_write (file);
51 inode_close (file->inode);
56 /* Returns the inode encapsulated by FILE. */
58 file_get_inode (struct file *file)
63 /* Reads SIZE bytes from FILE into BUFFER,
64 starting at the file's current position.
65 Returns the number of bytes actually read,
66 which may be less than SIZE if end of file is reached.
67 Advances FILE's position by the number of bytes read. */
69 file_read (struct file *file, void *buffer, off_t size)
71 off_t bytes_read = inode_read_at (file->inode, buffer, size, file->pos);
72 file->pos += bytes_read;
76 /* Reads SIZE bytes from FILE into BUFFER,
77 starting at offset FILE_OFS in the file.
78 Returns the number of bytes actually read,
79 which may be less than SIZE if end of file is reached.
80 The file's current position is unaffected. */
82 file_read_at (struct file *file, void *buffer, off_t size, off_t file_ofs)
84 return inode_read_at (file->inode, buffer, size, file_ofs);
87 /* Writes SIZE bytes from BUFFER into FILE,
88 starting at the file's current position.
89 Returns the number of bytes actually written,
90 which may be less than SIZE if end of file is reached.
91 (Normally we'd grow the file in that case, but file growth is
93 Advances FILE's position by the number of bytes read. */
95 file_write (struct file *file, const void *buffer, off_t size)
97 off_t bytes_written = inode_write_at (file->inode, buffer, size, file->pos);
98 file->pos += bytes_written;
102 /* Writes SIZE bytes from BUFFER into FILE,
103 starting at offset FILE_OFS in the file.
104 Returns the number of bytes actually written,
105 which may be less than SIZE if end of file is reached.
106 (Normally we'd grow the file in that case, but file growth is
107 not yet implemented.)
108 The file's current position is unaffected. */
110 file_write_at (struct file *file, const void *buffer, off_t size,
113 return inode_write_at (file->inode, buffer, size, file_ofs);
116 /* Prevents write operations on FILE's underlying inode
117 until file_allow_write() is called or FILE is closed. */
119 file_deny_write (struct file *file)
121 ASSERT (file != NULL);
122 if (!file->deny_write)
124 file->deny_write = true;
125 inode_deny_write (file->inode);
129 /* Re-enables write operations on FILE's underlying inode.
130 (Writes might still be denied by some other file that has the
133 file_allow_write (struct file *file)
135 ASSERT (file != NULL);
136 if (file->deny_write)
138 file->deny_write = false;
139 inode_allow_write (file->inode);
143 /* Returns the size of FILE in bytes. */
145 file_length (struct file *file)
147 ASSERT (file != NULL);
148 return inode_length (file->inode);
151 /* Sets the current position in FILE to NEW_POS bytes from the
152 start of the file. */
154 file_seek (struct file *file, off_t new_pos)
156 ASSERT (file != NULL);
157 ASSERT (new_pos >= 0);
161 /* Returns the current position in FILE as a byte offset from the
162 start of the file. */
164 file_tell (struct file *file)
166 ASSERT (file != NULL);