def getxattr(self, path, name, position=0): if path not in self.files: print("GETXATTR in other location") packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for GETXATTR', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for GETXATTR', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_GETXATTR) packet['path'] = path packet['name'] = name print('Sending OP_GETXATTR', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_GETXATTR_R', reply) if reply['getxattr']: return reply['getxattr'] else: attrs = self.files[path].get('attrs', {}) try: return attrs[name] except KeyError: return '' # Should return ENOATTR
def rename(self, old, new): print("In Rename") brename = packets.new_packet(OP_RENAME) brename['old'] = old brename['new'] = new if old not in self.files: packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = old print('Sending OP_FIND for RENAME', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for RENAME', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_RENAME) packet['old'] = old packet['new'] = new print('Sending OP_RENAME', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_RENAME_R', reply) else: self.data[new] = self.data[old] del self.data[old] self.files[new] = self.files.pop(old) boot_sock = socks.tcp_sock() boot_sock.connect(self.bootstrap) boot_sock.send(packets.build(brename))
def getattr(self, path, fh=None): print("memory.py getattr called") if path not in self.files: packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for getattr', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_Rfor getattr', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_GETATTR) packet['path'] = path print('Sending OP_GETATTR', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_GETATTR_R', reply) if reply['getattr']: return reply['getattr'] else: raise FuseOSError(ENOENT) return self.files[path]
def unlink(self, path): print("In Unlink") brename = packets.new_packet(OP_DELETE) brename['path'] = path if path not in self.files: print("In other location") packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for DELETE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for DELETE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_DELETE) packet['path'] = path print('Sending OP_DELETE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_DELETE_R', reply) else: print("In self") del self.data[path] self.files.pop(path) boot_sock = socks.tcp_sock() boot_sock.connect(self.bootstrap) boot_sock.send(packets.build(brename))
def getattr(self, path, fh=None): print("memory.py getattr called") if path not in self.files: packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for getattr', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_Rfor getattr', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_GETATTR) packet['path'] = path print('Sending OP_GETATTR', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_GETATTR_R', reply) if reply['getattr']: return reply['getattr'] else: raise FuseOSError(ENOENT) return self.files[path]
def getxattr(self, path, name, position=0): if path not in self.files: print("GETXATTR in other location") packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for GETXATTR', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for GETXATTR', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_GETXATTR) packet['path'] = path packet['name'] = name print('Sending OP_GETXATTR', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_GETXATTR_R', reply) if reply['getxattr']: return reply['getxattr'] else: attrs = self.files[path].get('attrs', {}) try: return attrs[name] except KeyError: return '' # Should return ENOATTR
def truncate(self, path, length, fh=None): if path not in self.files: print("Truncate in other location") packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for TRUNCATE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for TRUNCATE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_TRUNC) packet['path'] = path packet['length'] = length print('Sending OP_TRUNCATE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_TRUNCATE_R', reply) else: # make sure extending the file fills in zero bytes self.data[path] = self.data[path][:length].ljust( length, b'\x00'.decode()) self.files[path]['st_size'] = length
def unlink(self, path): print("In Unlink") brename = packets.new_packet(OP_DELETE) brename['path'] = path if path not in self.files: print("In other location") packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for DELETE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for DELETE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_DELETE) packet['path'] = path print('Sending OP_DELETE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_DELETE_R', reply) else: print("In self") del self.data[path] self.files.pop(path) boot_sock = socks.tcp_sock() boot_sock.connect(self.bootstrap) boot_sock.send(packets.build(brename))
def truncate(self, path, length, fh=None): if path not in self.files: print("Truncate in other location") packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for TRUNCATE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for TRUNCATE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_TRUNC) packet['path'] = path packet['length'] = length print('Sending OP_TRUNCATE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_TRUNCATE_R', reply) else: # make sure extending the file fills in zero bytes self.data[path] = self.data[path][:length].ljust( length, b'\x00'.decode()) self.files[path]['st_size'] = length
def rename(self, old, new): print("In Rename") brename = packets.new_packet(OP_RENAME) brename['old'] = old brename['new'] = new if old not in self.files: packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = old print('Sending OP_FIND for RENAME', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for RENAME', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_RENAME) packet['old'] = old packet['new'] = new print('Sending OP_RENAME', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_RENAME_R', reply) else: self.data[new] = self.data[old] del self.data[old] self.files[new] = self.files.pop(old) boot_sock = socks.tcp_sock() boot_sock.connect(self.bootstrap) boot_sock.send(packets.build(brename))
def write(self, path, data, offset, fh): #THIS IS WHERE WRITE HAPPENS print("WRITE in Memory") if path not in self.files: print('remote write') packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for WRITE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for WRITE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_WRITE) packet['path'] = path packet['data'] = data.decode() packet['offset'] = offset packet['fh'] = fh print('Sending OP_WRITE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_WRITE_R', reply) if 'datalen' in reply: return reply['datalen'] else: raise FuseOSError(ENOENT) print("local write") print("current contents:", self.data[path]) print("offset", offset) print("fh", fh) self.data[path] = self.data[path][:offset].ljust( offset, b'\x00'.decode()) self.data[path] += data.decode() self.data[path] += self.data[path][offset + len(data):] # self.data[path] = ( # # make sure the data gets inserted at the right offset # self.data[path][:offset].ljust(offset, '\x00'.encode('ascii')) # + data.decode() # # and only overwrites the bytes that data is replacing # + self.data[path][offset + len(data):]) self.files[path]['st_size'] = len(self.data[path]) print("current contents:", self.data[path]) return len(data)
def write(self, path, data, offset, fh): #THIS IS WHERE WRITE HAPPENS print("WRITE in Memory") if path not in self.files: print('remote write') packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for WRITE', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for WRITE', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_WRITE) packet['path'] = path packet['data'] = data.decode() packet['offset'] = offset packet['fh'] = fh print('Sending OP_WRITE', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_WRITE_R', reply) if 'datalen' in reply: return reply['datalen'] else: raise FuseOSError(ENOENT) print("local write") print("current contents:", self.data[path]) print("offset", offset) print("fh", fh) self.data[path] = self.data[path][:offset].ljust(offset, b'\x00'.decode()) self.data[path] += data.decode() self.data[path] += self.data[path][offset + len(data):] # self.data[path] = ( # # make sure the data gets inserted at the right offset # self.data[path][:offset].ljust(offset, '\x00'.encode('ascii')) # + data.decode() # # and only overwrites the bytes that data is replacing # + self.data[path][offset + len(data):]) self.files[path]['st_size'] = len(self.data[path]) print("current contents:", self.data[path]) return len(data)
def readdir(self, path, fh): #This is where LS happens #return ['.', '..'] + [x[1:] for x in self.files if x != '/'] packet = packets.new_packet(OP_LS) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) ls = packets.unpack(sock.recv(MAX_READ)) print('ls contents', ls['ls']) return ['.', '..'] + [x[1:] for x in ls['ls']]
def leave(): global listen_sock temp_sock = socks.tcp_sock() temp_sock.connect((bootstrap_ip,args.port)) print('connected to bootstrap for exit', temp_sock) packet = packets.leave_packet(listen_sock.getsockname()) print('sending leave notice', packet) temp_sock.send(packets.build(packet)) #TODO: recv ack? temp_sock.close()
def read(self, path, size, offset, fh): print("memory.py read called") if path not in self.files: packet = packets.new_packet( OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for READ', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for READ', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_READ) packet['path'] = path packet['size'] = size packet['offset'] = offset packet['fh'] = fh print('Sending OP_READ', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack( tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_READ_R', reply) if 'read' in reply: return reply['read'].encode('utf-8') else: raise FuseOSError(ENOENT) print("offset: ", offset) print("size: ", size) print("read contents:", self.data[path]) datalen = len(self.data[path]) print("datalen", datalen) end = datalen if datalen > size: end = size return self.data[path][offset:offset + end].encode('utf-8')
def sigint_handler(signal,frame): for n in nodes: print(tuple(n)) tcp_sock = socks.tcp_sock() try: tcp_sock.connect(tuple(n)) except: continue msg = packets.new_packet(OP_LEAVE) tcp_sock.send(packets.build(msg)) tcp_sock.close() print("Goodbye~!") sys.exit(0)
def sigint_handler(signal, frame): for n in nodes: print(tuple(n)) tcp_sock = socks.tcp_sock() try: tcp_sock.connect(tuple(n)) except: continue msg = packets.new_packet(OP_LEAVE) tcp_sock.send(packets.build(msg)) tcp_sock.close() print("Goodbye~!") sys.exit(0)
def read(self, path, size, offset, fh): print("memory.py read called") if path not in self.files: packet = packets.new_packet(OP_FIND) # ask bootstrap - find location of file packet['path'] = path print('Sending OP_FIND for READ', packet) sock = socks.tcp_sock() sock.connect(self.bootstrap) sock.send(packets.build(packet)) reply = packets.unpack(sock.recv(MAX_READ)) print('Received OP_FIND_R for READ', reply) if 'loc' not in reply: raise FuseOSError(ENOENT) loc = tuple(reply['loc']) tcp_sock = socks.tcp_sock() tcp_sock.connect(loc) # connect to other node packet = packets.new_packet(OP_READ) packet['path'] = path packet['size'] = size packet['offset'] = offset packet['fh'] = fh print('Sending OP_READ', packet) tcp_sock.send(packets.build(packet)) # send syscall reply = packets.unpack(tcp_sock.recv(MAX_READ)) # TODO: listen for reply in select print('Received OP_READ_R', reply) if 'read' in reply: return reply['read'].encode('utf-8') else: raise FuseOSError(ENOENT) print("offset: ", offset) print("size: ", size) print("read contents:",self.data[path]) datalen = len(self.data[path]) print("datalen", datalen) end = datalen if datalen > size: end = size return self.data[path][offset:offset + end].encode('utf-8')
def join(): global listen_sock listen_sock = socks.listen_sock() listen_sock.setblocking(False) temp_sock = socks.tcp_sock() # only a temp sock is required to join network try: temp_sock.connect((bootstrap_ip, args.port)) except: print("Could not connect to " + str(bootstrap_ip) + " on port " + str(args.port)) sys.exit(0) print('connected to bootstrap', temp_sock) packet = packets.join_packet((socket.gethostbyname(socket.gethostname()),listen_sock.getsockname()[1])) print('Sending join request', packet) temp_sock.send(packets.build(packet)) #TODO: recv ack? temp_sock.close()
def create(self, path, mode): self.files[path] = dict(st_mode=(S_IFREG | mode), st_nlink=1, st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) self.data[path] = "" packet = packets.new_packet(OP_CREATE) packet['path'] = path packet['loc'] = self.node sock = socks.tcp_sock() sock.connect(self.bootstrap) print('Connected to bootstrap', sock) print('Sending packet', packet) print('Built packet', packets.build(packet)) sock.send(packets.build(packet)) # send new file to bootstrap sock.close() # should we wait for ack here? self.fd += 1 return self.fd
def create(self, path, mode): self.files[path] = dict( st_mode=(S_IFREG | mode), st_nlink=1, st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) self.data[path] = "" packet = packets.new_packet(OP_CREATE) packet['path'] = path packet['loc'] = self.node sock = socks.tcp_sock() sock.connect(self.bootstrap) print('Connected to bootstrap', sock) print('Sending packet', packet) print('Built packet', packets.build(packet)) sock.send(packets.build(packet)) # send new file to bootstrap sock.close() # should we wait for ack here? self.fd += 1 return self.fd