Skip to content

Commit

Permalink
Fix file system delete of folder
Browse files Browse the repository at this point in the history
Deleting a folder without recursive enabled failed as the used
nodejs API unlink does not support folder deletion.
Fixing it by first checking if it is a folder, and if so
using rmdir instead.

Added a simple unit test, which also exposed that
the folder deletion also fails when using trash support,
but that is a different issue.

Signed-off-by: Daniel Friederich <[email protected]>
  • Loading branch information
dfriederich authored and jfaltermeier committed Feb 9, 2024
1 parent b7ed5e9 commit 3034c0a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 34 additions & 1 deletion packages/filesystem/src/node/disk-file-system-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { promises as fs } from 'fs';
import { join } from 'path';
import * as temp from 'temp';
import { v4 } from 'uuid';
import { FilePermission, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files';
import { FilePermission, FileSystemProviderCapabilities, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files';
import { DiskFileSystemProvider } from './disk-file-system-provider';
import { bindFileSystemWatcherServer } from './filesystem-backend-module';

Expand Down Expand Up @@ -93,6 +93,39 @@ describe('disk-file-system-provider', () => {
});
});

describe('delete', () => {
it('delete is able to delete folder', async () => {
const tempDirPath = tracked.mkdirSync();
const testFolder = join(tempDirPath, 'test');
const folderUri = FileUri.create(testFolder);
for (const recursive of [true, false]) {
// Note: useTrash = true fails on Linux
const useTrash = false;
if ((fsProvider.capabilities & FileSystemProviderCapabilities.Access) === 0 && useTrash) {
continue;
}
await fsProvider.mkdir(folderUri);
if (recursive) {
await fsProvider.writeFile(FileUri.create(join(testFolder, 'test.file')), Buffer.from('test'), { overwrite: false, create: true });
await fsProvider.mkdir(FileUri.create(join(testFolder, 'subFolder')));
}
await fsProvider.delete(folderUri, { recursive, useTrash });
}
});

it('delete is able to delete file', async () => {
const tempDirPath = tracked.mkdirSync();
const testFile = join(tempDirPath, 'test.file');
const testFileUri = FileUri.create(testFile);
for (const recursive of [true, false]) {
for (const useTrash of [true, false]) {
await fsProvider.writeFile(testFileUri, Buffer.from('test'), { overwrite: false, create: true });
await fsProvider.delete(testFileUri, { recursive, useTrash });
}
}
});
});

function createContainer(): Container {
const container = new Container({ defaultScope: 'Singleton' });
const module = new ContainerModule(bind => {
Expand Down
7 changes: 6 additions & 1 deletion packages/filesystem/src/node/disk-file-system-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,12 @@ export class DiskFileSystemProvider implements Disposable,
if (opts.recursive) {
await this.rimraf(filePath);
} else {
await promisify(unlink)(filePath);
const stat = await promisify(lstat)(filePath);
if (stat.isDirectory() && !stat.isSymbolicLink()) {
await promisify(rmdir)(filePath);
} else {
await promisify(unlink)(filePath);
}
}
} else {
await trash(filePath);
Expand Down

0 comments on commit 3034c0a

Please sign in to comment.