The file system
In this assignment, you will implement a simple file system.
Just like the one in your computer, our file system is a tree of
directories and files, where a directory could contain other
directories and files, but a file cannot. In file_sys.h, you can
find the definition of two structures, Dir and File. These are the
two structures that we use to represent directories and files in
this assignment. Here are the meanings of their attributes:
Dir
File
You will also find an enumeration type enum Tag{VIDEO, IMAGE,
DOC, COMPRESSED, PROGRAM, MUSIC, OTHER}. There are 7 different tags
in this assignment, and each file will have one and
only one tag.
Then we use an example to elaborate more on the pointers in Dir
and File. The figure below shows a file system from a user's view
(left side) and how it looks in this assignment (right side). We
only show the pointers here for simplicity. In this example, the
directory "/" has two sub-directories "A", "B" and one sub-file
"C". Directory "A" is empty, and directory "B" has two sub-files
"D" and "E".
Finally, you don't have to worry whether a name is too long, we
won't test any cases with names beyond MAX_NAME_LEN. The name of
the root directory is, by convention, always "/".
File system tasks
There are 8 functions you need to implement in this section
which are all file system operations. Implement these functions and
your own helper functions in file_sys.cpp.
int createFile(Dir* dir, char* name, unsigned int size, Tag
tag)
This function creates new files. It returns an integer status
code that indicates the status of running this function.
The newly created file will be added to the head of the linked
list. Plus, we have some extra requirements on name (which is also
applicable to the next function):
It will return:
Except 0, all other status codes indicate an error. When there
are more than one error, always return the smallest status code.
For example, if dir is null and at the same time name has a
conflict, you should still return 1. Functions you may need:
strcpy(), strcmp(), strlen().
int createDir(Dir* dir, char* name)
This function creates new directories. It returns an integer
status code.
You should check the name in this function as well by the
criteria mentioned in last function (unique, non-empty, leagal,
non-keyword, beginning-character). The newly created directory will
be added to the head of the linked list. This function will
return:
Again, return the smallest one if there are multiple errors.
Functions you may need: strcpy(), strcmp(), strlen().
int deleteFile(Dir* dir, char* name)
This function deletes files. It returns an integer status
code.
The dynamic memory allocated for this file object should be
released, and the linked list that contains this file object should
be correctly updated. This function will return:
Again, return the smallest one if there are multiple errors.
int deleteDir(Dir* dir, char* name, bool recursive)
This function deletes directories. We add one more parameter
recursive. It works like this:
Parameters:
Like last function, you should correctly release dynamic memory
and update the linked list. This function will return:
Again, return the smallest one if there are multiple errors.
unsigned int getSize(const Dir* dir)
This function computes and returns the size of a directory. The
size of a directory is defined recursively as the sum of the size
of all sub-files and sub-directories. The size of an empty
directory is 0.
int moveFile(File* tgt, Dir* dest)
This function moves files. It returns an integer status
code.
The target file will be removed from the old linked list and
added to the head of the new linked list. This function will
return:
Again, return the smallest one if there are multiple errors.
int moveDir(Dir* tgt, Dir* dest)
This function moves directories. It returns an integer status
code.
Note that a directory cannot be moved to any of its descendants
(a descendant is a sub-directory or a sub-sub-directory or
sub-sub-sub ...). This is because the descendants will also be
moved together with the target, so they cannot be the destination.
The target directory will be added to the head of the new linked
list. This function will return:
Again, return the smallest one if there are multiple errors.
const File** filesOfTag(const Dir* dir, Tag
tag, unsigned int& length)
This function looks for all the files of a certain tag and
returns them in a dynamic array.
This function returns a dynamic array of pointers. Each pointer
here points to a file of the specified tag. Also remember to
set length to the correct value.
If dir is null or you cannot find any files of
that tag, length should be set to 0 and the function
should return null. Don't create a dynamic array
with size 0. The order of the pointers doesn't
matter, we will sort your array while grading.
file_sys.cpp
#include <iostream>
#include "file_sys.h"
#include "utils.h"
using namespace std;
int createFile(Dir *dir, const char *name, unsigned int size,
Tag tag)
{
return -1; // Remove this line after you finish.
}
int createDir(Dir *dir, const char *name)
{
return -1; // Remove this line after you finish.
}
int deleteFile(Dir *dir, const char *name)
{
return -1; // Remove this line after you finish.
}
int deleteDir(Dir *dir, const char *name, bool recursive)
{
return -1; // Remove this line after you finish.
}
unsigned int getSize(const Dir *dir)
{
return 0; // Remove this line after you finish.
}
const File **filesOfTag(const Dir *dir, Tag tag, unsigned int
&length)
{
length = 0; // Remove these two lines after you finish.
return NULL; // ....
}
int moveFile(File *tgt, Dir *dest)
{
return -1; // Remove this line after you finish.
}
int moveDir(Dir *tgt, Dir *dest)
{
return -1; // Remove this line after you finish.
}
The file system In this assignment, you will implement a simple file system. Just like the one in your computer, our fil
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
The file system In this assignment, you will implement a simple file system. Just like the one in your computer, our fil
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!