22
File System API Operating System Hebrew University Spring 2004

File System API

  • Upload
    petula

  • View
    15

  • Download
    0

Embed Size (px)

DESCRIPTION

File System API. Operating System Hebrew University Spring 2004. RTFM. Man pages Advanced Programming in the Unix Environment by Stevens. open. #include #include #include int open(const char *pathname, int oflag, mode_t mode);. - PowerPoint PPT Presentation

Citation preview

Page 1: File System API

File System API

Operating System

Hebrew University

Spring 2004

Page 2: File System API

RTFM

• Man pages

• Advanced Programming in the Unix Environment by Stevens

Page 3: File System API

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

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

oflag

O_RDONLY

O_WRONLY

O_RDWR

O_APPEND

O_TRUNC

O_NONBLOCK

O_SYNC

Page 6: File System API

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

close

#include <unistd.h>

int close(int filedes)

Page 8: File System API

lseek

#include <sys/types.h>

#include <unistd.h>

off_t lseek(int filedes,

off_t offset,

int whence)

Page 9: File System API

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

Lseek –file size

• Extends file size in kernel

• Zero fills

Page 11: File System API

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

write

#include <unistd.h>

ssize_t write(int filedes,

const void *buff,

size_t nbytes)

Page 13: File System API

Sharing files

• Kernel structures

• Dup

#include <unistd.h>

int dup(int filedes);

Page 14: File System API

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

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

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

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

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

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

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

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

chown

#include <sys/types.h>

#include <unistd.h>

int chown(const char *pathname,

uid_t owner,

gid_t group);

lchown – does not follow link