Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:sync): apply mtime to temporary file before publication
johaven43dbbd7d906d
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.tsindex 1b9f94a4..e92a9ea2 100644--- a/backend/src/applications/files/services/files-manager.service.spec.ts+++ b/backend/src/applications/files/services/files-manager.service.spec.ts@@ -239,20 +239,31 @@ describe(FilesManager.name, () => {)})- it('should write stream, emit event and release lock', async () => {+ it('should write tmp stream, apply mtime, move, emit event and release lock', async () => {const space = makeSpace()+ const tmpPath = '/data/users/john/tmp/sync-in-file.txt'setPathExists({ [space.realPath]: false, [path.dirname(space.realPath)]: true }, false)const emitSpy = vi.spyOn(FileEvent, 'emit')- const result = await service.saveStream(user, space, { method: 'PUT', headers: {}, raw: Readable.from(['hello']) } as any)+ const result = await service.saveStream(user, space, { method: 'PUT', headers: {}, raw: Readable.from(['hello']) } as any, {+ tmpPath,+ validateTmpFile: async () => {+ await filesUtils.touchFile(tmpPath, 1710000000)+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()+ expect(emitSpy).not.toHaveBeenCalled()+ expect(filesLockManager.removeLock).not.toHaveBeenCalled()+ }+ })expect(result).toBe(false)expect(filesLockManager.create).toHaveBeenCalledWith(user, space.dbFile, 'Sync-in', DEPTH.RESOURCE)expect(filesUtils.writeUploadFromStream).toHaveBeenCalledWith(- space.realPath,+ tmpPath,expect.anything(),expect.objectContaining({ limiter: expect.objectContaining({ initialFileSize: 0, consume: expect.any(Function) }) }))+ expect(filesUtils.touchFile).toHaveBeenCalledExactlyOnceWith(tmpPath, 1710000000)+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpPath, space.realPath, true)expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: space.realPath })})diff --git a/backend/src/applications/sync/services/sync-manager.service.spec.ts b/backend/src/applications/sync/services/sync-manager.service.spec.tsindex ba8cd770..0a077f1c 100644--- a/backend/src/applications/sync/services/sync-manager.service.spec.ts+++ b/backend/src/applications/sync/services/sync-manager.service.spec.ts@@ -163,7 +163,11 @@ describe(SyncManager.name, () => {it('should upload with checksum OK and return ino', async () => {const req = makeReq({ space: { realPath: '/tmp/up.bin', url: '/space/up.bin' } })const dto = { checksum: 'abc', size: 10, mtime: 1710000000 }- filesManager.saveStream.mockResolvedValue('abc')+ filesManager.saveStream.mockImplementation(async (_user, _space, _req, options) => {+ await options.validateTmpFile({ tmpPath: options.tmpPath, realPath: req.space.realPath, checksum: 'abc' })+ expect(touchFile).toHaveBeenCalledWith(options.tmpPath, dto.mtime)+ return 'abc'+ })fsPromises.stat.mockResolvedValue({ size: 10, ino: 123, mtime: new Date(1710000000 * 1000) })vi.mocked(touchFile).mockResolvedValue(undefined)@@ -174,7 +178,7 @@ describe(SyncManager.name, () => {checksumAlg: SYNC_CHECKSUM_ALG,validateTmpFile: expect.any(Function)})- expect(touchFile).toHaveBeenCalledWith('/tmp/up.bin', 1710000000)+ expect(touchFile).toHaveBeenCalledExactlyOnceWith(filesManager.saveStream.mock.calls[0][3].tmpPath, dto.mtime)expect(r).toEqual({ ino: 123 })expect(removeFiles).not.toHaveBeenCalled()})@@ -195,7 +199,9 @@ describe(SyncManager.name, () => {it('should upload without checksum', async () => {const req = makeReq({ space: { realPath: '/tmp/up2.bin', url: '/space/up2.bin' } })const dto = { size: 5, mtime: 1710000100 }- filesManager.saveStream.mockResolvedValue(undefined)+ filesManager.saveStream.mockImplementation(async (_user, _space, _req, options) => {+ await options.validateTmpFile({ tmpPath: options.tmpPath, realPath: req.space.realPath })+ })fsPromises.stat.mockResolvedValue({ size: 5, ino: 321, mtime: new Date(1710000100 * 1000) })const r = await service.upload(req as any, dto as any)@@ -204,7 +210,7 @@ describe(SyncManager.name, () => {expectedUploadSize: 5,validateTmpFile: expect.any(Function)})- expect(touchFile).toHaveBeenCalledWith('/tmp/up2.bin', 1710000100)+ expect(touchFile).toHaveBeenCalledExactlyOnceWith(filesManager.saveStream.mock.calls[0][3].tmpPath, dto.mtime)expect(r).toEqual({ ino: 321 })})diff --git a/backend/src/applications/sync/services/sync-manager.service.ts b/backend/src/applications/sync/services/sync-manager.service.tsindex 6067cbc0..dc75bacc 100644--- a/backend/src/applications/sync/services/sync-manager.service.ts+++ b/backend/src/applications/sync/services/sync-manager.service.ts@@ -61,10 +61,10 @@ export class SyncManager {if (syncUploadDto.checksum && checksum !== syncUploadDto.checksum) {throw new FileError(HttpStatus.BAD_REQUEST, 'checksums are not identical')}+ // Apply mtime before moving the file and publishing its event.+ await touchFile(tmpPath, syncUploadDto.mtime)}})- // update mtime- await touchFile(req.space.realPath, syncUploadDto.mtime)// return inode numberconst fileStats = await fs.stat(req.space.realPath)return { ino: fileStats.ino }