def more(fs: FileSystem, name: str): """ 展示文件 :param fs: :param name: :return: """ if name and name == '-h': print(""" 显示文本内容 more file_name 不可跨目录层级调用 完全显示文本内容 """) return pwd_cat = fs.load_pwd_obj() # 当前目录 flag = pwd_cat.is_exist_son_files(name) if flag == -1: print("{} 文件不存在".format(name)) if flag == DIR_TYPE: print("{} 是文件夹".format(name)) if flag == FILE_TYPE: inode_io = pwd_cat.get_file(name) inode = fs.get_inode(inode_id=inode_io) flag, text = fs.load_files_block(inode) if flag: print(text) else: print("cannot open file .: Permission denied")
def cp(fs: FileSystem, *args): """ 复制文件/目录参数-r :param fs: :param args: :return: """ if args[0] == '-h': print(""" 复制文件 cp xx/xx/src_filename xx/xx/tgt_dir 复制文件到其他目录 支持跨目录层级调用 仅支持复制文件 """) return if args[0] == '-r': path_src = args[1] path_tgt = args[2] else: path_src = args[0] path_tgt = args[1] name = path_src.split('/')[-1] # 取出文件名 cnt1 = len(path_src.split('/')) - 1 # 第一个目录的深度 cnt2 = len(path_tgt.split('/')) # 第二个目录的深度 text_copy = "" # 文件内容 cd(fs, '/'.join(path_src.split('/')[:-1])) pwd_cat = fs.load_pwd_obj() flag = pwd_cat.is_exist_son_files(name) if flag == -1: print("{} 文件不存在".format(name)) cd(fs, '/'.join(['..'] * cnt1)) return else: if flag == FILE_TYPE: inode_io = pwd_cat.son_files[name] inode = fs.get_inode(inode_id=inode_io) flag2, text = fs.load_files_block(inode) if flag2: text_copy = text # 传递内容 if flag == DIR_TYPE: print("不能复制文件夹") cd(fs, '/'.join(['..'] * cnt1)) return cd(fs, '/'.join(['..'] * cnt1)) # 增加到现在的目录下 cd(fs, '/'.join(path_tgt.split('/'))) touch(fs, name) pwd_cat_new = fs.load_pwd_obj() new_inode_io = pwd_cat_new.son_files[name] new_inode = fs.get_inode(inode_id=new_inode_io) fs.write_back(new_inode, pickle.dumps(text_copy)) new_inode.write_back(fs.fp) cd(fs, '/'.join(['..'] * cnt2))