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 3 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
8 changes: 7 additions & 1 deletion .github/workflows/io_file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ jobs:
fail-fast: false
matrix:
sdk: [stable, dev]
os: [ubuntu-latest, windows-latest, macos-latest]
os:
- ubuntu-20.04
- ubuntu-latest
- windows-2019
- windows-latest
- macos-13
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
Expand Down
56 changes: 56 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 @@ -60,6 +60,33 @@ int _tempFailureRetry(int Function() f) {
return result;
}

final class PosixMetadata {
@override
final bool isDirectory;

@override
final bool isFile;

@override
final bool isLink;

@override
final int size;

@override
final bool isHidden;

PosixMetadata({
this.isDirectory = false,
this.isFile = false,
this.isLink = false,

this.size = 0,

this.isHidden = false,
});
}

/// The POSIX `read` function.
///
/// See https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
Expand Down Expand Up @@ -150,4 +177,33 @@ base class PosixFileSystem extends FileSystem {
}
return buffer.asTypedList(length, finalizer: ffi.calloc.nativeFree);
}

PosixMetadata metadata(String path) {
final stat = stdlibc.stat(path);
if (stat == null) {
final errno = stdlibc.errno;
throw _getError(errno, 'stat failed', path);
}

final bool isHidden;
if (io.Platform.isIOS || io.Platform.isMacOS) {
final flags = stat.st_flags!;
isHidden = flags & stdlibc.UF_HIDDEN != 0;
} else {
isHidden = false;
}

final isDirectory = stat.st_mode & stdlibc.S_IFDIR != 0;
final isLink = stat.st_mode & stdlibc.S_IFLNK != 0;
final isFile = !(isDirectory || isLink);

// st_birthtimespec;
return PosixMetadata(
isDirectory: isDirectory,
isFile: isFile,
isLink: isLink,
size: stat.st_size,
isHidden: isHidden,
);
}
}
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);
});
});
});
}
Loading