No prerequisite regarding file system is required.
We do not include a reference count for memory safety guarantee
In this homework, you will build a C/C++ program that simulates a
file explorer(e.g. finder on MacOS, explorer.exe on
Windows, dolphin in KDE plasma and nautilus in Gnome
DE). We have provided a template for you and some of the functions have
already been implemented. You should implement all functions in
file.c
and dir.c
and part of the
explorer.c
. There is also bonus functions in
explore.c
. We will give you some handy testers. You can use
them to write your own test case. The example are given in
main.c
.
Make sure that the compilation of the source code generates no
errors/warnings and conformity to C89 standard. We have provided a
Makefile
, you can run make to compile your code. You can
run make memcheck to check whether there is any memory leak (you will
need to install valgrind first). You can also run make clean to delete
all compiled files.
bool
: see
bool.h
.open_func
: see
file.h:21
.find_func
: see
explorer.h:142
.Notation: We use the extended regular expression
A{X,Y}
to represent the set {AX, AY}
.
file.{h,c}
dir.{h,c}
node.{h,c}
struct file
and struct directory
does not
store any metadata (e.g. file and directory name
and
path
). The metadata of a file/directory is held the node
into which the file/directory is embedded.explorer.{h,c}
node
, dir
or
file
.chdir
and cdpar
.For task 1, 2 and 3, you are required to complete the C source code
files.
For task 4 and 5, you are required to modify the makefile.
In our explore simulator, the size of the file is exactly equal to the memory reserved for the file. When more bytes need to be stored in the file, we will reallocate more memory for the new data.
In this task, you are required to implement all the empty functions
in file.c
. file_new(int, char *)
and
file_release(struct file *file)
are provided by us and you
are not suggested to change them. Please read the description in the
header file carefully and follow the instruction. Deal with the memory
allocation carefully.
The directory in the simulator is tree-structured. Each dir node will have several subordinates(children), each of them is either a dir node or a file node. A file node does not contain any subordinate. We identify the nodes by their names, so all the nodes with the same parent should have different names.
In this task, you are required to implement all the empty functions
in dir.c
. dir_new(int, char *)
and
dir_release(struct file *file)
are provided by us and you
are not suggested to change them. Please read the description in the
header file carefully and follow the instruction. Deal with the memory
allocation carefully.
The explorer contains a directory tree, one pointer points to the root of the tree and one pointer records the current working directory(cwd). The cwd is initially set to the root. The explore also maintain a list of supported file types and the functions to open them (e.g. pdf reader for .pdf in real world, music player for .wav). Before opening a file, its type and corresponding function should be submitted to the expolorer. Our explorer only supports limited number of types.
In this task, you are required to implement
bool explorer_cdpar(struct explorer *)
bool explorer_chdir(struct explorer *, const char *)
bool explorer_support_filetype(struct explorer *, open_func, int)
bool explorer_open(const struct explorer *, const char *)
in explore.c
.
The following functions are provided by us and you are not suggested to change them.
struct explorer *explorer_new(void)
void explorer_release(struct explorer *)
bool explorer_read(const struct explorer *, const char *, int, int, char *)
bool explorer_write(struct explorer *, const char *, int, int, const char *)
bool explorer_create(struct explorer *, char *, int)
bool explorer_mkdir(struct explorer *, char *)
bool explorer_delete(struct explorer *, const char *)
bool explorer_contain(struct explorer *, const char *)
Please read the description in the header file carefully and follow the instruction. Deal with the memory allocation carefully.
We would like our explore to support search and have a flexible
configuration on the result. Implementing
explorer_search_recursive(struct explorer*, const char *, find_func)
.
Note that the bonus only worth 1 point. It will be fine if you do not
feel like to finish it.
To allow code reuse, we shall provide the explorer as a static library.
Your task is to add a static library (archive of objects) target and
a statically linked executable target in the Makefile
. More
precisely:
Makefile
, add one target
libexplorer.a
.libexplorer.a
from the .c
source code
files.Makefile
, add one target
test.static.out
.libexplorer.a
with other
objects to obtain an statically linked standalone ELF executable
test.static.out
.You may use the following commands to verify your result
./test.static.out
: run the executable, should see no
error coming out.ar t libexplorer.a
: list the files included in the
static library.file libexplorer.a
: inspect the file type.nm test.static.out
: list the symbols in the ELF file,
you should see a T
which stands for text segment in front
of every file/directory/explorer function.Providing a dynamic library of the explorer is a favorable choice as it enables code reuse with less memory and disk footprint.
Your task is to add a dynamic library (shared objects) target and a
dynamically linked executable target in the Makefile
. More
precisely:
Makefile
, add one target
libexplorer.so
.libexplorer.a
from the .c
source code
files.Makefile
, add one target
test.dynamic.out
.libexplorer.so
with other
objects to obtain an ELF executable test.static.out
with
dynamically loaded dependencies.You may use the following commands to verify your result
./test.dynamic.out
: run the executable, should see no
error coming out.file libexplorer.so
: inspect the file type.ldd test.dynamic.out
: print the shared object
dependencies.nm test.dynamic.out
: list the symbols in the ELF file,
you should see a U
in front of every
file/directory/explorer function.You may take a look at this blog post for a concise instruction.
valgrind
and address sanitizer is used to detect memory
leak on runtime.Submit a tarball named
academic_integrity_and_data_integrity.tar
containing
Makefile
, node.c
, dir.c
,
file.c
and explorer.c
.
You may use the following command to create such archive file.
# make sure that these files are indeed situated in the current working directory
tar cvf academic_integrity_and_data_integrity.tar \
\
Makefile \
node.c \
dir.c \
file.c explorer.c
Still have any question? Post it on piazza!
Contact TA: Cheng Peng if you have any problem regarding this homework assignment.
Having a strong opinion regarding the homework? You may