def FileReceived(self, packet, state): if state in ['in queue', 'shutdown', 'exist']: return if state != 'received': dhnio.Dprint(4, "backup_rebuilder.FileReceived WARNING incorrect state [%s] for packet %s" % (str(state), str(packet))) return packetID = packet.PacketID filename = os.path.join(settings.getLocalBackupsDir(), packetID) if not packet.Valid(): # TODO # if we didn't get a valid packet ... re-request it or delete it? dhnio.Dprint(2, "backup_rebuilder.FileReceived WARNING " + packetID + " is not a valid packet") return if os.path.exists(filename): dhnio.Dprint(4, "backup_rebuilder.FileReceived WARNING rewriting existed file" + filename) try: os.remove(filename) except: dhnio.DprintException() dirname = os.path.dirname(filename) if not os.path.exists(dirname): try: dhnio._dirs_make(dirname) except: dhnio.Dprint(2, "backup_rebuilder.FileReceived ERROR can not create sub dir " + dirname) return if not dhnio.WriteFile(filename, packet.Payload): return backup_matrix.LocalFileReport(packetID) self.automat('inbox-data-packet', packetID)
def doSavePacket(self, NewPacket): packetID = NewPacket.PacketID pathID, version, packetBlockNum, SupplierNumber, dataORparity = packetid.SplitFull(packetID) if dataORparity == 'Data': self.OnHandData[SupplierNumber] = True elif NewPacket.DataOrParity() == 'Parity': self.OnHandParity[SupplierNumber] = True filename = os.path.join(settings.getLocalBackupsDir(), packetID) dirpath = os.path.dirname(filename) if not os.path.exists(dirpath): try: dhnio._dirs_make(dirpath) except: dhnio.DprintException() # either way the payload of packet is saved if not dhnio.WriteFile(filename, NewPacket.Payload): dhnio.Dprint(6, "restore.doSavePacket WARNING unable to write to %s" % filename) return dhnio.Dprint(6, "restore.doSavePacket %s saved" % packetID) if self.packetInCallback is not None: self.packetInCallback(self.BackupID, NewPacket)
def Data(request): # 1. this is our Data! if request.OwnerID == misc.getLocalID(): if request.PacketID in [ settings.BackupIndexFileName(), ]: backup_control.IncomingSupplierBackupIndex(request) # elif request.PacketID in [ settings.BackupInfoFileName(), settings.BackupInfoFileNameOld(), settings.BackupInfoEncryptedFileName(), ]: # return return # 2. this Data is not belong to us if not contacts.IsCustomer(request.OwnerID): # SECURITY # may be we did not get the ListCustomers packet from the Central yet? dhnio.Dprint(6, "p2p_service.Data WARNING %s not a customer, packetID=%s" % (request.OwnerID, request.PacketID)) central_service.SendRequestCustomers() return filename = makeFilename(request.OwnerID, request.PacketID) if filename == "": transport_control.SendFail(request, 'empty filename, you are not a customer maybe?') dhnio.Dprint(6,"p2p_service.Data WARNING got empty filename, bad customer or wrong packetID? ") return dirname = os.path.dirname(filename) if not os.path.exists(dirname): try: dhnio._dirs_make(dirname) except: dhnio.Dprint(2, "p2p_service.Data ERROR can not create sub dir " + dirname) transport_control.SendFail(request, 'write error') return data = request.Serialize() if not dhnio.WriteFile(filename, data): dhnio.Dprint(2, "p2p_service.Data ERROR can not write to " + str(filename)) transport_control.SendFail(request, 'write error') return transport_control.SendAck(request, str(len(request.Payload))) reactor.callLater(3, local_tester.TestSpaceTime) del data dhnio.Dprint(8, "p2p_service.Data saved from [%s/%s], packetID is %s" % ( nameurl.GetName(request.OwnerID), nameurl.GetName(request.CreatorID), request.PacketID,))