X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Ffile.c;h=d5fc10de15004e98d4f8490aa8a7ec8a4c7efcff;hb=69abf67e46fa36f6db047586a56204dcad4f07b8;hp=e6f282f8062dc2c383f3a540c08b73d0a9628ce7;hpb=2cf38a4ba2dd965c892767b0d8ee97e67f33b274;p=pintos-anon diff --git a/src/filesys/file.c b/src/filesys/file.c index e6f282f..d5fc10d 100644 --- a/src/filesys/file.c +++ b/src/filesys/file.c @@ -1,171 +1,168 @@ -#include "file.h" - -#ifdef FILESYS_STUB -#include "debug.h" -#include "filesys-stub.h" -#include "lib.h" -#include "malloc.h" - -bool -file_open (struct file *file, const char *name) +#include "filesys/file.h" +#include +#include "filesys/inode.h" +#include "threads/malloc.h" + +/* An open file. */ +struct file + { + struct inode *inode; /* File's inode. */ + off_t pos; /* Current position. */ + bool deny_write; /* Has file_deny_write() been called? */ + }; + +/* Opens a file for the given INODE, of which it takes ownership, + and returns the new file. Returns a null pointer if an + allocation fails or if INODE is null. */ +struct file * +file_open (struct inode *inode) { - struct dir dir; - disk_sector_no hdr_sector; - bool success = false; - - dir_init (&dir, NUM_DIR_ENTRIES); - dir_read (&dir, &root_dir_file); - if (dir_lookup (&dir, name, &hdr_sector)) - success = file_open_sector (file, hdr_sector); - - dir_destroy (&dir); - return success; + struct file *file = calloc (1, sizeof *file); + if (inode != NULL && file != NULL) + { + file->inode = inode; + file->pos = 0; + file->deny_write = false; + return file; + } + else + { + inode_close (inode); + free (file); + return NULL; + } } -bool -file_open_sector (struct file *file, disk_sector_no hdr_sector) +/* Opens and returns a new file for the same inode as FILE. + Returns a null pointer if unsuccessful. */ +struct file * +file_reopen (struct file *file) { - file->pos = 0; - return filehdr_read (&file->hdr, hdr_sector); + return file_open (inode_reopen (file->inode)); } +/* Closes FILE. */ void file_close (struct file *file) { - filehdr_destroy (file); + if (file != NULL) + { + file_allow_write (file); + inode_close (file->inode); + free (file); + } +} + +/* Returns the inode encapsulated by FILE. */ +struct inode * +file_get_inode (struct file *file) +{ + return file->inode; } +/* Reads SIZE bytes from FILE into BUFFER, + starting at the file's current position. + Returns the number of bytes actually read, + which may be less than SIZE if end of file is reached. + Advances FILE's position by the number of bytes read. */ off_t file_read (struct file *file, void *buffer, off_t size) { - off_t retval = file_read_at (file, buffer, size, file->pos); - file->pos += retval; - return retval; + off_t bytes_read = inode_read_at (file->inode, buffer, size, file->pos); + file->pos += bytes_read; + return bytes_read; } +/* Reads SIZE bytes from FILE into BUFFER, + starting at offset FILE_OFS in the file. + Returns the number of bytes actually read, + which may be less than SIZE if end of file is reached. + The file's current position is unaffected. */ off_t -file_read_at (struct file *file, void *buffer_, off_t size, off_t start) +file_read_at (struct file *file, void *buffer, off_t size, off_t file_ofs) { - uint8_t *buffer = buffer_; - off_t bytes_read = 0; - uint8_t *bounce; - - bounce = malloc (DISK_SECTOR_SIZE); - while (size > 0) - { - /* Disk sector to read, starting byte offset within sector. */ - off_t sector_idx = filehdr_byte_to_sector (file->hdr); - int sector_ofs = start % DISK_SECTOR_SIZE; - - /* Bytes left in file, bytes left in sector. */ - off_t file_left = filehdr_size (file->hdr) - start; - off_t sector_left = DISK_SECTOR_SIZE - sector_ofs; - - /* Number of bytes to actually copy out of this sector. */ - int chunk_size = file_left < sector_left ? file_left : sector_left; - if (chunk_size == 0) - break; - - /* 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); - - /* Advance. */ - size -= chunk_size; - start += chunk_size; - bytes_read += chunk_size; - } - free (bounce); - - return bytes_read; + return inode_read_at (file->inode, buffer, size, file_ofs); } +/* Writes SIZE bytes from BUFFER into FILE, + starting at the file's current position. + Returns the number of bytes actually written, + which may be less than SIZE if end of file is reached. + (Normally we'd grow the file in that case, but file growth is + not yet implemented.) + Advances FILE's position by the number of bytes read. */ off_t file_write (struct file *file, const void *buffer, off_t size) { - off_t retval = file_write_at (file, buffer, size, file->pos); - file->pos += retval; - return retval; + off_t bytes_written = inode_write_at (file->inode, buffer, size, file->pos); + file->pos += bytes_written; + return bytes_written; } +/* Writes SIZE bytes from BUFFER into FILE, + starting at offset FILE_OFS in the file. + Returns the number of bytes actually written, + which may be less than SIZE if end of file is reached. + (Normally we'd grow the file in that case, but file growth is + not yet implemented.) + The file's current position is unaffected. */ off_t -file_write_at (struct file *file, const void *buffer_, off_t size, - off_t start) +file_write_at (struct file *file, const void *buffer, off_t size, + off_t file_ofs) { - uint8_t *buffer = buffer_; - off_t bytes_read = 0; - uint8_t *bounce; + return inode_write_at (file->inode, buffer, size, file_ofs); +} - bounce = malloc (DISK_SECTOR_SIZE); - while (size > 0) +/* Prevents write operations on FILE's underlying inode + until file_allow_write() is called or FILE is closed. */ +void +file_deny_write (struct file *file) +{ + ASSERT (file != NULL); + if (!file->deny_write) { - /* Disk sector to read, starting byte offset within sector. */ - off_t sector_idx = filehdr_byte_to_sector (file->hdr); - int sector_ofs = start % DISK_SECTOR_SIZE; - - /* Bytes left in file, bytes left in sector. */ - off_t file_left = filehdr_size (file->hdr) - start; - off_t sector_left = DISK_SECTOR_SIZE - sector_ofs; - - /* Number of bytes to actually copy out of this sector. */ - int chunk_size = file_left < sector_left ? file_left : sector_left; - if (chunk_size == 0) - break; - - /* 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); - - /* Advance. */ - size -= chunk_size; - start += chunk_size; - bytes_read += chunk_size; + file->deny_write = true; + inode_deny_write (file->inode); } - free (bounce); +} - return bytes_read; +/* Re-enables write operations on FILE's underlying inode. + (Writes might still be denied by some other file that has the + same inode open.) */ +void +file_allow_write (struct file *file) +{ + ASSERT (file != NULL); + if (file->deny_write) + { + file->deny_write = false; + inode_allow_write (file->inode); + } } +/* Returns the size of FILE in bytes. */ off_t file_length (struct file *file) { - int32_t length; - - filesys_stub_lock (); - filesys_stub_put_string ("length"); - filesys_stub_put_file (file); - filesys_stub_match_string ("length"); - length = filesys_stub_get_int32 (); - filesys_stub_unlock (); - - return length; + ASSERT (file != NULL); + return inode_length (file->inode); } +/* Sets the current position in FILE to NEW_POS bytes from the + start of the file. */ void -file_seek (struct file *file, off_t pos) +file_seek (struct file *file, off_t new_pos) { - filesys_stub_lock (); - filesys_stub_put_string ("seek"); - filesys_stub_put_file (file); - filesys_stub_put_uint32 (pos); - filesys_stub_match_string ("seek"); - filesys_stub_unlock (); + ASSERT (file != NULL); + ASSERT (new_pos >= 0); + file->pos = new_pos; } +/* Returns the current position in FILE as a byte offset from the + start of the file. */ off_t file_tell (struct file *file) { - int32_t pos; - - filesys_stub_lock (); - filesys_stub_put_string ("tell"); - filesys_stub_put_file (file); - filesys_stub_match_string ("tell"); - pos = filesys_stub_get_int32 (); - filesys_stub_unlock (); - - return pos; + ASSERT (file != NULL); + return file->pos; } -#endif /* FILESYS_STUB */