1 / 22

File System API

File System API. Operating System Hebrew University Spring 2004. RTFM. Man pages Advanced Programming in the Unix Environment by Stevens. open. #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int oflag, mode_t mode);.

petula
Download Presentation

File System API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File System API Operating System Hebrew University Spring 2004

  2. RTFM • Man pages • Advanced Programming in the Unix Environment by Stevens

  3. open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int oflag, mode_t mode);

  4. open - errno #include <errno.h> extern int errno; EEXIST, EISDIR, EACCES, ENAMETOOLONG, ENOENT, ENOTDIR, ENXIO, ENODEV, EROFS, ETXTBSY, EFAULT, ELOOP, ENOSPC, ENOMEM, EMFILE, ENFILE

  5. oflag O_RDONLY O_WRONLY O_RDWR O_APPEND O_TRUNC O_NONBLOCK O_SYNC

  6. creat #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int creat(const char *pathname, mode_t mode) == open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode)

  7. close #include <unistd.h> int close(int filedes)

  8. lseek #include <sys/types.h> #include <unistd.h> off_t lseek(int filedes, off_t offset, int whence)

  9. lseek -- whence SEEK_SET SEEK_CUR SEEK_END ----- Currpos = lseek(fd, 0, SEEK_CUR) Currpos = -1, errno = EPIPE, cannot seek fd

  10. Lseek –file size • Extends file size in kernel • Zero fills

  11. read #include <unistd.h> ssize_t read(int filedes, void *buff, size_t nbytes) Ssize_t = signed integer, size_t = unsigned int Ssize = 0 && errno = EAGAIN = non-block

  12. write #include <unistd.h> ssize_t write(int filedes, const void *buff, size_t nbytes)

  13. Sharing files • Kernel structures • Dup #include <unistd.h> int dup(int filedes);

  14. fcntl #include <sys/types.h> #include <unistd.h> #include <fcntl.h> int fcntl(int filedes, int cmd) F_DUPFD, F_GETFL (O_RDONLY,…)

  15. links • Soft and hard #include <unistd.h> int link(const char *existingpath, const char *newpath) int symlink(const char *actualpath, const char *newpath); int unlink(const char *pathname); int remove(const char *pathname); int rename (const char *oldname, const char *newname);

  16. stat, fstat, lstat #include <sys/types.h> #include <sys/stat.h> int stat(const char *pathname, struct stat *buf) int fstat(int filedes, struct stat *buf) int lstat(const char *pathname, struct stat *buf)

  17. struct stat Struct stat { mode_t st_mode; /* file type and mode (permissions) */ ino_t st_ino; /* serial number */ dev_t st_dev; /* device number (file system) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes for regular files */ time_t st_atime; /* last access */ time_t st_mtime; /* last modified */ time_t st_ctime; /* last file status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */ }

  18. st_mode ST_ISREG(m) ST_ISDIR(m) ST_ISCHR(m) ST_ISBLK(m) ST_ISFIFO(m) ST_ISLNK(m) – symbolic ST_ISSOCK(m)

  19. File permissions • srwxsrwxtrwx • setuid, setgid, sticky • To access • must have x in all directories in path • r and x in directories are different • Must have wx in dir to create files • To delete, must have wx in dir, file is irrelevent

  20. umask #include <sys/types.h> #include <sys/stat.h> Mode_t umask(mode_t cmask) S_IRUSR, S_IWUSR, S_IXUSR (grp, oth)

  21. chmod #include <sys/types.h> #include <sys/stat.h> int chmod(const char *pathname, mode_t mode) • must be owner to change mode

  22. chown #include <sys/types.h> #include <unistd.h> int chown(const char *pathname, uid_t owner, gid_t group); lchown – does not follow link

More Related