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

An in-depth look into the unix file system, focusing on the posix api and file descriptors. Topics covered include file and directory concepts, i-nodes, file descriptors, and error handling. The document also discusses the use of the restart library for simplifying read and write operations.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-aog
koofers-user-aog 🇺🇸

10 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS241
The Unix File System
Lawrence Angrave
Midterm statistics
http://www.cs.uiuc.edu/class/fa06/cs241/MidTerm.pdf
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

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