def Store(self, request_iterator, context): print "store req received" with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp_filename = tmp.name act_filename = None actlen = 0 count = 0 for chunk in request_iterator: if count == 0: print "Not read, opening file: ", chunk.data_block count += 1 act_filename = chunk.data_block if count == 1: print "Not read, opening file: ", chunk.data_block count += 1 actlen = chunk.data_block else: tmp.write(chunk.data_block) tmp.flush() os.fsync(tmp.fileno()) tmplen = os.stat(tmp_filename).st_size print "tmplen:", tmplen, "actual len:", actlen if tmplen < actlen: return zfs_pb2.StdReply(status=0, error_message='not stored') os.rename(tmp_filename, act_filename) return zfs_pb2.StdReply(status=1, error_message='')
def RemoveFile(self, request, context): print "unlink req received" os.unlink(request.path) return zfs_pb2.StdReply(status=1, error_message='')
def MakeDir(self, request, context): print "Mk dir req received" print "Req path: " + request.path os.mkdir(request.path, request.mode) return zfs_pb2.StdReply(status=1, error_message='')
def RemoveDir(self, request, context): print "Rm dir req received" print "Req path: " + request.path os.rmdir(request.path) return zfs_pb2.StdReply(status=1, error_message='')
def CreateFile(self, request, context): print "create req received" print "Req path: " + request.path ret = os.open(request.path, os.O_WRONLY | os.O_CREAT, request.mode) print "Status: ", ret return zfs_pb2.StdReply(error_message='Returned, %s!' % ret, status=0)
def Rename(self, request, context): os.rename(request.old, request.new) return zfs_pb2.StdReply(status=1, error_message='')
class ZfsServer(zfs_pb2.BetaZfsRpcServicer): def _full_path(self, partial): if partial.startswith("/"): partial = partial[1:] path = os.path.join(self.root, partial) return path def CreateFile(self, request, context): print "create req received" print "Req path: " + request.path ret = os.open(request.path, os.O_WRONLY | os.O_CREAT) return zfs_pb2.StdReply(error_message='Returned, %s!' % ret, status=0) def GetFileStat(self, request, context): print "Get attr req received" print "Req path: " + request.path st = os.lstat(request.path) return zfs_pb2.FileStat(st_atime=getattr(st, 'st_atime'), st_ctime=getattr(st, 'st_ctime'), st_gid=getattr(st, 'st_gid'), st_mode=getattr(st, 'st_mode'), st_mtime=getattr(st, 'st_mtime'), st_nlink=getattr(st, 'st_nlink'), st_size=getattr(st, 'st_size'), st_uid=getattr(st, 'st_uid'), st_ino=getattr(st, 'st_ino'), st_dev=getattr(st, 'st_dev')) def RemoveDir(self, request, context): print "Rm dir req received" print "Req path: " + request.path os.rmdir(request.path) return zfs_pb2.StdReply(status=1, error_message='') # print "Status: " + retdef release(self, path, fh): # print "sending release req"# # return os.close(fh) # return zfs_pb2.IntRet(message='Returned, %s!' % ret) def MakeDir(self, request, context): print "Mk dir req received" print "Req path: " + request.path os.mkdir(request.path, request.mode) return zfs_pb2.StdReply(status=1, error_message='') # print "Status: " + ret # return zfs_pb2.IntRet(message='Returned, %s!' % ret) def RemoveFile(self, request, context): print "unlink req received" os.unlink(request.path) return zfs_pb2.StdReply(status=1, error_message='') def Fetch(self, request, context): print "read file req recvd for file: ", request.path try: yield zfs_pb2.FileDataBlock(data_block=str(os.stat(request.path).st_size)) fd = open(request.path, 'r+') with fd as reader: for chunk in iter(partial(reader.read, BLOCK_SIZE), ''): # print "read block", chunk yield zfs_pb2.FileDataBlock(data_block=chunk) except (OSError, ValueError, IOError): print "error", traceback.print_exc() def read(self, path, length, offset, fh): return os.lsos.read(fh, length) def Store(self, request_iterator, context): print "store req received" rand = random.randint(1000000000, 9999999999) tmpFileName = "/users/vvaidhy/tmp/" + str(rand) print "tmpFileName: ", tmpFileName try: fd = open(tmpFileName, 'w') except Exception, err: print 'print_exc():', err traceback.print_exc(file=sys.stdout) count = 0 print "Entering request iterator" for chunk in request_iterator: print "Doing something for each request iteration" if count == 0: names = chunk.data_block.split(":") act_filename = names[0] actlen = int(names[1]) print "Actual File for Write: ", act_filename print "setting actlen to: ", actlen count += 1 else: print "Block: ", chunk.data_block # fd.write(chunk.data_block) # fd.flush() # fd.close() # tmplen = os.stat(tmpFileName).st_size tmplen = 0 print "tmplen:", tmplen, "actual len:", actlen if tmplen < actlen: return zfs_pb2.StdReply(status=0, error_message='not stored') print "tmp file name:", tmpFileName, "renaming to:", act_filename os.rename(tmpFileName, act_filename) return zfs_pb2.StdReply(status=1, error_message='')