240-491 Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system 240-491 Special.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
UNIX File Systems How UNIX Organizes and Accesses Files on Disk.
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Exploring the UNIX File System and File Security
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 333 Introduction to Operating Systems Class 17 - File Systems Jonathan Walpole Computer Science Portland State University.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
Guide To UNIX Using Linux Third Edition
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Guide To UNIX Using Linux Fourth Edition
1 UNIX Systems Programming Interprocess communication.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
The file structure and related utilities CS240 Computer Science II.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1Fall 2008, Chapter 11 Disk Hardware Arm can move in and out Read / write head can access a ring of data as the disk rotates Disk consists of one or more.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Slide 1 Interprocess communication. Slide 2 1. Pipes  A form of interprocess communication Between processes that have a common ancestor  Typical use:
Chapter 5 Files and Directories Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
File Systems Long-term Information Storage Store large amounts of information Information must survive the termination of the process using it Multiple.
ENGR 3950U / CSCI 3020U: Operating Systems Description and C Code of Major Functions in Simulated Unix File System. Instructor: Dr. Kamran Sartipi Faculty.
Chapter Two Exploring the UNIX File System and File Security.
Chapter 4. INTERNAL REPRESENTATION OF FILES
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
CS333 Intro to Operating Systems Jonathan Walpole.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
UNIX Files File organization and a few primitives.
Chapter Two Exploring the UNIX File System and File Security.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Navigating Directories
CE Operating Systems Lecture 17 File systems – interface and implementation.
1 © 2001 John Urrutia. All rights reserved. Chapter 4 The LINUX Filesystem.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
Adv. UNIX: FileStr/11 Advanced UNIX v Objectives –to supplement the “Introduction to UNIX” slides with extra information on files Special.
File Systems. 2 What is a file? A repository for data Is long lasting (until explicitly deleted).
CS 333 Introduction to Operating Systems Class 17 - File Systems Jonathan Walpole Computer Science Portland State University.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
Linux File system Implementations
Slide: 1 UNIX FILE SYSTEM By:Qing Yang ID: Operating System Research Topic December, 2000.
NCHU System & Network Lab Lab 14 File and Directory.
CSCI 330 UNIX and Network Programming
Files & Directories 1 CS 360 dir1. Slide 2 dir1 CS 360, WSU Vancouver Course Topics Program development review Files & Directories Tool Building Processes.
File system In computing, a file system is a method of storing and organizing computer files and the data they contain to make it easy to find and access.
Lecture 19 Linux/Unix – File System
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Learning basic Unix command It 325 operating system.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 3.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
Exploring the UNIX File System and File Security
CS510 Operating System Foundations
An overview of the kernel structure
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
January 26th, 2004 Class Meeting 2
Chapter 5 File Systems -Compiled for MCA, PU
Presentation transcript:

Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special Topics in Comp. Eng. 2 Semester 2, The Directory

Adv. UNIX: dirs/182 Overview 1. Directory Implementation 2. UNIX Memory Structure 3. link() and unlink() again 4. Subdirectory Creation 5. “. ” and “.. ” 6. mkdir() 7. rmdir() continued

Adv. UNIX: dirs/ Reading Directories 9. chdir() 10. getcwd() 11. Walking over Directories 12. telldir() and seekdir() 13. scandir()

Adv. UNIX: dirs/ Directory Implementation v A UNIX directory is a file: –it has an owner, group owner, size, access permissions, etc. –many file operations can be used on directories v Differences: –modern UNIXs have special directory operations e.g. opendir(), readdir()

Adv. UNIX: dirs/185 Structure v A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The data is stored as binary, so we cannot simply use cat to view it –but some older UNIXs allow: $ od -c dir-name : : “fred.html” “abc” “bookmark.c”

Adv. UNIX: dirs/ UNIX Memory Structure partition disk drive file system super block boot block I-listData blocks for files, dirs, etc i-node

Adv. UNIX: dirs/187 File System in More Detail datadir blki-listdata dir blk Data Blocks i-node filename i-node number no : : filenameno : :

