From eea413df25349a0d195a18fedc0db770c99f2978 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 27 Mar 2025 14:57:40 -0700 Subject: [PATCH 1/3] Work --- .../io_file/lib/src/vm_posix_file_system.dart | 50 +++++++++++++++++++ pkgs/io_file/test/metadata_apple_test.dart | 40 +++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 pkgs/io_file/test/metadata_apple_test.dart diff --git a/pkgs/io_file/lib/src/vm_posix_file_system.dart b/pkgs/io_file/lib/src/vm_posix_file_system.dart index 3b4fcf75..1590476d 100644 --- a/pkgs/io_file/lib/src/vm_posix_file_system.dart +++ b/pkgs/io_file/lib/src/vm_posix_file_system.dart @@ -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 @@ -150,4 +177,27 @@ base class PosixFileSystem extends FileSystem { } return buffer.asTypedList(length, finalizer: ffi.calloc.nativeFree); } + + Object metadata(String path) { + final stat = stdlibc.stat(path); + if (stat == null) { + final errno = stdlibc.errno; + throw _getError(errno, 'stat failed', path); + } + + if (io.Platform.isIOS || io.Platform.isMacOS) { + final flags = stat.st_flags!; + if (flags & stdlibc.UF_HIDDEN != 0) { + print('Hidden'); + } else { + print('Not hidden'); + } + } + + stat.st_mode; + return PosixMetadata(isDirectory: stat.st_mode & stdlibc.S_IFDIR != 0, + isFile: , + isLink: stat.st_mode & stdlibc.S_IFLNK != 0, + size: stat.st_size); + } } diff --git a/pkgs/io_file/test/metadata_apple_test.dart b/pkgs/io_file/test/metadata_apple_test.dart new file mode 100644 index 00000000..22693310 --- /dev/null +++ b/pkgs/io_file/test/metadata_apple_test.dart @@ -0,0 +1,40 @@ +// 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:io'; + +import 'package:io_file/src/vm_posix_file_system.dart'; +import 'package:test/test.dart'; + +import 'test_utils.dart'; + +void main() { + final posixFileSystem = PosixFileSystem(); + + group('windows metadata', () { + late String tmp; + + setUp(() => tmp = createTemp('metadata')); + + tearDown(() => deleteTemp(tmp)); + + group('isReadOnly', () { + test('false', () { + final path = '$tmp/file1'; + File(path).writeAsStringSync('Hello World'); + + final data = posixFileSystem.metadata(path); + }); + test('false', () { + final path = '$tmp/file1'; + File(path).writeAsStringSync('Hello World'); + + final data = posixFileSystem.metadata('/Users/bquinlan/Library'); + }); + }); + }); +} From d95e9a2bd08c19af8ecc815cadc81380e47c9e22 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 27 Mar 2025 16:23:49 -0700 Subject: [PATCH 2/3] Posix --- .../io_file/lib/src/vm_posix_file_system.dart | 30 +++++++++++-------- pkgs/io_file/test/metadata_apple_test.dart | 19 +++++++++--- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/pkgs/io_file/lib/src/vm_posix_file_system.dart b/pkgs/io_file/lib/src/vm_posix_file_system.dart index 1590476d..d6dd8c54 100644 --- a/pkgs/io_file/lib/src/vm_posix_file_system.dart +++ b/pkgs/io_file/lib/src/vm_posix_file_system.dart @@ -178,26 +178,32 @@ base class PosixFileSystem extends FileSystem { return buffer.asTypedList(length, finalizer: ffi.calloc.nativeFree); } - Object metadata(String path) { + 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!; - if (flags & stdlibc.UF_HIDDEN != 0) { - print('Hidden'); - } else { - print('Not hidden'); - } + isHidden = flags & stdlibc.UF_HIDDEN != 0; + } else { + isHidden = false; } - - stat.st_mode; - return PosixMetadata(isDirectory: stat.st_mode & stdlibc.S_IFDIR != 0, - isFile: , - isLink: stat.st_mode & stdlibc.S_IFLNK != 0, - size: stat.st_size); + + 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, + ); } } diff --git a/pkgs/io_file/test/metadata_apple_test.dart b/pkgs/io_file/test/metadata_apple_test.dart index 22693310..3752b2f6 100644 --- a/pkgs/io_file/test/metadata_apple_test.dart +++ b/pkgs/io_file/test/metadata_apple_test.dart @@ -5,13 +5,19 @@ @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, Uint32)>(isLeaf: false) +external int chflags(Pointer buf, int count); + void main() { final posixFileSystem = PosixFileSystem(); @@ -22,18 +28,23 @@ void main() { tearDown(() => deleteTemp(tmp)); - group('isReadOnly', () { + group('isHidden', () { test('false', () { - final path = '$tmp/file1'; + final path = '$tmp/file'; File(path).writeAsStringSync('Hello World'); final data = posixFileSystem.metadata(path); + expect(data.isHidden, isFalse); }); test('false', () { - final path = '$tmp/file1'; + final path = '$tmp/file'; File(path).writeAsStringSync('Hello World'); + using((arena) { + chflags(path.toNativeUtf8(), stdlibc.UF_HIDDEN); + }); - final data = posixFileSystem.metadata('/Users/bquinlan/Library'); + final data = posixFileSystem.metadata(path); + expect(data.isHidden, isTrue); }); }); }); From 6b2226a5c4f5983e483d96ba73bec36db502de7d Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 27 Mar 2025 17:12:56 -0700 Subject: [PATCH 3/3] Add more OS --- .github/workflows/io_file.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/io_file.yml b/.github/workflows/io_file.yml index 794bc6bb..b141f1ed 100644 --- a/.github/workflows/io_file.yml +++ b/.github/workflows/io_file.yml @@ -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