Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

Similar documents
Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

CptS 360 (System Programming) Unit 6: Files and Directories

OPERATING SYSTEMS: Lesson 12: Directories

CSE 333 SECTION 3. POSIX I/O Functions

Operating System Labs. Yuanbin Wu

CSE 333 SECTION 3. POSIX I/O Functions

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

File and Directories. Advanced Programming in the UNIX Environment

File Management 1/34

CSci 4061 Introduction to Operating Systems. File Systems: Basics

The UNIX File System

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

The UNIX File System

CS , Spring Sample Exam 3

Files and Directories

CSC 271 Software I: Utilities and Internals

CS240: Programming in C

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

CSCE 313 Introduction to Computer Systems

Chapter 4. File Systems. Part 1

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

CSCI-E28 Lecture 4 Outline. Internal Structure of Files, Directories, File Systems. Work from user view tosystem view, write pwd

Operating System Labs. Yuanbin Wu

Files and Directories Filesystems from a user s perspective

Lecture 21 Systems Programming in C

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

File Types in Unix. Regular files which include text files (formatted) and binary (unformatted)

csci-e28 lecture 4samples page 1

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Homework 4 Answers. Due Date: Monday, May 27, 2002, at 11:59PM Points: 100. /* * macros */ #define SZBUFFER 1024 /* max length of input buffer */

Implementation should be efficient. Provide an abstraction to the user. Abstraction should be useful. Ownership and permissions.

File Systems Ch 4. 1 CS 422 T W Bennet Mississippi College

Files and File Systems

Typical File Extensions File Structure

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

RTEMS Filesystem Design Guide

CS4500/5500 Operating Systems File Systems and Implementations

Files and File System

File Management. Ezio Bartocci.

File Systems. Information Server 1. Content. Motivation. Motivation. File Systems. File Systems. Files

File Systems. What do we need to know?

CS333 Intro to Operating Systems. Jonathan Walpole

Files. Eric McCreath

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

FILE SYSTEMS. Jo, Heeseung

Input & Output 1: File systems

OPERATING SYSTEMS, ASSIGNMENT 4 FILE SYSTEM

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Long-term Information Storage Must store large amounts of data Information stored must survive the termination of the process using it Multiple proces

UNIX System Programming

Unix Filesystem. January 26 th, 2004 Class Meeting 2

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

UNIX File Systems. How UNIX Organizes and Accesses Files on Disk

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

System Programming. Introduction to Unix

File Systems. q Files and directories q Sharing and protection q File & directory implementation

structs as arguments

Files and Directories

Virtual File System. Don Porter CSE 306

CSCI 2132 Software Development. Lecture 4: Files and Directories

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

Systems Programming. 09. Filesystem in USErspace (FUSE) Alexander Holupirek

OPERATING SYSTEMS CS136

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

Files and the Filesystems. Linux Files

UNIX File Hierarchy: Structure and Commands

File Management. Chapter 12

5/8/2012. Creating and Changing Directories Chapter 7

File System. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Files and Directories

Linux C C man mkdir( ) mkdir Linux man mkdir mkdir( ) mkdir mkdir( )

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

Operating systems. Lecture 7

File System (FS) Highlights

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

File System: Interface and Implmentation

Inode. Local filesystems. The operations defined for local filesystems are divided in two parts:

Lecture 23: System-Level I/O

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Introduction to Linux

There is a general need for long-term and shared data storage: Files meet these requirements The file manager or file system within the OS

Chapter 1 - Introduction. September 8, 2016

UNIX System Calls. Sys Calls versus Library Func

CSE 410: Systems Programming

Virtual File System. Don Porter CSE 506

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Transcription:

