COMP1521 2020T3 Q11 · 目录遍历
COMP1521 2020T3 Q11 · 9 marks · C
知识点: 目录遍历(DIR) · 次: 递归/stat/文件大小 · 难度: 进阶
Topic: Filesystem / directories · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
一句话 / Summary: 比较两个目录树输出4个计数
We need a program to check if the same files are present in two directory trees.
Write a C program, 20t3final_q11.c , which is given either 2 arguments which are the pathnames of directories.
20t3final_q11.c should print a single line of output containing 4 integers:
number of files which are present at the same position in both directory trees and are the same size.
number of files which are present at the same position in both directory trees but are different sizes.
number of files which are present only in the first directory tree.
number of files which are present only in the second directory tree.
Note, a file needs to be present in both directory trees at the same relative pathname to be counted.
For example, if we are comparing the directory trees dir1 and dir2 .
and the file dir1/1521/lab09/main.c exists in the first directory tree,
We consider it present in the second directory tree only if the file dir2/1521/lab09/main.c exists.
Other files named main.c elsewhere in the second directory tree, e.g. dir2/2521/lab05/main.c
are not counted.
When a file is present at the same relative pathname in both directory tree 20t3final_q11.c does not have check
it contains the same contents (bytes), just whether both files are the same size (number of bytes).
For example:
these commands create 2 directory tree named d1 and d2 containing the same 3 files.
mkdir -p d1/b/c
echo hello andrew > d1/file1
echo bye andrew > d1/b/file2
echo 1 > d1/b/c/one
mkdir -p d2/b/c
echo HELLO andrew > d2/file1
echo Bye Andrew > d2/b/file2
echo 2 > d2/b/c/one
Their contents of the 3 files are different but their sizes are the same.
find d1 d2 -type f | xargs stat -c '%3s %n'
13 d1/file1
11 d1/b/file2
2 d1/b/c/one
13 d2/file1
11 d2/b/file2
2 d2/b/c/one
20t3final_q11 reports 3 files present in both tree of the same size.
dcc 20t3final_q11.c -o 20t3final_q11
./20t3final_q11 d1 d2
3 0 0 0
If we change the size of file1 in the first directory tree
20t3final_q11 reports 2 files present in both tree of the same size.
and 1 file present in both tree but different size.
echo hello everyone >d1/file1
./20t3final_q11 d1 d2
2 1 0 0
If we add a file to the second directory tree:
echo 3 > d2/b/c/three
./20t3final_q11 d1 d2
2 1 0 1
If we add a different file to the first directory tree:
echo 3 > d1/b/three
./20t3final_q11 d1 d2
2 1 1 1
Note, the first directory tree contains a file named b/three and the second tree contains a file named b/c/three .
This is not considered as the file being present in both directory trees, as the absolute pathname of each file is different.
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.
There is no supplied code for this question.
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 trees to be compared contain only directories and regular files.
You can assume they do not contain links or other special files.
You can assume they do 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 consider permissions, modification times, or other file metadata.
You can assume the maxmimum absolute pathname length of each file or directory will not exceed 65536 bytes.
🤖argv[1],argv[2]are two directories in the run workspace. Print one line with 4 integers: (a) files at the same relative path in both with equal size, (b) same path but different size, (c) only in dir1, (d) only in dir2.
Verified on the sandbox AND cross-checked against an independent oracle.
样例
2 1 4 1