Unix Files Course code: 10CS62 Prepared by Department of CSE

Slides:



Advertisements
Similar presentations
The UNIX File System Harry Chen Department of CSEE University of MD Baltimore County.
Advertisements

Concepts about the file system 2. The disk structure 3. Files in disk – The ext2 FS 4. The Virtual File System (c) 2013, Prof. Jordi Garcia.
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Chapter 10: File-System Interface
File System Interface CSCI 444/544 Operating Systems Fall 2008.
Exploring the UNIX File System and File Security
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
Linux+ Guide to Linux Certification, Second Edition
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
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
1 Course Outline Processes & Threads CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Networks, Protection and Security.
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.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
CSCI 1730 April 2 nd, C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled;
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Files & Directories Objectives –to be able to describe and use the Unix file system model and concepts Contents –directory structure –file system concepts.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Linux+ Guide to Linux Certification, Second Edition
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.
Chapter Two Exploring the UNIX File System and File Security.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
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.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
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?
Linux File system Implementations
File Systems cs550 Operating Systems David Monismith.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Chapter 10: File-System Interface Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 1, 2005 File-System Interface.
The Unix File system (UFS) Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University.
THE FILE SYSTEM Files long-term storage RAM short-term storage Programs, data, and text are all stored in files, which is stored on.
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
File Systems - Part I CS Introduction to Operating Systems.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
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.
Chapter 39 File and Directory Chien-Chung Shen CIS/UD
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.
BILKENT UNIVERSITY DEPARTMENT OF COMPUTER TECHNOLOGY AND INFORMATION SYSTEMS CTIS156 INFORMATION TECHNOLOGIES II FILES AND FILE SYSTEM STRUCTURE.
Linux Filesystem Management
Week 12 - Thursday CS222.
Today topics: File System Implementation
Operation System Program 4
Exploring the UNIX File System and File Security
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S
An overview of the kernel structure
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
Directory Structure A collection of nodes containing information about all files Directory Files F 1 F 2 F 3 F 4 F n Both the directory structure and the.
File Structure Related system calls
Department of School of Computing and Engineering
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
Chien-Chung Shen CIS, UD
Internal Representation of Files
Chapter 4: The Linux Filesystem
Lecture 4: File-System Interface
January 26th, 2004 Class Meeting 2
Chapter 5 File Systems -Compiled for MCA, PU
Introduction to Operating Systems
Presentation transcript:

Unix Files Course code: 10CS62 Prepared by Department of CSE Engineered for Tomorrow Prepared by Department of CSE

Contents File Types The UNIX and POSIX File System The UNIX and POSIX File Attributes Inodes in UNIX System V Application Program Interface to Files UNIX Kernel Support for Files Relationship of C Stream Pointers and File Descriptors Directory Files Hard and Symbolic Links.

Engineered for Tomorrow Files are the building blocks of any OS , as most operations in a system deal with files. When commands are executed in UNIX, the UNIX kernel fetches the corresponding executable file from the file system, loads its instruction text to memory and creates a process to execute the command.

FILE TYPES Regular file Directory File FIFO File Character Device File Engineered for Tomorrow FILE TYPES Regular file Directory File FIFO File Character Device File Block Device File

May be either text file or binary file. Engineered for Tomorrow 1.Regular file May be either text file or binary file. UNIX and Posix system don’t make any distinction between these two files. Both may be executable provided that the execution rights of these files are set and these files may be read or written to by the users with the appropriate access permission.

They can be removed by specific system commands like rm . Engineered for Tomorrow Regular files may be created, browsed through and modified by various means such as rext editors or compilers. They can be removed by specific system commands like rm .

Engineered for Tomorrow 2.Directory File It is like a file folder that contains other files, subdirectory files. It provides a mean for users to organize their files into some heirarchical structures based on the file relationship or users. /bin directory contains all system executable programs such as cat, rm, ls etc