Overview Unix System Programming Directories & Continuation Maria Hybinette, UGA 1 Last Week: Efficiency read/write The File File pointer File control/access Permissions, Meta Data, Ownership, umask, holes This Week: How to program with directories more» Reading: (finish Ch 4, skim Ch 5 IO Library (skim), Ch 6 (skim)). Repeat looking at the UNIX file system (and structure) Links Maria Hybinette, UGA 2 Outline Directory Implementation Directory implementation UNIX file system Links Subdirectory creation, and.. mkdir() & rmdir() Reading directories chdir() & getcwd() Walking over directories telldir() & seekdir() scandir() Maria Hybinette, UGA 3 A UNIX directory is a file:» owner, group owner, size, access, permissions, etc» many file operations can be used on directories Differences between file/directory:» modern UNIXs have special directory operations e.g. opendir(), readdir() Maria Hybinette, UGA 4 Directory Implementation Directory Structure Directory system function: Maps ASCII names onto what is needed to locate the data Where do we store the file s (Meta Data)?» Option 1: In a simple directory: fixed sized entries stored with the directory entry : MS DOS/Windows approach (start & end of data o file).» Option 2: Directory in each entry just refers to an i-node (UNIX implementation) that contains the (and pointer to actual data). games mail news work games mail news work Maria Hybinette, UGA 5 A directory file in UNIX is a sequence of lines; each line holds an i-node number (index-node) and a file name The data is stored as binary so we cannot simply cat to view it:» but some UNIXs allow an octal dump (other formats also available) : atlas:maria:187 od -c. 0000000 \0 \r 252 312 \0 \f \0 001. \0 \0 \0 \0 004 g 377 0000020 \0 \f \0 002.. \0 \0 \0 004 b 013 \0 024 \0 \n 0000040 m a r i a. h t m l \0 \0 \0 004 b 033 0000060 \0 024 \0 \n Maria Hybinette, UGA g u n n a r. t x t \0 6 \0

Something about i-nodes. "ls -i" lists the inode of a file nike:maria:22 ls -lai total 12 23335653 drwxr-xr-x. 3 maria users 4096 Sep 16 11:59. 23199745 drwxr-x---. 18 maria apache 4096 Sep 16 11:48.. 23594400 drwxr-xr-x. 2 maria users 4096 Sep 16 11:59 adir 23335656 -rw-r--r--. 1 maria users 0 Sep 16 11:59 afile nike:maria:36 cd / ; ls -lai sort -n -k 1» Across partitions Inode numbers could repeat» Inodes are unique per partition Find. inum xxxx delete (danger) Df Entire Disk: File System: Big Picture: The Unix File Structure Partition Table MBR Partition Partition Partition Free Space Management Boot block Super Block i-list Data blocks for files, directories, etc. i-node i-node i-node i-node Maria Hybinette, UGA 7 Maria Hybinette, UGA 8 Entire Disk & Booting Computer Disk is divided into 1+ partitions: one file system per partition Master Boot Record (typically sector 0) MBR- Pentium» first sector on disk» used to boot computer Partition Table» staring and ending address of each partition A program (e.g. the system Basic Input Output System or BIOS for Pentiums) reads in and executes the MBR» MBR searches for first active partition (noted in the partition table)» reads in its first block (the boot block) and executes it. Partition Table Partition Layout Boot block:» contains a hardware specific program that is called automatically to load UNIX at system startup time (loads OS in kernel space) Super block:» file system type, #blocks in file system Free space management (two lists):» a chain of free data block numbers» a chain of free i-node numbers i-list/i-node table:» administrative information about a file (meta-data: name, type, location, size, protection bits, ) structured into an array: inode table or simply the i-list» An i-node number: uniquely identifies a file in a file system is an index to the i-node table MBR Partition Partition Partition Maria Hybinette, UGA 9 Boot block Super Block i-list Data blocks for files, directories, etc. Maria Hybinette, UGA Free Space Management 10 File System Expanded UNIX Directories: Tree-Structured (not three) BB SB i-list directory data data directory i-node 895690 i-node Maria Hybinette, UGA 11 data data data Directory listing contains <name, index>, but a name can be directory, making branches.» Directory is stored and treated like a file» Special bit set in meta-data () for directories User programs can read directories (stat, fcntl). Only system programs can write directories» Specify full pathname by separating directories and files with special characters (e.g., \ or /) Special directories» Root / : Fixed index for meta-data (e.g., 2)» This directory:.» Parent directory:.. Example: mkdir /a/b/c» Read meta-data 2 / (by default 2 is root in linux), look for a : find < a, 5>» Read 5, look for b : find < b, 9>» Read 9, verify no c exists; allocate c and add c to directory Maria Hybinette, UGA 12

