Exemple #1
0
def release(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        client = file_system_manager.get_active_client(client_id)
        res = file_system_manager.release_item(client, split_data[1])
        if res == 0:
            response = split_data[1] + " released"
        elif res == -1:
            response = "you do not hold the lock for %s" % split_data[1]
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def cd(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        res = file_system_manager.change_directory(split_data[1], client_id)
        response = ""
        if res == 0:
            response = "changed directory to %s" % split_data[1]
        elif res == 1:
            response = "directory %s doesn't exist" % split_data[1]
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def ls(connection, client_id, split_data, file_system_manager):
    response = ""
    if len(split_data) == 1:
        response = file_system_manager.list_directory_contents(client_id)
        connection.sendall(response.encode())
    elif len(split_data) == 2:
        response = file_system_manager.list_directory_contents(
            client_id, split_data[1])
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def mkdir(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        response = ""
        res = file_system_manager.make_directory(client_id, split_data[1])
        if res == 0:
            response = "new directory %s created" % split_data[1]
        elif res == 1:
            response = "file of same name exists"
        elif res == 2:
            response = "directory of same name exists"
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def rmdir(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        response = ""
        res = file_system_manager.remove_directory(client_id, split_data[1])
        if res == -1:
            response = "%s doesn't exist" % split_data[1]
        elif res == 0:
            response = "%s removed" % split_data[1]
        elif res == 1:
            response = "%s is a file" % split_data[1]
        elif res == 2:
            response = "directory has locked contents"
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
Exemple #6
0
def delete(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        res = file_system_manager.delete_file(client_id, split_data[1])
        response = ""
        if res == 0:
            response = "delete successfull"
        elif res == 1:
            response = "file locked"
        elif res == 2:
            response = "use rmdir to delete a directory"
        elif res == 3:
            response = "file doesn't exist"
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
Exemple #7
0
def lock(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        client = file_system_manager.get_active_client(client_id)
        res = file_system_manager.lock_item(client, split_data[1])
        response = ""
        if res == 0:
            response = "file locked"
        elif res == 1:
            response = "file already locked"
        elif res == 2:
            response = "file doesn't exist"
        elif res == 3:
            response = "locking directories is not supported"
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def start_client_interaction(connection, client_address):
    try:
        #A client id is generated, that is associated with this client
        client_id = file_system_manager.add_client(connection)
        while True:
            data = connection.recv(1024).decode()
            split_data = seperate_input_data(data)
            # Respond to the appropriate message
            if data == "KILL_SERVICE":
                kill_service(connection)
            elif split_data[0] == "ls":
                ls(connection, client_id, split_data, file_system_manager)
            elif split_data[0] == "cd":
                cd(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "up":
                up(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "read":
                read(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "write":
                write(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "delete":
                delete(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "lock":
                lock(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "release":
                release(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "mkdir":
                mkdir(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "rmdir":
                rmdir(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "pwd":
                pwd(connection, split_data, client_id, file_system_manager)
            elif split_data[0] == "exit":
                exit(connection, split_data, client_id, file_system_manager)
            else:
                error_response(connection, 1)
    except:
        error_response(connection, 0)
        connection.close()
Exemple #9
0
def write(connection, split_data, client_id, file_system_manager):
    response = ""
    if len(split_data) == 2:
        res = file_system_manager.write_item(client_id, split_data[1], "")
        if res == 0:
            response = "write successfull"
        elif res == 1:
            response = "file locked"
        elif res == 2:
            response = "cannot write to a directory file"
        connection.sendall(response.encode())
    elif len(split_data) == 3:
        res = file_system_manager.write_item(client_id, split_data[1],
                                             split_data[2])
        if res == 0:
            response = "write successfull"
        elif res == 1:
            response = "file locked"
        elif res == 2:
            response = "cannot write to a directory file"
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def pwd(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 1:
        response = file_system_manager.get_working_dir(client_id)
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)
def up(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 1:
        file_system_manager.move_up_directory(client_id)
    else:
        error_response(connection, 1)
Exemple #12
0
def read(connection, split_data, client_id, file_system_manager):
    if len(split_data) == 2:
        response = file_system_manager.read_item(client_id, split_data[1])
        connection.sendall(response.encode())
    else:
        error_response(connection, 1)