def cp_recursive(source, dest): new_file_name = dest.split("/")[-1]#get just the new file's name #check if the source file is a directory if(not os.path.isdir(source)): #not a directory, just make sure the path exists, then copy the file into the virtual FS path_str = check_path_exists(dest) copy_file(source, path_str, new_file_name) else: #if it IS a dir we need to do a little more work. path_str = check_path_exists(dest)#add the path up to the directory lind_fs_calls.chdir_syscall(path_str)#chdir to the end of that new path mode = stat.S_IMODE(os.stat(source).st_mode) #print "in cp " + source + "/" + new_file_name + "..." + str(mode) + "..." + str(S_IRWXU) try: lind_fs_calls.chdir_syscall(new_file_name)#chdir to that new directory except lind_fs_calls.SyscallError: lind_fs_calls.mkdir_syscall(new_file_name, mode)#make a directory for the new directory lind_fs_calls.chdir_syscall(new_file_name)#chdir to that new directory #then we loop through each of the subfiles of the directory, and recursively call the cp function #keeping track of both the native file location as well as the virtual file location! for c in os.listdir(source): cp_recursive(source + "/" + c, dest + "/" + c)
def check_path_exists(path): path_list = path.split("/")[0:-1]#get each directory in the path, excluding the new file's name lind_fs_calls.chdir_syscall("/")#start at root path_str = "/"#this string will hold the whole path of the destination file (excluding the filename itself) #loop through each dir in the path for p in path_list: if(p == ""): continue path_str += (p + "/")#concat the dir to the full path string #try to chdir (if it fails, means the directory doesnt exist) try: lind_fs_calls.chdir_syscall(p) #if (when) the exception is caught, create a new dir and chdir to it except lind_fs_calls.SyscallError as e: mode = stat.S_IMODE(os.stat(path_str + p).st_mode) #print "in check: " + path + p + "..." + str(mode) + "..." + str(S_IRWXU) lind_fs_calls.mkdir_syscall(p, mode) lind_fs_calls.chdir_syscall(p) return path_str
parser = argparse.ArgumentParser(description='Creates a one or more directories with the specified names') parser.add_argument('directory', metavar='dir', nargs='+', type=str, help='The directory names you want to create') #start off with an empty list mkdir_cmd.args = [] #try to parse the args try: mkdir_cmd.args = parser.parse_args(input_list) except SystemExit, e: pass else: #if they were correctly parsed, try to create a new directory for each name the user specified try: for new_dir_name in mkdir_cmd.args.directory: retval = lind_fs_calls.mkdir_syscall(new_dir_name, S_IRWXU) except lind_fs_calls.SyscallError, e: print "Could not mkdir. Error: %s" % e #changes directory def cd_cmd(input_list): #create a new argument parser, with 1 argument options (the directory to cd to). This arg may be null parser = argparse.ArgumentParser(description='Changes the current working directory to the one specified by dir') parser.add_argument('directory', metavar='dir', nargs='?', type=str, help='The path (relative or absolute) to cd to') #start with an empty list cd_cmd.args = [] #try to parse the args