__________________ LAB 05 QUESTIONS __________________ - Name: Michael Zhang - NetID: zhan4854 Answer the questions below according to the lab specification. Write your answers directly in this text file and submit it to complete Lab01. PROBLEM 1 `dirops.c' ==================== A ~ Examine the source code of `dirops.c' closely. It makes use of a variety of system calls to produce a semi-interesting effect. Compile and run it several times. Describe the overall intent of the program based on its output and the code you understand. It lists the files in the current directory, their files sizes, determines the largest file in the directory, and then copies that to a new file ".copy". An observation I made was that the directories . and .. are fixed at 4096 bytes. B ~ What set of system calls is used by the program to determine all the files in the current directory? Describe briefly how these calls work together. readdir returns the next entry in the directory listing. C ~ Identify the system call that `dirops.c' uses to find the sizes of files. Describe briefly how this call works. This line: stat(file->d_name, &sb); retrieves information from the operating system about how the file is stored on the disk, including inode numbers, ownership, file size, and so on. D ~ The following line sets up the read/write permissions that the copied file will have. ,---- | mode_t perms = S_IRUSR | S_IWUSR; `---- Modify this line so that the copied file is readable by the group as well as the user. /Optional challenge:/ Set the permissions to be identical to the original file. `stat()' is one way to find out the permissions for the original file. I found the list of user permissions in the 'stat' manpage: S_IRGRP. The modified line looks like: mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP; In order to find out what the original permissions were, another stat call must be performed on the file with the largest size. Ideally, this should be done by file descriptor, but since we are traversing the directory, it's impractical to keep all the file descriptors open until we identify the largest file. Here's how I would do it: struct stat s; stat(max_name, &s); mode_t perms = s.st_mode; E ~ `dirops.c' contains a subtle bug in the following bit of code towards the end of the file. ,---- | while( (nbytes = read(infd, buf, BUFSIZE)) > 0){ | write(outfd, buf, BUFSIZE); | } `---- You should observe that every time program is run, it will identify a copied file as the largest and make another copy due to this bug. It may help to examine the ends of the copied files with the `tail file.txt.copy' command which will show the last 10 lines. Explain what is wrong with the loop and paste a fixed version below. The problem is that the buffer is full before the final loop, which may be less than BUFSIZE bytes, but the write command is still writing BUFSIZE bytes. Sicne there's no mechanism to automatically clear out the remaining bytes, they get written to the file as well. The solution is to only write the number of bytes that have been read, since it was stored anyways but not used. 44 while ((nbytes = read(infd, buf, BUFSIZE)) > 0) { 45 write(outfd, buf, nbytes); 46 }