A directory may be created using mkdir command mkdir /usr/foo/xyz Engineered for Tomorrow A directory may be created using mkdir command mkdir /usr/foo/xyz A directory is considered to be empty if it contains no other files except “.” and “..” files And it may be removed via rmdir command. Rmdir /usr/foo/xyz

Engineered for Tomorrow 3.Block Device File It represents a physical device that transmits data a block at a time. Examples include harddisk drives and floppy disk drives.

Engineered for Tomorrow 4.Character Device File A character device file represents a physical device that transmitts data in a character-based manner. Examples include line printers, modems and consoles.

A device file is created via mknod command mknod /dev/cdsk c 115 5 Engineered for Tomorrow A device file is created via mknod command mknod /dev/cdsk c 115 5 The above command creates character device file with name /dev/cdsk Major and minor number of the device file are 115 and 5 ‘c’ specifies that files to be created is a character file.

Engineered for Tomorrow Major number: is an index to kernel table that contains the address of all device driver functions known to the system. Whenever a process reads data from or writes data to a device file, kernel uses the device file’s major number to select and invoke a device driver function .

Engineered for Tomorrow Minor number: it is an integer value to be passed as an argument to device driver function when it is called. It tells device driver function what actual physical device it is talking to. And I/O buffering scheme to be used for data transfer.

A block device file is created by mknod command using ‘b’ argument. Engineered for Tomorrow A block device file is created by mknod command using ‘b’ argument. mknod /dev/bdsk b 287 101 mknod must be invoked through super user privileges.

Engineered for Tomorrow To access a device accordingly, the operating system must be told what to do. Inside the kernel are functions for each of the devices the kernel is going to access. All the routines for a specific device are jointly referred to as the device driver. Each device on the system has its own device driver. Within each device driver are the functions that are used to access the device.

Engineered for Tomorrow For devices such as a hard disk or terminal, the system needs to be able to (among other things) open the device, write to the device, read from the device, and close the device. Therefore, the respective drivers will contain the routines needed to open, write to, read from, and close (among other things) those devices. All devices controlled by the same device driver have a common major device number. The minor device numbers are used to distinguish between different devices and their controllers.

Engineered for Tomorrow 5.FIFO FILE It is a special pipe device file which provides temporary buffer for two or more process to communicate by reading data from or writing data to the buffer. Size of the buffer associated with a FIFO file is fixed to PIPE_BUF.

Thus the data stored in the FIFO is temporary. Engineered for Tomorrow Buffer associated with the FIFO file is allocated when the first process opens the FIFO file read or write. The buffer is discarded when all process which are connected to the FIFO close their references to the FIFO file. Thus the data stored in the FIFO is temporary.

FIFO file is created using mkfifo command mkfifo /usr/prog/fifo_pipe Engineered for Tomorrow FIFO file is created using mkfifo command mkfifo /usr/prog/fifo_pipe UNIX system V.3 creates FIFO files using mknod command UNIX system V.4 supports both mknod and mkfifo

BSD supports only mkfifo command Engineered for Tomorrow BSD supports only mkfifo command FIFO file may be removed like an regular file via rm command. BSD UNIX and UNIX System V.4 also defines Symbolic link file type.

Symbolic link file created via ln command Engineered for Tomorrow A symbolic link file contains a path name which references another file either local or remote file system. Symbolic link file created via ln command ln –s /usr/jose/original /usr/mary/slink cat –n /usr/mary/slink The above command creates a symbolic link /usr/mary/slink which references the file /usr/jose/original

The path name can be obtained by using ls –l command Engineered for Tomorrow The path name can be obtained by using ls –l command $ ls –l /usr/mary/slink Sr—r—r-- 1 terry 20 Aug 20,1994, slink->/usr/jose/original

