Skip to content

Place FileSystem public method in alphabetical order #266

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 1 commit into from
Aug 14, 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
32 changes: 16 additions & 16 deletions pkgs/io_file/lib/src/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ abstract class FileSystem {
/// ```
String createTemporaryDirectory({String? parent, String? prefix});

/// The current
/// [working directory](https://en.wikipedia.org/wiki/Working_directory) of
/// the Dart process.
///
/// Setting the value of this field will change the working directory for
/// *all* isolates.
///
/// On Windows, unless
/// [long paths are enabled](https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation),
/// the maximum length of the [currentDirectory] path is 260 characters.
String get currentDirectory;
set currentDirectory(String path);

/// TODO(brianquinlan): Add an `exists` method that can determine if a file
/// exists without mutating it on Windows (maybe using `FindFirstFile`?)

Expand All @@ -224,19 +237,6 @@ abstract class FileSystem {
/// written to is to attempt to open it.
Metadata metadata(String path);

/// The current
/// [working directory](https://en.wikipedia.org/wiki/Working_directory) of
/// the Dart process.
///
/// Setting the value of this field will change the working directory for
/// *all* isolates.
///
/// On Windows, unless
/// [long paths are enabled](https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation),
/// the maximum length of the [currentDirectory] path is 260 characters.
String get currentDirectory;
set currentDirectory(String path);

/// Deletes the directory at the given path.
///
/// If `path` is a directory but the directory is not empty, then
Expand All @@ -256,6 +256,9 @@ abstract class FileSystem {
/// those links are deleted but their targets are not.
void removeDirectoryTree(String path);

/// Reads the entire file contents as a list of bytes.
Uint8List readAsBytes(String path);

/// 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 All @@ -274,9 +277,6 @@ abstract class FileSystem {
/// fails and raises [PathExistsException].
void rename(String oldPath, String newPath);

/// Reads the entire file contents as a list of bytes.
Uint8List readAsBytes(String path);

/// Checks whether two paths refer to the same object in the file system.
///
/// Throws `PathNotFoundException` if either path doesn't exist.
Expand Down
104 changes: 52 additions & 52 deletions pkgs/io_file/lib/src/vm_posix_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,24 +341,6 @@ final class PosixFileSystem extends FileSystem {
}
});

@override
bool same(String path1, String path2) => ffi.using((arena) {
final stat1 = arena<libc.Stat>();
if (libc.stat(path1.toNativeUtf8(allocator: arena).cast(), stat1) == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'stat', path1: path1);
}

final stat2 = arena<libc.Stat>();
if (libc.stat(path2.toNativeUtf8(allocator: arena).cast(), stat2) == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'stat', path1: path2);
}

return (stat1.ref.st_ino == stat2.ref.st_ino) &&
(stat1.ref.st_dev == stat2.ref.st_dev);
});

@override
void createDirectory(String path) => ffi.using((arena) {
if (libc.mkdir(
Expand All @@ -371,6 +353,22 @@ final class PosixFileSystem extends FileSystem {
}
});

@override
String createTemporaryDirectory({String? parent, String? prefix}) =>
ffi.using((arena) {
final directory = parent ?? temporaryDirectory;
final template = p.join(directory, '${prefix ?? ''}XXXXXX');

final path = libc.mkdtemp(
template.toNativeUtf8(allocator: arena).cast(),
);
if (path == nullptr) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'mkdtemp', path1: template);
}
return path.cast<ffi.Utf8>().toDartString();
});

@override
set currentDirectory(String path) => ffi.using((arena) {
if (libc.chdir(path.toNativeUtf8(allocator: arena).cast()) == -1) {
Expand All @@ -389,22 +387,6 @@ final class PosixFileSystem extends FileSystem {
return buffer.cast<ffi.Utf8>().toDartString();
});

@override
String createTemporaryDirectory({String? parent, String? prefix}) =>
ffi.using((arena) {
final directory = parent ?? temporaryDirectory;
final template = p.join(directory, '${prefix ?? ''}XXXXXX');

final path = libc.mkdtemp(
template.toNativeUtf8(allocator: arena).cast(),
);
if (path == nullptr) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'mkdtemp', path1: template);
}
return path.cast<ffi.Utf8>().toDartString();
});

@override
PosixMetadata metadata(String path) => ffi.using((arena) {
final stat = arena<libc.Stat>();
Expand Down Expand Up @@ -535,24 +517,6 @@ final class PosixFileSystem extends FileSystem {
);
});

@override
void rename(String oldPath, String newPath) => ffi.using((arena) {
// See https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html
if (libc.rename(
oldPath.toNativeUtf8(allocator: arena).cast(),
newPath.toNativeUtf8(allocator: arena).cast(),
) !=
0) {
final errno = libc.errno;
throw _getError(
errno,
systemCall: 'rename',
path1: oldPath,
path2: newPath,
);
}
});

@override
Uint8List readAsBytes(String path) => ffi.using((arena) {
final fd = _tempFailureRetry(
Expand Down Expand Up @@ -637,6 +601,42 @@ final class PosixFileSystem extends FileSystem {
}
}

@override
void rename(String oldPath, String newPath) => ffi.using((arena) {
// See https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html
if (libc.rename(
oldPath.toNativeUtf8(allocator: arena).cast(),
newPath.toNativeUtf8(allocator: arena).cast(),
) !=
0) {
final errno = libc.errno;
throw _getError(
errno,
systemCall: 'rename',
path1: oldPath,
path2: newPath,
);
}
});

@override
bool same(String path1, String path2) => ffi.using((arena) {
final stat1 = arena<libc.Stat>();
if (libc.stat(path1.toNativeUtf8(allocator: arena).cast(), stat1) == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'stat', path1: path1);
}

final stat2 = arena<libc.Stat>();
if (libc.stat(path2.toNativeUtf8(allocator: arena).cast(), stat2) == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'stat', path1: path2);
}

return (stat1.ref.st_ino == stat2.ref.st_ino) &&
(stat1.ref.st_dev == stat2.ref.st_dev);
});

@override
String get temporaryDirectory => ffi.using((arena) {
var env = libc.getenv('TMPDIR'.toNativeUtf8(allocator: arena).cast());
Expand Down
Loading
Loading