Lecture 5 ● Lecture files in /home/hwang/cs375/lecture05 on csserver. cp -r /home/hwang/cs375/lecture05 . scp -r user@csserver.evansville.edu:/home/hwang/cs375/lecture05 . ● Project 1 posted, due next Thursday ● Questions? Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 1 Outline ● Exercises from last lecture ● Working with files ● File Descriptors ● Read, Write, Open and Close ● File Management ● File Information Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 2 Working with Files ● ● C, C++, Java, etc. each provide some means of file input/output. Each of these languages build upon the lower-level file access routines provided by the OS. The system routines provided by UNIX for file/device I/O are: open, close, read, write, and ioctl. They are documented in section 2 of the man pages, e.g. man 2 write. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 3 Why Use Low-Level Routines? ● ● ● Normally we use the file access methods provided by our programming language (I/O streams in C++, for example). They are more efficient and portable. BUT, interprocess communication (IPC) requires that we work with the OS routines. Writing kernel code (i.e., device drivers) ALSO requires knowledge of the low-level routines. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 4 File Descriptors ● ● ● Information about every open file is kept in a system-wide file table. (A file may be opened multiple times with different access modes; there would then be multiple entries in the system file table.) Every process has a file table that contains indexes into the system file table. Indexes into the per-process file table are called file descriptors. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 5 File Descriptors Per Process File Table File Descriptors Thursday, September 10 System File Table 0 1 2 3 4 5 6 CS 375 UNIX System Programming - Lecture 5 6 File Descriptors ● ● ● Programs usually (automatically) have standard input, standard output, and standard error open. These are associated with file descriptors 0, 1, and 2, respectively. You can associate other file descriptors with other files and devices by using the open system routine. System I/O routines require a file descriptor as an argument in the routine call. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 7 Example: Byte I/O // File: bytcat.cpp // byte at a time cat from stdin using system calls #include <unistd.h> int main() { char c; ssize_t nread, nwrite; while ((nread = read(0, &c, sizeof(c))) != ­1) { if (nread == 0) break; // break on end­of­file if((nwrite = write(1, &c, nread)) == ­1) break; // break on write error } if (nread == ­1 || nwrite == ­1) return 1; // error occurred return 0; } Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 8 Example: Block I/O // File: blkcat.cpp // block cat from stdin using system calls #include <unistd.h> int main() { const int BLKSZ = 512; char c[BLKSZ]; ssize_t nread, nwrite; while ((nread = read(0, c, BLKSZ)) != ­1) { if (nread == 0) break; // break on end­of­file if((nwrite = write(1, c, nread)) == ­1) break; // break on write error } if (nread == ­1 || nwrite == ­1) return 1; // error occurred return 0; } Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 9 Read and Write ● ● There is no guarantee that read will read the number of bytes requested. (Fewer bytes will be read when near the end of the file. Also, only a single line of data is read when reading from a terminal, not 512 bytes.) Notice the request is made to write nread bytes and not BLKSZ bytes. write may also write fewer bytes than requested, but this usually can be attributed to more serious problems. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 10 Open and Close ● The open call is used to associate a file descriptor with a physical filename. // Required header files #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> // open returns a file descriptor (an int) // Use this form int open(const char *pathname, int flags); // or this one. int open(const char *pathname, int flags, mode_t mode); ● The close call closes a file descriptor. int close(int fd); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 11 Open and Close ● ● ● flags should be O_RDONLY, O_WRONLY, or O_RDWR possibly bitwise or'd (|) with O_CREAT, O_EXCL, O_TRUNC, etc. mode (optional) specifies permissions (see the man page). There is also a creat (not create!) call: int creat(const char *pathname, mode_t mode); // this is equivalent to int open(const char *pathname, O_CREAT|O_WRONLY|O_TRUNC, mode_t mode); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 12 Using lseek ● lseek is used to reposition the file position pointer associated with a file descriptor: #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence); ● offset is the # of bytes to move. whence is either SEEK_SET, SEEK_CUR, or SEEK_END to move relative to the start of file, the current position, or the end of the file (offset may be negative). Returns offset from the start or -1. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 13 Duplicating File Descriptors ● dup and dup2 are used to duplicate a file descriptor. The duplicated descriptor will have the same file pointer in the file table. These calls are useful for interprocess communication. #include <unistd.h> int dup(int oldfd); # use the lowest avail. int dup2(int oldfd, int newfd) # make newfd a copy of oldfd ● They return the new file descriptor (or -1 on error). Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 14 File Information ● stat, fstat, and lstat are used to obtain all file information (type, owner, group, permissions, times, # of links, etc): #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *file_name, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *file_name, struct stat *buf); Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 15 File Information ● ● ● stat and lstat differ in regard to symlinks. stat returns info on the file pointed to while lstat returns info on the symlink. fstat acts like stat. Each of the routines require a pointer to a stat structure. The header files define the stat structure and also define several useful macros. Use "man 2 stat" to get the man page. ("man stat" will get the command-line stat.) Refer to the getstat.cpp program. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 16 File Information ● There is also a stat command line utility that will display file information: $ stat /etc/passwd File: `/etc/passwd' Size: 32889 Blocks: 72 IO Block: 4096 regular file Device: 801h/2049d Inode: 1712752 Links: 1 Access: (0644/­rw­r­­r­­) Uid: (0/root) Gid: (0/root) Access: 2010­09­08 08:17:16.000000000 ­0500 Modify: 2010­08­27 08:16:42.000000000 ­0500 Change: 2010­08­27 08:16:42.000000000 ­0500 Birth: ­ Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 17 Other File Related Routines chmod(path, mode) chown(path,owner,group) unlink(path) link(path1, path2) symlink(path1, path2) mkdir(path, mode) rmdir(path) chdir(path) getcwd(buffer, size) Thursday, September 10 # change file perms # change file owner (root) # delete file, # (decr # of links by 1) # create a hard link to # a file # create a symbolic link # create a new directory # delete directory # change process directory # get current working # directory CS 375 UNIX System Programming - Lecture 5 18 In-class Exercises ● ● ● Modify getstat.cpp so that it also displays the inode #, # of links, and the file size. Reminder: the man page for stat(2) has information about the stat structure and various macros. Further modify getstat.cpp to display file permissions in both octal and symbolic form. Further modify getstat.cpp to display all the information shown by the stat command. Thursday, September 10 CS 375 UNIX System Programming - Lecture 5 19