CPS 346 &
444/544 Lecture notes: Files & directories
(data structures, inodes, hard & symbolic links)
Coverage:
[UPE] Chapters 2 and 7 (pp. 214-219) and [USP] Chapters 4
(pp. 102-107, 119-128) and
5
(pp. 145-173)
Disk statistics
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 (allows one to execute file);
slightly different interpretation for directories where is mean
(searchable --
access to any files within the directory to which permission
has been granted
File permissions, owners, and groups
- what are the permissions on /bin/ls?
- file access permissions (modes) are changed
with the chmod (change mode) command:
$ chmod permissions <file(s)>
- permissions can be set for:
(regenerated from [USP] Fig. 4.1, p. 105)
u user (owner)
g group
o others
a all users
- 3 types of access:
r read (view and copy access)
w write (modify and erase access)
x execute (allows cd access to directories)
- use octal permission specification;
the rwx
access of each class are bits which are on (1) or off (0)
and can be specified to chmod as 3 separate digits:
$ chmod 644 index.html
$ chmod 700 script.ksh
- use abbreviations;
permission can modified by referencing a class and adding
(+), subtracting (-), or setting (=)
permissions
(general format)
chmod [ugoa][+-=][rwx] <file(s)>
$ chmod u=rw,g=r,o=r index.html
$ chmod u=rwx script.ksh
$ chmod a+r README
- to change the owner of a file use the chown command:
chown <new-owner> <file(s)>
- users can belong to groups to facilitate file sharing;
students enrolled in CPS 444 are a member of the cps444 group
- to change the group of a file use the chgrp command:
chgrp <new-group> <file(s)>
- note: recursive option -R to chmod,
chown, or chgrp
changes the permissions, owner, or group, respectively,
for all files in the directory and all subdirectories
- umask command
- with no arguments, gets the user's file creation mask
- with an argument, sets the user's file creation mask
- use octal code
- specify the denied permissions (opposite of
chmod)
- investigate the id command
Relevant accessor/modifier functions, and structs
- ref. [USP] Chapter 5
- functions
- chdir
- getcwd
- fpathconf
- pathconf;
do not assume PATH_MAX will be defined;
use #ifndef, #define, and #endif
- 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?
(regenerated from [USP] Fig. 5.3, p. 160)
(regenerated from [USP] Fig. 5.4, p. 163)
- ls -i
prints the inode number in the first column of the listing
File links: hard vs. soft
- UNIX filesystem is a tree
- including links make it a DAG (Directed Acyclic Graph)
- links are filenames which 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
- two types of links
- hard links
- soft links (also sometimes called symbolic links or
symlinks)
- note:
- rm does not actually erase a file (i.e., inode struct
not freed);
rather
it 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)
Hard links
Symbolic (soft) links
Editor examples
- editing a file which has a hard link to it
- with basic editor (does not create a backup of file);
drew diagrams of directory entries and inodes
- with 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
- how can we change our editor so that a symbolic or
hard link to a file it modifies always reference the new file?
use O_WRONLY and O_TRUNC flags;
causes same inode to be used
od (octal dump) command
File `types' and `names'
- no such concept as a file type in UNIX
- how does the file command do its job?
uses clues (e.g., binary `magic' #)
- if filename is not stored in the inode, where is a file's name located?
- try an od on a directory
- notice anything interesting?
- compare ordering of filenames to that in output of ls -l
- a directory consists of 16-byte chunks
- first 2 hold the inode #
- the only connection between name of a file and its contents
- this is why each filename in a directory is called a link;
links name of file to inode and, thus, data
- rm removes directory entries, not inodes
- an inode # of 0 of a file in a directory indicates that the link
been removed, but not necessarily the contents of the file
- last 14 hold the filename, padded with ASCII NULLs;
filename size limits?
Question to investigate
- why cannot we od or xxd on directories we own?
- places to look for information:
- section 4 of the UNIX manual covers `File Formats'
- dir_ufs(4), dir(4)
Set-uid program
Login process
- your login name is just a mnemonic for your uid
- when you enter your password during login, the system
encrypts it (e.g., using crypt)
and compares the encrypted form to value (which is
encrypted there) of the appropriate
field in your entry in the passwd file
- if they agree, you are permitted to access the system
Things to do
- find the entry in the passwd file which corresponds to your
account
- because of the shared file systems you will have to do some exploring
- once your find your entry, record your uid and gid
- see the following manpages for help:
- passwd(1)
- passwd(4)
- finger(1)
- ypcat(1)
- ypmatch(1)
- getent(1)
find command
walk a file hierarchy to find files and directories, and optionally execute
a command line on all files found
$ find . -name wc.c -print
$ find . -name "*.c" -print
$ find ~ -name "*.c" -print
$ find . -name "sf[1-9].cpp" -print
$ find . -type d -print
$ find ~ -type d -print
$ find $HOME -type f -print
$ find . -name "*.c" -exec chmod 660 {} \;
$ find . -name "*" -type f -exec chmod 400 {} \;
$ find . -name "*" -type d -exec chmod 500 {} \;
$ find . -name .DS_Store -exec rm {} \;
$ find . -name .DS_Store -exec rm {} \;
$ find . -name ".*rc" -print
$ find . \( -name "*~" -o -name "*.bak" \) -exec rm {} \;
can we solve the file renaming problem with the find command? E.g.,
$ find home -name route.c -exec mv {} route.cpp \;
Accounts
- su (allows you to become superuser or another user
- root assumed if no user specified
- passing a - (e.g., su -)
changes the environment to what would
be expected if the user actually logged in as the specified user;
otherwise environment inherited
- wheel group (allows execution of su command)
- adduser
References
| [C] |
C Language for Experienced Programmers, Version 2.0.0, AT&T, 1988. |
| [UPE] |
B.W. Kernighan and R. Pike. The UNIX Programming Environment.
Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
|
| [USP] |
K.A. Robbins and S. Robbins.
UNIX Systems Programming: Concurrency, Communication, and Threads.
Prentice Hall, Upper Saddle River, NJ, Second edition, 2003. |
|