10 file_open (struct file *file, disk_sector_no hdr_sector)
12 file->hdr = filehdr_read (hdr_sector);
13 file->bounce = malloc (DISK_SECTOR_SIZE);
15 if (file->hdr != NULL && file->bounce != NULL)
19 filehdr_destroy (file->hdr);
26 file_close (struct file *file)
28 filehdr_destroy (file->hdr);
32 file_read (struct file *file, void *buffer, off_t size)
34 off_t bytes_read = file_read_at (file, buffer, size, file->pos);
35 file->pos += bytes_read;
40 file_read_at (struct file *file, void *buffer_, off_t size,
43 uint8_t *buffer = buffer_;
48 /* Disk sector to read, starting byte offset within sector. */
49 off_t sector_idx = filehdr_byte_to_sector (file->hdr, file_ofs);
50 int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
52 /* Bytes left in file, bytes left in sector, lesser of the two. */
53 off_t file_left = filehdr_length (file->hdr) - file_ofs;
54 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
55 int min_left = file_left < sector_left ? file_left : sector_left;
57 /* Number of bytes to actually copy out of this sector. */
58 int chunk_size = size < min_left ? size : min_left;
62 /* Read sector into bounce buffer, then copy into caller's
64 disk_read (filesys_disk, sector_idx, file->bounce);
65 memcpy (buffer + bytes_read, file->bounce + sector_ofs, chunk_size);
69 file_ofs += chunk_size;
70 bytes_read += chunk_size;
77 file_write (struct file *file, const void *buffer, off_t size)
79 off_t bytes_written = file_write_at (file, buffer, size, file->pos);
80 file->pos += bytes_written;
85 file_write_at (struct file *file, const void *buffer_, off_t size,
88 const uint8_t *buffer = buffer_;
89 off_t bytes_written = 0;
93 /* Starting byte offset within sector to read. */
94 off_t sector_idx = filehdr_byte_to_sector (file->hdr, file_ofs);
95 int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
97 /* Bytes left in file, bytes left in sector, lesser of the two. */
98 off_t file_left = filehdr_length (file->hdr) - file_ofs;
99 int sector_left = DISK_SECTOR_SIZE - sector_ofs;
100 int min_left = file_left < sector_left ? file_left : sector_left;
102 /* Number of bytes to actually writen into this sector. */
103 int chunk_size = size < min_left ? size : min_left;
107 /* If the sector contains data before or after the chunk
108 we're writing, then we need to read in the sector
109 first. Otherwise we start with a sector of all zeros. */
110 if (sector_ofs > 0 || chunk_size < sector_ofs)
111 disk_read (filesys_disk, sector_idx, file->bounce);
113 memset (file->bounce, 0, DISK_SECTOR_SIZE);
114 memcpy (file->bounce + sector_ofs, buffer + bytes_written, chunk_size);
115 disk_write (filesys_disk, sector_idx, file->bounce);
119 file_ofs += chunk_size;
120 bytes_written += chunk_size;
124 return bytes_written;
128 file_length (struct file *file)
130 ASSERT (file != NULL);
131 return filehdr_length (file->hdr);
135 file_seek (struct file *file, off_t file_ofs)
137 ASSERT (file != NULL);
138 ASSERT (file_ofs >= 0);
139 file->pos = file_ofs;
143 file_tell (struct file *file)
145 ASSERT (file != NULL);