def Unlock(self, request, context): filename = request.filename uid = request.userid self.locked_files = self.__ReadData('lock.json') if self.checkIfLocked(filename) and self.locked_files[filename] == uid: self.locked_files.pop(filename) self.__WriteData('lock.json', self.locked_files) return data_pb2.Response(code=0, message='Unlocked.') return data_pb2.Response(code=1, message='Permission denied.')
def Lock(self, request, context): # file_path = _DIR + request.filename filename = request.filename uid = request.userid self.locked_files = self.__ReadData('lock.json') if self.checkIfLocked(filename) and self.locked_files[filename] != uid: return data_pb2.Response(code=1, message='File was locked by others.') else: self.locked_files[filename] = uid self.__WriteData('lock.json', self.locked_files) return data_pb2.Response(code=0, message='Locked.')
def DeleteFile(self, request, context): filename = request.filename file_path = _DIR + filename file = Path(file_path) if not file.is_file(): return data_pb2.Response(code=1, message='File not exists.') else: file.unlink() for no, response in enumerate( self.tracker_stub.GetServers( data_pb2.Filename(filename=filename))): address = response.address if no == 0 and address != self.address: break if no == 0 and address == self.address: continue self.AddUncomplete('delete', filename, address) self.TryComplete() return data_pb2.Response(code=0, message='Success.')
def CreateFile(self, request, context): self.TryComplete() filename = request.filename file_path = _DIR + filename # file = Path(file_path) f = open(file_path, 'w') f.close() for no, response in enumerate( self.tracker_stub.GetServers( data_pb2.Filename(filename=filename))): address = response.address if no == 0 and address != self.address: break if no == 0 and address == self.address: continue self.AddUncomplete('create', filename, address) self.TryComplete() return data_pb2.Response(code=0, message='Success.')
def Upload(self, request_iterator, context): self.TryComplete() for no, request in enumerate(request_iterator): if no == 0: filename = request.filename file_path = _DIR + filename file = open(file_path, 'wb') else: file.write(request.buffer) file.close() for no, response in enumerate( self.tracker_stub.GetServers( data_pb2.Filename(filename=filename))): address = response.address if no == 0 and address != self.address: break if no == 0 and address == self.address: continue self.AddUncomplete('update', filename, address) self.TryComplete() return data_pb2.Response(code=0, message='ok')
def DeleteFile(self, request, context): data = self.__ReadData() data.pop(request.filename) self.__WriteData(data) return data_pb2.Response(code=0, message='Done.')