#P2262. FTP Server Simulation
FTP Server Simulation
FTP Server Simulation
This problem requires you to simulate an FTP server which processes a series of commands over a period of time. The server maintains a tree‐structured file system, where all resources are stored as files or folders. The root directory is the lowest level, and each folder may contain files and/or subfolders. No two files or folders in the same directory will have the same name.
Each user has three attributes:
- userType: the type of user;
- userState: the current operation the user is executing;
- userPosition: the folder that the user is browsing.
There are 3 user types:
uploadUser
(represented by A=1 in the connect command)downloadUser
(A=2)guest
(A=3)
Each FTP command has its own rules and permission requirements. Notably, a user may only perform an operation they have permission for. For example, a guest
user is not allowed to download any file or folder.
Each file or folder has three attributes:
fileName/folderName
: name (no spaces or newline characters)fileSize/folderSize
: size in bytes. Files have size in range \(0 < fileSize < 10^5\) and folders have size in range \(0 < folderSize < 10^8\). The size of a folder is defined as the sum of the sizes of all files contained in it.fileState/folderState
: state, which can be eithernormal
(when operations are allowed) oruploading
(only browsing is allowed). In addition, if any file within a folder is in theuploading
state, then the folder’s state isuploading
.
The FTP server has additional global properties:
- Maximum number of users allowed concurrently (denoted as
maxUserNumber
, and it is less than 100). If the current number of connected users equalsmaxUserNumber
, then any new connection fails. - Maximum server flux (
maxServerFlux
, less than \(10^7\)). - Maximum user flux (
maxUserFlux
) allowed for a single user.
The FTP server runs in discrete time units (seconds). The upload and download operations take a certain amount of time based on the user's flux which is given by:
[ \mathrm{userFlux} = \min(\mathrm{presentMaxUserFlux},,\mathrm{maxUserFlux}) ]
where
[ \mathrm{presentMaxUserFlux} = \lfloor \frac{\mathrm{maxServerFlux}}{\mathrm{userTotal}} \rfloor ]
and \(userTotal\) is the number of users currently uploading or downloading.
The server supports the following commands:
connect
Command
- Format:
[name] connect [A]
(e.g.tsinghua connect 1
). - If the number of connected users is less than
maxUserNumber
, and the user is not already connected, then the connection is accepted and the server responds withsuccess
. Otherwise, the server responds withunsuccess
. On a successful connection the user’suserPosition
is set to the root directory.
quit
Command
- Format:
[name] quit
. - If the user is connected, disconnect and respond with
success
; otherwise, respond withunsuccess
.
cd
Command
- Format:
[name] cd [folderName]
. - If the folder exists in the user’s current directory and is in a
normal
state, change the user’s location to that folder and respond withsuccess
; otherwise, respond withunsuccess
.
cd..
Command
- Format:
[name] cd..
. - If the user is not in the root directory and is connected, move the user to the parent directory and respond with
success
. If the user is at the root or not connected, respond withunsuccess
.
download
Command
- Format:
[name] download [target]
. - The user must be connected, have download permission (i.e. must not be a
guest
), and the target file/folder must exist in the current directory and be innormal
state. Otherwise, respond withunsuccess
. On success, the response issuccess
and the download begins immediately (using the file/folder as it is at the moment of command execution).
upload
Command
- Format:
[name] upload [target] [size]
(e.g.A upload B 1
). - If
size = 0
, the command uploads an empty folder; ifsize > 0
, it uploads a file of the given size. - The command succeeds if the user is connected, has upload permission (only
uploadUser
is allowed to upload), and there is no file/folder with the same name in the current directory; otherwise, respond withunsuccess
. On success, respond withsuccess
.
Except for the upload and download commands (which simulate time consumption), all commands are executed instantaneously. For this problem, you can assume that operations are processed sequentially and instantaneously without simulating the actual passage of time or flux distribution.
Your task is to simulate the FTP server over the given sequence of commands. For each command, output a line with the server's response: either success
or unsuccess
.
inputFormat
The input begins with a line containing three integers:
maxUserNumber
: maximum number of users allowed concurrently.maxServerFlux
.maxUserFlux
.
The next line contains an integer Q
specifying the number of commands. Each of the following Q
lines contains one command in one of the following formats:
[name] connect [A]
[name] quit
[name] cd [folderName]
[name] cd..
[name] download [target]
[name] upload [target] [size]
Note that usernames, folder names, and file names do not contain spaces. It is guaranteed that Q > 0
.
outputFormat
For each command, output a single line containing either success
or unsuccess
representing the server's response.
sample
3 1000000 1000
9
Alice connect 1
Alice upload folderA 0
Alice cd folderA
Alice cd..
Bob connect 2
Bob download folderA
Charlie connect 3
Charlie download folderA
Bob quit
success
success
success
success
success
success
success
unsuccess
success
</p>