Week 09 Tutorial Questions
Objectives
- learning how to access file metadata via stat
- develop an understanding of how the UNIX file permissions system works
- learning how to retrieve environment variables
Code Review
The code review for this week is the float_2048.c
The reviewees should give a brief description of their code, and the class should ask questions, comment on the quality of the code, and suggest improvements. Each review should take about 10 minutes.
Code reviews should be conducted in the second hour of tutorials in the week. Please let your tutor know if you want to volunteer to do one
Tutorial Questions
-
The
stat()
andlstat()
functions both take an argument which is a pointer to astruct stat
object, and fill it with the meta-data for a named file.On Linux, a
struct stat
contains the following fields (among others, which have omitted for simplicity):struct stat { ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };
Explain what each of the fields represents (in more detail than given in the comment!) and give a typical value for a regular file which appears as follows:
ls -ls stat.c 8 -rw-r--r-- 1 jas cs1521 1855 Sep 9 14:24 stat.c
Assume that
jas
has user id 516, and thecs1521
group has group id 36820. -
Consider the following (edited) output from the command
ls -l ~cs1521
:drwxr-x--- 11 cs1521 cs1521 4096 Aug 27 11:59 17s2.work drwxr-xr-x 2 cs1521 cs1521 4096 Aug 20 13:20 bin -rw-r----- 1 cs1521 cs1521 38 Jul 20 14:28 give.spec drwxr-xr-x 3 cs1521 cs1521 4096 Aug 20 13:20 lib drwxr-x--x 3 cs1521 cs1521 4096 Jul 20 10:58 public_html drwxr-xr-x 12 cs1521 cs1521 4096 Aug 13 17:31 spim drwxr-x--- 2 cs1521 cs1521 4096 Sep 4 15:18 tmp lrwxrwxrwx 1 cs1521 cs1521 11 Jul 16 18:33 web -> public_html
Who can access the
17s2.work
directory?What operations can a typical user perform on the
public_html
directory?What is the file
web
?What is the difference between
stat("web", &info)
andlstat("web", &info)
?
(whereinfo
is an object of type(struct stat)
)
-
Write a C program, chmod_if_public_write.c, which is given 1+ command-line arguments which are the
pathnames of files or directories
If the file or directory is publically-writeable, it should change it to be not publically-writeable, leaving other permissions unchanged.
It also should print a line to stdout as in the example below
dcc chmod_if_public_write.c -o chmod_if_public_write ls -ld file_modes.c file_modes file_sizes.c file_sizes -rwxr-xrwx 1 z5555555 z5555555 116744 Nov 2 13:00 file_sizes -rw-r--r-- 1 z5555555 z5555555 604 Nov 2 12:58 file_sizes.c -rwxr-xr-x 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw-r--rw- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c ./file_modes file_modes file_modes.c file_sizes file_sizes.c removing public write from file_sizes file_sizes.c is not publically writable file_modes is not publically writable removing public write from file_modes.c ls -ld file_modes.c file_modes file_sizes.c file_sizes -rwxr-xr-x 1 z5555555 z5555555 116744 Nov 2 13:00 file_sizes -rw-r--r-- 1 z5555555 z5555555 604 Nov 2 12:58 file_sizes.c -rwxr-xr-x 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw-r--r-- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c
Make sure you handle errors. -
Write a C program, list_dir.c, which opens a directory named "test_dir" and lists its contents.
You should use
opendir(3)
in your answer and useperror
to print error messages for example if the directory does not exist or if you do not have permission to list its contents.For example:
ls -a test_dir . .. f g dcc list_dir.c -o list_dir ./list_dir . .. f g chmod 310 list_dir ./list_dir test_dir: Permission denied rm -r test_dir ./list_dir test_dir: No such file or directory
What are the first two entries that are listed ie
.
and..
?
Assignment 2
Discuss/demonstrate how to get started on the assignment including
- Fetching the code
- Unzipping the examples
- Running the model solution
- Opening the starter code you have been given
-
Explain the output of
dump_drop
andprint_bytes
below:Note: print_bytes is one of the programs you wrote in lab08.
echo Hello >hi.txt echo Goodbye >bye.txt 1092 rain -c a.drop hi.txt bye.txt Adding: hi.txt Adding: bye.txt 1092 dump_drop a.drop ------------------------------------------------------------------------------ Field Name Field Offset Field Hex Field ASCII Field Numeric magic 0x00000000 63 c format 0x00000001 38 8 mode 0x00000002 2d 72 77 2d 72 2d 2d 2d 2d 2d -rw-r----- path length 0x0000000c 06 00 6 pathname 0x0000000e hi.txt content length 0x00000014 06 00 00 00 00 00 6 contents 0x0000001a """ Hello. """ hash 0x00000020 1e ------------------------------------------------------------------------------ Field Name Field Offset Field Hex Field ASCII Field Numeric magic 0x00000021 63 c format 0x00000022 38 8 mode 0x00000023 2d 72 77 2d 72 2d 2d 2d 2d 2d -rw-r----- path length 0x0000002d 07 00 7 pathname 0x0000002f bye.txt content length 0x00000036 08 00 00 00 00 00 8 contents 0x0000003c """ Goodbye. """ hash 0x00000044 31 ./print_bytes a.drop byte 0: 99 0x63 'c' byte 1: 56 0x38 '8' byte 2: 45 0x2d '-' byte 3: 114 0x72 'r' byte 4: 119 0x77 'w' byte 5: 45 0x2d '-' byte 6: 114 0x72 'r' byte 7: 45 0x2d '-' byte 8: 45 0x2d '-' byte 9: 45 0x2d '-' byte 10: 45 0x2d '-' byte 11: 45 0x2d '-' byte 12: 6 0x06 byte 13: 0 0x00 byte 14: 104 0x68 'h' byte 15: 105 0x69 'i' byte 16: 46 0x2e '.' byte 17: 116 0x74 't' byte 18: 120 0x78 'x' byte 19: 116 0x74 't' byte 20: 6 0x06 byte 21: 0 0x00 byte 22: 0 0x00 byte 23: 0 0x00 byte 24: 0 0x00 byte 25: 0 0x00 byte 26: 72 0x48 'H' byte 27: 101 0x65 'e' byte 28: 108 0x6c 'l' byte 29: 108 0x6c 'l' byte 30: 111 0x6f 'o' byte 31: 10 0x0a byte 32: 30 0x1e byte 33: 99 0x63 'c' byte 34: 56 0x38 '8' byte 35: 45 0x2d '-' byte 36: 114 0x72 'r' byte 37: 119 0x77 'w' byte 38: 45 0x2d '-' byte 39: 114 0x72 'r' byte 40: 45 0x2d '-' byte 41: 45 0x2d '-' byte 42: 45 0x2d '-' byte 43: 45 0x2d '-' byte 44: 45 0x2d '-' byte 45: 7 0x07 byte 46: 0 0x00 byte 47: 98 0x62 'b' byte 48: 121 0x79 'y' byte 49: 101 0x65 'e' byte 50: 46 0x2e '.' byte 51: 116 0x74 't' byte 52: 120 0x78 'x' byte 53: 116 0x74 't' byte 54: 8 0x08 byte 55: 0 0x00 byte 56: 0 0x00 byte 57: 0 0x00 byte 58: 0 0x00 byte 59: 0 0x00 byte 60: 71 0x47 'G' byte 61: 111 0x6f 'o' byte 62: 111 0x6f 'o' byte 63: 100 0x64 'd' byte 64: 98 0x62 'b' byte 65: 121 0x79 'y' byte 66: 101 0x65 'e' byte 67: 10 0x0a byte 68: 49 0x31 '1'
Revision questions
The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.