def create(): print("started CREATING file") # obtain filename filename = request.json['filename'] filesize = 0 if request.json['filesize']: filesize = request.json['filesize'] # check whether file already exists if fs.get_file(filename) or fs.get_dir(filename): print(f"FILE {filename} already exists") # app.logger.info(f"file already exists {filename}") return Response("FAILED: file or dir with this name already exists", 409) # create file, return info about datanodes and id else: # app.logger.info(f"filesize: {filesize} free_space:{fs.free_space}") if filesize > fs.free_space: # check if there's available space print("FAILED: not enough space") return Response("not enough space", 413) file_dir = os.path.dirname(filename) file_name = os.path.basename(filename) file_parent = fs.get_dir(file_dir) if file_parent: file = fs.create_file(file_name, file_parent, filesize) return jsonify({"file": file}) else: # TODO write Response return Response(f"FAILED: dir {file_dir} doesn't exist", 404)
def copy(): print("started copying file in namenode") filename = request.json['filename'] dirname = request.json['dirname'] # node with the file original_node = fs.get_file(filename) if original_node: print(f"file {filename} found") # node with the new dir if dirname[-1] == '/': new_node_par = fs.get_dir(dirname) if new_node_par: # resolving name collision new_name = os.path.basename(filename) + '_copy' count = 1 file = fs.get_file(new_name) while file: new_name = new_name + str(count) count += 1 file = fs.get_file(new_name) # creating copy of the file file = fs.create_file(new_name, new_node_par, original_node.file['size']) print(f"file was copied under the filename {filename}") return jsonify({'original': original_node.file, 'copy': file}) else: dir_name = os.path.dirname(dirname) file_name = os.path.basename(dirname) file = fs.get_file(dirname) if file or fs.get_dir(dirname): print("specified file already exists") return Response("FAILED: specified file already exists", 404) else: parent_dir = fs.get_dir(dir_name) if parent_dir: file = fs.create_file(file_name, parent_dir, original_node.file['size']) print(f"file was copied under the filename {filename}") return jsonify({ 'original': original_node.file, 'copy': file }) else: print("specified directory does not exist") return Response( "FAILED: specified directory does not exist", 404) else: return Response("FAILED: file doesn't exist", 404)
def move(): # get file name filename = request.json['filename'] # get path path = request.json['path'] # obtain file from the filesystem file_node = fs.get_file(filename) if file_node: # resolve filename dir_name = '' file_name = '' if path[-1] == '/': dir_name = path file_name = os.path.basename(filename) else: dir_name = os.path.dirname(path) file_name = os.path.basename(path) # obtain the directory where to move node = fs.get_dir(dir_name) if node: if file_name in [x.name for x in node.children]: return Response( f'FAILED: file {filename} already exists in this dir', 419) else: # move the file in a filesystem file_node.parent = node file_node.name = file_name return Response('', 200) else: return Response( f'FAILED: specified path {dir_name} does not exist', 404) else: return Response(f'FAILED: file {filename} could not be found', 404)
def mkdir(): # get directory name dirname = request.json['dirname'] if fs.get_file(dirname) or fs.get_dir(dirname): return Response("dir or file with such name exists", 409) else: # add directory to fs tree dir_parent = os.path.dirname(dirname) dir_name = os.path.basename(dirname) parent_node = fs.get_dir(dir_parent) # TODO write Response if parent_node: fs.create_directory(dir_name, parent_node) return Response("ok", 200) else: return Response("path doesn't exist", 404)
def mkdir(): # get directory name dirname = request.json['dirname'] if fs.get_file(dirname) or fs.get_dir(dirname): return Response("FAILED: dir or file with such name exists", 409) else: # add directory to fs tree dir_parent = os.path.dirname(dirname) dir_name = os.path.basename(dirname) parent_node = fs.get_dir(dir_parent) if parent_node: fs.create_directory(dir_name, parent_node) return Response("ok", 200) else: return Response( f"FAILED: specified path {dir_parent} does not exist", 404)
def cd(): # get directory name dirname = request.json['dirname'] node = fs.get_dir(dirname) if node: fs.cur_node = node return jsonify({ 'dirname': fs.cur_node.name, 'cur_dir': fs.get_current_dirname() }) else: return Response('', 404)
def delete_dir_sure(): print("starting deleting dir") dirname = request.json['dirname'] print(f"dirname: {dirname}") dir_node = fs.get_dir(dirname) if dir_node: files = fs.get_all_files_rec(dir_node) dir_node.parent = None return jsonify({"files": files}) else: print("dir doesn't exist") return Response("dir doesn't exist", 404)
def cd(): # get directory name dirname = request.json['dirname'] # get the directory from the filesystem node = fs.get_dir(dirname) if node: # change the current directory to the obtained one fs.cur_node = node return jsonify({ 'dirname': fs.cur_node.name, 'cur_dir': fs.get_current_dirname() }) else: return Response( f'FAILED: specified directory {dirname} does not exist', 404)
def delete_dir_sure(): print("starting deleting dir") dirname = request.json['dirname'] print(f"dirname: {dirname}") # obtain the directory from the filesystem dir_node = fs.get_dir(dirname) if dir_node: # get all files in the specified directory files = fs.get_all_files_rec(dir_node) # remove directory and all its children from the filesystem dir_node.parent = None return jsonify({"files": files}) else: print("dir doesn't exist") return Response("FAILED: dir doesn't exist", 404)
def ls(): # get directory name dirname = request.json['dirname'] dirs = [] files = [] dir_node = fs.get_dir(dirname) # TODO write Response if not dir_node: return Response("dir doesn't exist", 404) for node in dir_node.children: # check whether file or directory if node.is_file: files.append(node.name) else: dirs.append(node.name) return jsonify({'dirs': dirs, 'files': files})
def delete_dir_notsure(): print("starting deleting dir") dirname = request.json['dirname'] print(f"dirname: {dirname}") dir_node = fs.get_dir(dirname) if dir_node: children = [x for x in dir_node.children] if len(children) == 0: dir_node.parent = None print("directory empty, was removed successfully") return jsonify({"empty": True}) else: print("directory not empty") return jsonify({"empty": False}) else: print("dir doesn't exist") return Response("dir doesn't exist", 404)
def ls(): # get directory name dirname = request.json['dirname'] dirs = [] files = [] # get directory from the dilesystem dir_node = fs.get_dir(dirname) if not dir_node: return Response( f"FAILED: specified directory {dirname} does not exist", 404) for node in dir_node.children: # check whether file or directory if node.is_file: files.append(node.name) else: dirs.append(node.name) return jsonify({'dirs': dirs, 'files': files})
def delete_dir_notsure(): print("starting deleting dir") dirname = request.json['dirname'] print(f"dirname: {dirname}") # try to obtain directory from the filesystem dir_node = fs.get_dir(dirname) if dir_node: children = [x for x in dir_node.children] # check if directory contains something if len(children) == 0: dir_node.parent = None print("directory empty, was removed successfully") return jsonify({"empty": True}) else: print("directory not empty") return jsonify({"empty": False}) else: print("dir doesn't exist") return Response("FAILED: dir doesn't exist", 404)
def move(): # get file name filename = request.json['filename'] # get path path = request.json['path'] # TODO write Response file_node = fs.get_file(filename) if file_node: node = fs.get_dir(path) if node: if filename in [x.name for x in node.children]: return Response( 'file with such name already exists in this dir', 419) else: file_node.parent = node print(RenderTree(fs.root)) return Response('', 200) else: return Response('no such dir exists', 404) else: return Response('no such file exists', 404)