System Calls – Programming Project: Recursive Copy Files using System Calls

The programming project is to use Linux system calls to write a C program, mycp, which recursively copies src to dest. It should work exactly the same as the Linux command

cp -r src dest

which recursively copies src to dest.

1. Hints and Helps

  1. Analyze the various conditions whether copy is allowed. The following are some example cases, but they are incomplete. The reader should complete the case analyses before attempting to develop any code.
  •  src must exist but dest may or may not exist.
  •  If src is a file, dest may not exist, is a file or a directory.
  •  If src is a directory, dest must be an existing or non-existing directory.
  •  If src is a DIR and dest does not exist, create dest DIR and copy src to dest.
  •  If src is a DIR and dest is an existing DIR: do not copy if dest is a descendant of src. Otherwise, copy src into dest/, i.e. as dest/(basename(src))
  •  Never copy a file or directory to itself.

Whenever in doubt, run the Linux cp -r src dest command and compare the results.

  1. Organize the project program into 3 layers:

Base case: cpf2f(file 1, filef2): copy file 1 to file 2; handle REG and LNK files

Middle case: cpf2d(file, dir): copy file into an existing dir

Top case: cpd2d(dir1,dir2): copy (recursively) dir1 to dir2

2. Sample Solution

Sample solution of the programming project can be downloaded from the book’s website. Source code for instructors is also available upon request from the author.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

Your email address will not be published. Required fields are marked *