Acyclic-Graph Directories Links - Outline More general than tree structure» Add connections across the tree (no cycles)» Create links from one file (or directory) to another Hard link: ln a b ( a must exist already)» Idea: Can use name a or b to get to same file data» Implementation: Multiple directory entries point to same meta-data link( maria.html, tucker.html ); Why Links? Creating a Link Seeing Links Removing a Link Symbolic Links Implementation Hybinette, UGA 287243 tucker.html Maria 13 Maria Hybinette, UGA 14 A link is a pointer to a file Useful for sharing files; Why Links?» A file can be shared by giving each person their own link (pointer to it) ln <existing-file> <new-pointer> Maria types, in directory: ~/maria ln tucker-todo.txt /home/tucker/todo.txt / A link is a pointer to a file Useful for sharing files; What is a Link?» A file can be shared by giving each person their own link (pointer to it) ln <existing-file> <new-pointer> Maria types, in directory: ~/maria ln tucker-todo.txt /home/tucker/todo.txt / /home/maria/tucker-todo.txt bin home tmp /home/maria/tucker-todo.txt bin home tmp and /home/tucker/todo.txt tucker maria and /home/tucker/todo.txt tucker maria a.txt tucker-todo.txt g.txt a.txt tucker-todo.txt g.txt Maria Hybinette, UGA 15 Maria Hybinette, UGA 16 Creating Links Seeing Links Changes to a file affect every link: atlas cat file_a This is file A. atlas ln file_a file_b atlas cat file_b This is file A atlas echo appending this to b >> file_b atlas cat file_b This is file A. appending this to b atlas cat file_a This is file A. appending this to b Maria Hybinette, UGA 17 Compare status information : saffron:maria:104 ls -l file_a file_b file_c -rw-r--r-- 2 maria 36 May 24 10:52 file_a -rw-r--r-- 2 maria 36 May 24 10:52 file_b -rw-r--r-- 1 maria 16 May 24 10:55 file_c File mode, # links, owners name, group name, #bytes, date, pathname Look at i-node number: saffron:maria:105 ls -i file_a file_b file_c 3534 file_a 3534 file_b 5800 file_c Directories may appear to have more links: saffron:maria:106 ls -ld dir drwxr-xr-x 2 maria users 68 Apr 7 17:57 dir/ saffron:maria:107 mkdir dir/hello saffron:maria:108 ls -ld dir drwxr-xr-x 3 maria users 68 Apr 7 17:58 dir/ This is because subdirectories (e.g. directories inside dir/)have a link back to their parent. Maria Hybinette, UGA 18

Removing a Link Symbolic Links Removing or deleting a link does not necessarily remove the file Only when the file and every link is gone will the file be removed The links described so far are hard links» A hard link is a pointer to a file which must be on the same file system A symbolic link is an in-direct» Stores the pathname of the file that it points to» Symbolic links can link across file systems Symbolic links are listed differently: to a file saffron:ingrid:62 ln -s dir ~/unix/d/sdir saffron:ingrid:62 ls -lfd dir ~/unix/d/sdir lrwxr-xr-x 1 ingrid staff 3 1 Apr 21:51 /home/ingrid/unix/d/sdir@ -> dir drwxr-xr-x 3 ingrid staff 102 1 Apr 21:39 dir/ Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Link Creation, Update & Removal link() cp bob new ln bob new ln -s bob new new bob <unistd.h> long link( const char *oldpath, const char *newpath ); Meaning of: update delete bob new» link( maria.html, tucker.html ); bob new XY bob new XY new? bob XY 287243 tucker.html Maria Hybinette, UGA 21 Maria Hybinette, UGA 22 unlink() Example unlink clears directory record» usually means that the i-node number is set to 0» the file may not be affected The i-node is only deleted when the last link to it is removed; the file data blocks are deleted/reclaimed when there are no processes having the file opened. Maria Hybinette, UGA 23 <sys/stat.h> <sys/types.h> <fcntl.h> int main(void) if( open( temp, O_RDWR ) < 0 ) perror( open error ); exit( 1 ); if( unlink( temp ) < 0 ) perror( unlink error ); exit( 1 ); printf( file temp is unlinked\n ); exit( 0 ); saffron make unlinktemp gcc unlinktemp.c -o unlinktemp saffron./unlinktemp file temp is unlinked saffron./unlinktemp open error: No such file or directory Maria Hybinette, UGA 24