UNIX and POSIX FILE SYSTEMS Engineered for Tomorrow UNIX and POSIX FILE SYSTEMS Files in UNIX and POSIX system are stored in tree-like hierarchical file system. The root of the file system is “root” directory denoted by “/”. Each intermediate node in a file system tree is a directory file. The leaf nodes of a file systems tree are either empty directory files or other types of files.

Directory names are delimited by “/” characters in a path name. Engineered for Tomorrow Absolute path name of a file consists of the names of all the directories, specified in descending order of the directory hierarchy starting from root “/”. Directory names are delimited by “/” characters in a path name. Ex: /usr/xyz/a.out

Relative path name may consist of the”.” and “..” characters. Engineered for Tomorrow Relative path name may consist of the”.” and “..” characters. These are references to the current and parent directories, respectively. Ex: ../../.login This denotes a file .login ,may be found in a directory two levels up from the current directory.

A file name may not exceed NAME_MAX Engineered for Tomorrow A file name may not exceed NAME_MAX A path name may not exceed PATH_MAX POSIX.1 defined minimum values for NAME_MAX and PATH_MAX are _POSIX_NAME_MAX and POSIX_PATH_MAX.

The path name of the file is called hard link. Engineered for Tomorrow POSIX.1 specifies the following characters to be supported by all POSIX Compliant OS as a legal file name characters. A to Z a to z 0 to 9 _ The path name of the file is called hard link. A file may be referenced by more than one path name if user creates one or more hard links to the file using ln command ln /usr/fn/path1 /usr/prog/new/n1

Files which are commonly defined in UNIX Systems Engineered for Tomorrow Files which are commonly defined in UNIX Systems /etc - Stores system admin files and progms. /etc/passwd - Stores all user information /etc/shadow - Stores user passwords(for UNIX system V only) /etc/group - stores all group info /bin - Stores all the system programs like cat,rm,cp etc /dev - Stores all characters and block device file /usr/include – Stores standard header files. /usr/lib/ - stores standard libraries /tmp - Stores temporary files created

UNIX & POSIX File Attributes Engineered for Tomorrow UNIX & POSIX File Attributes Both UNIX & POSIX. 1 maintain a set of common attributes for each file in a file system. Attribute Meaning 1.File type - Type of a file 2.access permission - The file access permission for owner, group & others 3.Hard Link Count - No of hard links of a file 4.UID - The file owner user ID 5.GID - The file group ID

6. File size - The file size in bytes Engineered for Tomorrow 6. File size - The file size in bytes 7.Last access Time - The time the file was last accessed. 8.Last Modify Time - The time the file was last modified. 9.Last Change Time - The time the file access permission,UID,GID or hard link count was last changed. 10.inode number - The system inode number of the file. 11.File system ID - The file system ID where the file is stored.

All these attributes are essential for the kernel to manage the files. Engineered for Tomorrow \ All these attributes are essential for the kernel to manage the files. When a user attempts access a file, kernel matches the user’s UID and GID against those of the file to determine which category(user, group,or others) of access permission should be used for the access privileges of the user. Not all the file types make use of all these file information. For ex, and block and character device files have least use of file size attribute.

Engineered for Tomorrow Unix system also stores the major and minor device numbers for each device file. In Posix.1 , the support for device file is implementation dependent. Thus it does not specify major and minor number as standard attributes for device files. All these attributes are assigned by the kernel to a file when it is created. Some of these attributes stay unchanged for entire life of the file, whereas others may change as the file is being used.

Attributes that are constant for any file are: File type Engineered for Tomorrow Attributes that are constant for any file are: File type File inode number File System ID Major and Minor device number(for device files on Unix system only).

Engineered for Tomorrow The other attributes are changed by following UNIX commands or System Calls Unix Command System Call Attributes changed chmod Changes access permission, last change time chown Changes UID, last change time chgrp Changes GID, last change time touch Utime Changes last access and modification time ln link Increases hard link count rm unlink Decreases hard link count, if it is zero,file is removed from the system Vi,emac Changes file size, last access time, last modification time

