Download - File System API
![Page 1: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/1.jpg)
File System API
Operating System
Hebrew University
Spring 2004
![Page 2: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/2.jpg)
RTFM
• Man pages
• Advanced Programming in the Unix Environment by Stevens
![Page 3: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/3.jpg)
open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname,
int oflag,
mode_t mode);
![Page 4: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/4.jpg)
open - errno
#include <errno.h>extern int errno;
EEXIST, EISDIR, EACCES, ENAMETOOLONG, ENOENT, ENOTDIR, ENXIO, ENODEV, EROFS, ETXTBSY, EFAULT, ELOOP, ENOSPC, ENOMEM, EMFILE, ENFILE
![Page 5: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/5.jpg)
oflag
O_RDONLY
O_WRONLY
O_RDWR
O_APPEND
O_TRUNC
O_NONBLOCK
O_SYNC
![Page 6: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/6.jpg)
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)
![Page 7: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/7.jpg)
close
#include <unistd.h>
int close(int filedes)
![Page 8: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/8.jpg)
lseek
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes,
off_t offset,
int whence)
![Page 9: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/9.jpg)
lseek -- whence
SEEK_SET
SEEK_CUR
SEEK_END
-----
Currpos = lseek(fd, 0, SEEK_CUR)
Currpos = -1, errno = EPIPE, cannot seek fd
![Page 10: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/10.jpg)
Lseek –file size
• Extends file size in kernel
• Zero fills
![Page 11: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/11.jpg)
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
![Page 12: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/12.jpg)
write
#include <unistd.h>
ssize_t write(int filedes,
const void *buff,
size_t nbytes)
![Page 13: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/13.jpg)
Sharing files
• Kernel structures
• Dup
#include <unistd.h>
int dup(int filedes);
![Page 14: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/14.jpg)
fcntl
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fcntl(int filedes, int cmd)
F_DUPFD, F_GETFL (O_RDONLY,…)
![Page 15: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/15.jpg)
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);
![Page 16: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/16.jpg)
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)
![Page 17: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/17.jpg)
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 */
}
![Page 18: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/18.jpg)
st_mode
ST_ISREG(m)
ST_ISDIR(m)
ST_ISCHR(m)
ST_ISBLK(m)
ST_ISFIFO(m)
ST_ISLNK(m) – symbolic
ST_ISSOCK(m)
![Page 19: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/19.jpg)
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
![Page 20: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/20.jpg)
umask
#include <sys/types.h>
#include <sys/stat.h>
Mode_t umask(mode_t cmask)
S_IRUSR, S_IWUSR, S_IXUSR (grp, oth)
![Page 21: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/21.jpg)
chmod
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname,
mode_t mode)
• must be owner to change mode
![Page 22: File System API](https://reader036.vdocument.in/reader036/viewer/2022070408/56814428550346895db0c489/html5/thumbnails/22.jpg)
chown
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,
uid_t owner,
gid_t group);
lchown – does not follow link