1 of 40

1

CS644 week 3:

Filesystems, part 2

2 of 40

Types of files

2

3 of 40

Regular file – sequence of bytes

3

Types of files

4 of 40

Directory – collection of other files

4

Types of files

5 of 40

Hard link – link to another file

(hard link is identical to "original")

5

Types of files

6 of 40

Symbolic link – link to another file

(symlink points to original, may be left dangling)

6

Types of files

7 of 40

Classic Unix file permissions

7

8 of 40

Read permission:

Files: can read contents

Directories: can list files

8

File permissions

9 of 40

Write permission:

Files: can write to file

Directories: can create/rename/delete entries

9

File permissions

10 of 40

Execute permission:

Files: can run as program

Directories: can "traverse"

10

File permissions

11 of 40

Every file has an owner and a group

11

File permissions

12 of 40

{read, write, exec} X {owner, group, other}

12

File permissions

13 of 40

chmod 755 myfile

13

File permissions

14 of 40

chmod 755 myfile

usr = 7 = 111 = rwx

14

File permissions

15 of 40

chmod 755 myfile

usr = 7 = 111 = rwx

grp = 5 = 101 = r-x

15

File permissions

16 of 40

chmod 755 myfile

usr = 7 = 111 = rwx

grp = 5 = 101 = r-x

all = 5 = 101 = r-x

16

File permissions

17 of 40

chmod 755 myfile

r = 4, w = 2, x = 1

17

File permissions

18 of 40

chmod 755 myfile

r = 4, w = 2, x = 1

7 = 4 + 2 + 1 = rwx

18

File permissions

19 of 40

chmod 755 myfile

r = 4, w = 2, x = 1

7 = 4 + 2 + 1 = rwx

5 = 4 + 1 = r-x

19

File permissions

20 of 40

chmod u+x myfile

20

File permissions

21 of 40

Syscalls for today

  • File metadata and permissions
  • Directories
  • Moving and deleting files
  • File locking

21

22 of 40

int stat(

const char* pathname,

struct stat* statbuf,

)

22

Getting file metadata

23 of 40

int fstat(

int fd,

struct stat* statbuf,

)

23

Getting file metadata

24 of 40

struct stat {

mode_t st_mode;

uid_t st_uid;

gid_t st_gid;

off_t st_size;

/* other fields */

}

24

Getting file metadata

25 of 40

int chmod(

const char* pathname,

mode_t mode,

)

25

Changing file permissions

26 of 40

int fchmod(

int fd,

mode_t mode,

)

26

Changing file permissions

27 of 40

Syscalls for today

  • File metadata and permissions
  • Directories
  • Moving and deleting files
  • File locking

27

28 of 40

int mkdir(

const char* pathname,

mode_t mode,

)

28

Creating a directory

29 of 40

ssize_t getdents64(

int fd,

void* dirp,

size_t count,

)

29

Listing a directory

30 of 40

"These are not the interfaces you

are interested in."

- man 2 getdents

30

Listing a directory

31 of 40

DIR* opendir(

const char* pathname,

)

struct dirent* readdir(

DIR* dirp,

)

31

Listing a directory

32 of 40

struct linux_dirent {

unsigned char d_type;

char d_name[];

/* other fields */

}

32

Getting file metadata

33 of 40

Syscalls for today

  • File metadata and permissions
  • Directories
  • Moving and deleting files
  • File locking

33

34 of 40

int rename(

const char* oldpath,

const char* newpath,

)

34

Moving a file

35 of 40

int unlink(

const char* pathname,

)

35

Deleting a file

36 of 40

int rmdir(

const char* pathname,

)

36

Deleting a directory

37 of 40

Syscalls for today

  • File metadata and permissions
  • Directories
  • Moving and deleting files
  • File locking

37

38 of 40

int flock(

int fd,

int op

)

38

Locking files

39 of 40

flock(fd, LOCK_SH)

flock(fd, LOCK_EX)

flock(fd, LOCK_EX | LOCK_NB)

flock(fd, LOCK_UN)

39

Locking files

40 of 40

In-class exercises

40