Engineered for Tomorrow Inodes in UNIX SYSTEM V In UNIX System V , a file system has a inode table which keeps track of all files. Each entry of the inode table is an inode record which contains all the attributes of a file, including unique inode number and physical address where the data of the file is stored.

Engineered for Tomorrow If a kernel needs to access information of a file with an inode number say 15, it will scan the inode table to find an entry which contains inode number 15, in order to access the necessary data. OS may have access to multiple systems at one time, and inode is unique within a file system only, a file inode record is identified by a file system ID and an inode number.

Foo directory file content Engineered for Tomorrow OS does not keep the name of the file in inode record, bcoz the mapping of file names to inode numbers is done via directory files. A directory file contains list of file names and their respective inode numbers for all files stored under that directory. Foo directory file content Inode number filename 115 . 89 .. 201 xyz 346 a.out xyz_lnl

It then looks for the inode number of joe file Engineered for Tomorrow For ex, To access a file /usr/joe, the kernel always knows “/” directory inode number. It will scan the”/”directory file, via “/” inode record to find the inode number of usr file. Once it gets the usr file inode number it checks that calling process has permission to search the usr directory and access the content of usr file. It then looks for the inode number of joe file

Engineered for Tomorrow Whenever a new file is created in a directory, Kernel allocates a new entry in the inode table to store the information of the new file. It will unique inode number to the file and add the new file name and the file inode number to the directory file that it contains. Inode tables are kept in the file systems on disk, but kernel maintains in-memory inode table to contain a copy of the recently accessed inode records.

Application Program Interface to Files Engineered for Tomorrow Application Program Interface to Files Both UNIX and POSIX systems provide an application interface similar to files as follows: Files are identified by path names. File must be created before they can be used.

Engineered for Tomorrow The UNIX commands and corresponding system calls to create various types of files are: File Type UNIX Command UNIX & POSIX System Call Regular File Vi,ex,etc Open,creat Directory File mkdir Mkdir,mknod FIFO File mkfifo Mkfifo,mknod Device File mknod Mknod Symbolic Links Ln -s symlink

Open API can be used to open any files. Engineered for Tomorrow File must be opened before they can be accessed by be application programs. Open API can be used to open any files. The open function returns an integer file descriptor , which is a file handle to be used in other system calls to manipulate the open file. A process may open at most OPEN_MAX files of any types at any one time.

File attributes can be queried by the stat or fstat system call. Engineered for Tomorrow The read and write system calls can be used to read data from and write data to opened files. File attributes can be queried by the stat or fstat system call. File attributes can be changed by the chmod, chown, utime and link system calls. File hard links can be removed by unlink system call.

Engineered for Tomorrow To facilitate the query of file attributes by application programs, UNIX and POSIX.1 define a struct stat data type in the <sys/stat.h> header. A struct stat record contains all the user-visible attributes of any file being queried, and it is assigned and returned by the stat or fstat function.

dev_t st_dev; /*file system ID */ ino_t st_ino; /*File inode number */ Engineered for Tomorrow Struct stat { dev_t st_dev; /*file system ID */ ino_t st_ino; /*File inode number */ mode_t st_mode; /*Contains file type and access flags* nlink_t st_nlink; /*Hard link count */ uid_t st_uid; /* File user ID*/ gid_t st_gid; /* File group ID*/ dev_t st_rdev; /* Contains major & minor numbers*/ off_t st_size; /* File size in number of bytes */ time_t st_atime; /* Last access time */ time_t st_mtime; /*Last modification time */ time_t st_ctime; /* Last status change time */ };

UNIX Kernel support for Files Engineered for Tomorrow UNIX Kernel support for Files The kernel has a file table that keeps track of all opened files in the system. There is also an inode table that contains a copy of the file inodes most recently accessed. When a user executes a command, a process is created by the kernel to carry out the command execution. The process has its own data structure called “file descriptor table”, which has OPEN_MAX entries and records all the files opened by the process,

