Assignment 2 - Simple Nautilus*
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Assignment 2 - Simple Nautilus*
You are going to be writing an interactive application in Python that allows the user to send commands to a file management application and receive outputs.
The application largely mocks common Unix commands such as ls , cd , pwd , and so on.
You will need to implement and thoroughly test your solution.
On the high level, the application is capable of:
1. interpret commands received on standard input.
2. produce messages on standard output.
3. maintain some data structure that keeps track of a Uirtual name space.
4. create, delete and move Uirtual files and folders during program run time.
5. support user and permission management in addition to file management.
In Linux system, you can interact with the Terminal prompt to navigate in the
file system using cd , to perform actions to create, move, delete and copy files using touch mv rm cp , to alter the ownership and permission of files by chown chmod and to add or remove users by adduser deluser .
In this assignment, you need to write a Python program which provides an interactive prompt as if a Linux Terminal. Human users can interact with the Python program via standard input and standard output. One can input commands, and as a result, the program may print out feedback messages due to certain input.
Note that the system is incapable of storing content but purely the file system structure. This is an intensional design and therefore the size of a file is not defined and is omitted in the system. Similarly, the concept of time and group is omitted intentional. Please see the whole specification for more details.
The assignment has three tasks that you will need to implement and thoroughly test. You will be provided a test suite to assist in developing your solutions.
The three tasks are:
1. Implement a minimal Simple Nautilus.
2. Implement a simplified Simple Nautilus.
3. Implement a full Simple Nautilus.
Please read the whole specification to better understand these tasks.
* Nautilus: a file manager for GNOME.
Formatting Syntax
It is very common that command strings are written in a simple template lan- guage: characters are usually interpreted literally into themselves, but format specifiers, which are enclosed by parentheses, indicate the location and method to translate a piece of placeholder to characters.
In Simple Nautilus, the following formatting is used:
• Formatted arguments enclosed in sharp brackets < and > can be replaced by any single syntactically valid string.
• Formatted arguments enclosed in square brackets [ and ] can be replaced by zero or one syntactically valid string.
• Parameters that are not further specified can be any string. In order to be considered as a single syntactically valid parameter, a string that contains whitespace has to be enclosed by double quotation marks.
• Syntactically valid does not suggest semantically valid. E.g. "/a/b/c/d" is a syntactically valid string, but it might be semantically invalid given an environment where "/a/b/c/d" does not exist and the user tries to
ls /a/b/c/d .
Example
A formatted
string
can be extended by
are extended to
"Give You" and
or
is
omitted.
Permission and Ownership
Like in Unix, this system supports user and permission management. However, the concept of group is omitted and therefore the mode string for permission
is shorted than its form in Unix.
To view the permissions for all files in a directory, use the ls command with the -l option.
For example, if you enter:
You should see output similar to the following:
The first column indicates the file type and the permission information The second column indicates the owner of the file or directory. The third column indicates the name of the file or directory.
The first character in the first column indicates whether the listed object is a file or a directory. Directories are indicated by a ( d ); files are indicated by a dash ( - ), which is the absence of a d at the beginning of the first line.
Then, there are two sets of three characters that represent different levels of
ownership. Hence, myfile .txt is a file and Example is a directory. The letters rwx represent different permission levels:
Permission |
Files |
Directories |
||
|
can read the file |
can |
ls |
the directory |
can write the file |
can modify the directory’s contents |
|||
can execute the file |
can |
cd |
to the directory |
For example,
• drwxr-x : d being the first character indicates directory; the owner permissions are rwx , indicating that the owner can view, modify, and enter the directory; other (anyone other than the owner) can view as well as enter the directory. (Group is not defined, therefore there are no group permissions.)
• -rw-r-- : - being the first character indicates file; the owner per- missions are rw- , indicating that the owner can read and write to the file but can’t execute it as a program; other (anyone other than the owner) can only read the file. (Again, group is not defined, therefore there are no group permissions.)
When the file or directory is being created, the current effective user will deter- mine the owner of it.
Before the file or directory is being created, the current effective user can be updated by su . This indicates the owner of file or directory to be created can be different from time to time, depending on who the current effective user is.
Furthermore, the ownership of the file or directory can be directly updated by chown . This indicates the ownership does not remain unchanged indefinitely after creation.
Default setting and lifetime
The default user upon starting the program is root with the initial working directory being the root directory ( / ).
Whenever the program starts, the initial effective user root and an empty virtual name space are created; whenever the program exits, the whole virtual user space and virtual name space are lost.
root user is the most powerful user in this system. When the current effective user is root , they can ignore all permission requirements stated below. In other words, root will face no Permission denied .
Files are created with -rw-r-- permission on default.
Directories are created with drwxr-x permission on default.
Prompt Formation
The input prompt should have the following formation, where the leading prompt is ended by a dollar symbol and a single white space:
White spaces that come after a valid command should be ignored.
If there are only whitespace characters being inputted by the user as a commond string, nothing will happen when the user executes this command.
Example
If user root is at path |
|
, the prompt printed before any input should be: |
, then the combination of the input and prompt
forms the line:
Valid character set
User names, file names and directory names are valid when it only contains:
• All alphanumeric characters, a to z , A to Z , and 0 to 9 ;
• the Space character,
– An valid argument that contains Space must be surrounded by "
• the Hyphen character, - ;
• the Dot character, . ;
– However itself alone . (dot) or .. (dot-dot) has special meaning, see below.
• the Underscore character, _ .
Special path
dot
A dot component in a path refer to the preceding component.
For a path a/b/c/ ./d/ . , the preceding component of the first (left-most) dot is a/b/c . The preceding component of the second (right-most) dot is a/b/c/ ./d , which shall evaluate to a/b/c/d . The whole path shall evaluate to a/b/c/d .
dot-dot
A dot-dot component in a path refer to the parent of the preceding component. That is, it moved up one level toward the root / .
For a path a/b/c/ . ./d/ . . , the preceding component of the first (left-most) dot-dot is a/b/c . The preceding component of the second (right-most) dot- dot is a/b/c/ . ./d , which shall evaluate to a/b/d . The whole path shall evaluate to a/b if every prefix of the path is valid. Otherwise, the execution of the command is aborted and an error should be triggered and an error message should be printed as per command, as different command prints different error message upon non-exist error.
If the preceding component is empty, e.g. . ./a/b/c , assume the preceding component to the current working directory. If the preceding component already refers to the root / , e.g. / . . , then dot-dot component will also refer to root / (not the parent of / nor an error).
Example
Assume there exists a tree of directories /a/b/c/d/e , where a to e are valid directories. There are no other files or directories on the system.
Commands
Please note that all format string to be printed should be expended by the real parameter in-place.
exit
• To quit the program.
• Print: bye,
• Print name of current working virtual directory.
cd <dir>
• Change the working directory to
• Require current effective user’s execute bit x on
• If
• If
mkdir [-p] <dir>
• Create the directory
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• If -p is not specified and any ancestor directory in
• If -p is not specified and
• If -p is specified, do not print error and do nothing if
• If -p is specified, do not print error if any ancestor directory in
• Directories are created with drwxr-x permission (with regards to the current effective user).
touch <file>
• Update the access and modification times of a
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
•
• Files are created with -rw-r-- permission (with regards to the current effective user).
• If
• If any ancestor directory in
cp <src>
• Copy a file
• Require current effective user’s read bit r on
• Require current effective user’s execute bit x on
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• If
• If
• If
• If
• If
mv
• Move a file
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• If
• If
• If
• If
• If
refers to a directory, print: mv: Destination is a direcotry refers to a directory, print: mv: Source is a directory does not exist, print: mv: No such file or directory
rm
• Remove the file at
• Require current effective user’s write bit w on
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• If
• If
rmdir <dir>
• Remove empty directory.
• Require current effective user’s execute bit x on
• Require current effective user’s write bit w on
• If
• If
• If
• If
2022-09-15