Skip to content

Posix metadata #204

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 7 commits 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 @@ -18,6 +18,8 @@ abstract interface class Metadata {
bool get isFile;
bool get isDirectory;
bool get isLink;

// Size of directories and links is platform specific.
int get size;
}

Expand Down
56 changes: 53 additions & 3 deletions pkgs/io_file/lib/src/vm_posix_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ int _tempFailureRetry(int Function() f) {
return result;
}

/// File system entity data available on Windows.
final class PosixMetadata implements Metadata {
final int _mode;
final int _flags;

int get _fmt => _mode & libc.S_IFMT;

@override
bool get isDirectory => _fmt == libc.S_IFDIR;

@override
bool get isFile => !(isDirectory || isLink);

@override
bool get isLink => _fmt == libc.S_IFLNK;

@override
final int size;

bool get isHidden {
if (io.Platform.isIOS || io.Platform.isMacOS) {
return _flags & libc.UF_HIDDEN != 0;
}
throw UnsupportedError('isHidden is only supported on iOS/macOS');
}

PosixMetadata._(this._mode, this._flags, this.size);

/// TODO(bquinlan): Document this constructor.
///
/// Make sure to reference:
/// [File Attribute Constants](https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants)
factory PosixMetadata.fromFileAttributes({
required int mode,
int flags = 0,
int size = 0,
}) => PosixMetadata._(mode, flags, size);
}

/// The POSIX `read` function.
///
/// See https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
Expand Down Expand Up @@ -112,9 +151,20 @@ final class PosixFileSystem extends FileSystem {
});

@override
Metadata metadata(String path) {
throw UnimplementedError();
}
PosixMetadata metadata(String path) => ffi.using((arena) {
final stat = arena<libc.Stat>();

if (libc.lstat(path.toNativeUtf8(allocator: arena).cast(), stat) == -1) {
final errno = libc.errno;
throw _getError(errno, 'stat failed', path);
}

return PosixMetadata.fromFileAttributes(
mode: stat.ref.st_mode,
flags: stat.ref.st_flags,
size: stat.ref.st_size,
);
});

@override
void removeDirectory(String path) => ffi.using((arena) {
Expand Down
51 changes: 51 additions & 0 deletions pkgs/io_file/test/metadata_apple_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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('ios || mac-os')
library;

import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:io_file/src/vm_posix_file_system.dart';
import 'package:stdlibc/stdlibc.dart' as stdlibc;
import 'package:test/test.dart';

import 'test_utils.dart';

@Native<Int Function(Pointer<Utf8>, Uint32)>(isLeaf: false)
external int chflags(Pointer<Utf8> buf, int count);

void main() {
final posixFileSystem = PosixFileSystem();

group('windows metadata', () {
late String tmp;

setUp(() => tmp = createTemp('metadata'));

tearDown(() => deleteTemp(tmp));

group('isHidden', () {
test('false', () {
final path = '$tmp/file';
File(path).writeAsStringSync('Hello World');

final data = posixFileSystem.metadata(path);
expect(data.isHidden, isFalse);
});
test('false', () {
final path = '$tmp/file';
File(path).writeAsStringSync('Hello World');
using((arena) {
chflags(path.toNativeUtf8(), stdlibc.UF_HIDDEN);
});

final data = posixFileSystem.metadata(path);
expect(data.isHidden, isTrue);
});
});
});
}
39 changes: 19 additions & 20 deletions pkgs/io_file/test/metadata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import 'dart:io';

import 'package:io_file/io_file.dart';
import 'package:test/test.dart';
import 'package:win32/win32.dart' as win32;

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

void main() {
Expand All @@ -26,13 +28,11 @@ void main() {
expect(
() => fileSystem.metadata('$tmp/file1'),
throwsA(
isA<PathNotFoundException>()
.having((e) => e.message, 'message', 'metadata failed')
.having(
(e) => e.osError?.errorCode,
'errorCode',
2, // ENOENT, ERROR_FILE_NOT_FOUND
),
isA<PathNotFoundException>().having(
(e) => e.osError?.errorCode,
'errorCode',
Platform.isWindows ? win32.ERROR_FILE_NOT_FOUND : errors.enoent,
),
),
);
});
Expand All @@ -53,7 +53,7 @@ void main() {
expect(data.isFile, isTrue);
expect(data.isLink, isFalse);
});
test('link', () {
test('file link', () {
File('$tmp/file1').writeAsStringSync('Hello World');
final path = '$tmp/link';
Link(path).createSync('$tmp/file1');
Expand All @@ -63,13 +63,20 @@ void main() {
expect(data.isFile, isFalse);
expect(data.isLink, isTrue);
});

test('directory link', () {
File('$tmp/file1').writeAsStringSync('Hello World');
final path = '$tmp/link';
Link(path).createSync('$tmp/dir');

final data = fileSystem.metadata(path);
expect(data.isDirectory, isFalse);
expect(data.isFile, isFalse);
expect(data.isLink, isTrue);
});
});

group('size', () {
test('directory', () {
final data = fileSystem.metadata(tmp);
expect(data.size, 0);
});
test('empty file', () {
final path = '$tmp/file1';
File(path).writeAsStringSync('');
Expand All @@ -84,14 +91,6 @@ void main() {
final data = fileSystem.metadata(path);
expect(data.size, 12);
});
test('link', () {
File('$tmp/file1').writeAsStringSync('Hello World');
final path = '$tmp/link';
Link(path).createSync('$tmp/file1');

final data = fileSystem.metadata(path);
expect(data.size, 0);
});
});
});
}