From 8833f09fcda9ade5daae95e953f75292473bc58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Gonz=C3=A1lez?= Date: Fri, 15 Mar 2024 20:20:37 -0400 Subject: [PATCH] feat: apply double check in folder deletion --- src/apps/sync-engine/BindingManager.ts | 15 ------- .../application/FilesPlaceholderDeleter.ts | 21 +++------- .../application/FolderPlaceholderDeleter.ts | 39 ++++++++++++------- .../infrastructure/HttpRemoteFileSystem.ts | 2 +- 4 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/apps/sync-engine/BindingManager.ts b/src/apps/sync-engine/BindingManager.ts index 8decb5b77..bdd3fdaf3 100644 --- a/src/apps/sync-engine/BindingManager.ts +++ b/src/apps/sync-engine/BindingManager.ts @@ -55,16 +55,6 @@ export class BindingsManager { await this.container.repositoryPopulator.run(tree.files); await this.container.filesPlaceholderCreator.run(tree.files); - Logger.debug( - 'Trashed in load', - tree.trashedFilesList, - tree.trashedFoldersList - ); - - Logger.debug( - 'Trashed folder', - tree.trashedFoldersList.map((f) => f.path) - ); await this.container?.filesPlaceholderDeleter?.run(tree.trashedFilesList); await this.container?.folderPlaceholderDeleter?.run( tree.trashedFoldersList @@ -317,11 +307,6 @@ export class BindingsManager { try { const tree = await this.container.existingItemsTreeBuilder.run(); - Logger.debug( - 'Trashed folder', - tree.trashedFoldersList.map((f) => f.path) - ); - // Delete all the placeholders that are not in the tree await this.container?.filesPlaceholderDeleter?.run(tree.trashedFilesList); await this.container?.folderPlaceholderDeleter?.run( diff --git a/src/context/virtual-drive/files/application/FilesPlaceholderDeleter.ts b/src/context/virtual-drive/files/application/FilesPlaceholderDeleter.ts index 87b798717..845815ad8 100644 --- a/src/context/virtual-drive/files/application/FilesPlaceholderDeleter.ts +++ b/src/context/virtual-drive/files/application/FilesPlaceholderDeleter.ts @@ -25,19 +25,14 @@ export class FilesPlaceholderDeleter { ); Logger.info( - `localdb path: ${remote.path}\n - localdb uuid: ${remote['contentsId']}\n - localStatus: ${remote.status.value}\n - syncroot uuidd: ${localUUID.split(':')[1]}\n - syncroot uuidd2: ${localUUID}\n - remoteo status: ${fileStatus}\n - trashed condition: ${ + ` + Localdb path: ${remote.path}\n + ___________\n + Condition Status: ${ fileStatus === FileStatuses.TRASHED || fileStatus === FileStatuses.DELETED }\n - localUUID condition: ${localUUID.split(':')[1] === remote['contentsId']}\n - lenuuid: ${localUUID.split(':')[1]?.trim().length}\n - lenremote: ${remote['contentsId']?.trim().length}\n` + Condition ID: ${localUUID.split(':')[1] === remote['contentsId']}\n` ); return ( (fileStatus === FileStatuses.TRASHED || @@ -48,13 +43,7 @@ export class FilesPlaceholderDeleter { private async delete(remote: File): Promise { const hasToBeDeleted = await this.hasToBeDeleted(remote); - Logger.info(`hasToBeDeleted: ${hasToBeDeleted}`); if (hasToBeDeleted) { - Logger.info(`deleting file: ${remote.path}`); - // const win32AbsolutePath = this.relativePathToAbsoluteConverter.run( - // remote.path - // ); - // Logger.info(`win32AbsolutePath: ${win32AbsolutePath}`); await this.local.deleteFileSyncRoot(remote.path); } } diff --git a/src/context/virtual-drive/folders/application/FolderPlaceholderDeleter.ts b/src/context/virtual-drive/folders/application/FolderPlaceholderDeleter.ts index c8cc83feb..dc08a130e 100644 --- a/src/context/virtual-drive/folders/application/FolderPlaceholderDeleter.ts +++ b/src/context/virtual-drive/folders/application/FolderPlaceholderDeleter.ts @@ -1,12 +1,10 @@ import { RelativePathToAbsoluteConverter } from '../../shared/application/RelativePathToAbsoluteConverter'; import { Folder } from '../domain/Folder'; import { FolderStatuses } from '../domain/FolderStatus'; -import fs from 'fs/promises'; import { RemoteFileSystem } from '../domain/file-systems/RemoteFileSystem'; import { LocalFileSystem } from '../domain/file-systems/LocalFileSystem'; import Logger from 'electron-log'; import { sleep } from '../../../../apps/main/util'; -import fs from 'fs/promises'; export class FolderPlaceholderDeleter { constructor( @@ -16,10 +14,14 @@ export class FolderPlaceholderDeleter { ) {} private async hasToBeDeleted(remote: Folder): Promise { - Logger.info(`remote path in hastobedeleted: ${remote.path}`); + if (!remote.path) { + return false; + } const localUUID = await this.local.getFileIdentity(remote.path); - Logger.info(`localUUID in hastobedeleted: ${localUUID}`); + Logger.info(`Local UUID: ${localUUID}, remote path: ${remote.path}`); + if (!localUUID) { + Logger.info(`Local UUID not found for ${remote.path}, skipping deletion`); return false; } @@ -28,20 +30,28 @@ export class FolderPlaceholderDeleter { remote['uuid'] ); + // temporal condition to avoid deleting folders that are not in the trash + // https://github.com/internxt/drive-desktop/blob/60f2ee9a28eab37438b3e8365f4bd519e748a047/src/context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem.ts#L70 + if ( + folderStatus === FolderStatuses.DELETED && + !remote.status.is(FolderStatuses.DELETED) && + !remote.status.is(FolderStatuses.TRASHED) + ) { + Logger.info( + `Folder ${remote.path} with undefined status, skipping deletion` + ); + return false; + } + Logger.info( - `localdb path: ${remote.path}\n - localdb uuid: ${remote['uuid']}\n - localStatus: ${remote.status.value}\n - syncroot uuidd: ${localUUID.split(':')[1]}\n - syncroot uuidd2: ${localUUID}\n - remoteo status: ${folderStatus}\n - trashed condition: ${ + ` + Localdb path: ${remote.path}\n + ___________\n + Condition Status: ${ folderStatus === FolderStatuses.TRASHED || folderStatus === FolderStatuses.DELETED }\n - localUUID condition: ${localUUID.split(':')[1] === remote['uuid']}\n - lenuuid: ${localUUID.split(':')[1]?.trim().length}\n - lenremote: ${remote['uuid']?.trim().length}\n` + Condition ID: ${localUUID.split(':')[1] === remote['uuid']}\n` ); return ( @@ -58,7 +68,6 @@ export class FolderPlaceholderDeleter { const win32AbsolutePath = this.relativePathToAbsoluteConverter.run( remote.path ); - Logger.info(`win32AbsolutePath in delete: ${win32AbsolutePath}`); //await fs.rm(win32AbsolutePath, { recursive: true, force: true }); await this.local.deleteFileSyncRoot(remote.path); } diff --git a/src/context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem.ts b/src/context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem.ts index c5ddd37e8..0b48056c4 100644 --- a/src/context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem.ts +++ b/src/context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem.ts @@ -66,7 +66,7 @@ export class HttpRemoteFileSystem implements RemoteFileSystem { response = await this.trashClient.get( `${process.env.NEW_DRIVE_URL}/drive/folders/${uuid}/meta` ); - } catch { + } catch (error) { return FolderStatuses.DELETED; }