def __init__(self, path, flags, *mode): log("open", path, hex(flags)) self.direct_io = False self.keep_cache = False lindfd = lind.open_syscall(path, flags, lind.S_IRWXA) self.fd = lindfd
def ls_cmd(dir_list): # we want to print the dirname if we are ls'ing multiple dirs print_dir = 0 if len(dir_list) > 1: print_dir = 1 for dir in dir_list: try: lindfd = lind_test_server.open_syscall(dir, lind_test_server.O_RDONLY, lind_test_server.S_IRWXU) #first open the file except lind_test_server.SyscallError, e: print "Could not open the local file. Error: %s" % e return -1 getdents_size = 20 #the number of nodes to check per getdents ls_list = [] #the list that will hold all the subfiles prev_ls_list_count = -1#keeps track of the list count during the previous iteration #while there are still files to look at, add them to the list. Once the list size is the same for 2 iterations in a row, we know there is nothing left to look at! while not len(ls_list) == prev_ls_list_count: try: prev_ls_list_count = len(ls_list)#update the count ls_list.extend(lind_test_server.getdents_syscall(lindfd, getdents_size))#add the current group of <getdents_size> files to the list of all files except lind_test_server.SyscallError, e: print "getdents failed. Error: %s" % e
def cpout_cmd(source, dest): try: lindfd = lind_test_server.open_syscall(source, lind_test_server.O_RDWR, 0) except lind_test_server.SyscallError, e: print "Couldnt open local file. Error: %s" %e return -1
def readdir(self, path, offset): log("readdir", path, offset) lindfd = lind.open_syscall(path, lind.O_RDONLY, lind.S_IRWXU) dents = map(lambda x: x[1], lind.getdents_syscall(lindfd, 999)) assert len(dents) < 998, "Readdir max was hit..." for e in dents: yield fuse.Direntry(e)
def md5_cmd(input_list): """print the md5 digest of all the files""" for filename in input_list: m = hashlib.md5() lindfd = lind_test_server.open_syscall( filename, lind_test_server.O_CREAT | lind_test_server.O_RDWR, 0) try: while True: s = lind_test_server.read_syscall(lindfd, BLOCK_SIZE) m.update(s) if len(s) == 0: break print m.hexdigest() + " " + filename except lind_test_server.SyscallError as e: print "Could not read file %(f)s. Error: %(err)s" % {'f': filename, 'err': e}
def cat_cmd(input_list): """print the contents of all the files""" for filename in input_list: if len(input_list) > 1: print filename + ":" lindfd = lind_test_server.open_syscall( filename, lind_test_server.O_CREAT | lind_test_server.O_RDWR, 0) try: while True: s = lind_test_server.read_syscall(lindfd, BLOCK_SIZE) print s if len(s) == 0: break except lind_test_server.SyscallError as e: print "Could not read file %(f)s. Error: %(err)s" % {'f': filename, 'err': e}
def _find_all_paths_recursively(startingpath): # helper for list_all_lind_paths. It recursively looks at all child dirs knownitems = [] # I need to open the dir to use getdents... dirfd = lind_test_server.open_syscall(startingpath,0,0) # build a list of all dents. These have an odd format: # [(inode, filename, d_type (DT_DIR, DR_REG, etc.), length of entry), ...] # We only care about the filename and d_type. mydentslist = [] # Note: the length parameter is odd, it's related to data structure space, so # it doesn't map to python cleanly. So long as it's > the largest possible # entry size, this code should work though. thesedents = lind_test_server.getdents_syscall(dirfd,10000) while thesedents: mydentslist += thesedents thesedents = lind_test_server.getdents_syscall(dirfd,10000) lind_test_server.close_syscall(dirfd) # to make the output correct, if the dir is '/', don't print it. if startingpath == '/': startingpath = '' for dent in mydentslist: # ignore '.' and '..' because they aren't interesting and we don't want # to loop forever. if dent[1]=='.' or dent[1]=='..': continue thisitem = (dent[0], startingpath+'/'+dent[1]) # add it... knownitems.append(thisitem) print thisitem # if it's a directory, recurse... if dent[2]==DT_DIR: knownitems = knownitems + _find_all_paths_recursively(thisitem[1]) return knownitems
def deltree_lind(dirname): # took part of the code from _find_all_paths_recursively method below. # It recursively looks at all child dirs # I need to open the dir to use getdents... dirfd = lind_test_server.open_syscall(dirname,0,0) # build a list of all dents. These have an odd format: # [(inode, filename, d_type (DT_DIR, DR_REG, etc.), length of entry), ...] # We only care about the filename and d_type. mydentslist = [] # Note: the length parameter is odd, it's related to data structure space, so # it doesn't map to python cleanly. So long as it's > the largest possible # entry size, this code should work though. thesedents = lind_test_server.getdents_syscall(dirfd,10000) while thesedents: mydentslist += thesedents thesedents = lind_test_server.getdents_syscall(dirfd,10000) lind_test_server.close_syscall(dirfd) for dent in mydentslist: # ignore '.' and '..' because they aren't interesting and we don't want # to loop forever. if dent[1]=='.' or dent[1]=='..': continue thisitem = (dent[0], dirname+'/'+dent[1]) print "deleting",thisitem[1] # if it's a directory, recurse... if dent[2]==DT_DIR: deltree_lind(thisitem[1]) else: lind_test_server.unlink_syscall(thisitem[1]) lind_test_server.rmdir_syscall(dirname) return
from lind_fs_constants import * import lind_test_server lind_test_server._blank_fs_init() myfd = lind_test_server.creat_syscall('/hello', S_IRWXA) lind_test_server.close_syscall(myfd) # Read only file descriptor opened rdfd = lind_test_server.open_syscall('/hello', O_RDONLY, S_IRWXA) try: # File written lind_test_server.write_syscall(rdfd, 'Hello everyone!') except lind_test_server.SyscallError: # should be an error pass else: print "Error! Should have been blocked from writing with O_RDONLY" lind_test_server.lseek_syscall(rdfd, 0, SEEK_SET) # should work... lind_test_server.read_syscall(rdfd, 100) lind_test_server.close_syscall(rdfd) # Alternately
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) # Okay, I have made the path now. Only one thing remains, adding the file posixfo = open(posixfn) filecontents = posixfo.read() posixfo.close() # make the new file, truncating any existing contents... lindfd = lind_test_server.open_syscall(normalizedlindfn, O_CREAT|O_EXCL|O_TRUNC|O_WRONLY, S_IRWXA) # should write all at once... datalen = lind_test_server.write_syscall(lindfd, filecontents) assert(datalen == len(filecontents)) lind_test_server.close_syscall(lindfd) # fix stat, etc. _mirror_stat_data(posixfn, normalizedlindfn) def _cp_dir_into_lind(fullfilename, rootpath='.', createmissingdirs=True): # check for the file. posixfn = os.path.join(rootpath, fullfilename)
import lind_test_server from lind_fs_constants import * # Let's add a few files, etc. to the system and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall('/foo',O_CREAT | O_EXCL | O_WRONLY,S_IRWXA) # write should succeed assert(lind_test_server.write_syscall(myfd,'hi') == 2) stat_result = lind_test_server.stat_syscall('/foo') # ensure the file has size 2 assert(stat_result[7] == 2) # ensure the link count is 1 assert(stat_result[3] == 1) # create a file with no perms... lind_test_server.link_syscall('/foo','/foo2') stat_result = lind_test_server.stat_syscall('/foo') stat_result2 = lind_test_server.stat_syscall('/foo2') # ensure they are the same now... assert(stat_result2 == stat_result) # and that the link count is 2
""" File: ut_lind_fs_mknod.py Unit test for mknod_syscall(), which is used to create special files. """ import lind_test_server from lind_fs_constants import * lind_test_server._blank_fs_init() lind_test_server._load_lower_handle_stubs() # let's create /dev/null... lind_test_server.mknod_syscall('/null', S_IFCHR, (1, 3)) fd = lind_test_server.open_syscall('/null', O_CREAT | O_RDWR, S_IRWXA) assert lind_test_server.fstat_syscall(fd)[2] & S_FILETYPEFLAGS == S_IFCHR,\ "File should be a Character file." assert lind_test_server.fstat_syscall(fd)[6] == (1, 3),\ "File is not /dev/null." assert lind_test_server.write_syscall(fd, "test") == 4,\ "Write failed to /dev/null file." assert lind_test_server.read_syscall(fd, 10) == '',\ "Read failed from /dev/null file." lind_test_server.close_syscall(fd) # let's create /dev/random... lind_test_server.mknod_syscall('/random', S_IFCHR, (1, 8)) fd = lind_test_server.open_syscall('/random', O_CREAT | O_RDWR, S_IRWXA)
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) # Okay, I have made the path now. Only one thing remains, adding the file posixfo = open(posixfn) filecontents = posixfo.read() posixfo.close() # make the new file, truncating any existing contents... lindfd = lind_test_server.open_syscall( normalizedlindfn, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, S_IRWXA) # should write all at once... datalen = lind_test_server.write_syscall(lindfd, filecontents) assert (datalen == len(filecontents)) lind_test_server.close_syscall(lindfd) # fix stat, etc. _mirror_stat_data(posixfn, normalizedlindfn) def _cp_dir_into_lind(fullfilename, rootpath='.', createmissingdirs=True): # check for the file. posixfn = os.path.join(rootpath, fullfilename)
# copy a file into the lind fs def copy_file(source, path, new_name): # try to open the native file, if unsuccessful print an error and return try: filedata = open(source, 'r') except IOError, e: print "Could not open the file. Error: %s" % e return -1 # try to open/create the native file. if unsucessful, error and return try: mode = stat.S_IMODE(os.stat(source).st_mode) #print "in copy " + source+ "..." + str(mode) + "..." + str(stat.S_IRWXU) lindfd = lind_test_server.open_syscall(path + "/" + new_name, lind_test_server.O_CREAT | lind_test_server.O_RDWR, mode) except lind_test_server.SyscallError, e: print "Could not open the local file. Error: %s" % e return -1 # try to write the file content to the newly file try: lind_test_server.write_syscall(lindfd, filedata.read()) except lind_test_server.SyscallError, e: print "Could not write file. Error: %s" % e return -1 lind_test_server.close_syscall(lindfd)
of a client. """ import lind_test_server import emultimer import emulmisc from lind_net_constants import * from lind_fs_constants import * SyscallError = lind_test_server.SyscallError #Try read/write of a file and see if it works. lind_test_server._blank_fs_init() #Create a file, to read/write using select. filefd = lind_test_server.open_syscall('/foo.txt', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) #Create 3 socket, one for server and two client sockets. serversockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0) clientsockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0) client2sockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0) #Bind and listen the socket to a particular address. lind_test_server.bind_syscall(serversockfd, '127.0.0.1', 50300) lind_test_server.listen_syscall(serversockfd, 4) #Register socket that needs to be polled, by default we provide server #socket and file descriptor, new client sockets will be added/removed #as and when server requires. server_poll = {'fd': serversockfd, 'events': lind_test_server.POLLIN,
import lind_test_server from lind_fs_constants import * # Try to do dup2 lind_test_server._blank_fs_init() flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = 'double_open_file' myfd = lind_test_server.open_syscall(name, flags, mode) assert(lind_test_server.write_syscall(myfd,'hi')==2) # duplicate the file descriptor... myfd2 = lind_test_server.dup2_syscall(myfd,myfd+1) # this should actually be essentially a no-op. It closes myfd+1 and sets it # to be the same as the current fd (which was done by the prior call) myfd2 = lind_test_server.dup2_syscall(myfd,myfd+1) # should be at the same place... assert(lind_test_server.lseek_syscall(myfd,0,SEEK_CUR) == lind_test_server.lseek_syscall(myfd2,0,SEEK_CUR)) # write some data to move the first position assert(lind_test_server.write_syscall(myfd,'yo')==2) # _still_ should be at the same place... assert(lind_test_server.lseek_syscall(myfd,0,SEEK_CUR) ==
import lind_test_server from lind_fs_constants import * # Try read / write of a file and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall("/foo", O_CREAT | O_EXCL | O_RDWR, S_IRWXA) myfd2 = lind_test_server.open_syscall("/foo", O_RDWR, S_IRWXA) assert myfd != myfd2 flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = "double_open_file" # print "CM: failing double open here:" myfd3 = lind_test_server.open_syscall(name, flags, mode) assert lind_test_server.write_syscall(myfd3, "hi") == 2 myfd4 = lind_test_server.open_syscall(name, flags, mode) # should still work assert lind_test_server.write_syscall(myfd3, "boo") == 3 # reading data should get \0\0boo assert lind_test_server.read_syscall(myfd4, 10) == "\0\0boo"
Test that a file system can be saved then re-opened. This program re-opens the filesystem and checks the file is there. See ut_lind_fs_persistance_setup.py for filesystem init """ import lind_test_server import os import sys from lind_fs_constants import * from ut_lind_fs_persistance_setup import TEST_FILENAME SyscallError = lind_test_server.SyscallError if not os.access(DEFAULT_METADATA_FILENAME, os.W_OK): print "Must run ut_lind_fs_persistance_setup.py first!!!" sys.exit(1) lind_test_server.restore_metadata(DEFAULT_METADATA_FILENAME) # Everything is okay, so now make a file try: myfd = lind_test_server.open_syscall(TEST_FILENAME, \ O_CREAT | O_EXCL | O_RDWR, S_IRWXA) except SyscallError: pass else: assert False, "This file should exist!"
import lind_test_server from emultimer import sleep from lind_net_constants import * from lind_fs_constants import * lind_test_server._blank_fs_init() SyscallError = lind_test_server.SyscallError #create a file and socket descriptor... sockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0) filefd = lind_test_server.open_syscall("/tmp", O_CREAT | O_EXCL, S_IRWXA) #Set FD_CLOEXEC flag... assert lind_test_server.fcntl_syscall(sockfd, F_SETFD, FD_CLOEXEC) == 0,\ "F_SETFD failed..." #Checking if the FD_CLOEXEC is set or not... assert lind_test_server.fcntl_syscall(sockfd, F_GETFD) == 1, "F_GETFD failed." #Reset FD_CLOEXEC flag... #assert lind_test_server.fcntl_syscall(sockfd, F_SETFD, 0) == 0, "F_SETFD failed." #Set some extra flags on file descriptor... assert lind_test_server.fcntl_syscall(filefd, F_SETFL, O_RDONLY|O_NONBLOCK) == 0,\ "F_SETFL failed." #check if the flags are updated or not... assert lind_test_server.fcntl_syscall(filefd, F_GETFL) == 2048, "F_GETFL failed." lind_test_server.close_syscall(sockfd)
import lind_test_server from lind_fs_constants import * # Try read / write of a file and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall('/foo',O_CREAT | O_EXCL | O_RDWR,S_IRWXA) myfd2 = lind_test_server.open_syscall('/foo',O_RDWR,S_IRWXA) assert(myfd!= myfd2) flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = 'double_open_file' #print "CM: failing double open here:" myfd3 = lind_test_server.open_syscall(name, flags, mode) assert(lind_test_server.write_syscall(myfd3,'hi')==2) myfd4 = lind_test_server.open_syscall(name, flags, mode) # should still work assert(lind_test_server.write_syscall(myfd3,'boo')==3) # reading data should get \0\0boo assert(lind_test_server.read_syscall(myfd4,10)=='\0\0boo')
import lind_test_server from lind_fs_constants import * # Let's add a few files, etc. to the system and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_WRONLY, S_IRWXA) stat_result = lind_test_server.stat_syscall('/foo') # ensure the mode is a regular file with all bits on. assert (stat_result[2] == S_IRWXA | S_IFREG) # create a file with no perms... myfd2 = lind_test_server.open_syscall('/foo2', O_CREAT | O_EXCL | O_WRONLY, 0) stat_result2 = lind_test_server.stat_syscall('/foo2') # ensure the mode is a regular file with all bits off. assert (stat_result2[2] == S_IFREG) stat_result = lind_test_server.stat_syscall('.')
import lind_test_server from lind_fs_constants import * # Try to do dup2 lind_test_server._blank_fs_init() flags = O_TRUNC | O_CREAT | O_RDWR mode = 438 # 0666 name = 'double_open_file' myfd = lind_test_server.open_syscall(name, flags, mode) assert (lind_test_server.write_syscall(myfd, 'hi') == 2) # duplicate the file descriptor... myfd2 = lind_test_server.dup2_syscall(myfd, myfd + 1) # this should actually be essentially a no-op. It closes myfd+1 and sets it # to be the same as the current fd (which was done by the prior call) myfd2 = lind_test_server.dup2_syscall(myfd, myfd + 1) # should be at the same place... assert (lind_test_server.lseek_syscall( myfd, 0, SEEK_CUR) == lind_test_server.lseek_syscall(myfd2, 0, SEEK_CUR)) # write some data to move the first position assert (lind_test_server.write_syscall(myfd, 'yo') == 2) # _still_ should be at the same place... assert (lind_test_server.lseek_syscall( myfd, 0, SEEK_CUR) == lind_test_server.lseek_syscall(myfd2, 0, SEEK_CUR))
import lind_test_server from lind_fs_constants import * # Try read / write of a file and see if it works... lind_test_server._blank_fs_init() myfd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # write should succeed assert (lind_test_server.write_syscall(myfd, 'hello there!') == 12) # seek to the beginning... assert (lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0) # read the first 5 bytes (hello) assert (lind_test_server.read_syscall(myfd, 5) == 'hello') # change it to hello world! assert (lind_test_server.write_syscall(myfd, ' world') == 6) # seek to the beginning again... assert (lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0) # and read it all... assert (lind_test_server.read_syscall(myfd, 100) == 'hello world!') lind_test_server.close_syscall(myfd)
if host_time<=lind_time: print "lind file is newer than host file, skipping" return if lind_exists and not lind_isfile: print fullfilename+" on lind file system is not a regular file, skipping" return samefile = True if host_size == lind_size: fd = open(posixfn, "rb") host_content = fd.read() fd.close() lindfd = lind_test_server.open_syscall(fullfilename, O_RDONLY, 0) lind_content = lind_test_server.read_syscall(lindfd, lind_size) lind_test_server.close_syscall(lindfd) samefile = (host_content == lind_content) else: samefile = False if not samefile: if lind_exists: print "removing "+fullfilename lind_test_server.unlink_syscall(fullfilename) cp_into_lind(fullfilename, rootpath, True) else: print "same file, skipping"
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)
import lind_test_server from lind_fs_constants import * # Let's add a few files, etc. to the system and see if it works... lind_test_server._blank_fs_init() filefd = lind_test_server.open_syscall('/foo',O_CREAT | O_EXCL | O_WRONLY,\ S_IRWXA) assert lind_test_server.stat_syscall('/foo')[2] == S_IRWXA | S_IFREG, \ "Failed to have full access to all users." lind_test_server.chmod_syscall('/foo', S_IRUSR | S_IRGRP) assert lind_test_server.stat_syscall('/foo')[2] == S_IRUSR | S_IRGRP | S_IFREG\ , "Failed to set Read access to user and group." lind_test_server.chmod_syscall('/foo', S_IRWXA) assert lind_test_server.stat_syscall('/foo')[2] == S_IRWXA | S_IFREG, \ "Failed to set back full access to all users." lind_test_server.close_syscall(filefd)