overview
play

Overview Last Week: Efficiency read/write The File Unix System - PDF document

Overview Last Week: Efficiency read/write The File Unix System Programming File pointer File control/access Directories and File System This Week: How to program with directories Brief introduction to the UNIX file system


  1. Overview Last Week: � Efficiency read/write � The File Unix System Programming � File pointer � File control/access Directories and File System This Week: � How to program with directories � Brief introduction to the UNIX file system 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Outline Directory Implementation � Directory implementation � UNIX file system � A UNIX directory is a file: � Links » owner, group owner, size, access, permissions, etc » many file operations can be used on directories � Subdirectory creation � “,” and “..” � Differences: � mkdir() & rmdir() » modern UNIXs have special directory operations � Reading directories – e.g. opendir(), readdir() � chdir() & getcwd() � Walking over directories � telldir() & seekdir() � scandir() 3 4 Maria Hybinette, UGA Maria Hybinette, UGA Unix Memory Structure Entire Disk & Booting Computer � Disk is divided into 1+ partitions: one file system per partition Partition Table � Master Boot Record (sector 0) - Pentium Entire Disk: MBR Partition Partition … Partition » used to boot computer � Partition Table » staring and ending address of each partition Free Space Management � “A program (e.g. the system Basic Input Output System or BIOS for Pentiums)” reads in and executes the MBR File System: Boot block Super Block i-list Data blocks for files, directories, etc. » searches for first active partition (noted in the partition table) » reads in its first block (the boot block) and executes it. Partition Table i-node i-node i-node … i-node MBR Partition Partition … Partition 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Partition Layout Directory Implementation � Boot block: � Directory system function: Maps ASCII names onto » contains a hardware specific program that is called automatically to what is needed to locate the data load “UNIX” at system startup time � Where do we store the files’ attributes? � Super block: » file system type, #blocks in file system » A simple directory: fixed sized entries attributes stored � Free space management (two lists): with the entry games attributes » a chain of free data block numbers mail attributes » a chain of free i-node numbers news attributes � i-list/i-node table: work attributes » administrative information about a file (meta-data: name, type, » Directory in each entry just refers to an i-node (UNIX location, size, protection bits, … ) structured into an array: inode table or simply the i-list implementation) attributes » An i-node number: – uniquely identifies a file in a file system games attributes – is an index to the i-node table mail news attributes Boot block Super Block i-list Data blocks for files, directories, etc. work attributes Free Space Management 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Directory Structure File System Expanded � A directory “file” is a sequence of lines; each line BB SB i-list directory data data directory data data data holds an i-node number (index-node) and a file name 895690 “.” 288767 “..” 287243 “maria.html” 287259 “gunnar.txt” i-node 895690 … i-node � The data is stored as binary so we cannot simply 895690 “.” cat to view it: 288767 “..” » but some UNIXs allow an “octal dump” (other formats 287243 “maria.html” also available) : 287259 “gunnar.txt” {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 g u n n a r . t x t \0 \0 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Links - Outline What is a Link? � A link is a pointer to a file � What is a Link? � Useful for sharing files; » A file can be shared by giving each person their own � Creating a Link link (pointer to it) � Seeing Links ln <existing-file> <new-pointer> � Maria types, in directory: ~/maria � Removing a Link ln tucker-todo.txt /home/tucker/todo.txt / � Symbolic Links � Implementation bin home tmp /home/maria/tucker-todo.txt and tucker maria /home/tucker/todo.txt a.txt tucker-todo.txt g.txt 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Creating Links Seeing Links � Compare status information : {saffron:maria:104} ls -l file_a file_b file_c � Changes to a file affect every link: -rw-r--r-- 2 maria 36 May 24 10:52 file_a -rw-r--r-- 2 maria 36 May 24 10:52 file_b {atlas} cat file_a -rw-r--r-- 1 maria 16 May 24 10:55 file_c This is file A. File mode, # links, owners name, group name, #bytes, date, pathname {atlas} ln file_a file_b {atlas} cat file_b � Look at i-node number: This is file A {saffron:maria:105} ls -i file_a file_b file_c 3534 file_a 3534 file_b {atlas} echo “appending this to b” >> file_b 5800 file_c � Directories may appear to have more links: {atlas} cat file_b {saffron:maria:106} ls -ld dir This is file A. drwxr-xr-x 2 maria users 68 Apr 7 17:57 dir/ appending this to b {saffron:maria:107} mkdir dir/hello {atlas} cat file_a {saffron:maria:108} ls -ld dir This is file A. drwxr-xr-x 3 maria users 68 Apr 7 17:58 dir/ appending this to b � This is because subdirectories (e.g. directories inside dir/) have a link back to their parent. 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Removing a Link Symbolic Links � Removing or deleting a link does not � The links described so far are hard links necessarily remove the file » A hard link is a pointer to a file which must be on the same file system � A symbolic link is an indirect pointer to a file � Only when the file and every link is gone will » Stores the pathname of the file that it points to the file be removed » Symbolic links can link across file systems � Symbolic links are listed differently: {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/ 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Link Creation, Update & Removal link() cp bob new ln bob new ln -s bob new new bob #include <unistd.h> long link( const char *oldpath, const char *newpath ); abc abc abc abc � Meaning of: » link( “maria.html”, “tucker.html” ); update new delete bob bob new bob new new ? bob 895690 “.” 895690 “.” 288767 “..” 288767 “..” 287243 “maria.html” 287243 “maria.html” 287259 “gunnar.txt” abc abc XY abc XY abc XY 287259 “gunnar.txt” 287243 “tucker.html” 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

  4. unlink() Example unlink #include <stdio.h> #include <sys/stat.h> {saffron} make unlinktemp � clears directory record #include <sys/types.h> gcc unlinktemp.c -o unlinktemp #include <fcntl.h> {saffron} ./unlinktemp » usually means that the i-node number is set to 0 file temp is unlinked int main(void) {saffron} ./unlinktemp » the file may not be affected { open error: No such file or directory if( open( “temp”, O_RDWR ) < 0 ) { � The i-node is only deleted when the last link perror( “open error” ); to it is removed; the file data blocks are exit( 1 ); } deleted/reclaimed when there are no if( unlink( “temp” ) < 0 ) processes having the file opened. { perror( “unlink error “ ); exit( 1 ); } printf( “file temp is unlinked\n” ); exit( 0 ); } 19 20 Maria Hybinette, UGA Maria Hybinette, UGA remove() rename() #include <stdio.h> #include <stdio.h> int remove( const char *pathname ); int rename( const char *oldpath, const char *newpath ); � C Library function (not a system call) � Changes the names of oldpath to newpath (for both directories and files) � Delete a name and possibly the file it refers to. � If oldpath is open or is a nonexistent file, or if » It calls unlink() for files and rmdir() for newpath names a file that already exists, then the directories action of rename() is implementation dependent. 21 22 Maria Hybinette, UGA Maria Hybinette, UGA symlink() readlink() #include <stdio.h> #include <stdio.h> int readlink( const char *path, char buf, size_t int symlink( const char *oldpath, const char bufsize ); *newpath ); � Creates a symbolic link named newpath � Read value of a symbolic link (does not follow the link) which contains the string oldpath » Places the contents of the symbolic link path in the buffer buf � Symbolic links are interpreted at run-time which has size bufsize � Dangling link - may point to a nonexisting file » Does not append a NULL character to buf � Return value � If newpath exists it will not be overwritten » The count of characters placed in the buffer if it succeeds » -1 on error 23 24 Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend