def write(self, buf, offset): log("write", self.fd, buf, offset) try: lind.lseek_syscall(self.fd, offset, 0) ret = lind.write_syscall(self.fd, buf) except lind.SyscallError, e: ret = -errno[e[1]]
def process_request(): """ <Purpose> Server thread for processing connections using select ... """ while True: #Pass list of Inputs, Outputs for select which returns if any activity #occurs on any socket. ret = lind_test_server.select_syscall(11, inputs, outputs, excepts, 5) #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) inputs.append(newsockfd[2]) #Write to a file... elif sock is filefd: emultimer.sleep(1) assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) inputs.remove(sock) #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #We make the ouput ready, so that it sends out data... if sock not in outputs: outputs.append(sock) else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from readfd's. lind_test_server.close_syscall(sock) inputs.remove(sock) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from monitoring. outputs.remove(sock) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out this socket, it's no longer ready for writing #remove this socket from writefd's. outputs.remove(sock) lind_test_server.close_syscall(serversockfd)
# 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)) inode = lind_test_server.fstat_syscall(lindfd)[1] lind_test_server.close_syscall(lindfd) print "Copied "+posixfn+" as "+fullfilename+"("+str(inode)+")" # fix stat, etc. _mirror_stat_data(posixfn,normalizedlindfn) def deltree_lind(dirname): # took part of the code from _find_all_paths_recursively method below.
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 * # 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
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)
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"
from lind_net_constants import * import lind_test_server SyscallError = lind_test_server.SyscallError # 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) lind_test_server.close_syscall(myfd) # Now, re open the file myfd = lind_test_server.open_syscall('/foo', O_RDWR, S_IRWXA) lind_test_server.close_syscall(myfd) # let's do a few basic things with connect. This will be UDP only for now... sockfd = lind_test_server.socket_syscall(AF_INET, SOCK_STREAM, 0)
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) # pass in a path, check if it exists in the virtual fs. if it doesnt, it creates the path 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:
import os from lind_fs_constants import * TEST_FILENAME = "/someSimpleFileName" TEST2_FILENAME = "/otherSimpleFileName2" if __name__ == "__main__": lind_test_server._blank_fs_init() # Everything is okay, so now make a file myfd = lind_test_server.open_syscall(TEST_FILENAME, 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!"
import lind_test_server from lind_fs_constants import * lind_test_server._blank_fs_init() fd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # Make a big file msg = "Hello" * 100 size = len(msg) # how small will we make the file TRUNCATE_SIZE = 10 NEW_TRUNCATE_SIZE = 200 lind_test_server.write_syscall(fd, msg) lind_test_server.ftruncate_syscall(fd, TRUNCATE_SIZE) # seek to the beginning... assert (lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0) data = lind_test_server.read_syscall(fd, 100) assert len(data) == TRUNCATE_SIZE, "File must be smaller after truncate" assert data == msg[:TRUNCATE_SIZE], "New files contents must match: \n%s\n%s" \ % (data, msg[:TRUNCATE_SIZE]) lind_test_server.ftruncate_syscall(fd, NEW_TRUNCATE_SIZE)
def process_request(): """ <Purpose> Server thread for processing connections using poll ... """ while True: #Pass list of sockets that needs for polling which returns if any activity #occurs on any socket. poll_vals = lind_test_server.poll_syscall(polled, 5) ret = [poll_vals[0],[],[],[]] ret[1] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLIN] ret[2] = [x['fd'] for x in poll_vals[1] if x['revents'] & lind_test_server.POLLOUT] #Check for any activity in any of the Input sockets... for sock in ret[1]: #If the socket returned was listerner socket, then there's a new conn. #so we accept it, and put the client socket in the list of Inputs. if sock is serversockfd: newsockfd = lind_test_server.accept_syscall(sock) new_poll = {'fd':newsockfd[2], 'events':lind_test_server.POLLIN, 'revents':0} polled.append(new_poll) #Write to a file... elif sock is filefd: assert lind_test_server.write_syscall(sock, 'test') == 4, \ "Failed to write into a file..." lind_test_server.lseek_syscall(sock, 0, SEEK_SET) #Once the write is successful into a file, Modify the file descriptor #so that its ready for reading out of the file. [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLOUT #If the socket is in established conn., then we recv the data, if #there's no data, then close the client socket. else: data = lind_test_server.recv_syscall(sock, 100, 0) if data: assert data == "test", "Recv failed in select..." #This socket is ready for writing, modify the socket descriptor #to be in read-write mode. This socket can write data out to network [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLIN | lind_test_server.POLLOUT else: #No data means remote socket closed, hence close the client socket #in server, also remove this socket from polling. lind_test_server.close_syscall(sock) to_go = [x for x in polled if x['fd'] == sock] map(polled.remove, to_go) #Check for any activity in any of the output sockets... for sock in ret[2]: if sock is filefd: assert lind_test_server.read_syscall(sock, 4) == "test", \ "Failed to read from a file..." #test for file finished, remove from polling. to_go = [x for x in polled if x['fd'] == sock] map(polled.remove, to_go) else: lind_test_server.send_syscall(sock, data, 0) #Data is sent out of this socket, it's no longer ready for writing, #modify it only read mode. [x for x in polled if x['fd'] == sock][0]['events'] = \ lind_test_server.POLLIN lind_test_server.close_syscall(serversockfd)
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 * 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) 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, 8),\ "File is not /dev/random."
import lind_test_server from lind_fs_constants import * lind_test_server._blank_fs_init() fd = lind_test_server.open_syscall('/foo', O_CREAT | O_EXCL | O_RDWR, S_IRWXA) # Make a big file msg = "Hello" * 100 size = len(msg) # how small will we make the file TRUNCATE_SIZE = 10 NEW_TRUNCATE_SIZE = 200 lind_test_server.write_syscall(fd, msg) lind_test_server.ftruncate_syscall(fd, TRUNCATE_SIZE) # seek to the beginning... assert(lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0) data = lind_test_server.read_syscall(fd, 100) assert len(data) == TRUNCATE_SIZE, "File must be smaller after truncate" assert data == msg[:TRUNCATE_SIZE], "New files contents must match: \n%s\n%s" \ % (data, msg[:TRUNCATE_SIZE]) lind_test_server.ftruncate_syscall(fd, NEW_TRUNCATE_SIZE)
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
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')
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) if not os.path.exists(posixfn): raise IOError("Cannot locate dire on POSIX FS: '" + posixfn + "'")
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') == 5) # seek past the end assert (lind_test_server.lseek_syscall(myfd, 10, SEEK_SET) == 10) # write should succeed again (past the end) assert (lind_test_server.write_syscall(myfd, 'yoyoyo') == 6) # seek to the start assert (lind_test_server.lseek_syscall(myfd, 0, SEEK_SET) == 0) # read 20 bytes but get 16 (hello\0\0\0\0\0yoyoyo) assert (lind_test_server.read_syscall(myfd, 20) == 'hello\0\0\0\0\0yoyoyo') lind_test_server.close_syscall(myfd)
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
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') == 5) # seek past the end assert(lind_test_server.lseek_syscall(myfd,10,SEEK_SET) == 10) # write should succeed again (past the end) assert(lind_test_server.write_syscall(myfd,'yoyoyo') == 6) # seek to the start assert(lind_test_server.lseek_syscall(myfd,0,SEEK_SET) == 0) # read 20 bytes but get 16 (hello\0\0\0\0\0yoyoyo) assert(lind_test_server.read_syscall(myfd,20)=='hello\0\0\0\0\0yoyoyo') lind_test_server.close_syscall(myfd)