The steps are as follows: Engineered for Tomorrow Whenever process calls open function to open a file for read and/ write kernel will resolve the path name to the file inode. If the file inode is not found, or process lacks appropriate permission to access inode data , open call fails returns -1 to the process. If the file inode is accessible, kernel will establish a path an entry in the file descriptor table , through a file table, onto the inode table for the file being opened. The steps are as follows:

if an unused entry is found following events will occur: Engineered for Tomorrow Kernel will search the process file descriptor table and look for the first unused entry. If an entry is found, that entry will be designated to reference the file. The entry index will be returned to the process as file descriptor of the opened file. The kernel will scan the file table in its kernel space to find an entry that can be assigned to reference the file. if an unused entry is found following events will occur:

Engineered for Tomorrow The process’s file descriptor table entry will be set to point to this file table entry. b. The file table entry will be set to point to the inode table entry where the inode record of that file is stored. c. The file table entry will contain the current file pointer of the open file, which is offset from the beginning of the file where the next read or write operation will occur.

Engineered for Tomorrow The file table entry will contain an open mode that specifies that file is opened for read-only, write-only or read and write etc. this mode is specified from the calling process as an argument to the open function call. The reference count in the file table entry is set to 1. the reference count keeps track of how many file descriptors from any process are referencing the entry. The reference count of inode table is also incremented by 1. this count specifies how many file table entries are pointing to that inode.

Data structure for file manipulation Engineered for Tomorrow Data structure for file manipulation File Table Inode table File Descriptor Table r rc=1 rw w rc=1 rc=2

This causes file table reference count to be larger than 1 Engineered for Tomorrow Dup or dup2 function can be used to make multiple file descriptor table entries point to the same file table entry. This causes file table reference count to be larger than 1 Similarly , if the reference count in the inode table for a particular inode record is not zero. It means that one or more processes are currently opening the file for access.

Engineered for Tomorrow Once the open call succeeds , process can use the file descriptor for future use. When the process attempts to read / write data from the file, it uses the Fd as the first argument to read(write) system call. Kernel use the FD to index the process’s FDT to find the FT entry of the opened file. It then checks the FT entry data to make sure the file opened with appropriate mode.

Engineered for Tomorrow If the read(write) operation found compatible with the file’s open mode, kernel uses the pointer specified in the file table entry to access file’s inode record. It will use file pointer stored in FT entry to determine where the read(write) operation should occur in the file. Finally, kernel checks the file’s file type in inode record and invoke appropriate driver function to initiate the actual data transfer with the physical device.

Engineered for Tomorrow If a process calls lseek function, to change the file pointer to different offset for next read (write operation), kernel will use the FD to index the process’s FDT to find the pointer to file table entry. Kernel then accesses FT entry to get pointer to the file’s inode record. If the file type is compatible with lseek, kernel will change the file pointer in the FT entry according to the value specified in the lseek argument.

When process calls close function following events takes place. Engineered for Tomorrow When process calls close function following events takes place. Kernel sets the corresponding FDT entry to be unused. It decrements the reference count in the corresponding file table entry by 1. if the reference count is still non-zero, go to 6 The FT entry is marked as unused. The reference count in the corresponding inode table is decremented by 1, if count is still non-zero , go to 6

6. It returns to the process with 0 status. Engineered for Tomorrow 5. If the hard link count of the inode is not zero, it returns to the caller with success status, else it marks inode table entry as unused and de allocates all the physical disk storage of the file, as all path names have been removed by some process. 6. It returns to the process with 0 status.

Relationship of C stream Pointers & File Pointers Engineered for Tomorrow Relationship of C stream Pointers & File Pointers C stream pointers (FILE *) are allocated via the fopen C function call. A stream pointer is more efficient to use for applications doing extensive sequential read from or write to files, as the C library functions perform I/O buffering with streams. A file descriptor is allocated by an open system call and is more efficient for applications that do frequent random access of data , & I/O buffering is not desired.

