170 likes | 302 Vues
File I/O System Calls Reference. COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004. Contents. Calls Overview open() read() write() close() lseek() References. open(). Opens/creates files (whatever is abstracted by a file). SYNOPSIS #include <sys/types.h>
E N D
File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004 Serguei Mokhov, mokhov@cs.concordia.ca
Contents • Calls Overview • open() • read() • write() • close() • lseek() • References Serguei Mokhov, mokhov@cs.concordia.ca
open() • Opens/creates files (whatever is abstracted by a file). SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); Serguei Mokhov, mokhov@cs.concordia.ca
open(): flags • O_RDONLY or O_WRONLY or O_RDWR + zero or more of the following, concatenated by “|” (some are omitted, see man page for details): • O_CREAT, doesn’t fail if file exists • O_EXCL, in conjunction with O_CREAT will cause open() to fail if file exists • O_TRUNC, if file exists and permissions allow, make it of 0 length • O_APPEND, all stuff written to an open file will be appended regardless lseeks() you do. • O_NONBLOCK, non-blocking I/O • O_SYNC, synchronous I/O, thus blocking • O_NOFOLLOW, don’t follow symlinks, i.e. fail • O_DIRECTORY, fail if a directory Serguei Mokhov, mokhov@cs.concordia.ca
open(): modes • Permissions on new files: • S_IRWXU – User: RWX • S_IRUSR (S_IREAD) – User: R • S_IWUSR (S_IWRITE) – User: W • S_IXUSR (S_IEXEC) – User: X • S_IRWXG – Group: RWX • S_IRGRP – Group: R • S_IWGRP – Group: W • S_IXGRP – Group: X • S_IROTH – Others: R • S_IWOTH – Others: W • S_IXOTH – Others: X Serguei Mokhov, mokhov@cs.concordia.ca
open() • Performs file path to file descriptor translation, to use with read(), write(), etc. • Returns -1 on error. • File descriptor – next available integer starting from 0. • 0 – STDIN_FILENO • 1 – STDOUT_FILENO • 2 – STDERR_FILENO • 3 – probably your file • File descriptors are not shared by opening; however, a forked process among other things inherits parent’s open files. Serguei Mokhov, mokhov@cs.concordia.ca
Raw Device Read • /dev/hda0 – first partition of the first hard disk • To read you need superuser privileges. • open() just like filename, advice: do it read-only :-), i.e. O_RDONLY Serguei Mokhov, mokhov@cs.concordia.ca
read() • Well, read our bytes from somewhere, where our file descriptor points to, to a pre-allocated buffer. SYNOPSIS #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); Serguei Mokhov, mokhov@cs.concordia.ca
read() • Returns • number of bytes read on success • 0 - EOF • (-1) on failure • Make sure you read() as many bytes as you intended to before proceeding to processing of what you read(). Serguei Mokhov, mokhov@cs.concordia.ca
write() • Writes out data to a file descriptor. SYNOPSIS #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); Serguei Mokhov, mokhov@cs.concordia.ca
write() • Returns • (-1) on error • Number of bytes written on success • 0 if count is 0 • The rest is more or less specific • Double check that the # of bytes written is indeed as requested in count before you proceed. • Note, neither having successful write() nor successful close() after it mean your data physically has reached the disk; use fsync() to have more guarantee that this will happen since kernels usually defer writes for performance reasons. Serguei Mokhov, mokhov@cs.concordia.ca
close() • “Release” file descriptor. SYNOPSIS #include <unistd.h> int close(int fd); Serguei Mokhov, mokhov@cs.concordia.ca
close() • Descriptor ready for re-use. • All locks removed. • Returns • 0 on success • (-1) on error • ALWAYS CHECK THE RETURN VALUES! Serguei Mokhov, mokhov@cs.concordia.ca
lseek() • Navigate within the file using file pointer. SYNOPSIS #include <sys/types.h> #include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); Serguei Mokhov, mokhov@cs.concordia.ca
lseek() • whence: • SEEK_SET offset is set to offset bytes • SEEK_CUR current + offset • SEEK_END file size + offset • If file pointer is beyond file boundaries, zeros will be returned by read() from the gap between current position and actual file end until the gap is filled in with smth. Serguei Mokhov, mokhov@cs.concordia.ca
lseek() • Returns • Resulting offset on success • ((off_t)-1) on error Serguei Mokhov, mokhov@cs.concordia.ca
References • Rusling • Chapter 8, Vahalia • Chapters 3-5, Stevens • Manual pages: • man 2 open • man 2 read • man 2 write • man 2 close • man 2 lseek Serguei Mokhov, mokhov@cs.concordia.ca