06 Files Dirs
06 Files Dirs
Chris Kauffman
Last Updated:
Wed Feb 17 03:58:38 PM CST 2021
1
Logistics
Reading
Stevens/Rago Ch 3, 4, 5, 6
Date Event
Goals for Week Mon 2/15 Lab: dup2()
⊠ read()/write() Basic I/O, Filesystem
Wed 2/17 Filesystem
⊠ I/O Redirection Mon 2/22 Lab: Review
⊠ Pipes Lec: Practice Exam
⊠ C FILE* vs Unix FDs Project 1 Due
Wed 2/24 Exam 1
□ Filesystem Last day to submit P1 late
□ Permissions
□ Hard/Symbolic Links P1 Questions?
□ File / Directory Due date approaching rapidly
Functions
2
Permissions / Modes
▶ Unix enforces file security via modes: permissions as to who can read /
write / execute each file
▶ See permissions/modes with ls -l
▶ Look for series of 9 permissions
> ls -l
total 140K
-rwx--x--- 2 kauffman faculty 8.6K Oct 2 17:39 a.out
-rw-r--r-- 1 kauffman devel 1.1K Sep 28 13:52 files.txt
-rw-rw---- 1 kauffman faculty 1.5K Sep 26 10:58 gettysburg.txt
-rwx--x--- 2 kauffman faculty 8.6K Oct 2 17:39 my_exec
---------- 1 kauffman kauffman 128 Oct 2 17:39 unreadable.txt
-rw-rw-r-x 1 root root 1.2K Sep 26 12:21 scripty.sh
U G O O G S M T N
S R T W R I O I A
E O H N O Z D M M
R U E E U E E E
P R R P
^^^^^^^^^^
PERMISSIONS
▶ Every file has permissions set from somewhere on creation
3
Changing Permissions
Owner of file (and sometimes group member) can change
permissions via chmod
> ls -l a.out
-rwx--x--- 2 kauffman faculty 8.6K Oct 2 17:39 a.out
> ls -l a.out
-r-xr-x--x 2 kauffman faculty 8.6K Oct 2 17:39 a.out
▶ chmod also works via octal bits (suggest against this unless
you want to impress folks at parties)
▶ Programs specify file permissions via system calls
▶ Curtailed by Process User Mask which indicates permissions
that are disallowed by the process
▶ umask shell function/setting: $> umask 007
▶ umask() system call: umask(S_IWGRP | S_IWOTH);
▶ Common program strategy: create files with very liberal
read/write/execute permissions, umask of user will limit this
4
Exercise: Regular File Creation Basics
5
Answers: Regular File Creation Basics
C Standard I/O Unix System Calls
▶ Write/Read data? ▶ Write/Read data?
Directories
▶ List names and associated inode
▶ Each entry constitutes a hard link to an inode or a symbolic
link to another file
▶ Files with 0 hard links are deleted
8
Rough Filesystem in Pictures 1
9
Rough Filesystem in Pictures 2
Figure 4.14 Cylinder group’s i-nodes and data blocks in more detail (Stevens/Rago)
10
Shell Demo of Hard and Symbolic Links
> rm *
> touch fileX # create empty fileX
> touch fileY # create empty fileY
> ln fileX fileZ # hard link to fileX called fileZ
> ln -s fileX fileW # symbolic link to fileX called fileW
> ls -li # -i for inode numbers
total 12K
6685588 -rw-rw---- 2 kauffman kauffman 0 Oct 2 21:24 fileX
6685589 -rw-rw---- 1 kauffman kauffman 0 Oct 2 21:24 fileY
6685588 -rw-rw---- 2 kauffman kauffman 0 Oct 2 21:24 fileZ
6685591 lrwxrwxrwx 1 kauffman kauffman 5 Oct 2 21:29 fileB -> fileA
6685590 lrwxrwxrwx 1 kauffman kauffman 5 Oct 2 21:25 fileW -> fileX
↑↑↑↑↑↑↑ ↑ ↑ ↑↑↑↑↑↑↑↑
inode# regular hard link count symlink target
or symlink
11
Linking Commands and Functions
12
FYI: inodes are a complex beast themselves
13
sync() and Internal OS Buffers
14
File Caching Demo
15
Movement within Files, Changing Sizes
▶ Can move OS internal position in a file around with lseek()
▶ Note that size is arbitrary: can seek to any positive position
▶ File automatically expands if position is larger than current
size - fills holes with 0s (null chars)
▶ Can manually set size of a file with ftruncate(fd, size)
▶ Examine file_hole1.c and file_hole2.c
C function Effect
int res = lseek(fd, offset, option); Move position in file
lseek(fd, 20, SEEK_CUR); Move 20 bytes forward
lseek(fd, 50, SEEK_SET); Move to position 50
lseek(fd, -10, SEEK_END); Move 10 bytes from end
lseek(fd, +15, SEEK_END); Move 15 bytes beyond end
ftruncate(fd, 64); Set file to be 64 bytes big
If file grows, new space is
zero-filled
> stat /
File: /
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 2 Links: 17
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-10-02 00:56:47.036241675 -0500
Modify: 2017-05-07 11:34:37.765751551 -0500
Change: 2017-05-07 11:34:37.765751551 -0500
Birth: -
18
Exercise: Sketch Code for Total Size of Regular Files
19
Answers: Sketch Code for Total Size of Regular Files
// total_size.c
int main(int argc, char *argv[]){
size_t total_size = 0;
DIR *dir = opendir(".");
while(1){
struct dirent *file = readdir(dir);
if(file == NULL){
break;
▶ Scans only current directory
} ▶ Recursive scanning is
struct stat sb;
lstat(file->d_name, &sb); trickier and involves…
if(S_ISREG(sb.st_mode)){
printf("%8lu %s\n",
recursion
sb.st_size, file->d_name); ▶ OR the very useful nftw()
total_size += sb.st_size;
} library function, discussed in
else{ upcoming HW
printf("%-8s %s\n",
"SKIP", file->d_name); ▶ Techniques required for
}
} upcoming P2
closedir(dir);
printf("==================\n");
printf("%8lu total bytes from REGULAR files\n",
total_size);
return 0;
} 20
Files in Trees
▶ Frequently one wants to visit all files in a directory tree
▶ P2: check all files for changes / revision control
▶ Options for this on the command line and via system calls
22
select() and poll(): Non-busy waiting
▶ Recall polling is a busy wait on something: constantly check
until ready
▶ Alternative is interrupt-driven wait: ask for notification when
something is ready, go to sleep, get woken up
▶ Waiting is often associated with input from other processes
through pipes or sockets
▶ Both select() and poll() allow for waiting on input from
multiple file descriptors
▶ Confusingly, both select() and poll() are interrupt-driven:
will put process to sleep until something changes in one or
more files
▶ poll() doesn’t do polling (busy wait) - it does interrupt
driven I/O (!!)
▶ Example application: database system is waiting for any of 10
users to enter a query, don’t know which one will type first
23
poll() System Call
24
select() System Call and File Descriptor Sets
25
Multiplexing: Efficient input from multiple sources
▶ select() block a process until at least one of member of the
fd_set is “ready”
▶ Most common use: waiting for input from multiple sources
▶ Example: Multiple child processes writing to pipes at different
rates
#include <sys/select.h>
fd_set read_set, write_set, // sets of fds to wake up for
except_set;