Skip to content

Commit 79df9a1

Browse files
authored
Move GetLastError priming to a separate function (#217)
1 parent 262c824 commit 79df9a1

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

pkgs/io_file/lib/src/vm_windows_file_system.dart

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import 'package:win32/win32.dart' as win32;
1414
import 'file_system.dart';
1515
import 'internal_constants.dart';
1616

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+
1724
String _formatMessage(int errorCode) {
1825
final buffer = win32.wsalloc(1024);
1926
try {
@@ -70,10 +77,8 @@ Exception _getError(int errorCode, String message, String path) {
7077
base class WindowsFileSystem extends FileSystem {
7178
@override
7279
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+
7782
if (win32.CreateDirectory(path.toNativeUtf16(), nullptr) == win32.FALSE) {
7883
final errorCode = win32.GetLastError();
7984
throw _getError(errorCode, 'create directory failed', path);
@@ -82,10 +87,8 @@ base class WindowsFileSystem extends FileSystem {
8287

8388
@override
8489
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+
8992
if (win32.MoveFileEx(
9093
oldPath.toNativeUtf16(allocator: arena),
9194
newPath.toNativeUtf16(allocator: arena),
@@ -99,10 +102,7 @@ base class WindowsFileSystem extends FileSystem {
99102

100103
@override
101104
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();
106106

107107
final f = win32.CreateFile(
108108
path.toNativeUtf16(),
@@ -214,10 +214,7 @@ base class WindowsFileSystem extends FileSystem {
214214
Uint8List data, [
215215
WriteMode mode = WriteMode.failExisting,
216216
]) => 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();
221218

222219
var createFlags = 0;
223220
createFlags |= switch (mode) {

0 commit comments

Comments
 (0)