February 6, 2006
Coverage:
[USP]
Chapter 5
Basic UNIX File Nomenclature
- absolute or fully qualified path vs. relative path
- current working directory (stored in PWD variable)
- . and ..
- PATH variable
- single leading or trailing colon
- advisable to omit current directory (.)
- types of files
- see man ls
- ls -l
| b | block special file |
| c | character special file |
| d | directory |
| l | symbolic link |
| s | socket link |
| p | FIFO |
| - | regular file |
Explanation of Fields in ls -l Output
- total line gives the #blocks in the directory.
- a block in most UNIX systems = 1/2k (512 bytes)
- #links to the file
- size of file in bytes (characters)
File Access (3 Types)
- r read (view and copy access; permits ls and od)
- w write (modify and erase access; permits rm,
vi, cp, and mv)
- x execute (allow one to execute file)
- slightly different interpretation for directories
- x (searchable,
access to any files within the directory to which permission
has been granted)
Relevant Accessor/Modifier Functions, and structs
- functions
- chdir
- getcwd
- fpathconf
- pathconf
- don't assume PATH_MAX will be defined
- an example of use of #ifndef, #define, and
#endif
- helpful for controlling the inclusion/exclusion of echo prints for
debugging
- sysconf
- opendir
- readdir
- returns a pointer to static storage
- is not thread-safe
- use readddir_r
- closedir
- rwinddir
- stat
- if passed a link, returns information about the file referred to by
the link
- lstat
- returns information about the link itself
- stat struct
- various fields
- functions for determining the type of a file
Inodes
- structure containing bookkeeping information for the file
- schematic of inode struct
- does not contain filename, why?
- why are indirect pointers necessary?
File Links
- links are filenames that refer to the same file
- used to reduce redundancy
- changes to one affects the other
- both refer to the same physical file on the disk
- created with the ln command or the link,
symlink functions
Hard vs. Symbolic Links
- hard link
- a direct pointer to a file
- if the original file is moved the link
remains valid
- a file is accessible as long as any hard link to it remains
- created with the ln command (and no options) or the link
function
$ ln ../dir2/proga prog1
(creates a second (hard link) file reference to proga)
- cannot be used across file systems
- symbolic link
- (or symlink or soft link) is a pointer to a filename
- if the original file is moved the link becomes invalid
- deleting the link does not delete the original file
- created with the -s option to ln or the symlink
function
$ ln -s ../dir2/proga prog1
(same effect as above command, except
that a symbolic (soft) link is created)
- displayed in the ls -l listing as a file type of l
lrw-rw-r-- 1 sperugin cps445 104
Sep 12 19:54 note2
- can be used across file systems
note:
- rm does not actually erase a file
- i.e., inode struct not freed
- rm removes the link to the file (rationale behind name of
C function and UNIX command unlink)
- when the last link to a file
is removed, UNIX reclaims the file space (i.e., the blocks pointed
to by the inode list are returned to the free list)
note: mv is implemented as ln and rm
- a new link is established and then the old link removed
Editor Examples
- editing a file which has a hard link to it
- basic editor (does not create a backup of file)
- drew diagrams of directory entries and inodes
- editor which creates a backup (.bak) file
- how does the diagram change?
- editing a file which has a symbolic link to it
- with basic editor
- drew diagrams of directory entries and inodes
- with backup editor: self-exercise