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
#start off with an empty list cp_cmd.args = [] #try to parse the args try: cp_cmd.args = parser.parse_args(input_list) except SystemExit, e: pass else: #the args were parsed correctly, call the actual cp function cp_recursive(cp_cmd.args.source, cp_cmd.args.dest); #it is very likely that the current working directory will be changed, so go back to where the user was before the cp try: lind_fs_calls.chdir_syscall(cur_dir) except lind_fs_calls.SyscallError, e: print "In cp_cmd. Could not cd. Error: %s" % e #this function is just for debugging. Copies a file (NON directory) back to disk so we can diff def cpout_cmd(source, dest): try: lindfd = lind_fs_calls.open_syscall(source, O_RDWR, 0) except lind_fs_calls.SyscallError, e: print "Couldnt open local file. Error: %s" %e return -1 try: