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)): # keep same name if none specified if not new_file_name: new_file_name = source.split("/")[-1] # not a directory, just make sure the path exists, then copy the file into the virtual FS path_str = check_path_exists(dest)# we should create the path if it doesnt exist (but print a warning)! 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_test_server.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_test_server.chdir_syscall(new_file_name)#chdir to that new directory except lind_test_server.SyscallError: lind_test_server.mkdir_syscall(new_file_name, mode)#make a directory for the new directory lind_test_server.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_test_server.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 == ""): # ignore the empty string returned from splitting root dir '/' continue path_str += (p + "/")#c oncat the dir to the full path string # try to chdir (if it fails, means the directory doesnt exist) try: lind_test_server.chdir_syscall(p) # if (when) the exception is caught, create a new dir and chdir to it except lind_test_server.SyscallError: # this seems to cause an assertion to fail! #try: # get the mode if the dir exists in the fs # mode = stat.S_IMODE(os.stat(path_str).st_mode) #except OSError: # if not set to default 777 # mode = lind_test_server.S_IRWXA mode = lind_test_server.S_IRWXA lind_test_server.mkdir_syscall(p, mode) lind_test_server.chdir_syscall(p) return path_str
def mkdir_cmd(dir_list): assert(len(dir_list)) try: for new_dir_name in dir_list: # this follows paths and correctly creates subdirs and such! lind_test_server.mkdir_syscall(new_dir_name, lind_test_server.S_IRWXU) except lind_test_server.SyscallError, e: print "Could not mkdir. Error: %s" % e
def _cp_dir_into_lind(fullfilename, rootpath='.', createmissingdirs=True): # check for the file. posixfn = os.path.join(rootpath, fullfilename) if not os.path.exists(posixfn): raise IOError("Cannot locate dire on POSIX FS: '" + posixfn + "'") if os.path.isfile(posixfn): raise IOError("POSIX FS path is not a directory: '" + posixfn + "'") # now, we should check / make intermediate dirs if needed... # we will walk through the components of the dir and look for them... # this removes '.', '///', and similar. # BUG: On Windows, this converts '/' -> '\'. I don't think lind FS handles # '\'... normalizedlindfn = os.path.normpath(fullfilename) # go through the directories and check if they are there, possibly creating # needed ones... currentdir = '' # NOT os.path.split! I want each dir!!! for thisdir in normalizedlindfn.split('/'): currentdir += thisdir + '/' try: # check if this is a directory that exists if IS_DIR(lind_test_server.stat_syscall(currentdir)[2]): # all is well continue # otherwise, it exists, but isn't a dir... raise IOError("LIND FS path exists and isn't a dir: '" + currentdir + "'") except lind_test_server.SyscallError, e: # must be missing dir or else let the caller see this! if e[1] != "ENOENT": raise # okay, do I create it? if not createmissingdirs: raise IOError( "LIND FS path does not exist but should not be created: '" + currentdir + "'") # otherwise, make it ... lind_test_server.mkdir_syscall(currentdir, S_IRWXA) # and copy over the perms, etc. _mirror_stat_data(os.path.join(rootpath, currentdir), currentdir)
def _cp_dir_into_lind(fullfilename, rootpath='.', createmissingdirs=True): # check for the file. posixfn = os.path.join(rootpath, fullfilename) if not os.path.exists(posixfn): raise IOError("Cannot locate dire on POSIX FS: '" + posixfn + "'") if os.path.isfile(posixfn): raise IOError("POSIX FS path is not a directory: '" + posixfn + "'") # now, we should check / make intermediate dirs if needed... # we will walk through the components of the dir and look for them... # this removes '.', '///', and similar. # BUG: On Windows, this converts '/' -> '\'. I don't think lind FS handles # '\'... normalizedlindfn = os.path.normpath(fullfilename) # go through the directories and check if they are there, possibly creating # needed ones... currentdir = '' # NOT os.path.split! I want each dir!!! for thisdir in normalizedlindfn.split('/'): currentdir += thisdir + '/' try: # check if this is a directory that exists if IS_DIR(lind_test_server.stat_syscall(currentdir)[2]): # all is well continue # otherwise, it exists, but isn't a dir... raise IOError("LIND FS path exists and isn't a dir: '" + currentdir + "'") except lind_test_server.SyscallError, e: # must be missing dir or else let the caller see this! if e[1] != "ENOENT": raise # okay, do I create it? if not createmissingdirs: raise IOError("LIND FS path does not exist but should not be created: '" + currentdir + "'") # otherwise, make it ... lind_test_server.mkdir_syscall(currentdir,S_IRWXA) # and copy over the perms, etc. _mirror_stat_data(os.path.join(rootpath, currentdir), currentdir)
import lind_test_server from lind_fs_constants import * lind_test_server._blank_fs_init() # Let's add a few directories to the system and see if it works... lind_test_server.mkdir_syscall('/bar',S_IRWXA) lind_test_server.mkdir_syscall('/bar/baz',S_IRWXA) lind_test_server.mkdir_syscall('/bar/bap',0) # Create a new file... fd = lind_test_server.open_syscall('/bar/bam',O_CREAT,0) lind_test_server.close_syscall(fd) # Read the root directory... rootfd = lind_test_server.open_syscall('/',0,0) val = lind_test_server.getdents_syscall(rootfd, 100) assert (val==[(3, 'bar', DT_DIR, 24), (1, '..', DT_DIR, 24),\ (1, '.', DT_DIR, 24)]), "Found: %s"%(str(val)) # Read the /bar directory... barfd = lind_test_server.open_syscall('/bar',0,0) # The buffer size is given small, only few entries are read. val = lind_test_server.getdents_syscall(barfd, 80) assert (val == [(6, 'bam', DT_REG, 24), (4, 'baz', DT_DIR, 24),\ (5, 'bap', DT_DIR, 24)]), "Found: %s"%(str(val)) # Again call on the same FD, should continue parsing the /bar directory. val = lind_test_server.getdents_syscall(barfd, 80)
def _cp_file_into_lind(fullfilename, rootpath='.', createmissingdirs=True): """ <Purpose> Copies a file from POSIX into the Lind FS. It takes the abs path to the file ('/etc/passwd') and looks for it in the POSIX FS off of the root path. For example, rootpath = '/foo' -> '/foo/etc/passwd'. If the file exists, it is overwritten... <Arguments> fullfilename: The location the file should live in the Lind FS. This must be an absolute path (from the root). rootpath: The directory in the POSIX fs to start looking for that file. Note: it must be in the directory structure specified by fullfilename createmissingdirs: Should missing dirs in the path be created? <Exceptions> IOError: If the file does not exist, the directory can't be created (for example, if there is a file of the same name), or createmissingdirs is False, but the path is invalid. <Side Effects> The obvious file IO operations <Returns> None """ # check for the file. posixfn = os.path.join(rootpath, fullfilename) if not os.path.exists(posixfn): raise IOError("Cannot locate file on POSIX FS: '" + posixfn + "'") if not os.path.isfile(posixfn): raise IOError("POSIX FS path is not a file: '" + posixfn + "'") # now, we should check / make intermediate dirs if needed... # we will walk through the components of the dir and look for them... # this removes '.', '///', and similar. # BUG: On Windows, this converts '/' -> '\'. I don't think lind FS handles # '\'... normalizedlindfn = os.path.normpath(fullfilename) normalizedlinddirname = os.path.dirname(normalizedlindfn) # go through the directories and check if they are there, possibly creating # needed ones... currentdir = '' # NOT os.path.split! I want each dir!!! for thisdir in normalizedlinddirname.split('/'): currentdir += thisdir + '/' try: # check if this is a directory that exists if IS_DIR(lind_test_server.stat_syscall(currentdir)[2]): # all is well continue # otherwise, it exists, but isn't a dir... raise IOError("LIND FS path exists and isn't a dir: '" + currentdir + "'") except lind_test_server.SyscallError, e: # must be missing dir or else let the caller see this! if e[1] != "ENOENT": raise # okay, do I create it? if not createmissingdirs: raise IOError("LIND FS path does not exist but should not be created: '" + currentdir + "'") # otherwise, make it ... lind_test_server.mkdir_syscall(currentdir, S_IRWXA) # and copy over the perms, etc. _mirror_stat_data(os.path.join(rootpath, currentdir), currentdir)
def main(): # I can be imported as a module too!!! if len(sys.argv)==1: print_usage() return command = sys.argv[1] args = sys.argv[2:] #help : print this message #usage : print this message if command == 'help' or command == 'usage': print_usage() #cp root file1 [file2...] : copies files from the root into the lindfs. For # example, cp bar /etc/passwd /bin/ls will copy # bar/etc/passwd, bar/bin/ls as /etc/passwd, /bin/ls elif command == 'cp': lind_test_server.load_fs() if len(args)<2: print 'Too few arguments to cp. Must specify root and at least one file' return root = args[0] for filetocp in args[1:]: cp_into_lind(filetocp, rootpath=root) #find [startingpath] : print the names of all files / paths in the fs # This is much like 'find /' elif command == 'find': lind_test_server.load_fs() if len(args)>1: print 'Too many arguments to find. Takes an optional path' return elif len(args) == 0: startingpath = '/' elif len(args)==1: startingpath = args[0] allpathlist = list_all_lind_paths(startingdir=startingpath) # want to print this more cleanly than as a list for thispath in allpathlist: print thispath #format : make a new blank fs, removing the current one. elif command == 'format': lind_test_server._blank_fs_init() #deltree dirname : delete a directory and all it contains elif command == 'deltree': lind_test_server.load_fs() if len(args)!= 1: print 'deltree takes exactly one argument, the dir to remove' return print 'Unimplemented...' #rm file1 [file2...] : delete a file elif command == 'rm': lind_test_server.load_fs() if len(args) == 0: print 'rm takes one or more arguments' return for filename in args: lind_test_server.unlink_syscall(filename) #mkdir dir1 [dir2...] : create a directory elif command == 'mkdir': lind_test_server.load_fs() if len(args) == 0: print 'mkdir takes one or more arguments' return for dirname in args: lind_test_server.mkdir_syscall(dirname,S_IRWXA) #rmdir dir1 [dir2...] : delete a directory elif command == 'rmdir': lind_test_server.load_fs() if len(args) == 0: print 'rmdir takes one or more arguments' return for dirname in args: lind_test_server.rmdir_syscall(dirname) # OTHERWISE? else: print "ERROR: command unknown" print print_usage() return
def mkdir(self, path, mode): log("mkdir", path, mode) try: ret = lind.mkdir_syscall(path, mode) except lind.SyscallError, e: ret = -errno[e[1]]
def main(): # I can be imported as a module too!!! if len(sys.argv)==1: print_usage() return command = sys.argv[1] args = sys.argv[2:] #help : print this message #usage : print this message if command == 'help' or command == 'usage': print_usage() #cp root file1 [file2...] : copies files from the root into the lindfs. For # example, cp bar /etc/passwd /bin/ls will copy # bar/etc/passwd, bar/bin/ls as /etc/passwd, /bin/ls elif command == 'cp': lind_test_server.load_fs() if len(args)<2: print 'Too few arguments to cp. Must specify root and at least one file' return root = args[0] for filetocp in args[1:]: cp_dir_into_lind(filetocp, rootpath=root) #update root file1 [file2...] : copies files from the root into the lindfs if they are different. For # example, cp bar /etc/passwd /bin/ls will copy # bar/etc/passwd, bar/bin/ls as /etc/passwd, /bin/ls elif command == 'update': lind_test_server.load_fs() if len(args)<2: print 'Too few arguments to update. Must specify root and at least one file' return root = args[0] for filetoup in args[1:]: update_dir_into_lind(filetoup, rootpath=root) #find [startingpath] : print the names of all files / paths in the fs # This is much like 'find /' elif command == 'find': lind_test_server.load_fs() if len(args)>1: print 'Too many arguments to find. Takes an optional path' return elif len(args) == 0: startingpath = '/' elif len(args)==1: startingpath = args[0] allpathlist = list_all_lind_paths(startingdir=startingpath) # want to print this more cleanly than as a list #for thispath in allpathlist: # print thispath #format : make a new blank fs, removing the current one. elif command == 'format': lind_test_server._blank_fs_init() #deltree dirname : delete a directory and all it contains elif command == 'deltree': lind_test_server.load_fs() if len(args)!= 1: print 'deltree takes exactly one argument, the dir to remove' return deltree_lind(args[0]) #rm file1 [file2...] : delete a file elif command == 'rm': lind_test_server.load_fs() if len(args) == 0: print 'rm takes one or more arguments' return for filename in args: lind_test_server.unlink_syscall(filename) #mkdir dir1 [dir2...] : create a directory elif command == 'mkdir': lind_test_server.load_fs() if len(args) == 0: print 'mkdir takes one or more arguments' return for dirname in args: lind_test_server.mkdir_syscall(dirname,S_IRWXA) #rmdir dir1 [dir2...] : delete a directory elif command == 'rmdir': lind_test_server.load_fs() if len(args) == 0: print 'rmdir takes one or more arguments' return for dirname in args: lind_test_server.rmdir_syscall(dirname) # OTHERWISE? else: print "ERROR: command unknown" print print_usage() return
def cp_into_lind(fullfilename, rootpath='.', createmissingdirs=True): """ <Purpose> Copies a file from POSIX into the Lind FS. It takes the abs path to the file ('/etc/passwd') and looks for it in the POSIX FS off of the root path. For example, rootpath = '/foo' -> '/foo/etc/passwd'. If the file exists, it is overwritten... <Arguments> fullfilename: The location the file should live in the Lind FS. This must be an absolute path (from the root). rootpath: The directory in the POSIX fs to start looking for that file. Note: it must be in the directory structure specified by fullfilename createmissingdirs: Should missing dirs in the path be created? <Exceptions> IOError: If the file does not exist, the directory can't be created (for example, if there is a file of the same name), or createmissingdirs is False, but the path is invalid. <Side Effects> The obvious file IO operations <Returns> None """ # check for the file. posixfn = os.path.join(rootpath,fullfilename) if not os.path.exists(posixfn): raise IOError("Cannot locate file on POSIX FS: '"+posixfn+"'") if not os.path.isfile(posixfn): raise IOError("POSIX FS path is not a file: '"+posixfn+"'") # now, we should check / make intermediate dirs if needed... # we will walk through the components of the dir and look for them... # this removes '.', '///', and similar. # BUG: On Windows, this converts '/' -> '\'. I don't think lind FS handles # '\'... normalizedlindfn = os.path.normpath(fullfilename) normalizedlinddirname = os.path.dirname(normalizedlindfn) # go through the directories and check if they are there, possibly creating # needed ones... currentdir = '' # NOT os.path.split! I want each dir!!! for thisdir in normalizedlinddirname.split('/'): currentdir +=thisdir + '/' try: # check if this is a directory that exists if IS_DIR(lind_test_server.stat_syscall(currentdir)[2]): # all is well continue # otherwise, it exists, but isn't a dir... raise IOError("LIND FS path exists and isn't a dir: '"+currentdir+"'") except lind_test_server.SyscallError, e: # must be missing dir or else let the caller see this! if e[1] != "ENOENT": raise # okay, do I create it? if not createmissingdirs: raise IOError("LIND FS path does not exist but should not be created: '"+currentdir+"'") # otherwise, make it ... lind_test_server.mkdir_syscall(currentdir,S_IRWXA) # and copy over the perms, etc. _mirror_stat_data(os.path.join(rootpath,currentdir),currentdir)