Page 1 of 1

The file system In this assignment, you will implement a simple file system. Just like the one in your computer, our fil

Posted: Sat May 14, 2022 2:36 pm
by answerhappygod
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.
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.
}
file_sys.h
/**
* Do not modify this file, it won't be submitted to ZINC.
* Define your helper functions in file_sys.cpp
*/
#ifndef FILE_SYS_H
#define FILE_SYS_H
#include <cstring>
using namespace std;
const int MAX_NAME_LEN = 20;
enum Tag
{
VIDEO,
IMAGE,
DOC,
COMPRESSED,
PROGRAM,
MUSIC,
OTHER
};
struct Dir;
struct File
{
char name[MAX_NAME_LEN];
unsigned int size;
Tag tag;
Dir *parent;
File *next;
};
struct Dir
{
char name[MAX_NAME_LEN];
Dir *parent;
Dir *subdir;
File *subfile;
Dir *next;
};
// Creation
int createFile(Dir *, const char *, unsigned int, Tag =
OTHER);
int createDir(Dir *, const char *);
// Deletion
int deleteFile(Dir *, const char *);
int deleteDir(Dir *, const char *, bool = false);
// Size
unsigned int getSize(const Dir *);
// Tag
const File **filesOfTag(const Dir *, Tag, unsigned int
&);
// Movement
int moveFile(File *, Dir *);
int moveDir(Dir *, Dir *);
#endif