1 #include "filesys/file.h"
4 #include "filesys/directory.h"
5 #include "filesys/inode.h"
6 #include "filesys/filesys.h"
7 #include "threads/malloc.h"
12 struct inode *inode; /* File's inode. */
13 uint8_t *bounce; /* Bounce buffer for reads and writes. */
14 off_t pos; /* Current position. */
17 /* Opens and returns the file whose inode is in sector
18 INODE_SECTOR. Returns a null pointer if unsuccessful. */
20 file_open (disk_sector_t inode_sector)
22 struct file *file = calloc (1, sizeof *file);
26 file->inode = inode_open (inode_sector);
27 file->bounce = malloc (DISK_SECTOR_SIZE);
29 if (file->inode == NULL || file->bounce == NULL)
31 inode_close (file->inode);
41 file_close (struct file *file)
46 inode_close (file->inode);
50 /* Reads SIZE bytes from FILE into BUFFER,
51 starting at the file's current position,
52 and advances the current position.
53 Returns the number of bytes actually read,
54 which may be less than SIZE if end of file is reached. */
56 file_read (struct file *file, void *buffer, off_t size)
58 off_t bytes_read = file_read_at (file, buffer, size, file->pos);
59 file->pos += bytes_read;
63 /* Reads SIZE bytes from FILE into BUFFER,
64 starting at offset FILE_OFS in the file.
65 The file's current position is unaffected
66 Returns the number of bytes actually read,
67 which may be less than SIZE if end of file is reached. */
69 file_read_at (struct file *file, void *buffer_, off_t size,
72 uint8_t *buffer = buffer_;
77 /* Disk sector to read, starting byte offset within sector. */
78 disk_sector_t sector_idx;
79 int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
81 /* Bytes left in file, bytes left in sector, lesser of the two. */
82 off_t file_left = inode_length (file->inode) - file_ofs;
83 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
84 int min_left = file_left < sector_left ? file_left : sector_left;
86 /* Number of bytes to actually copy out of this sector. */
87 int chunk_size = size < min_left ? size : min_left;
91 /* Read sector into bounce buffer, then copy into caller's
93 sector_idx = inode_byte_to_sector (file->inode, file_ofs);
94 disk_read (filesys_disk, sector_idx, file->bounce);
95 memcpy (buffer + bytes_read, file->bounce + sector_ofs, chunk_size);
99 file_ofs += chunk_size;
100 bytes_read += chunk_size;
106 /* Writes SIZE bytes from BUFFER into FILE,
107 starting at the file's current position,
108 and advances the current position.
109 Returns the number of bytes actually written,
110 which may be less than SIZE if end of file is reached.
111 (Normally we'd grow the file in that case, but file growth is
112 not yet implemented.) */
114 file_write (struct file *file, const void *buffer, off_t size)
116 off_t bytes_written = file_write_at (file, buffer, size, file->pos);
117 file->pos += bytes_written;
118 return bytes_written;
121 /* Writes SIZE bytes from BUFFER into FILE,
122 starting at offset FILE_OFS in the file.
123 The file's current position is unaffected
124 Returns the number of bytes actually written,
125 which may be less than SIZE if end of file is reached.
126 (Normally we'd grow the file in that case, but file growth is
127 not yet implemented.) */
129 file_write_at (struct file *file, const void *buffer_, off_t size,
132 const uint8_t *buffer = buffer_;
133 off_t bytes_written = 0;
137 /* Sector to write, starting byte offset within sector. */
139 int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
141 /* Bytes left in file, bytes left in sector, lesser of the two. */
142 off_t file_left = inode_length (file->inode) - file_ofs;
143 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
144 int min_left = file_left < sector_left ? file_left : sector_left;
146 /* Number of bytes to actually write into this sector. */
147 int chunk_size = size < min_left ? size : min_left;
151 /* If the sector contains data before or after the chunk
152 we're writing, then we need to read in the sector
153 first. Otherwise we start with a sector of all zeros. */
154 sector_idx = inode_byte_to_sector (file->inode, file_ofs);
155 if (sector_ofs > 0 || chunk_size < sector_left)
156 disk_read (filesys_disk, sector_idx, file->bounce);
158 memset (file->bounce, 0, DISK_SECTOR_SIZE);
159 memcpy (file->bounce + sector_ofs, buffer + bytes_written, chunk_size);
160 disk_write (filesys_disk, sector_idx, file->bounce);
164 file_ofs += chunk_size;
165 bytes_written += chunk_size;
168 return bytes_written;
171 /* Returns the size of FILE in bytes. */
173 file_length (struct file *file)
175 ASSERT (file != NULL);
176 return inode_length (file->inode);
179 /* Sets the current position in FILE to an offset of FILE_OFS
180 bytes from the start of the file. */
182 file_seek (struct file *file, off_t file_ofs)
184 ASSERT (file != NULL);
185 ASSERT (file_ofs >= 0);
186 file->pos = file_ofs;
189 /* Returns the current position in FILE as a byte offset from the
190 start of the file. */
192 file_tell (struct file *file)
194 ASSERT (file != NULL);