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 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: print("specified directory does not exist") return Response("specified directory does not exist", 404) else: return Response("file doesn't exist", 404)
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 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 info(): # get file name filename = request.json['filename'] node = fs.get_file(filename) if node: return jsonify({'info': node.file}) else: return Response('no such file found', 404)
def info(): # get file name filename = request.json['filename'] # obtain the file from the filesystem node = fs.get_file(filename) if node: return jsonify({'info': node.file}) else: return Response(f'FAILED: file {filename} not found', 404)
def get(): print("started GETTING the file in NAMENODE") filename = request.json['filename'] print(f"filename = {filename}") file = fs.get_file(filename) if file: print("file exists") print(f"file = {file.file}") return jsonify({"file": file.file}) else: print("file doesn't exist") return Response("file doesn't exist", 404)
def delete(): # delete file from FS print("starting deleting file") filename = request.json['filename'] print(f"filename = {filename}") node = fs.get_file(filename) if node: print("file exists") file = fs.delete_file(node) print(f"file = {file}") return jsonify({"file": file}) else: print("file doesn't exist") return Response("file 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("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 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)