Docsity

Understanding Unix File System: POSIX API and File Descriptors - Prof. Indranil Gupta, Study notes of Computer Science

Partial preview of the text

Download Understanding Unix File System: POSIX API and File Descriptors - Prof. Indranil Gupta and more Study notes Computer Science in PDF only on Docsity!

CS

The Unix File System Lawrence Angrave Midterm statistics http://www.cs.uiuc.edu/class/fa06/cs241/MidTerm.pdf

API... open? stat? Why so many options in open? How do I make my code robust? What concepts underpin the POISX filesystem API? What exactly is a file, directory...? I-node?

So what?

Unicode file Typed Fields and Records Key indices Unified. Get the contents of a file as bytes #include <unistd.h> ssize_t read(int fildes, void *buf, size_t nbyte);

read

Don't need to read entire file ... Grab bytes into your buffer ... Next call to read will read next unread byte Can even read the bytes of a 'directory'

It usually works

as you might assume

Upon successful completion, read(), readv(), and pread() return the number of bytes actually read and placed in the buffer. The system guarantees to read the number of bytes requested if the descriptor references a normal file that has that many bytes left before the end-of-file, but in no other case.

read() boundary cases

Practical Guide

Return 0: End of File Return -1: Check errno and act accordingly e.g. EINTR; restart Return +N: # bytes placed into buffer

What are we reading from?

Get and maintain

a reference to a file

Integer file descriptor POSIX open() Let's dig deeper... ... Errors and options ... Directory organization & implementation ... How does O/S Manage file descriptors?

open โ€“... โ€“ read โ€“ ... โ€“ close

fd=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 resources

What can go wrong with open?

EACCES

EEXIST

EFAULT

EISDIR

ELOOP

EMFILE

ENAMETOOLONG

ENFILE

ENODEV

ENOENT

ENOMEM

ENOSPC

ENOTDIR

ENXIO

EOVERFLOW

EPERM

EROFS

ETXTBSY

EWOULDBLOCK

EACCES

EEXIST

EFAULT

EISDIR

ELOOP

EMFILE

ENAMETOOLONG

ENFILE

ENODEV

ENOENT

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.

open("/tmp/1.txt", ...)

Pathnames -> travserse directories Imposes a hierarchy on files Files can be referenced from more than one directory Organization of files useful for security

stat

Meta-information about a file modification and access time Kind of file (e.g. Directory | regular file?) Support for symbolic links