@@ -14,6 +14,13 @@ import 'package:win32/win32.dart' as win32;
14
14
import 'file_system.dart' ;
15
15
import 'internal_constants.dart' ;
16
16
17
+ void _primeGetLastError () {
18
+ // Calling `GetLastError` for the first time causes the `GetLastError`
19
+ // symbol to be loaded, which resets `GetLastError`. So make a harmless
20
+ // call before the value is needed.
21
+ win32.GetLastError ();
22
+ }
23
+
17
24
String _formatMessage (int errorCode) {
18
25
final buffer = win32.wsalloc (1024 );
19
26
try {
@@ -70,10 +77,8 @@ Exception _getError(int errorCode, String message, String path) {
70
77
base class WindowsFileSystem extends FileSystem {
71
78
@override
72
79
void createDirectory (String path) => using ((arena) {
73
- // Calling `GetLastError` for the first time causes the `GetLastError`
74
- // symbol to be loaded, which resets `GetLastError`. So make a harmless
75
- // call before the value is needed.
76
- win32.GetLastError ();
80
+ _primeGetLastError ();
81
+
77
82
if (win32.CreateDirectory (path.toNativeUtf16 (), nullptr) == win32.FALSE ) {
78
83
final errorCode = win32.GetLastError ();
79
84
throw _getError (errorCode, 'create directory failed' , path);
@@ -82,10 +87,8 @@ base class WindowsFileSystem extends FileSystem {
82
87
83
88
@override
84
89
void rename (String oldPath, String newPath) => using ((arena) {
85
- // Calling `GetLastError` for the first time causes the `GetLastError`
86
- // symbol to be loaded, which resets `GetLastError`. So make a harmless
87
- // call before the value is needed.
88
- win32.GetLastError ();
90
+ _primeGetLastError ();
91
+
89
92
if (win32.MoveFileEx (
90
93
oldPath.toNativeUtf16 (allocator: arena),
91
94
newPath.toNativeUtf16 (allocator: arena),
@@ -99,10 +102,7 @@ base class WindowsFileSystem extends FileSystem {
99
102
100
103
@override
101
104
Uint8List readAsBytes (String path) => using ((arena) {
102
- // Calling `GetLastError` for the first time causes the `GetLastError`
103
- // symbol to be loaded, which resets `GetLastError`. So make a harmless
104
- // call before the value is needed.
105
- win32.GetLastError ();
105
+ _primeGetLastError ();
106
106
107
107
final f = win32.CreateFile (
108
108
path.toNativeUtf16 (),
@@ -214,10 +214,7 @@ base class WindowsFileSystem extends FileSystem {
214
214
Uint8List data, [
215
215
WriteMode mode = WriteMode .failExisting,
216
216
]) => using ((arena) {
217
- // Calling `GetLastError` for the first time causes the `GetLastError`
218
- // symbol to be loaded, which resets `GetLastError`. So make a harmless
219
- // call before the value is needed.
220
- win32.GetLastError ();
217
+ _primeGetLastError ();
221
218
222
219
var createFlags = 0 ;
223
220
createFlags | = switch (mode) {
0 commit comments