Adv. UNIX: dirs/188 v Boot block: a hardware specific program that is called automatically to load UNIX at system startup time. v Super block -- it contains two lists: –a chain of free data block numbers –a chain of free i-node numbers

Adv. UNIX: dirs/ link() and unlink() Again  Meaning of: link(“abc”, “xyz”) : : “fred.html” “abc” “bookmark.c” 207“xyz” continued

Adv. UNIX: dirs/1810  unlink() clears the directory record –usually means that the i-node number is set to 0 –the file may not be affected v The i-node is only deleted when the last link to it is removed; the data block for the file is also deleted (reclaimed).

Adv. UNIX: dirs/ Subdirectory Creation  mkdir foo causes: –the creation of a foo directory file and an i- node for it –an i-node number and name are added to the parent directory file : : “fred.html” “abc” “bookmark.c” 201“foo”

Adv. UNIX: dirs/ “. ” and “.. ”  “. ” and “.. ” are stored as ordinary file names with i-node numbers pointing to the correct directory files. v Example: ben bookmemos continued

Adv. UNIX: dirs/1813 In more detail: “.” “..” “book” 401“memos” Directory ben “.” “..” “chap1” 567“chap2” Directory book “chap3” “.” “..” “kh” “kd” Directory memos “mw”590

Adv. UNIX: dirs/ mkdir()  #include #include #include int mkdir(char *pathname, mode_t mode);  Creates a new directory with the specified mode : return 0 if ok, -1 on error continued

Adv. UNIX: dirs/1815  “. ” and “.. ” entries are added automatically  mode must include execute permissions so the user(s) can use cd. e.g.0755

Adv. UNIX: dirs/ rmdir()  #include int rmdir(char *pathname); v Delete an empty directory; return 0 if ok, -1 on error. v Will delay until other processes have stopped using the directory.

Adv. UNIX: dirs/ Reading Directories v #include #include DIR *opendir(char *pathname); struct dirent *readdir(DIR *dp); void rewinddir(DIR *dp); int closedir(DIR *dp); returns a pointer if ok, NULL on error returns a pointer if ok, NULL at end or on error return 0 if ok, -1 on error

Adv. UNIX: dirs/1818 dirent and DIR  struct dirent { long d_ino; /* i-node number */ char d_name[NAME_MAX+1]; /* fname */ off_t d_off; /* offset to next rec */ unsigned short d_reclen; /* record length */ }  DIR is a directory stream (similar to FILE ) –when a directory is first opened, the stream points to the first entry in the directory

Adv. UNIX: dirs/1819 Example: listdir.c #include #include int main() { DIR *dp; struct dirent *dir; if ((dp = opendir(“.”)) == NULL) { fprintf(stderr, “Cannot open dir\n”); exit(1); } : continued List the contents of the current directory.

Adv. UNIX: dirs/1820 /* read entries */ while ((dir = readdir(dp)) != NULL) /* ignore empty records */ if (dir->d_ino != 0) printf(“%s\n”, dir->d_name); closedir(dp); return 0; } /* read entries */ while ((dir = readdir(dp)) != NULL) /* ignore empty records */ if (dir->d_ino != 0) printf(“%s\n”, dir->d_name); closedir(dp); return 0; }

Adv. UNIX: dirs/ chdir()  #include int chdir(char *pathname); int fchdir(int fd); v Change the current working directory (cwd) of the calling process; return 0 if ok, -1 on error.

