Skip to content

Implement file copying on POSIX #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/io_file/lib/src/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class WriteMode {
/// be refered to by the path `r'\\.\NUL'`.
@sealed
abstract class FileSystem {
void copyFile(String oldPath, String newPath);

/// Create a directory at the given path.
///
/// If the directory already exists, then `PathExistsException` is thrown.
Expand Down
77 changes: 77 additions & 0 deletions pkgs/io_file/lib/src/vm_posix_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,83 @@ external int write(int fd, Pointer<Uint8> buf, int count);
/// A [FileSystem] implementation for POSIX systems (e.g. Android, iOS, Linux,
/// macOS).
final class PosixFileSystem extends FileSystem {
void _slowCopy(
String oldPath,
String newPath,
int fromFd,
int toFd,
Allocator arena,
) {
final buffer = arena<Uint8>(blockSize);

while (true) {
final r = _tempFailureRetry(() => read(fromFd, buffer, blockSize));
switch (r) {
case -1:
final errno = libc.errno;
throw _getError(errno, systemCall: 'read', path1: oldPath);
case 0:
return;
}

var writeRemaining = r;
var writeBuffer = buffer;
while (writeRemaining > 0) {
final w = _tempFailureRetry(
() => write(toFd, writeBuffer, writeRemaining),
);
if (w == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'write', path1: newPath);
}
writeRemaining -= w;
writeBuffer += w;
}
}
}

@override
void copyFile(String oldPath, String newPath) => ffi.using((arena) {
final oldFd = _tempFailureRetry(
() => libc.open(
oldPath.toNativeUtf8(allocator: arena).cast(),
libc.O_RDONLY | libc.O_CLOEXEC,
0,
),
);
if (oldFd == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'open', path1: oldPath);
}
try {
final stat = arena<libc.Stat>();
if (libc.fstat(oldFd, stat) == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'fstat', path1: oldPath);
}

final newFd = _tempFailureRetry(
() => libc.open(
newPath.toNativeUtf8(allocator: arena).cast(),
libc.O_WRONLY | libc.O_TRUNC | libc.O_CREAT | libc.O_CLOEXEC,
stat.ref.st_mode,
),
);
if (newFd == -1) {
final errno = libc.errno;
throw _getError(errno, systemCall: 'open', path1: newPath);
}

try {
_slowCopy(oldPath, newPath, oldFd, newFd, arena);
} finally {
libc.close(newFd);
}
} finally {
libc.close(oldFd);
}
});

@override
bool same(String path1, String path2) => ffi.using((arena) {
final stat1 = arena<libc.Stat>();
Expand Down
3 changes: 3 additions & 0 deletions pkgs/io_file/lib/src/vm_windows_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ final class WindowsMetadata implements Metadata {
/// (e.g. 'COM1'), must be prefixed with `r'\\.\'`. For example, `'NUL'` would
/// be refered to by the path `r'\\.\NUL'`.
final class WindowsFileSystem extends FileSystem {
@override
void copyFile(String oldPath, String newPath) {}

@override
bool same(String path1, String path2) => using((arena) {
// Calling `GetLastError` for the first time causes the `GetLastError`
Expand Down
5 changes: 5 additions & 0 deletions pkgs/io_file/lib/src/web_posix_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import 'file_system.dart';
/// A [FileSystem] implementation for POSIX systems (e.g. Android, iOS, Linux,
/// macOS).
final class PosixFileSystem extends FileSystem {
@override
void copyFile(String oldPath, String newPath) {
throw UnimplementedError();
}

@override
void createDirectory(String path) {
throw UnimplementedError();
Expand Down
5 changes: 5 additions & 0 deletions pkgs/io_file/lib/src/web_windows_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import 'file_system.dart';

/// A [FileSystem] implementation for Windows systems.
base class WindowsFileSystem extends FileSystem {
@override
void copyFile(String oldPath, String newPath) {
throw UnimplementedError();
}

@override
void createDirectory(String path) {
throw UnimplementedError();
Expand Down
183 changes: 183 additions & 0 deletions pkgs/io_file/test/copy_file_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@TestOn('posix')
library;

import 'dart:io' as io;
import 'dart:typed_data';

import 'package:io_file/io_file.dart';
import 'package:io_file/src/internal_constants.dart' show blockSize;
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:win32/win32.dart' as win32;

import 'errors.dart' as errors;
import 'fifo.dart';
import 'test_utils.dart';

void main() {
/// XXX check metadata

group('copyFile', () {
late String tmp;
late String cwd;

setUp(() {
tmp = createTemp('copyFile');
cwd = fileSystem.currentDirectory;
fileSystem.currentDirectory = tmp;
});

tearDown(() {
fileSystem.currentDirectory = cwd;
deleteTemp(tmp);
});

test('copy file absolute path', () {
final data = randomUint8List(1024);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
io.File(oldPath).writeAsBytesSync(data);

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});

test('copy between absolute paths, long file names', () {
final data = randomUint8List(1024);
final oldPath = p.join(tmp, '1' * 255);
final newPath = p.join(tmp, '2' * 255);
io.File(oldPath).writeAsBytesSync(data);

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});

test('copy between relative path, long file names', () {
final data = randomUint8List(1024);
final oldPath = '1' * 255;
final newPath = '2' * 255;
io.File(oldPath).writeAsBytesSync(data);

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});

test('copy file to existing', () {
final data = randomUint8List(1024);
final oldPath = '1' * 255;
final newPath = '2' * 255;
io.File(oldPath).writeAsBytesSync(data);
io.File(newPath).writeAsStringSync('Hello World!');

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});

test('copy non-existent', () {
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';

expect(
() => fileSystem.copyFile(oldPath, newPath),
throwsA(
isA<PathNotFoundException>()
.having((e) => e.path1, 'path1', oldPath)
.having(
(e) => e.errorCode,
'errorCode',
io.Platform.isWindows
? win32.ERROR_FILE_NOT_FOUND
: errors.enoent,
),
),
);
});

group('fifo', () {
for (var i = 0; i <= 1024; ++i) {
test('Read small file: $i bytes', () async {
final data = randomUint8List(i);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
final fifo =
(await Fifo.create(oldPath))
..write(data)
..close();

fileSystem.copyFile(fifo.path, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});
}

test('many single byte reads', () async {
final data = randomUint8List(20);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
final fifo = await Fifo.create(oldPath);
for (var byte in data) {
fifo
..write(Uint8List(1)..[0] = byte)
..delay(const Duration(milliseconds: 10));
}
fifo.close();

fileSystem.copyFile(fifo.path, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});

for (var i = blockSize - 2; i <= blockSize + 2; ++i) {
test('Read close to `blockSize`: $i bytes', () async {
final data = randomUint8List(i);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
final fifo =
(await Fifo.create(oldPath))
..write(data)
..close();

fileSystem.copyFile(fifo.path, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});
}
});

group('regular files', () {
for (var i = 0; i <= 1024; ++i) {
test('copyFile small file: $i bytes', () {
final data = randomUint8List(i);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
io.File(oldPath).writeAsBytesSync(data);

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});
}

for (var i = blockSize - 2; i <= blockSize + 2; ++i) {
test('copyFile close to `blockSize`: $i bytes', () {
final data = randomUint8List(i);
final oldPath = '$tmp/file1';
final newPath = '$tmp/file2';
io.File(oldPath).writeAsBytesSync(data);

fileSystem.copyFile(oldPath, newPath);

expect(io.File(newPath).readAsBytesSync(), data);
});
}
});
});
}
Loading