def open(self, path, flags): full_path = self._full_path(path) print "sending open req for file: ", full_path # check server mod time flag = 0 reply = zfs_pb2.TestAuthReply() if os.path.isfile(full_path): print "file exists:", full_path mtime = os.lstat(full_path).st_mtime reply = self.stub.TestAuth( zfs_pb2.TestAuthRequest(path=full_path, st_mtime=mtime), 10) else: print "file doesn't exist:", full_path files = self.stub.FetchDir( zfs_pb2.FilePath(path=os.getcwd(), mode=0), 10) strSplit = full_path.split("/")[-1] print "file name: ", strSplit isFound = False for f in files: print "file from server:", f.dir_list_block if f is strSplit: isFound = True if not isFound: return os.open(full_path, os.O_WRONLY | os.O_CREAT, 0777) else: flag = 1 print "reply", reply.flag if reply.flag == 1 or flag == 1: print "File modified on server, fetching it again" rand = random.randint(10000000, 99999999) tmpFileName = self.root + "/tmp/" + str(rand) fd = open(tmpFileName, 'w') data_blocks = self.stub.Fetch( zfs_pb2.FilePath(path=full_path, mode=0), 10) actlen = 0 count = 0 for block in data_blocks: print block.data_block if count == 0: count += 1 actlen = block.data_block else: fd.write(block.data_block) fd.close() tmplen = os.stat(tmpFileName).st_size print "tmplen:", tmplen, "actual len:", actlen if not tmplen < actlen: os.rename(tmpFileName, full_path) return os.open(full_path, flags)
def readdir(self, path, fh): full_path = self._full_path(path) print "sending readdir req" files = self.stub.FetchDir(zfs_pb2.FilePath(path=full_path, mode=0), 10) for f in files: yield f.dir_list_block
def readdir(self, path, fh): full_path = self._full_path(path) print "sending readdir req" dirBlocks = self.stub.FetchDir( zfs_pb2.FilePath(path=full_path, mode=0), 10) print "got response for readdir req" for f in dirBlocks: yield f.dir_list_block
def unlink(self, path): full_path = self._full_path(path) print "sending unlink req for file:", full_path # TODO : os.unlink(full_path) self.stub.RemoveFile(zfs_pb2.FilePath(path=full_path, mode=0), 10)
def mkdir(self, path, mode): # print path, " : Hi" full_path = self._full_path(path) print "sending mkdir req for: ", full_path os.mkdir(full_path, mode) self.stub.MakeDir(zfs_pb2.FilePath(path=full_path, mode=mode), 10)
def rmdir(self, path): full_path = self._full_path(path) print "sending rmdir req for:", full_path os.rmdir(full_path) self.stub.RemoveDir(zfs_pb2.FilePath(path=full_path, mode=0), 10)
def rename(self, old, new): print "sending rename req" return self.stub.Rename( zfs_pb2.FilePath(old=self._full_path(old), new=self._full_path(new)), 10)
def open(self, path, flags): full_path = self._full_path(path) print full_path, " : checking whether file is available locally ..." isFileAvailLocal = os.path.exists(full_path) # check server mod time taReply = zfs_pb2.TestAuthReply() print "Test Auth: File ", full_path, " Reply: ", taReply if isFileAvailLocal: print "file exists in local folder. Checking size of file next... " mstat = os.lstat(full_path) msize = getattr(mstat, 'st_size') print "File STAT: ", mstat print "File Size: ", msize if msize > 0: print "The local file is not empty. We will use the modified time of this file for TestAuth." mtime = os.lstat(full_path).st_mtime else: print "The local file is empty. We will need to get the file from the server in this case." mtime = 0 else: mtime = 0 print "Sending TestAuth request on: ", full_path taReply = self.stub.TestAuth( zfs_pb2.TestAuthRequest(path=full_path, st_mtime=mtime), 10) print "Got TestAuth reply: ", taReply if (taReply.flag == 9): print "Reply = 9, file does not exist on server" if (isFileAvailLocal): print "Case where file exists locally but not on server. Should this even occur?" return os.open(full_path, flags) else: print "Case where file is not available on server or locally" return os.open(full_path, os.O_WRONLY | os.O_CREAT, 0777) elif (taReply.flag == 0): print "Reply = 0, file exists but has not been modified. User can continue with local version" return os.open(full_path, flags) elif (taReply.flag == 1): print "Reply = 1, file exists and has been modified" checkSum = 0 checkSumTest = False while not (checkSumTest): rand = random.randint(1000000000, 9999999999) tmpFileName = self.root + "/tmp/" + str(rand) fd = open(tmpFileName, 'w') countBlocks = 0 data_blocks = self.stub.Fetch( zfs_pb2.FilePath(path=full_path, mode=0), 10) for block in data_blocks: # print block.data_block if countBlocks == 0: countBlocks += 1 checkSum = int(block.data_block) else: fd.write(block.data_block) fd.close() tmpFileLen = os.stat(tmpFileName).st_size print "Tmp File Len", tmpFileLen, " Server File Len:", checkSum if (tmpFileLen < checkSum): print "Checksum error in doing server file read. Will try again." else: checkSumTest = True print "Checksum is fine. Got authenticated version from server." os.rename(tmpFileName, full_path) return os.open(full_path, flags) else: print "Reply ", taReply.flag, " is not -1, 0 or 1. Should never see this state." return None