mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 19:25:00 +03:00
fix(electron): keep trashed local workspaces from coming back (#15914)
Trashing a local workspace copies its directory to `deleted-workspaces` and then removes the original. On Windows that removal often fails because the database file is still locked; the error was only logged. The leftover stays hidden while its backup exists, but deleting the backup from the archive removed only the backup, so the next scan of local workspaces found the leftover and the workspace came back. - Removing a workspace directory now retries (`fs.rm` with `maxRetries`), which covers a lock released shortly after the connection is closed. - Deleting a backup first removes the leftover original, then the backup. If the leftover still cannot be removed, the error is returned and the backup is kept, so the workspace stays in the archive (and hidden from the workspace list) and can be deleted again later instead of coming back. No data is removed unless the user deleted it. Adds tests for deleting a backup with a leftover original, both when it can be removed and when it is locked. fix #14731 Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
1 parent
10ea096689
commit
d70213bb17
2 files changed
+77
-2
No files matched your search
@@ -27,6 +27,17 @@ import {
|
||||
getWorkspaceMeta,
|
||||
} from './meta';
|
||||
|
||||
// On Windows a SQLite file can stay locked for a moment after its connection
|
||||
// is closed, so removing its directory needs a few retries.
|
||||
async function removeWorkspaceDir(dir: string) {
|
||||
await fs.rm(dir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 10,
|
||||
retryDelay: 100,
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteWorkspaceV1(workspaceId: string) {
|
||||
try {
|
||||
await ensureSQLiteDisconnected('workspace', workspaceId);
|
||||
@@ -47,7 +58,7 @@ export async function deleteWorkspace(universalId: string) {
|
||||
const dbPath = await getSpaceDBPath(peer, type, id);
|
||||
try {
|
||||
await getDocStoragePool().disconnect(universalId);
|
||||
await fs.remove(path.dirname(dbPath));
|
||||
await removeWorkspaceDir(path.dirname(dbPath));
|
||||
} catch (e) {
|
||||
logger.error('deleteWorkspace', e);
|
||||
}
|
||||
@@ -78,7 +89,10 @@ export async function trashWorkspace(universalId: string) {
|
||||
await fs.copy(path.dirname(dbPath), movedPath, {
|
||||
overwrite: true,
|
||||
});
|
||||
await fs.remove(path.dirname(dbPath));
|
||||
// If the original still cannot be removed, it stays hidden from
|
||||
// listLocalWorkspaceIds while its backup exists and is removed together
|
||||
// with the backup in deleteBackupWorkspace.
|
||||
await removeWorkspaceDir(path.dirname(dbPath));
|
||||
} catch (error) {
|
||||
logger.error('trashWorkspace', error);
|
||||
}
|
||||
@@ -323,6 +337,14 @@ async function importWorkspaceDb(originalPath: string) {
|
||||
}
|
||||
|
||||
export async function deleteBackupWorkspace(id: string) {
|
||||
// Remove what trashWorkspace could not remove before the backup that hides
|
||||
// it, so a failure keeps the workspace in the backup list instead of bringing
|
||||
// it back as a local workspace.
|
||||
const leftoverPath = path.dirname(
|
||||
await getSpaceDBPath('local', 'workspace', id)
|
||||
);
|
||||
await removeWorkspaceDir(leftoverPath);
|
||||
|
||||
const basePath = await getDeletedWorkspacesBasePath();
|
||||
const workspacePath = path.join(basePath, normalizeWorkspaceIdForPath(id));
|
||||
await fs.remove(workspacePath);
|
||||
|
||||
@@ -391,6 +391,59 @@ describe('workspace db management', () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ name: 'removes the leftover', locked: false },
|
||||
{ name: 'keeps the backup when the leftover is locked', locked: true },
|
||||
])(
|
||||
'deleting the backup of a workspace whose original survived trashing $name',
|
||||
async ({ locked }) => {
|
||||
const { deleteBackupWorkspace, listLocalWorkspaceIds } =
|
||||
await import('@affine/electron/helper/workspace/handlers');
|
||||
const workspaceId = v4();
|
||||
const workspacePath = path.join(
|
||||
appDataPath,
|
||||
'workspaces',
|
||||
'local',
|
||||
workspaceId
|
||||
);
|
||||
const backupPath = path.join(
|
||||
appDataPath,
|
||||
'deleted-workspaces',
|
||||
workspaceId
|
||||
);
|
||||
await fs.ensureFile(path.join(workspacePath, 'storage.db'));
|
||||
await fs.ensureFile(path.join(backupPath, 'storage.db'));
|
||||
|
||||
const rm = fs.rm;
|
||||
const rmSpy = vi.spyOn(fs, 'rm').mockImplementation(((
|
||||
target: string,
|
||||
options: Parameters<typeof rm>[1]
|
||||
) =>
|
||||
locked && target === workspacePath
|
||||
? Promise.reject(
|
||||
Object.assign(new Error('EBUSY: resource busy or locked'), {
|
||||
code: 'EBUSY',
|
||||
})
|
||||
)
|
||||
: rm(target, options)) as typeof rm);
|
||||
try {
|
||||
if (locked) {
|
||||
await expect(deleteBackupWorkspace(workspaceId)).rejects.toThrow(
|
||||
'EBUSY'
|
||||
);
|
||||
} else {
|
||||
await deleteBackupWorkspace(workspaceId);
|
||||
}
|
||||
} finally {
|
||||
rmSpy.mockRestore();
|
||||
}
|
||||
|
||||
expect(await fs.pathExists(backupPath)).toBe(locked);
|
||||
expect(await fs.pathExists(workspacePath)).toBe(locked);
|
||||
expect(await listLocalWorkspaceIds()).not.toContain(workspaceId);
|
||||
}
|
||||
);
|
||||
|
||||
test('delete workspace', async () => {
|
||||
const { deleteWorkspace } =
|
||||
await import('@affine/electron/helper/workspace/handlers');
|
||||
|
||||
Reference in new issue
Block a user