Skip to content

feat(io_file): Add POSIX implementation for createTemporaryDirectory #216

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions pkgs/io_file/lib/src/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ base class FileSystem {
]) {
throw UnsupportedError('writeAsBytes');
}

/// Creates a temporary directory based on [template].
///
/// The `template` is a path ending in `XXXXXX`. The `XXXXXX` is replaced by
/// a random string guaranteed not to already exist as a directory name.
///
/// Returns the path of the created temporary directory.
String createTemporaryDirectory(String template) {
throw UnsupportedError('createTemporaryDirectory');
}
}
35 changes: 35 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 @@ -66,6 +66,23 @@ base class PosixFileSystem extends FileSystem {
}
}

@override
String createTemporaryDirectory(String template) {
return ffi.using((arena) {
final templatePtr = template.toNativeUtf8(allocator: arena);
final resultPtr = stdlibc.mkdtemp(templatePtr.cast<ffi.Char>());

if (resultPtr == ffi.nullptr) {
final errno = stdlibc.errno;
throw _getError(errno, 'mkdtemp failed', template);
}

// mkdtemp modifies the string in place, so templatePtr now points to
// the actual path created.
return templatePtr.cast<ffi.Utf8>().toDartString();
});
}

@override
Uint8List readAsBytes(String path) {
final fd = _tempFailureRetry(
Expand Down Expand Up @@ -194,4 +211,22 @@ base class PosixFileSystem extends FileSystem {
stdlibc.close(fd);
}
}

@override
String createTemporaryDirectory(String template) {
return ffi.using((arena) {
final templatePtr = template.toNativeUtf8(allocator: arena);
// mkdtemp requires Pointer<Char>, not Pointer<Utf8>.
final resultPtr = stdlibc.mkdtemp(templatePtr.cast<ffi.Char>());

if (resultPtr == ffi.nullptr) {
final errno = stdlibc.errno;
throw _getError(errno, 'mkdtemp failed', template);
}

// mkdtemp modifies the string in place, so templatePtr now points to
// the actual path created. Convert it back to a Dart string.
return templatePtr.cast<ffi.Utf8>().toDartString();
});
}
}
65 changes: 64 additions & 1 deletion pkgs/io_file/test/file_system_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,76 @@
// 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.

import 'dart:io' show Platform;
import 'dart:typed_data'; // Import needed for Uint8List if used elsewhere

import 'package:io_file/io_file.dart';
// ignore: implementation_imports
import 'package:io_file/src/vm_posix_file_system.dart';
import 'package:io_file/src/exceptions.dart';
import 'package:test/test.dart';

void main() {
group('FileSystem', () {
test('rename', () {
// Test that the base FileSystem.rename throws UnsupportedError
test('rename abstract method throws', () {
expect(() => FileSystem().rename('a', 'b'), throwsUnsupportedError);
});
// Note: We don't test the other abstract methods for throwing
// UnsupportedError here, as that's their defined behavior.
// We only test specific implementations below.
});

// --- PosixFileSystem Tests ---
if (Platform.isLinux || Platform.isMacOS) {
group('PosixFileSystem', () {
late PosixFileSystem fs;

setUp(() {
fs = PosixFileSystem();
});

group('createTemporaryDirectory', () {
test('successfully creates a temporary directory', () {
const templatePrefix = '/tmp/io_file_test.';
const template = '$templatePrefixXXXXXX';
final resultPath = fs.createTemporaryDirectory(template);

expect(resultPath, isNotNull);
expect(resultPath, isNotEmpty);
expect(resultPath, startsWith(templatePrefix));
expect(resultPath.length, template.length);
expect(resultPath.substring(resultPath.length - 6), isNot('XXXXXX'));
// Cannot reliably check for actual directory existence here,
// but success implies mkdtemp worked.
// In a real environment, we might clean up here, but it's complex
// in this test setup.
});

test('throws PathNotFoundException for non-existent parent directory',
() {
const template = '/no/such/directory/test.XXXXXX';
expect(
() => fs.createTemporaryDirectory(template),
throwsA(isA<PathNotFoundException>()),
reason: 'mkdtemp should fail with ENOENT for invalid paths.',
);
});

test('throws FileSystemException for template without XXXXXX suffix',
() {
const template = '/tmp/io_file_test_no_suffix';
expect(
() => fs.createTemporaryDirectory(template),
throwsA(isA<FileSystemException>().having(
(e) => e.osError?.errorCode, 'osError.errorCode', isNot(0))),
reason: 'mkdtemp requires the XXXXXX suffix and should fail.',
);
});

// TODO(brianquinlan): add tests for permissions/access issues once
// those error codes are mapped correctly in _getError.
}); // group createTemporaryDirectory
}); // group PosixFileSystem
} // if POSIX platform
}
Loading