remove() rename() <stdio.h> int remove( const char *pathname ); C Library function (not a system call) Delete a name and possibly the file it refers to.» It calls unlink() for files and rmdir() for directories <stdio.h> int rename( const char *oldpath, const char *newpath ); Changes the names of oldpath to newpath (for both directories and files) If oldpath is open or is a nonexistent file, or if newpath names a file that already exists, then the action of rename() is implementation dependent. Maria Hybinette, UGA 25 Maria Hybinette, UGA 26 symlink() readlink() <stdio.h> int symlink( const char *oldpath, const char *newpath ); Creates a symbolic link named newpath which contains the string oldpath Symbolic links are interpreted at run-time Dangling link - may point to a nonexisting file If newpath exists it will not be overwritten Maria Hybinette, UGA 27 int readlink( const char *path, char buf, size_t bufsize ); Read value of a symbolic link (does not follow the link)» Places the contents of the symbolic link path in the buffer buf which has size bufsize» Does not append a NULL character to buf Return value» The count of characters placed in the buffer if it succeeds» -1 on error Maria Hybinette, UGA 28 Subdirectory Creation shell command mkdir uga causes» the creation of uga directory file and new i-node» an i-node number and name are added to the parent directory 288000 uga. and... and.. are stored as ordinary file names with i-node numbers pointing to the correct directory files home Directory maria 300. 155.. 589 uga Directory faculty 155. 111.. 300 maria 600 tucker Directory tucker 600. 155.. 680 gtech uga maria tucker gtech Maria Hybinette, UGA 29 Maria Hybinette, UGA 30

mkdir() rmdir() <sys/types.h> <fcntl.h> <unistd.h> int mkdir( char *path, mode_t mode ); Creates a new directory with the specified mode Returns 0 if OK and -1 on error. and.. entries are added automatically mode must include execute permissions so user(s) can use cd» e.g. 0755 <unistd.h> int rmdir( char *pathname ); Deletes an empty directory Returns 0 if OK, -1 on error Will delay until other processes have stopped using the directory Maria Hybinette, UGA 31 Maria Hybinette, UGA 32 Reading Directories Directories can be read by any one who have access Directories can be modified only by the kernel» Note we may be able to create new files and remove files from a directory Maria Hybinette, UGA 33 Reading Directories <sys/types.h> <dirent.h> DIR *opendir( char *pathname ); struct dirent *readdir( DIR *dp ); void rewinddir( DIR *dp ); int closedir( DIR *dp ); DIR is a directory stream (similar to FILE)» when a directory is first opened the stream points to the first entry in the directory dirent next slide returns a pointer if OK NULL on error returns a pointer if OK NULL at end or on error returns 0 if OK -1 on error Maria Hybinette, UGA 34 dirent Example: listdir.c struct dirent long d_ino; /* i-node number */ char d_name[name_max + 1]; /* null terminated file name */ off_t d_off; /* offset to next record */ unsigned short d_reclen; /* record length */ NAME_MAX not available on all UNIXs, may need to use a function to define it (fpathconf defined in unistd.h) - gets configurable pathname variables) Maria Hybinette, UGA 35 <dirent.h> DIR *dp; struct dirent *dir; saffron:50./listdir... listdir.c listdir saffron:51 if( (dp = opendir(. )) == NULL ) fprintf( stderr, Cannot open dir\n ); exit( 1 ); /* read entries */ while( (dir = readdir( dp )) = NULL ) if( dir-> d_ino = 0 ) /* ignore empty records */ printf( %s\n, dir->d_name ); closedir( dp ); return 0; /* end main */ Maria Hybinette, UGA 36

