# HG changeset patch # User Edouard Tisserant <edouard@beremiz.fr> # Date 1736412403 -3600 # Node ID 803d4e64cc683f8d00109465aa27434b96547e32 # Parent 4ce63b8647d70a5cf9dc22aa54b3a436b88a29a8 python runtime: fix exception when transfering files with identical content. diff -r 4ce63b8647d7 -r 803d4e64cc68 runtime/PLCObject.py --- a/runtime/PLCObject.py Thu Jan 09 09:45:21 2025 +0100 +++ b/runtime/PLCObject.py Thu Jan 09 09:46:43 2025 +0100 @@ -591,46 +591,59 @@ return getPSKID(partial(self.LogMessage, 0)) def _init_blobs(self): - self.blobs = {} + self.blobs = {} # dict of list if os.path.exists(self.tmpdir): shutil.rmtree(self.tmpdir) os.mkdir(self.tmpdir) + def _append_blob(self, blob, newBlobID): + self.blobs.setdefault(newBlobID,[]).append(blob) + + def _pop_blob(self, blobID): + blobs = self.blobs.pop(blobID, None) + + if blobs is None: + return None + + blob = blobs.pop() + if blobs: + # insert same blob list back if not empty + blobs = self.blobs[blobID] = blobs + return blob + @RunInMain def SeedBlob(self, seed): blob = (mkstemp(dir=self.tmpdir) + (hashlib.new('md5'),)) _fd, _path, md5sum = blob md5sum.update(seed) newBlobID = md5sum.digest() - self.blobs[newBlobID] = blob + self._append_blob(blob, newBlobID) return newBlobID @RunInMain def AppendChunkToBlob(self, data, blobID): - blob = self.blobs.pop(blobID, None) - - if blob is None: - return None + blob = self._pop_blob(blobID) fd, _path, md5sum = blob md5sum.update(data) newBlobID = md5sum.digest() os.write(fd, data) - self.blobs[newBlobID] = blob + self._append_blob(blob, newBlobID) return newBlobID @RunInMain def PurgeBlobs(self): - for fd, _path, _md5sum in list(self.blobs.values()): - os.close(fd) + for blobs in list(self.blobs.values()): + for fd, _path, _md5sum in blobs: + os.close(fd) self._init_blobs() def BlobAsFile(self, blobID, newpath): - blob = self.blobs.pop(blobID, None) + blob = self._pop_blob(blobID) if blob is None: raise Exception( - _(f"Missing data to create file: {newpath}").decode()) + _(f"Missing data to create file: {newpath}")) self._BlobAsFile(blob, newpath)