Skip to content

Add support for removing directories #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/io_file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See
| create symbolic link | | | | | | | |
| create tmp directory | | | | | | | |
| create tmp file | | | | | | | |
| delete directory | | | | | | | |
| delete directory | | | | | | | |
| delete file | | | | | | | |
| delete tree | | | | | | | |
| enum dir contents | | | | | | | |
Expand Down
16 changes: 16 additions & 0 deletions pkgs/io_file/lib/src/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ base class FileSystem {
throw UnsupportedError('createDirectory');
}

/// Deletes the directory at the given path.
///
/// If `path` is a directory but the directory is not empty, then
/// `FileSystemException` is thrown.
///
/// TODO(bquinlan): Explain how to delete non-empty directories.
///
/// If `path` is not directory:
///
/// - On Windows, if `path` is a symbolic link to a directory then the
/// symbolic link is deleted. Otherwise, a `FileSystemException` is thrown.
/// - On POSIX, a `FileSystemException` is thrown.
void removeDirectory(String path) {
throw UnsupportedError('removeDirectory');
}

/// Renames, and possibly moves a file system object from one path to another.
///
/// If `newPath` is a relative path, it is resolved against the current
Expand Down
8 changes: 8 additions & 0 deletions pkgs/io_file/lib/src/vm_posix_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ base class PosixFileSystem extends FileSystem {
}
}

@override
void removeDirectory(String path) {
if (stdlibc.unlinkat(stdlibc.AT_FDCWD, path, stdlibc.AT_REMOVEDIR) == -1) {
final errno = stdlibc.errno;
throw _getError(errno, 'remove directory failed', path);
}
}

@override
void rename(String oldPath, String newPath) {
// See https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html
Expand Down
14 changes: 13 additions & 1 deletion pkgs/io_file/lib/src/vm_windows_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,24 @@ base class WindowsFileSystem extends FileSystem {
void createDirectory(String path) => using((arena) {
_primeGetLastError();

if (win32.CreateDirectory(path.toNativeUtf16(), nullptr) == win32.FALSE) {
if (win32.CreateDirectory(path.toNativeUtf16(allocator: arena), nullptr) ==
win32.FALSE) {
final errorCode = win32.GetLastError();
throw _getError(errorCode, 'create directory failed', path);
}
});

@override
void removeDirectory(String path) => using((arena) {
_primeGetLastError();

if (win32.RemoveDirectory(path.toNativeUtf16(allocator: arena)) ==
win32.FALSE) {
final errorCode = win32.GetLastError();
throw _getError(errorCode, 'remove directory failed', path);
}
});

@override
void rename(String oldPath, String newPath) => using((arena) {
_primeGetLastError();
Expand Down
128 changes: 128 additions & 0 deletions pkgs/io_file/test/remove_directory_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@TestOn('vm')
library;

import 'dart:io';

import 'package:io_file/io_file.dart';
import 'package:stdlibc/stdlibc.dart' as stdlibc;
import 'package:test/test.dart';
import 'package:win32/win32.dart' as win32;

import 'test_utils.dart';

void main() {
group('removeDirectory', () {
late String tmp;

setUp(() => tmp = createTemp('createDirectory'));

tearDown(() => deleteTemp(tmp));

//TODO(brianquinlan): test with a very long path.

test('success', () {
final path = '$tmp/dir';
Directory(path).createSync();

fileSystem.removeDirectory(path);

expect(FileSystemEntity.typeSync(path), FileSystemEntityType.notFound);
});

test('non-empty directory', () {
final path = '$tmp/dir';
Directory(path).createSync();
File('$tmp/dir/file').writeAsStringSync('Hello World!');

expect(
() => fileSystem.removeDirectory(path),
throwsA(
isA<FileSystemException>()
.having((e) => e.message, 'message', 'remove directory failed')
.having(
(e) => e.osError?.errorCode,
'errorCode',
Platform.isWindows
? win32.ERROR_DIR_NOT_EMPTY
: stdlibc.ENOTEMPTY,
),
),
);
});

test('non-existent directory', () {
final path = '$tmp/foo/dir';

expect(
() => fileSystem.removeDirectory(path),
throwsA(
isA<PathNotFoundException>()
.having((e) => e.message, 'message', 'remove directory failed')
.having(
(e) => e.osError?.errorCode,
'errorCode',
Platform.isWindows
? win32.ERROR_PATH_NOT_FOUND
: stdlibc.ENOENT,
),
),
);
});

test('file', () {
final path = '$tmp/file';
File(path).writeAsStringSync('Hello World!');

expect(
() => fileSystem.removeDirectory(path),
throwsA(
isA<FileSystemException>()
.having((e) => e.message, 'message', 'remove directory failed')
.having(
(e) => e.osError?.errorCode,
'errorCode',
Platform.isWindows ? win32.ERROR_DIRECTORY : stdlibc.ENOTDIR,
),
),
);
});

test('link', () {
final dirPath = '$tmp/dir';
final linkPath = '$tmp/link';
Directory(dirPath).createSync();
Link(linkPath).createSync(dirPath);

if (Platform.isWindows) {
fileSystem.removeDirectory(linkPath);
expect(
FileSystemEntity.typeSync(dirPath),
FileSystemEntityType.directory,
);
expect(
FileSystemEntity.typeSync(linkPath),
FileSystemEntityType.notFound,
);
} else {
expect(
() => fileSystem.removeDirectory(linkPath),
throwsA(
isA<FileSystemException>()
.having((e) => e.message, 'message', 'remove directory failed')
.having(
(e) => e.osError?.errorCode,
'errorCode',
Platform.isWindows
? win32.ERROR_PATH_NOT_FOUND
: stdlibc.ENOTDIR,
),
),
);
}
});
});
}
Loading