Skip to content

Commit 07e730e

Browse files
authored
Add temporary directory support for POSIX (#222)
1 parent bad1f29 commit 07e730e

File tree

4 files changed

+64
-31
lines changed

4 files changed

+64
-31
lines changed

pkgs/io_file/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See
1515
| create directory | ||||| | |
1616
| create hard link | | | | | | | |
1717
| create symbolic link | | | | | | | |
18-
| create tmp directory | | | | | | | |
18+
| create tmp directory | | | | | | | |
1919
| create tmp file | | | | | | | |
2020
| delete directory | ||||| | |
2121
| delete file | | | | | | | |

pkgs/io_file/lib/src/vm_windows_file_system.dart

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'dart:typed_data';
1010

1111
import 'package:ffi/ffi.dart';
1212
import 'package:ffi/ffi.dart' as ffi;
13+
import 'package:path/path.dart' as p;
1314
import 'package:win32/win32.dart' as win32;
1415

1516
import 'file_system.dart';
@@ -44,33 +45,37 @@ String _formatMessage(int errorCode) {
4445
}
4546
}
4647

47-
Exception _getError(int errorCode, String message, String path) {
48+
Exception _getError(int errorCode, String message, [String? path]) {
4849
final osError = io.OSError(_formatMessage(errorCode), errorCode);
4950

50-
switch (errorCode) {
51-
case win32.ERROR_ACCESS_DENIED:
52-
case win32.ERROR_CURRENT_DIRECTORY:
53-
case win32.ERROR_WRITE_PROTECT:
54-
case win32.ERROR_BAD_LENGTH:
55-
case win32.ERROR_SHARING_VIOLATION:
56-
case win32.ERROR_LOCK_VIOLATION:
57-
case win32.ERROR_NETWORK_ACCESS_DENIED:
58-
case win32.ERROR_DRIVE_LOCKED:
59-
return io.PathAccessException(path, osError, message);
60-
case win32.ERROR_FILE_EXISTS:
61-
case win32.ERROR_ALREADY_EXISTS:
62-
return io.PathExistsException(path, osError, message);
63-
case win32.ERROR_FILE_NOT_FOUND:
64-
case win32.ERROR_PATH_NOT_FOUND:
65-
case win32.ERROR_INVALID_DRIVE:
66-
case win32.ERROR_INVALID_NAME:
67-
case win32.ERROR_NO_MORE_FILES:
68-
case win32.ERROR_BAD_NETPATH:
69-
case win32.ERROR_BAD_NET_NAME:
70-
case win32.ERROR_BAD_PATHNAME:
71-
return io.PathNotFoundException(path, osError, message);
72-
default:
73-
return io.FileSystemException(message, path, osError);
51+
if (path != null) {
52+
switch (errorCode) {
53+
case win32.ERROR_ACCESS_DENIED:
54+
case win32.ERROR_CURRENT_DIRECTORY:
55+
case win32.ERROR_WRITE_PROTECT:
56+
case win32.ERROR_BAD_LENGTH:
57+
case win32.ERROR_SHARING_VIOLATION:
58+
case win32.ERROR_LOCK_VIOLATION:
59+
case win32.ERROR_NETWORK_ACCESS_DENIED:
60+
case win32.ERROR_DRIVE_LOCKED:
61+
return io.PathAccessException(path, osError, message);
62+
case win32.ERROR_FILE_EXISTS:
63+
case win32.ERROR_ALREADY_EXISTS:
64+
return io.PathExistsException(path, osError, message);
65+
case win32.ERROR_FILE_NOT_FOUND:
66+
case win32.ERROR_PATH_NOT_FOUND:
67+
case win32.ERROR_INVALID_DRIVE:
68+
case win32.ERROR_INVALID_NAME:
69+
case win32.ERROR_NO_MORE_FILES:
70+
case win32.ERROR_BAD_NETPATH:
71+
case win32.ERROR_BAD_NET_NAME:
72+
case win32.ERROR_BAD_PATHNAME:
73+
return io.PathNotFoundException(path, osError, message);
74+
default:
75+
return io.FileSystemException(message, path, osError);
76+
}
77+
} else {
78+
return io.FileSystemException(message, path, osError);
7479
}
7580
}
7681

@@ -132,6 +137,17 @@ base class WindowsFileSystem extends FileSystem {
132137
}
133138
});
134139

140+
@override
141+
String createTemporaryDirectory({String? parent, String? prefix}) {
142+
_primeGetLastError();
143+
144+
final suffix = win32.Guid.generate().toString();
145+
final directory = parent ?? temporaryDirectory;
146+
final path = p.join(directory, '${prefix ?? ''}$suffix');
147+
createDirectory(path);
148+
return path;
149+
}
150+
135151
@override
136152
void removeDirectory(String path) => using((arena) {
137153
_primeGetLastError();
@@ -266,6 +282,22 @@ base class WindowsFileSystem extends FileSystem {
266282
}
267283
}
268284

285+
@override
286+
String get temporaryDirectory {
287+
const maxLength = win32.MAX_PATH + 1;
288+
final buffer = win32.wsalloc(maxLength);
289+
try {
290+
final length = win32.GetTempPath2(maxLength, buffer);
291+
if (length == 0) {
292+
final errorCode = win32.GetLastError();
293+
throw _getError(errorCode, 'GetTempPath failed');
294+
}
295+
return p.canonicalize(buffer.toDartString());
296+
} finally {
297+
win32.free(buffer);
298+
}
299+
}
300+
269301
@override
270302
void writeAsBytes(
271303
String path,

pkgs/io_file/test/create_temporary_directory_test.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@TestOn('posix')
5+
@TestOn('vm')
66
library;
77

88
import 'dart:io';
@@ -11,6 +11,7 @@ import 'package:io_file/io_file.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:stdlibc/stdlibc.dart' as stdlibc;
1313
import 'package:test/test.dart';
14+
import 'package:win32/win32.dart' as win32;
1415

1516
import 'test_utils.dart';
1617

@@ -107,7 +108,7 @@ void main() {
107108
isA<PathNotFoundException>().having(
108109
(e) => e.osError?.errorCode,
109110
'errorCode',
110-
stdlibc.ENOENT,
111+
Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : stdlibc.ENOENT,
111112
),
112113
),
113114
);
@@ -146,7 +147,7 @@ void main() {
146147
isA<PathNotFoundException>().having(
147148
(e) => e.osError?.errorCode,
148149
'errorCode',
149-
stdlibc.ENOENT,
150+
Platform.isWindows ? win32.ERROR_PATH_NOT_FOUND : stdlibc.ENOENT,
150151
),
151152
),
152153
);
@@ -160,7 +161,7 @@ void main() {
160161
parent: tmp,
161162
prefix: 'dir1/file',
162163
);
163-
expect(tmp1, startsWith(subdir1));
164+
expect(p.canonicalize(tmp1), startsWith(p.canonicalize(subdir1)));
164165
expect(Directory(tmp1).existsSync(), isTrue);
165166
});
166167
});

pkgs/io_file/test/temporary_directory_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@TestOn('posix')
5+
@TestOn('vm')
66
library;
77

88
import 'package:io_file/io_file.dart';

0 commit comments

Comments
 (0)