chdir() Example to_tmp.c : Changing current working directory <unistd.h> int chdir( char *pathname ); int fchdir( int fd ); Every process has a current working directory (search for a relative pathname starts here) Change the current working director working directory (cwd) of the calling process Returns 0 if OK, -1 on error Directory change is limited to within the program <unistd.h> if( chdir( /tmp ) < 0 ) saffron pwd /usr/lib saffron./to_tmp In /tmp saffron pwd /usr/lib saffron fprintf( stderr, chdir failed\n ); exit( 1 ); else printf( In /tmp\n ); exit( 0 ); Maria Hybinette, UGA 37 Maria Hybinette, UGA 38 getcwd() Example printcwd.c <unistd.h> int *getcwd( char *buf, size_t 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 maximum length of buf ); Maria Hybinette, UGA 39 <unistd.h> #define BUFSIZE 255 saffron pwd /usr/lib saffron./printcwd Current directory is /usr/lib saffron char name[bufsize+1]; /* accommodate \0 */ if( getcwd( name, BUFSIZE+1 ) == NULL ) perror( getcwd error\n ); else printf( Current directory is %s\n, name ); exit(0); Maria Hybinette, UGA 40 Walking over Directories Visit every file in a specified directory and all of its subdirectories» visit means the get name of the file Apply a user-defined function to every visited file Maria Hybinette, UGA 41 ftw() <ftw.h> /* file tree walk starting at dir */ int ftw( char *dir, MyFunc *fp, int depth ); /* apply MyFunc() to each visited file */ typedef int MyFunc( const char *file, struct stat *sbuf, int flag ); depth is the maximum number of directories that can be opened at once. Safest value is 1, although it slows down ftw() Returns 0 on success (visiting every file), -1 on error file is the pathname relative to the start directory, it is passed to MyFunc() automatically by ftw() as it visits each file sbuf is a pointer to the stat information for the current file examined Maria Hybinette, UGA 42

ftw() - (cont) Example: shower.c 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 MyFunc() returns a non-zero value then ftw() will terminate <sys/types.h> <sys/stat.h> <ftw.h> int shower( const char *file, const struct stat *sbuf, int flag ); ftw(,, shower, 1 ); Print the names of all the file found below the current directory Maria Hybinette, UGA 43 Maria Hybinette, UGA 44 Example: shower.c (cont) telldir() & seekdir() int shower( const char *file, const struct stat *sbuf, int flag ) if( flag == FTW_F ) /* is a file */ printf( Found: %s, file ); return 0; <dirent.h> off_t telldir( DIR *dp ); Returns the location of the current record in the directory, -1 on error <dirent.h> off_t seekdir( DIR *dp, off_t loc ); Set the location in the directory file Maria Hybinette, UGA 45 Maria Hybinette, UGA 46 scandir() <dirent.h> off_t scandir( char *dir, 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 ); 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 by pitems The items in the array are sorted using qsort() using the comparison function (cfp) scandir() and alpasort() not implemented on Solaris Maria Hybinette, UGA 47 dirent.h includes one predefined comparison function, which compares directory item names int alphasort( const struct dirent *d1, const struct dirent *d2 ); Causes the pitems array to be sorted in increasing alphabetical order by item name pitems NULL item 1 item 2 item 3 Maria Hybinette, UGA 48

Example geth.c <dirent.h> <sys/types.h> /* our selection function */ int selecth( struct dirent *d ); struct dirent ** pitems; List files/subdirectories in the current directory which begin with an h sorted in alphabetical order int selecth( struct dirent *d ) if( d-> d_name[0] == h ) return 1; return 0; scandir(., &pitems, selecth, alphasort ); while( *pitems = NULL ) printf( %s\n, (*pitems)->d_name ); pitems++: exit( 0 ); Maria Hybinette, UGA 49 Maria Hybinette, UGA 50