Lecture 6 Delete Rename File Systems Open Close January 26, 2005 Write Read Seek Get Attributes Set Attributes Append
Total Page:16
File Type:pdf, Size:1020Kb
File Operations create Lecture 6 delete rename File Systems open close January 26, 2005 write read seek get attributes set attributes append Blocking vs Nonblocking File I/O Blocking: wait until finished or interrupt (Almost) everything a file: (EINTR). /dev/audio: audio device /dev/hda: first IDE hard drive Nonblocking: return immediately. /dev/cdrom: CDROM drive /dev/null: “black hole” directories Users: logical stream of bytes. Simultaneous access. Positioning. File I/O: Open File I/O: Positioning #include <sys/types.h> off_t lseek(int fd, off_t offset, int whence); #include <sys/stat.h> #include <fcntl.h> SEEK_SET: move to offset from beginning of file fd = open(pathname, flags, [mode]); SEEK_CUR: move to offset from current value SEEK_END: move to offset from end of file O_RDONLY: reading only O_WRONLY: writing only O_RDWR: reading/writing O_TRUNC: reset file O_EXCL: cancel if file exists O_APPEND: file position mode: permissions if new file is created 1 File System File Descriptor Duplication disk #include <unistd.h> partition partition partition partition /tmp /home /usr / int dup(int fd); int dup2(int fd, int new_fd); file system directory and data blocks boot super i-list dir. block block block Example: dup2(1,2); data blocks directory block i-node filename number i-node i-node ... i-node File Control Special Files #include <sys/types.h> /dev/urandom #include <unistd.h> /dev/fd #include <fcntl.h> /dev/zero int fcntl(fd, cmd, arg, ...); /dev/null /proc: F_DUPFD: dup() /proc/cpuinfo F_GETFD, F_SETFD: get/set flags /proc/meminfo F_GETOWN, F_SETOWN: owner (receives /proc/diskstats SIGIO) /proc/filesystems File Types File Information regular files #include <sys/types.h> directory files #include <sys/stat.h> character special files (/dev/) int stat(const char *pathname, struct stat *buf); block special files int fstat(int fd, struct stat *buf); FIFO (IPC) int lstat(const char *pathname, struct stat *buf); socket (IPC) symbolic link 2 struct stat File Permissions struct stat { dev_t st_dev; /* device */ permission bits: r,x,w ino_t st_ino; /* inode */ r: read umode_t st_mode; /* protection */ nlink_t st_nlink; /* number of links */ w: write uid_t st_uid; /* UID of owner */ gid_t st_gid; /* group ID of owner */ x: execute dev_t st_rdev; /* device type */ off_t st_size; /* size in bytes */ long st_blksize; /* block size for file system I/O */ long st_blocks; /* blocks allocated */ Pos: 1 2 3 4 5 6 7 8 9 10 time_t st_atime; /* access time */ -dl r w x r w x r w x time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ } File Permissions File Permissions drwxrwxrwx 4 cpoellab team1 122 Dec 12 18:02 Assignments Set user ID: type and permission (drwxrwxrwx) number of links: 4 chmod +s myfile owner: cpoellab file’s group: team1 users running this file have access to system size in bytes (122) date of last modification (Dec 12 18:02) resources as if they are the owner of the file: filename: Assignments -rws--x--x 1 root root 14920 Sep 9 1999 mfile Type: d = directory has meaning only if executable l = symbolic link s = socket p = named pipe - = regular file c = character special file b = block special file Sticky Bit umask Directory Permission ‘t’ - save text attribute Set and determine the default file creation (sticky bit) permissions on the system: /tmp: typically world-writable: 777 - executable files drwxrwxrwt 13 root root 4096 Apr 12 08:05 tmp 666 - text files everyone can read/write/access directory Permission for creation of new executable is calculated by subtracting the umask value: t indicates that only the user (and root) that created a file can delete that file 666-022=644 umask 022 chmod +t data /etc/profile 3 Changing Permissions chown #include <sys/types.h> int chown(const char *path, uid_t owner, gid_t #include <sys/stat.h> group); int fchown(int fd, uid_t owner, gid_t group); int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode); Command line: Command line: chown roger file1 file2 rwx: chmod 777 foo readonly: chmod -w foo must be owner of file Review: Unix Filesystem Creating/Deleting Links Disk drive = sum of partitions #include <unistd.h> int link(const char *path, const char *newpath); Filesystem: boot block, super block, i-list, directory/data blocks int unlink(const char *path); int remove(const char *path); i-list: i-nodes, with pointers to data blocks int rename(const char *old, const char *new); inode numbers unique within filesystem Directory blocks = filename, i-node number (ls -i) symbolic link: file data contains name of file Creating/Deleting Directories int mkdir (const char *path, mode_t mode); int rmdir (const char *path); can only delete empty directories 4.