Unix Files 15 213 A Unix file is a sequence of m bytes The course that gives CMU its Zip System Level I O October 31 2007 Unix I O Robust reading and writing Reading file metadata Sharing files I O redirection Standard I O Regular file Key Unix idea All input and output is handled in a consistent and uniform way A file that contains the names and locations of other files Basic Unix I O operations system calls Character special and block special files Opening and closing files Changing the current file position seek Reading and writing a file z open and close z lseek not discussed A file type used for interprocess communication z read and write Socket Terminals character special and disks block special FIFO named pipe 15 213 F 07 The elegant mapping of files to devices allows kernel to export simple interface called Unix I O Binary or text file Unix does not know the difference Directory file dev kmem kernel memory image proc kernel data structures Unix I O Unix File Types usr disk partition terminal 2 lecture 18 ppt dev sda2 dev tty2 Even the kernel is represented as a file Topics B0 B1 Bk Bm 1 All I O devices are represented as files A file type used for network communication between processes 15 213 F 07 3 4 15 213 F 07 Opening Files Closing Files Opening a file informs the kernel that you are getting ready to access that file Closing a file informs the kernel that you are finished accessing that file int fd file descriptor int fd file descriptor int retval return value if fd open etc hosts O RDONLY 0 perror open exit 1 if retval close fd 0 perror close exit 1 Returns a small identifying integer file descriptor fd 1 indicates that an error occurred Note Always check return codes even for seemingly benign functions such as close Each process created by a Unix shell begins life with three open files associated with a terminal 5 0 standard input 1 standard output 2 standard error 15 213 F 07 6 Page 1 15 213 F 07 Reading Files Writing Files Reading a file copies bytes from the current file position to memory and then updates file position Writing a file copies bytes from memory to the current file position and then updates current file position char buf 512 int fd file descriptor int nbytes number of bytes read char buf 512 int fd file descriptor int nbytes number of bytes read Open the file fd Then write up to 512 bytes from buf to file fd if nbytes write fd buf sizeof buf 0 perror write exit 1 Open file fd Then read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 Returns number of bytes written from buf to file fd fd Returns number of bytes read from file fd into buf 7 Return type ssize t is signed integer nbytes 0 indicates that an error occurred short counts nbytes sizeof buf are possible and are not errors 15 213 F 07 nbytes 0 indicates that an error occurred As with reads short counts are possible and are not errors Transfers up to 512 bytes from address buf to file fd 15 213 F 07 8 Unix I O Example Dealing with Short Counts Copying standard input to standard output one byte at a time Short counts can occur in these situations include csapp h int main void char c Short counts never occur in these situations while Read STDIN FILENO c 1 0 Write STDOUT FILENO c 1 exit 0 Reading from disk files except for EOF Writing to disk files One way to deal with short counts in your code Note the use of error handling wrappers for read and write Appendix B 15 213 F 07 9 Encountering end of file EOF on reads Reading text lines from a terminal Reading and writing network sockets or Unix pipes Use the RIO Robust I O package from your textbook s csapp c file Appendix B 15 213 F 07 10 The RIO Package Buffered I O Motivation RIO is a set of wrappers that provide efficient and robust I O in in applications such as network programs that are subject to short counts I O Applications Read Write One Character at a Time RIO provides two different kinds of functions z Read line of text stopping at newline Unbuffered input and output of binary data Implementing as Calls to Unix I O Expensive z rio readn and rio writen getc putc ungetc gets Buffered input of binary data and text lines z rio readlineb and rio readnb Read Write involve require Unix kernel calls z 10 000 clock cycles z Buffered RIO routines are thread safe and can be interleaved arbitrarily on Buffer the same descriptor already read Download from csapp cs cmu edu public ics code src csapp c csapp cs cmu edu public ics code include csapp h Buffered Read 11 unread 15 213 F 07 12 Page 2 Use Unix read to grab block of characters User input functions take one character at a time from buffer z Refill buffer when empty 15 213 F 07 File Metadata Buffered I O Implementation File has associated buffer to hold bytes that have been read from file but not yet read by user code Maintained by kernel accessed by users with the stat and fstat functions rio cnt Buffer already read rio buf Metadata is data about data in this case file data Metadata returned by the stat and fstat functions struct stat dev t st dev device ino t st ino inode mode t st mode protection and file type nlink t st nlink number of hard links uid t st uid user ID of owner gid t st gid group ID of owner dev t st rdev device type if inode device off t st size total size in bytes unsigned long st blksize blocksize for filesystem I O unsigned long st blocks number of blocks allocated time t st atime time of last access time t st mtime time of last modification time t st ctime time of last change 15 213 F 07 14 unread rio bufptr typedef struct int rio fd int rio cnt char rio bufptr char rio buf RIO BUFSIZE rio t descriptor for this internal buf unread bytes in internal buf next unread byte in internal buf internal buffer 15 213 F 07 13 Example of Accessing File Metadata Accessing Directories statcheck c Querying and manipulating a file s meta data include csapp h int main int argc char argv struct stat stat char type readok unix type unix unix type unix type unix type The only recommended operation on a directory is to read its entries statcheck statcheck c regular read yes chmod 000 statcheck c statcheck statcheck c regular read no statcheck directory read yes statcheck dev kmem other read yes Stat argv 1 stat if S ISREG stat st mode type regular else if S ISDIR stat st mode …
View Full Document