- /* Read sector into bounce buffer, then copy into caller's
- buffer. */
- disk_read (disk, sector_idx, bounce);
- memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
+ /* If the sector contains data before or after the chunk
+ we're writing, then we need to read in the sector
+ first. Otherwise we start with a sector of all zeros. */
+ if (sector_ofs > 0 || chunk_size < sector_ofs)
+ disk_read (filesys_disk, sector_idx, file->bounce);
+ else
+ memset (file->bounce, 0, DISK_SECTOR_SIZE);
+ memcpy (file->bounce + sector_ofs, buffer + bytes_written, chunk_size);
+ disk_write (filesys_disk, sector_idx, file->bounce);