Last Updated: 2021-02-28 Sun 22:27

CSCI 4061 HW06: File and Directory Operations

CODE DISTRIBUTION: hw06-code.zip

  • Download the code distribution every hw
  • See further setup instructions below

CHANGELOG: Empty

1 Rationale

File and directory manipulation is essential to most programs. While there are a large number of system calls related to this, some of the most important are covered in this HW: visiting all entries in a directory and determining basic file statistics such as size. This is typically done with the opendir()/readdir() and stat()/lstat() families of system calls.

In addition, this HW augments understanding of the open() call to allow different file permissions to be used and reviews the read() and write() calls.

1.1 Associated Reading

Stevens/Rago Ch 3 covers basic I/O functions like read() / write() as well as lseek() in Ch 3.6. These functions work equally as well for text and binary data.

Stevens/Rago Ch 4 covers interactions with the file system such as the stat() family of calls (Ch 4.2) and functions used to read directories like opendir() (Ch 4.22).

1.2 Grading Policy

Credit for this HW is earned by taking the associate Quiz which is linked under Gradescope. The quiz will ask similar questions as those that are present in the QUESTIONS.txt file and those that complete all answers in QUESTIONS.txt should have no trouble with the quiz.

See the full policy in the syllabus.

2 Codepack

The codepack for the HW contains the following files:

File Description  
QUESTIONS.txt Questions to answer  
dirops/ Directory Directory for Problems 1
dirops.c Provided Code to analyze for Problem 1
3K.txt Data Data for Problem 1
5K.txt   Data for Problem 1
8K.txt   Data for Problem 1

3 What to Understand

Ensure that you understand

  • How to scan a directory using opendir() and readdir()
  • Use of the stat() call for basic file information
  • Creation of files with specific permissions
  • Use of read() / write() to transfer information from one file into another
  • How data in files can be directly read() into arrays and structs.

4 Questions

                           __________________

                            LAB 06 QUESTIONS
                           __________________


- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)

Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete the
lab.


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.


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.


C
~

  Identify the system call that `dirops.c' uses to find the sizes of
  files. Describe briefly how this call works.


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 in
  addition to the other permissions present.

  /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 and the manual page for the system call shows the fields
  of the `struct stat', one of which shows the permissions/mode. Note:
  you may need to use `man 2 stat' to get the system call version of
  `stat' rather than the command line utility of the same name.


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.

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2021-02-28 Sun 22:27