OlveraeUNSW
返回题目列表
COMP1521

COMP1521 2020T2 Q10 · 目录遍历

COMP1521 2020T2 Q10 · 10 marks · C
知识点: 目录遍历(DIR) · 次: 递归+序列化 · 难度: 进阶
Topic: Filesystem / directories · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

一句话 / Summary: 保存/恢复整个目录树(类tar)


We need a program which will save an entire directory tree as a single file.
The directory tree may consist of many directories and files.

Write a C program, practice_q10 ,
which is given either 1 or 2 arguments.

If practice_q10 is given 2 arguments,
the first argument will be the pathname of a file it should create
and the second argument will be the pathname of a directory.
practice_q10 should then save
the entire contents of the specified directory tree
in the specified file.

If practice_q10 is given 1 argument,
that argument will be the pathname of a file
in which a directory tree has been saved.
practice_q10 should re-create
all the directories and files in the directory tree.

For example,
these commands create a directory tree named a :

mkdir -p a/b/c
echo hello andrew >a/file1
echo bye andrew >a/b/file2
echo 1 >a/b/c/one
echo 2 >a/b/c/two
ls -lR a
a:
total 8
drwxr-xr-x 3 z1234567 z1234567 4096 Aug 19 20:38 b
-rw-r--r-- 1 z1234567 z1234567 13 Aug 19 20:38 file1

a/b:
total 8
drwxr-xr-x 2 z1234567 z1234567 4096 Aug 19 20:38 c
-rw-r--r-- 1 z1234567 z1234567 11 Aug 19 20:38 file2

a/b/c:
total 8
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 one
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 two
cat a/file1
hello andrew
cat a/b/file2
bye andrew

In this example, practice_q10
saves the contents of the directory tree a
into a file named data :

dcc practice_q10.c -o practice_q10
./practice_q10 data a
ls -l data
-rw-r--r-- 1 z1234567 z1234567 4567 Aug 19 20:38 data

This example shows practice_q10
restoring the contents of the directory tree a
after it has been removed:

rm -rf a
ls -lR a
ls: cannot access 'a': No such file or directory
cat a/file1
cat: a/file1: No such file or directory
./practice_q10 data
ls -lR a
a:
total 8
drwxr-xr-x 3 z1234567 z1234567 4096 Aug 19 20:38 b
-rw-r--r-- 1 z1234567 z1234567 13 Aug 19 20:38 file1

a/b:
total 8
drwxr-xr-x 2 z1234567 z1234567 4096 Aug 19 20:38 c
-rw-r--r-- 1 z1234567 z1234567 11 Aug 19 20:38 file2

a/b/c:
total 8
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 one
-rw-r--r-- 1 z1234567 z1234567 2 Aug 19 20:38 two
cat a/file1
hello andrew
cat a/b/file2
bye andrew

This example shows practice_q10
restoring the contents of the directory tree a
in a different directory:

mkdir new_directory
cd new_directory
../practice_q10 ../data
cat a/file1
hello andrew
cat a/b/file2
bye andrew

Autotest will only be of limited assistance in debugging your program.
Do not expect autotest messages to be easy to understand for this problem.
You will need to debug your program yourself.

It is easily possible to destroy many files with rm(1) .
Do not test this on your working files for this exam, unless you have a backup.
Do not assume your program will make a good enough backup.

Your solution must be in C only.

You are not permitted to run external programs.
You are not permitted to use system, popen, posix_spawn, fork or exec.

You are not permitted to use libraries other than the default C libraries.
In other words your solution can not require use of dcc's -l flag.
If your solution compiles without dcc's -l flag, you are using only the default C libraries.
All functions discussed in lectures are part of the default C libraries.

No error checking is necessary.

You can assume the directory tree to be saved contains only directories and regular files.
You can assume it does not contain links or other special files.
You can assume it does not contain sparse files.

You can not assume anything about the size or contents of files in the directory tree.
The files may contain any byte.
The files may be any size.

Your program does not have to save or restore permissions, modification times, or other file metadata.

You can assume the directory tree to be saved contains at most 10000 directories and regular files.

You can assume the directory tree to be saved is at most 1000 levels deep.

You can assume files and directories do not already exist when restoring a directory tree.

You can use any format you choose to save the directory tree in the file.


🤖 Adapted for the online judge (the original tar-style archive format is unspecified, so we test the directory walk instead): argv[1] is a directory in the run workspace. Print every regular file under it as <relpath> <size_bytes> (relpath relative to that directory), one per line, sorted ascending.

Verified on the sandbox AND cross-checked against an independent oracle.

样例

stdin
期望输出
a 2
b/c/one 2
b/c/two 2
b/file2 11
dir1/a 2
dir1/b/a 2
file1 13
c-dcc
14