Engineered for Tomorrow Another difference is stream pointers are supported on all OSs such as VMS, CMS,DOS, and UNIX, that provide C compilers. File descriptors are used only in UNIX and POSIX.1 compliant systems; thus programs that use stream pointers are more portable than are those using file descriptors.

Engineered for Tomorrow A FILE * (also called ``file pointer'') is the handle by which we refer to an I/O stream in C. It is a structure that contains all the information required by the standard I/O library to manage the stream. To support stream pointers each unix process has a fixed size stream table with OPEN_MAX entries. Data stored in a FILE record includes a buffer for I/O data buffering, the file I/O error status and en end-of-file flag etc

int level; /* fill/empty level of buffer */ Engineered for Tomorrow typedef struct { int level; /* fill/empty level of buffer */ unsigned flags; /* File status flags */ char fd; /* File descriptor */ unsigned char hold; /*Ungetc char if no buffer */ int bsize; /* Buffer size */ unsigned char *buffer; /* Data transfer buffer */ unsigned char *curp; /* Current active pointer */ unsigned istemp; /* Temporary file indicator */ short token; /* Used for validity checking */ } FILE;

Engineered for Tomorrow When fopen is called ,It scans the calling process FILE table to find an unused entry, then assigns this entry to reference the file and returns the address of this entry(FILE *) as the stream pointer of the file. In UNIX, fopen function calls the open function to perform the actual file opening and a FILE record contains a file descriptor for the open file. One can extract the file descriptor associated with a stream pointer via fileno macro, which is defined in the <stdio.h> header.

Int fileno (FILE* stream_pointer); Engineered for Tomorrow Int fileno (FILE* stream_pointer); If a process calls fopen to open a file, there will be an entry in the process’sFILE table and an entry in the file’s descriptor table being used to reference the file. If process calls open function, only an entry in the file descriptor’s table is assigned to reference the file. one can associates a stream with the existing file descriptor via fdopen C library function.

FILE* fdopen(int file_descriptor, char* open_mode); Engineered for Tomorrow FILE* fdopen(int file_descriptor, char* open_mode); fdopen function assigns a process FILE table entry to reference the file , records the file descriptor value in the entry, and returns the address of the entry to the caller. After either the fileno or fdopen call, the process may reference the file via either the stream pointer or the file descriptor.

C library function Unix System Call used fopen open Engineered for Tomorrow The following lists some C library functions and the underlying UNIX APIs they use to perform their functions: C library function Unix System Call used fopen open fread,fgetc,fscanf,fgets read fwrite,fputc,fprintf,fputs write fseek,ftell,frewind lseek fclose close

Directory Files A directory is record oriented file. Engineered for Tomorrow Directory Files A directory is record oriented file. Each record contains the information of a file residing in that directory. The record data type is struct dirent in UNIX System V and POSIX.1 and struct direct in BSD UNIX. The record content is implementation dependent, but in UNIX and POSIX systems they contain two essential members: 1) file name 2)an inode number

struct dirent { long d_ino; char d_name[1]; }; Engineered for Tomorrow struct dirent {  long d_ino;  char d_name[1];  };

Engineered for Tomorrow Although an application can use the open.read, write , lseek and close system calls to manipulate directory files, UNIX and POSIX.1 define a set of portable function to open,browse and close directory files. These functions are defined under<dirent.h> for UNIX System V and POSIX.1 compliant systems or in <sys/dir.h> for BSD UNIX

Directory function Purpose opendir Opens a directory file Engineered for Tomorrow Directory function Purpose opendir Opens a directory file readdir Reads next record from file closedir closes a directory rewinddir sets file pointer to beginning of a file. Opendir function returns a handle of type DIR*.this handle is used in the readdir, rewinddir,and closedir function call to specify which directory to manipulate.

