1
CS644 week 3:
Filesystems, part 2
Types of files
2
Regular file – sequence of bytes
3
Types of files
Directory – collection of other files
4
Types of files
Hard link – link to another file
(hard link is identical to "original")
5
Types of files
Symbolic link – link to another file
(symlink points to original, may be left dangling)
6
Types of files
Classic Unix file permissions
7
Read permission:
Files: can read contents
Directories: can list files
8
File permissions
Write permission:
Files: can write to file
Directories: can create/rename/delete entries
9
File permissions
Execute permission:
Files: can run as program
Directories: can "traverse"
10
File permissions
Every file has an owner and a group
11
File permissions
{read, write, exec} X {owner, group, other}
12
File permissions
chmod 755 myfile
13
File permissions
chmod 755 myfile
usr = 7 = 111 = rwx
14
File permissions
chmod 755 myfile
usr = 7 = 111 = rwx
grp = 5 = 101 = r-x
15
File permissions
chmod 755 myfile
usr = 7 = 111 = rwx
grp = 5 = 101 = r-x
all = 5 = 101 = r-x
16
File permissions
chmod 755 myfile
r = 4, w = 2, x = 1
17
File permissions
chmod 755 myfile
r = 4, w = 2, x = 1
7 = 4 + 2 + 1 = rwx
18
File permissions
chmod 755 myfile
r = 4, w = 2, x = 1
7 = 4 + 2 + 1 = rwx
5 = 4 + 1 = r-x
19
File permissions
chmod u+x myfile
20
File permissions
Syscalls for today
21
int stat(
const char* pathname,
struct stat* statbuf,
)
22
Getting file metadata
int fstat(
int fd,
struct stat* statbuf,
)
23
Getting file metadata
struct stat {
mode_t st_mode;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
/* other fields */
}
24
Getting file metadata
int chmod(
const char* pathname,
mode_t mode,
)
25
Changing file permissions
int fchmod(
int fd,
mode_t mode,
)
26
Changing file permissions
Syscalls for today
27
int mkdir(
const char* pathname,
mode_t mode,
)
28
Creating a directory
ssize_t getdents64(
int fd,
void* dirp,
size_t count,
)
29
Listing a directory
"These are not the interfaces you
are interested in."
- man 2 getdents
30
Listing a directory
DIR* opendir(
const char* pathname,
)
struct dirent* readdir(
DIR* dirp,
)
31
Listing a directory
struct linux_dirent {
unsigned char d_type;
char d_name[];
/* other fields */
}
32
Getting file metadata
Syscalls for today
33
int rename(
const char* oldpath,
const char* newpath,
)
34
Moving a file
int unlink(
const char* pathname,
)
35
Deleting a file
int rmdir(
const char* pathname,
)
36
Deleting a directory
Syscalls for today
37
int flock(
int fd,
int op
)
38
Locking files
flock(fd, LOCK_SH)
flock(fd, LOCK_EX)
flock(fd, LOCK_EX | LOCK_NB)
flock(fd, LOCK_UN)
39
Locking files
In-class exercises
40