Skip to content

Commit 2b0ef9d

Browse files
committed
Refactor BSD-related st_flags check and create ZFS stubs
BSD-related OS's (i.e., FreeBSD and macOS) can have the st_flags check applied to them (as I implemented this for macOS), so this function was moved out into a separate branch. ZFS function stubs were also created (with errors in case people try to use them at this stage in development) so that I can (hopefully soon) start implementing them. Addresses #1, #3, #12, #13
1 parent caf0093 commit 2b0ef9d

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/HiddenFiles.jl

+36-24
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,34 @@ export ishidden
77
@static if Sys.isunix()
88
_ishidden_unix(f::AbstractString) = startswith(basename(f), '.')
99

10+
@static if Sys.isapple() || Sys.isbsd() # BDS-related
11+
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/chflags.2.html
12+
# https://opensource.apple.com/source/xnu/xnu-4570.41.2/bsd/sys/stat.h.auto.html
13+
const UF_HIDDEN = 0x00008000
14+
15+
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/lstat.2.html
16+
# http://docs.libuv.org/en/v1.x/fs.html # st_flags offset is at index 11, or 21 in 32-bit
17+
const ST_FLAGS_STAT_OFFSET = 0x15
18+
function _st_flags(f::AbstractString)
19+
statbuf = Vector{UInt32}(undef, ccall(:jl_sizeof_stat, Int32, ()))
20+
ccall(:jl_lstat, Int32, (Cstring, Ptr{UInt8}), f, statbuf)
21+
return statbuf[ST_FLAGS_STAT_OFFSET]
22+
end
23+
24+
# https://github.com/dotnet/runtime/blob/5992145db2cb57956ee444aa0f0c2f3f85ee3673/src/native/libs/System.Native/pal_io.c#L219
25+
# https://github.com/davidkaya/corefx/blob/4fd3d39f831f3e14f311b0cdc0a33d662e684a9c/src/System.IO.FileSystem/src/System/IO/FileStatus.Unix.cs#L88
26+
_isinvisible(f::AbstractString) = (_st_flags(f) & UF_HIDDEN) == UF_HIDDEN
27+
28+
_ishidden_bsd_related(f::AbstractString) = _ishidden_unix(f) || _isinvisible(f)
29+
end
30+
31+
include("utils/zfs.jl")
32+
if iszfs() # @static breaks here
33+
error("not yet implemented")
34+
_ishidden_zfs(f::AbstractString) = error("not yet implemented")
35+
end
1036

11-
@static if Sys.isapple()
37+
@static if Sys.isapple() # macOS/Darwin
1238
include("utils/darwin.jl")
1339

1440
###=== Hidden Files and Directories: Simplifying the User Experience ===##
@@ -31,30 +57,14 @@ export ishidden
3157
# - `/tmp`—Contains temporary files created by apps and the system.
3258
# - `/usr`—Contains non-essential command-line binaries, libraries, header files, and other data.
3359
# - `/var`—Contains log files and other files whose content is variable. (Log files are typically viewed using the Console app.)
60+
# TODO
3461

3562

3663
#=== Case 3: Explicitly hidden files and directories ===#
3764
# The Finder may hide specific files or directories that should not be accessed directly by the user. The most notable example of
3865
# this is the /Volumes directory, which contains a subdirectory for each mounted disk in the local file system from the command line.
3966
# (The Finder provides a different user interface for accessing local disks.) In macOS 10.7 and later, the Finder also hides the
40-
# `~/Library` directory—that is, the `Library` directory located in the user’s home directory.
41-
42-
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/chflags.2.html
43-
# https://opensource.apple.com/source/xnu/xnu-4570.41.2/bsd/sys/stat.h.auto.html
44-
const UF_HIDDEN = 0x00008000
45-
46-
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/lstat.2.html
47-
# http://docs.libuv.org/en/v1.x/fs.html # st_flags offset is at index 11, or 21 in 32-bit
48-
const ST_FLAGS_STAT_OFFSET = 0x15
49-
function _st_flags(f::AbstractString)
50-
statbuf = Vector{UInt32}(undef, ccall(:jl_sizeof_stat, Int32, ()))
51-
ccall(:jl_lstat, Int32, (Cstring, Ptr{UInt8}), f, statbuf)
52-
return statbuf[ST_FLAGS_STAT_OFFSET]
53-
end
54-
55-
# https://github.com/dotnet/runtime/blob/5992145db2cb57956ee444aa0f0c2f3f85ee3673/src/native/libs/System.Native/pal_io.c#L219
56-
# https://github.com/davidkaya/corefx/blob/4fd3d39f831f3e14f311b0cdc0a33d662e684a9c/src/System.IO.FileSystem/src/System/IO/FileStatus.Unix.cs#L88
57-
_isinvisible(f::AbstractString) = (_st_flags(f) & UF_HIDDEN) == UF_HIDDEN
67+
# `~/Library` directory—that is, the `Library` directory located in the user’s home directory. This case is handled by `_isinvisible`.
5868

5969

6070
#=== Case 4: Packages and bundles ===#
@@ -108,10 +118,12 @@ export ishidden
108118
end
109119

110120

111-
#=== All cases ===#
112-
_ishidden(f::AbstractString) = any((_ishidden_unix(f), _isinvisible(f), _exists_inside_package_or_bundle(f)))
113-
else
114-
_ishidden = _ishidden_unix
121+
#=== All macOS cases ===#
122+
_ishidden(f::AbstractString) = _ishidden_bsd_related(f) || _exists_inside_package_or_bundle(f)
123+
elseif Sys.isbsd() # BSD
124+
_hidden(f::AbstractString) = _ishidden_bsd_related(f) || (iszfs() && _ishidden_zfs(f))
125+
else # General UNIX
126+
_ishidden(f::AbstractString) = _ishidden_unix(f) || (iszfs() && _ishidden_zfs(f))
115127
end
116128
elseif Sys.iswindows()
117129
# https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
@@ -137,5 +149,5 @@ function ishidden(f::AbstractString)
137149
end
138150

139151

140-
end
152+
end # end module
141153

src/utils/zfs.jl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function iszfs()
2+
# TODO
3+
return false
4+
end
5+

0 commit comments

Comments
 (0)