Engineered for Tomorrow Unix systems also define the telldir and seekdir functions for random access of different records in a directory file. If a process adds or deletes a file in a directory file whether another process has opened the file via opendir, it is implementation dependent as to whether latter process will see the new changes via readdir function. If the latter process does a rewinddir and then reads the directory via readdir, then it should read the latest content of directory file.

Hard Links & Symbolic Links Engineered for Tomorrow Hard Links & Symbolic Links A hard link is a UNIX path name for a file. Most UNIX files have only one hard link. Users may create additional hard links for files via ln command. For ex: ln /usr/mary/fun.doc /usr/joe/book.new Now user may refer to the same file either by /usr/mary/fun.doc or /usr/joe/book.new

ln -s /usr/mary/fun.doc /usr/joe/book.new Engineered for Tomorrow Symbolic links can be created in the same manner as hard links except we must specify –s option to the ln command , therefore, ln -s /usr/mary/fun.doc /usr/joe/book.new Above command creates /usr/joe/book.new as a symbolic link instead of hard link. Symbolic links or hard links are used to provide alternative means of referencing files.

ln /usr/include/sys/unistd.h uniref Engineered for Tomorrow For Ex, A user is at /usr/jose/proj/doc directory and he wants to browse the file /usr/include/sys/unistd.h . Rather specifying the full path name /usr/include/sys/unistd.h every time he references it he can define a link to that file as follows: ln /usr/include/sys/unistd.h uniref From now on he can refer to that file as uniref.

For ex, ln /usr/jose/abc /usr/mary/xyz Engineered for Tomorrow ln differs from the cp command in that cp creates duplicated copy of a file to another file with different path name, where as ln creates a new directory entry to reference a file. For ex, ln /usr/jose/abc /usr/mary/xyz The directory files of /usr/jose/abc /usr/mary/xyz contain:

Engineered for Tomorrow /usr/jose /usr/mary Inode no filename 115 . 89 .. 201 abc 346 a.out Inode no filename 115 . 89 .. 201 xyz 346 a.out Both the /usr/joe/abc and /usr/mary/xyz refer to the same inode number 201. thus there is no new file is created.

Engineered for Tomorrow If we use ln –s or cp command to create /usr/mary/xyz file, a new inode will be created and the directory file of /usr/ joe nad /usr/mary will look like following. /usr/jose /usr/mary Inode no filename 115 . 89 .. 345 xyz 346 a.out Inode no filename 115 . 89 .. 201 abc 346 a.out

Engineered for Tomorrow If the /usr/mary/xyz file was created by the cp command, its data content is identical to that of /usr/joe/abc and two files will be separate objects in the file system. If the /usr/mary/xyz file was created by the ln -s command, its data will consist of only the path name /usr/joe/abc.

ln saves disk space by not creating duplicated files as cp. Engineered for Tomorrow ln saves disk space by not creating duplicated files as cp. Whenever user makes changes to a link of a file, the changes are visible from all other links of the file.

Limitations of hard links Engineered for Tomorrow Limitations of hard links User can create hard links for directories; unless they have a super user(root) privilege. This prevents user form creating cyclic links. ln /usr/jose/text/unix_link /usr/jose If this command succeeds , then whenever user executes ls –R /usr/jose the ls command will run into an infinite loop in displaying recursively the subdirectory tree of /usr/jose

Engineered for Tomorrow Users cant create hard links on a file system that references files on a different file system This is because a hard link is just a directory entry to reference the same inode number as the original link, but inode numbers are unique only within a file system.

Does not create a new inode Create a new inode Engineered for Tomorrow Hard Links Symbolic links Does not create a new inode Create a new inode Can not link directories, unless this is done by root Can link directories Can not link files across file systems Can link files across file systems Increase the hard link count of the linked inode Does not Increase the hard link count of the linked inode