DOC PREVIEW
U of I CS 241 - LECTURE NOTES

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 File SystemsFiles & Directories… a practical primer for LMP1Lawrence AngraveRobbins Ch4.1-4.3, 4.6, 5.2Stallings Ch12?API... open? stat?Why so many options in open?How do I make my code robust?What concepts underpin thePOISX filesystem API?What exactly is a file,directory...? I-node?Unix concept of a FILEByte-orientated & sequentialSo what?Unicode fileTyped Fields and RecordsKey indicesUnified. Get the contents of a file as bytes#include <unistd.h>ssize_t read(int dec, void *buf, size_t nbyte);readDon't need to read entire file... Grab bytes into your buffer... Next call to read will read next unread byteCan even read the bytes of a 'directory'read() boundary casesto trip you upMay not read anythingsignal interruptionend of fileasynchronous non-blocking modehardware issueNeed to check # bytes actually readIt usually worksas you might assumeUpon successful completion, read(), readv(),and pread() return the number of bytesactually read and placed in the buffer.The system guarantees to read the number ofbytes requested if the descriptor references anormal file that has that many bytes leftbefore the end-of-file, but in no other case.read() boundary casesPractical GuideReturn 0: End of FileReturn -1: Check errno and act accordinglye.g. EINTR; restartReturn +N: # bytes placed into bufferSequential bytes != C stringDoesn't know or care about strings and NULbytesEnsure NUL termination before using printf debugstatements contents!A good alternative is to use write…write(2,buf,nbytes)What are we reading from?Get and maintaina reference to a fileInteger file descriptorPOSIX open()Let's dig deeper...... Errors and options... Directory organization & implementation... How does O/S Manage file descriptors?open – read – closefd=open("/tmp/1.txt", options);read(fd,buffer,sizeof(buffer));close(fd);open –... – read – ... – closefd=open("/tmp/1.txt", options);if(fd <1) error (e.g. File doesn't exist)r=read(fd,buffer,sizeof(buffer))handle special cases (eof,restart, media errors)close(fd);free up resourcesWhat can go wrong with open?Just open the file. Please.EACCES The requested access to the file is not allowed, or search permission is denied for one of the directories in the path prefix of pathname, or the file didnot exist yet and write access to the parent directory is not allowed. (See also path_resolution(2).)EEXIST pathname already exists and O_CREAT and O_EXCL were used.EFAULT pathname points outside your accessible address space.EISDIR pathname refers to a directory and the access requested involved writing (that is, O_WRONLY or O_RDWR is set).ELOOP Too many symbolic links were encountered in resolving pathname, or O_NOFOLLOW was specified but pathname was a symbolic link.EMFILE The process already has the maximum number of files open.ENAMETOOLONG pathname was too long.ENFILE The system limit on the total number of open files has been reached.ENODEV pathname refers to a device special file and no corresponding device exists. (This is a Linux kernel bug; in this situation ENXIO must be returned.)ENOENT O_CREAT is not set and the named file does not exist. Or, a directory component in pathname does not exist or is a dangling symbolic link.ENOMEM Insufficient kernel memory was available.ENOSPC pathname was to be created but the device containing pathname has no room for the new file.ENOTDIR A component used as a directory in pathname is not, in fact, a directory, or O_DIRECTORY was specified and pathname was not a directory.ENXIO O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process has the file open for reading. Or, the file is a device special file and nocorresponding device exists.EOVERFLOW pathname refers to a regular file, too large to be opened; see O_LARGEFILE above.EPERM The O_NOATIME flag was specified, but the effective user ID of the caller did not match the owner of the file and the caller was not privileged(CAP_FOWNER).EROFS pathname refers to a file on a read-only filesystem and write access was requested.ETXTBSY pathname refers to an executable image which is currently being executed and write access was requested.EWOULDBLOCK The O_NONBLOCK flag was specified, and an incompatible lease was held on the file (see fcntl(2)).EACCESEEXISTEFAULTEISDIRELOOPEMFILEENAMETOOLONGENFILEENODEVENOENTENOMEMENOSPCENOTDIRENXIOEOVERFLOWEPERMEROFSETXTBSYEWOULDBLOCKEACCESEEXISTEFAULTEISDIRELOOPEMFILEENAMETOOLONGENFILEENODEVENOENT ENOENT :O_CREAT is not setand the named file doesnot exist. Or, a directorycomponent in pathnamedoes not exist or is adangling symbolic link.Options and modesR vs. R/W vs. Write onlyTruncate an existing file before writingRead-Write-Execute Permissionsopen("/tmp/1.txt", ...)Pathnames -> travserse directoriesImposes a hierarchy on filesFiles can be referenced from more than onedirectoryOrganization of files useful for securitystatMeta-information about a filemodification and access timeKind of file (e.g. Directory | regular file?)Support for symbolic links● Three flavorsint stat(const char *path, struct stat *buf);int fstat(int filedes, struct stat *buf);● Info about link (more on this later)int lstat(const char *path, struct stat *buf);struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */};Stat macros (st_mode)S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link?* S_ISSOCK(m) socket?**( Not in POSIX.1-1996.)open –... – stat – ... – closefd=open("/tmp/1.txt", options);if(fd <1) error (e.g. File doesn't exist)r=fstat(fd,&buffer)handle special cases (eof,restart, mediaerrors)S_ISDIR(buffer.st_mode)close(fd);free up resourcesDirectories Robbins Ch.5.2struct dirent *entry; // add error handing!DIR *dirp =


View Full Document

U of I CS 241 - LECTURE NOTES

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?