Adv. UNIX: dirs/1822 Example: cd to /tmp  Part of to_tmp.c : : if (chdir(“/tmp”) < 0 printf(“chdir error\n”); else printf(“In /tmp\n”);

Adv. UNIX: dirs/1823 Directory Change is Local v The directory change is limited to within the program. v e.g. $ pwd /usr/lib $ to_tmp /* from last slide */ In /tmp $ pwd /usr/lib

Adv. UNIX: dirs/ getcwd()  #include char *getcwd(char *buf, int size);  Store the cwd of the calling process in buf ; return buf if ok, NULL on error.  buf must be big enough for the pathname string ( size specifies the length of buf ).

Adv. UNIX: dirs/1825 Example #include #include #include /* for NAME_MAX */ int main() { char name[NAME_MAX+1]; if (getcwd(name, NAME_MAX+1) == NULL) printf(“getcwd error\n”); else printf(“cwd = %s\n”, name): :

Adv. UNIX: dirs/ Walking over Directories v 'Visit' every file in a specified directory and all of its subdirectories –visit means get the name of the file v Apply a user-defined function to every visited file.

Adv. UNIX: dirs/1827 Function Prototypes v #include /* ftw means file tree walk, staring at directory */ int ftw(char *directory, MyFunc *fp, int depth); /* apply MyFunc() to each visited file */ typedef int MyFunc(const char *file, struct stat *sbuf, int flag); continued

Adv. UNIX: dirs/1828  depth is the maximum number of directories that can be open at once. Safest value is 1, although it slows down ftw().  Result of ftw() : 0 for a successful visit of every file, -1 on error.

Adv. UNIX: dirs/1829 MyFunc Details  The file argument is the pathname relative to the start directory –it will be passed to MyFunc() automatically by ftw() as it visits each file  sbuf argument is a pointer to the stat information for the file being examined. continued

Adv. UNIX: dirs/1830  The flag argument will be set to one of the following for the item being examined: –FTW_F Item is a regular file. –FTW_D Item is a directory. –FTW_NS Could not get stat info for item. –FTW_DNR Directory cannot be read.  If the MyFunc function returns a non-zero value then the ftw() walk will terminate.

Adv. UNIX: dirs/1831 Example: shower.c #include #include #include #include int shower(const char *file, const struct stat *sbuf, int flag); void main() { ftw(“.”, shower, 1); } continued Print the names of all the files found below the current directory.

Adv. UNIX: dirs/1832 int shower(const char *file, const struct stat *sbuf, int flag) { if (flag == FTW_F) /* is a file */ printf("Found: %s\n", file); return 0; }

Adv. UNIX: dirs/ telldir() and seekdir()  #include off_t telldir(DIR *dp); v Returns the location of the current record in the directory file, -1 on error.  #include void seekdir(DIR *dp, off_t loc); v Set the location in the directory file.

Adv. UNIX: dirs/ scandir()  Scan the directory file calling the selection function ( sfp ) on each directory item.  Items for which sfp returns non-zero are stored in an array pointed to by pitems.  The items in the array are sorted using qsort() using the comparison function ( cfp ). continued

Adv. UNIX: dirs/1835 Function Prototypes v #include int scandir(char *directory, struct dirent ***pitems, SelectFnc *sfp, CompareFnc *cfp); /* selection function for searching */ typedef int SelectFnc(const struct dirent *d); /* comparison function for sorting */ typedef int CompareFnc(const struct dirent *d1, const struct dirent *d2); continued

Adv. UNIX: dirs/1836 pitems Pictured item1 NULL pitems continued item2 item3

Adv. UNIX: dirs/1837  dirent.h includes one pre-defined comparison function, which compares directory item names: int alphasort(const struct dirent *d1, const struct dirent *d2);  This causes the pitems array to be sorted into increasing alphabetical order by item name.

Adv. UNIX: dirs/1838 Example: geth.c #include #include /* our selection function */ int selectH(const struct dirent *d); void main() { struct dirent **pitems; scandir(“.”, &pitems, selectH, alphasort); while (*pitems != NULL) { printf(“%s\n”, (*pitems)->d_name); pitems++; } } continued List the files/subdirectories in the current directory which begin with 'h', sorted in alpha order.

Adv. UNIX: dirs/1839 int selectH(const struct dirent *d) /* Does item name start with ‘h’? */ { if (d->d_name[0] == ‘h’) return 1; return 0; } int selectH(const struct dirent *d) /* Does item name start with ‘h’? */ { if (d->d_name[0] == ‘h’) return 1; return 0; }