Skip to content

Commit ca2b0b8

Browse files
committed
Fail existing
1 parent d4d5d2e commit ca2b0b8

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

pkgs/io_file/lib/src/vm_posix_file_system.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ final class PosixFileSystem extends FileSystem {
322322
final newFd = _tempFailureRetry(
323323
() => libc.open(
324324
newPath.toNativeUtf8(allocator: arena).cast(),
325-
libc.O_WRONLY | libc.O_TRUNC | libc.O_CREAT | libc.O_CLOEXEC,
325+
libc.O_WRONLY | libc.O_CREAT | libc.O_EXCL | libc.O_CLOEXEC,
326326
_defaultMode,
327327
),
328328
);

pkgs/io_file/lib/src/vm_windows_file_system.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ final class WindowsFileSystem extends FileSystem {
462462
win32.FILE_GENERIC_WRITE,
463463
0,
464464
nullptr,
465-
win32.CREATE_ALWAYS,
466-
win32.FILE_ATTRIBUTE_NORMAL,
465+
win32.CREATE_NEW,
466+
win32.FILE_ATTRIBUTE_NORMAL & win32.FILE_FLAG_OPEN_REPARSE_POINT,
467467
0,
468468
);
469469
if (newHandle == win32.INVALID_HANDLE_VALUE) {

pkgs/io_file/test/copy_file_test.dart

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,55 @@ void main() {
111111
expect(io.File(newPath).readAsBytesSync(), data);
112112
});
113113

114-
test('copy file to existing', () {
114+
test('copy file to existing file', () {
115115
final data = randomUint8List(1024);
116116
final oldPath = '$tmp/file1';
117117
final newPath = '$tmp/file2';
118118
io.File(oldPath).writeAsBytesSync(data);
119119
io.File(newPath).writeAsStringSync('Hello World!');
120120

121-
fileSystem.copyFile(oldPath, newPath);
121+
expect(
122+
() => fileSystem.copyFile(oldPath, newPath),
123+
throwsA(
124+
isA<PathExistsException>()
125+
.having((e) => e.path1, 'path1', newPath)
126+
.having(
127+
(e) => e.errorCode,
128+
'errorCode',
129+
io.Platform.isWindows
130+
? win32.ERROR_ALREADY_EXISTS
131+
: errors.eexist,
132+
),
133+
),
134+
);
135+
});
122136

123-
expect(io.File(newPath).readAsBytesSync(), data);
137+
test('copy file to existing link', () {
138+
final data = randomUint8List(1024);
139+
final oldPath = '$tmp/file1';
140+
final linkedFile = '$tmp/file2';
141+
final newPath = '$tmp/link';
142+
io.File(oldPath).writeAsBytesSync(data);
143+
io.File(linkedFile).writeAsStringSync('Hello World');
144+
io.Link(newPath).createSync(linkedFile);
145+
146+
expect(
147+
() => fileSystem.copyFile(oldPath, newPath),
148+
throwsA(
149+
isA<PathExistsException>()
150+
.having((e) => e.path1, 'path1', newPath)
151+
.having(
152+
(e) => e.errorCode,
153+
'errorCode',
154+
io.Platform.isWindows
155+
? win32.ERROR_ALREADY_EXISTS
156+
: errors.eexist,
157+
),
158+
),
159+
);
124160
});
125161

126-
test('copy to existant directory', () {
162+
test('copy to existing directory', () {
127163
final data = randomUint8List(1024);
128164
final oldPath = '$tmp/file1';
129165
final newPath = '$tmp/file2';
@@ -133,13 +169,15 @@ void main() {
133169
expect(
134170
() => fileSystem.copyFile(oldPath, newPath),
135171
throwsA(
136-
isA<IOFileException>().having(
137-
(e) => e.errorCode,
138-
'errorCode',
139-
io.Platform.isWindows
140-
? 5 // ERROR_ACCESS_DENIED
141-
: 21, // EISDIR
142-
),
172+
isA<PathExistsException>()
173+
.having((e) => e.path1, 'path1', newPath)
174+
.having(
175+
(e) => e.errorCode,
176+
'errorCode',
177+
io.Platform.isWindows
178+
? win32.ERROR_ALREADY_EXISTS
179+
: errors.eexist,
180+
),
143181
),
144182
);
145183
});

0 commit comments

Comments
 (0)