def read(self, length, offset): log("read", self.fd, length, offset) try: lind.lseek_syscall(self.fd, offset, 0) ret = lind.read_syscall(self.fd, length) 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)
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}
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_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." assert lind_test_server.write_syscall(fd, "test") == 4,\ "Write failed to /dev/random file."
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)
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"
# 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) # seek to the beginning... assert(lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0) data2 = lind_test_server.read_syscall(fd, NEW_TRUNCATE_SIZE) assert data2 == (msg[:TRUNCATE_SIZE] + '\x00' * \
# 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)) # reset the position within the file lind_test_server.lseek_syscall(myfd2,0,SEEK_SET) # read from the other fd assert(lind_test_server.read_syscall(myfd,10)=='hiyo') # close one fd lind_test_server.close_syscall(myfd) # the other should still work... assert(lind_test_server.write_syscall(myfd2,'raar')==4) lind_test_server.lseek_syscall(myfd2,0,SEEK_SET) assert(lind_test_server.read_syscall(myfd2,10)=='hiyoraar') lind_test_server.close_syscall(myfd2)
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 try: newfd = open(dest, 'w') except IOError, e: print "Couldnt open native file. Error: %s" % e return -1 try: newfd.write(lind_test_server.read_syscall(lindfd, 1000000)) except IOError, e: print "Failed to write to disk. Error: %s" % e return -1 lind_test_server.close_syscall(lindfd) #create a new directory 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)
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!" lind_test_server.close_syscall(myfd) # Now make and remove a file: myfd = lind_test_server.open_syscall(TEST2_FILENAME, 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) # seek to the beginning... assert (lind_test_server.lseek_syscall(fd, 0, SEEK_SET) == 0) data2 = lind_test_server.read_syscall(fd, NEW_TRUNCATE_SIZE) assert data2 == (msg[:TRUNCATE_SIZE] + '\x00' * \ (NEW_TRUNCATE_SIZE - TRUNCATE_SIZE)), \
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)
# 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 wrfd = lind_test_server.open_syscall('/hello', O_WRONLY, S_IRWXA) try: # Can I read a write only file? lind_test_server.read_syscall(wrfd, 50) except lind_test_server.SyscallError: # should be an error pass else: print "Error! Should have been blocked from reading with O_WRONLY"
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)