5 #include "filesys-stub.h"
10 file_open (struct file *file, const char *name)
13 disk_sector_no hdr_sector;
16 dir_init (&dir, NUM_DIR_ENTRIES);
17 dir_read (&dir, &root_dir_file);
18 if (dir_lookup (&dir, name, &hdr_sector))
19 success = file_open_sector (file, hdr_sector);
26 file_open_sector (struct file *file, disk_sector_no hdr_sector)
29 return filehdr_read (&file->hdr, hdr_sector);
33 file_close (struct file *file)
35 filehdr_destroy (file);
39 file_read (struct file *file, void *buffer, off_t size)
41 off_t retval = file_read_at (file, buffer, size, file->pos);
47 file_read_at (struct file *file, void *buffer_, off_t size, off_t start)
49 uint8_t *buffer = buffer_;
53 bounce = malloc (DISK_SECTOR_SIZE);
56 /* Disk sector to read, starting byte offset within sector. */
57 off_t sector_idx = filehdr_byte_to_sector (file->hdr);
58 int sector_ofs = start % DISK_SECTOR_SIZE;
60 /* Bytes left in file, bytes left in sector. */
61 off_t file_left = filehdr_size (file->hdr) - start;
62 off_t sector_left = DISK_SECTOR_SIZE - sector_ofs;
64 /* Number of bytes to actually copy out of this sector. */
65 int chunk_size = file_left < sector_left ? file_left : sector_left;
69 /* Read sector into bounce buffer, then copy into caller's
71 disk_read (disk, sector_idx, bounce);
72 memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
77 bytes_read += chunk_size;
85 file_write (struct file *file, const void *buffer, off_t size)
87 off_t retval = file_write_at (file, buffer, size, file->pos);
93 file_write_at (struct file *file, const void *buffer_, off_t size,
96 uint8_t *buffer = buffer_;
100 bounce = malloc (DISK_SECTOR_SIZE);
103 /* Disk sector to read, starting byte offset within sector. */
104 off_t sector_idx = filehdr_byte_to_sector (file->hdr);
105 int sector_ofs = start % DISK_SECTOR_SIZE;
107 /* Bytes left in file, bytes left in sector. */
108 off_t file_left = filehdr_size (file->hdr) - start;
109 off_t sector_left = DISK_SECTOR_SIZE - sector_ofs;
111 /* Number of bytes to actually copy out of this sector. */
112 int chunk_size = file_left < sector_left ? file_left : sector_left;
116 /* Read sector into bounce buffer, then copy into caller's
118 disk_read (disk, sector_idx, bounce);
119 memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
124 bytes_read += chunk_size;
132 file_length (struct file *file)
136 filesys_stub_lock ();
137 filesys_stub_put_string ("length");
138 filesys_stub_put_file (file);
139 filesys_stub_match_string ("length");
140 length = filesys_stub_get_int32 ();
141 filesys_stub_unlock ();
147 file_seek (struct file *file, off_t pos)
149 filesys_stub_lock ();
150 filesys_stub_put_string ("seek");
151 filesys_stub_put_file (file);
152 filesys_stub_put_uint32 (pos);
153 filesys_stub_match_string ("seek");
154 filesys_stub_unlock ();
158 file_tell (struct file *file)
162 filesys_stub_lock ();
163 filesys_stub_put_string ("tell");
164 filesys_stub_put_file (file);
165 filesys_stub_match_string ("tell");
166 pos = filesys_stub_get_int32 ();
167 filesys_stub_unlock ();
171 #endif /* FILESYS_STUB */