DOC PREVIEW
U of I CS 241 - Discussion Week 8

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 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 28 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 28 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 28 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 28 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 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Navigating File SystemsOutlineUNIX File SystemsSlide 4Directories are files too!Directory functionsDirectory reading functionsExample 1: ds8-p1.cWhat’s in a directory entry?Example 2: ds8-p2.cMore Directory FunctionsWarning! Warning!How to recursively traverse a directory treeFile information: statExample 3: ds8-p3.cUseful fields and macros in struct statLinksLink FunctionsHard Link ExampleHard Link Example (contd)Slide 21Symbolic Link FunctionSoft Link ExampleSoft Link Example (contd)Slide 25Link numberExerciseSummaryNavigating File SystemsCS 241 Discussion Section Week 83/26/07 – 3/30/07Stephen KloderOutlineUNIX file systemsinodesdirectoriesUNIX File OperationsDirectoryFile StatusLinksUNIX File Systemsinode: per-file data structureAdvantageEfficient for small filesFlexible if the size changesDisadvantageFile must fit in a single disk partitionUNIX File Systemsinode (continued)Storing Large FilesDirectories are files too!Directories, like files, have inodes with attributes and pointers to disk blocksEach directory contains the name and i-node for each file in the directory.Directory functions#include <unistd.h>Change the directoryint chdir(const char *path);Get the current working directorychar *getcwd(char *buf, size_t size);Get the maximum path lengthlong fpathconf(int fildes, int name);long pathconf(const char *path, int name);long sysconf(int name);Directory reading functions#include <dirent.h>Open the directoryDIR *opendir(const char *dirname);Close the directoryint closedir(DIR *dirp);Read the directorystruct dirent *readdir(DIR *dirp);Example 1: ds8-p1.cUse opendir and readdir to print all the filenames in the current directory:#include <dirent.h>…DIR *dir;struct dirent *entry;dir = opendir(“.”);while(entry = readdir(dir))printf(“%s\n”,entry->d_name);closedir(dir);Remember to include error checking!!What’s in a directory entry?struct direntMember Fieldschar d_name[]Null-terminated file nameino_t d_filenoinode numberunsigned char d_namlenLength of file nameunsigned char d_typeType of fileDT_REG, DT_DIR, DT_FIFO, DT_SOCK, DT_CHR, DT_BLK, DT_UNKNOWNExample 2: ds8-p2.cModify ds8-p1.c to use the member fields of struct dirent to display the inode for each file, as well as whether the file is a directory or a regular file.if (entry.d_type == DT_DIR)// File is a directoryMore Directory Functions#include <dirent.h>Set the position of next readdirvoid seekdir(DIR *dir, off_t offset);Set the position back to the start of the directoryvoid rewinddir(DIR *dirp);Get the current location of directory streamoff_t telldir (DIR *dir);Warning! Warning!opendir and readdir are NOT thread-safe. DO NOT open two directories at the same time!How to recursively traverse a directory tree1. Open the directory (opendir)2. Read each entry (readdir)If the file is a directory (d_type), store it (e.g. in an array of strings).3. Close the directory (closedir)4. Traverse each saved subdirectory, EXCEPT '.' and '..'File information: statUse the stat functions to view the file’s inode’s attributes.#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>For a file:int stat(const char *restrict path, struct stat *restrict buf);For a link:int lstat(const char *restrict path, struct stat *restrict buf);For a file descriptor:int fstat(int fildes, struct stat *buf);Example 3: ds8-p3.cModify ds8-p2.c to also give file information about each file.–How large is each file?–Which files are world-readable?–Which files have been modified in the last 24 hours?Hint: man 2 statUseful fields and macros in struct stat•stat.st_size–File size•stat.st_mode–File type–User permissions–Etc.•stat.st_mtime–Time of last modification•S_ISDIR(stat.st_mode)–Is this a directory?LinksHard LinkDirectory Entrye.g. all regular filesSymbolic LinkAlso called a Soft LinkA special file that serves as a reference to another fileLink Functions#include <unistd.h>To create a new link:int link(const char *oldpath, const char *newpath);Same as lnTo removes an entry from the directory:int unlink(const char *path);Same as rmReturns 0 if successful, -1 with errno set if unsuccessfulHard Link ExampleCommand Lineln /dirA/name1 /dirB/name2C Code Segmentsif (link("/dirA/name1", "/dirB/name2") == -1) perror("Failed to make a new link in /dirB");Hard Link Example (contd)Q: What happens if /dirA/name1 is deleted and recreated?Hard Link Example (contd)A: /dirA/name1 and /dirB/name2 are now two distinct files.Symbolic Link Function#include <unistd.h>To create a symbolic link:int symlink(const char *oldpath, const char *newpath);Same function as command ln –sReturns 0 if successful, -1 with errno set if unsuccessfulSoft Link ExampleCommand Lineln –s /dirA/name1 /dirB/name2C Code Segmentsif (symlink("/dirA/name1", "/dirB/name2") == -1) perror("Failed to create a symbolic link in /dirB");Soft Link Example (contd)Q: What happens if /dirA/name1 to is deleted and recreated?Soft Link Example (contd)A: /dirA/name1 has a different inode, but /dir/name2 still links to it.Link numberThe link number (the st_nlink field in stat) tells how many directory entries link to this inode. The link number is:–Set to 1 when a file is created–Incremented when link is called–Decremented when unlink is calledThe link number appears in the second column of the output of ls –l. Try it!The link number only counts hard links, not soft links.ExerciseWhat happens in the following figure if rm /dirA/name1 is executed?SummaryUNIX File Systemsi-nodesdirectoriesFile operationsDirectoryopendir, readdir, closedirFile Statusstat, fstat, lstatLinkslink, symlink,


View Full Document

U of I CS 241 - Discussion Week 8

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 Discussion Week 8
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 Discussion Week 8 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 Discussion Week 8 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?