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);
open - errno
#include <errno.h>extern int errno;
EEXIST, EISDIR, EACCES, ENAMETOOLONG, ENOENT, ENOTDIR, ENXIO, ENODEV, EROFS, ETXTBSY, EFAULT, ELOOP, ENOSPC, ENOMEM, EMFILE, ENFILE
oflag
O_RDONLY
O_WRONLY
O_RDWR
O_APPEND
O_TRUNC
O_NONBLOCK
O_SYNC
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)
close
#include <unistd.h>
int close(int filedes)
lseek
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes,
off_t offset,
int whence)
lseek -- whence
SEEK_SET
SEEK_CUR
SEEK_END
-----
Currpos = lseek(fd, 0, SEEK_CUR)
Currpos = -1, errno = EPIPE, cannot seek fd
Lseek –file size
• Extends file size in kernel
• Zero fills
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
write
#include <unistd.h>
ssize_t write(int filedes,
const void *buff,
size_t nbytes)
Sharing files
• Kernel structures
• Dup
#include <unistd.h>
int dup(int filedes);
fcntl
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fcntl(int filedes, int cmd)
F_DUPFD, F_GETFL (O_RDONLY,…)
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);
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)
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 */
}
st_mode
ST_ISREG(m)
ST_ISDIR(m)
ST_ISCHR(m)
ST_ISBLK(m)
ST_ISFIFO(m)
ST_ISLNK(m) – symbolic
ST_ISSOCK(m)
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
umask
#include <sys/types.h>
#include <sys/stat.h>
Mode_t umask(mode_t cmask)
S_IRUSR, S_IWUSR, S_IXUSR (grp, oth)
chmod
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname,
mode_t mode)
• must be owner to change mode
chown
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,
uid_t owner,
gid_t group);
lchown – does not follow link