@@ -10,6 +10,7 @@ import 'dart:typed_data';
10
10
11
11
import 'package:ffi/ffi.dart' ;
12
12
import 'package:ffi/ffi.dart' as ffi;
13
+ import 'package:path/path.dart' as p;
13
14
import 'package:win32/win32.dart' as win32;
14
15
15
16
import 'file_system.dart' ;
@@ -44,33 +45,37 @@ String _formatMessage(int errorCode) {
44
45
}
45
46
}
46
47
47
- Exception _getError (int errorCode, String message, String path) {
48
+ Exception _getError (int errorCode, String message, [ String ? path] ) {
48
49
final osError = io.OSError (_formatMessage (errorCode), errorCode);
49
50
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);
74
79
}
75
80
}
76
81
@@ -132,6 +137,17 @@ base class WindowsFileSystem extends FileSystem {
132
137
}
133
138
});
134
139
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
+
135
151
@override
136
152
void removeDirectory (String path) => using ((arena) {
137
153
_primeGetLastError ();
@@ -266,6 +282,22 @@ base class WindowsFileSystem extends FileSystem {
266
282
}
267
283
}
268
284
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
+
269
301
@override
270
302
void writeAsBytes (
271
303
String path,
0 commit comments