Skip to content

Commit

Permalink
Satisfy JuliaFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewilliami committed Nov 5, 2024
1 parent 9ac18b7 commit d7f6acc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 55 deletions.
10 changes: 3 additions & 7 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ using HiddenFiles, Documenter
Documenter.makedocs(
clean = true,
doctest = true,
checkdocs=:exports, # discourse.julialang.org/t/70299/2
checkdocs = :exports, # discourse.julialang.org/t/70299/2
modules = Module[HiddenFiles],
repo = "",
highlightsig = true,
sitename = "HiddenFiles Documentation",
expandfirst = [],
pages = [
"Index" => "index.md",
]
pages = ["Index" => "index.md"],
)

deploydocs(;
repo = "github.com/jakewilliami/HiddenFiles.jl.git",
)
deploydocs(; repo = "github.com/jakewilliami/HiddenFiles.jl.git",)
32 changes: 16 additions & 16 deletions src/HiddenFiles.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module HiddenFiles


export ishidden


"""
```julia
ishidden(f::AbstractString)
Expand Down Expand Up @@ -36,7 +34,6 @@ ishidden
include("docs.jl")
include("path.jl")


@static if Sys.isunix()
include("utils/zfs.jl")
if iszfs() # @static breaks here # ZFS
Expand All @@ -49,7 +46,8 @@ include("path.jl")
_isdotfile(f::AbstractString) = startswith(basename(f), '.')

# Check dotfiles, but also account for ZFS
_ishidden_unix(ps::PathStruct) = _isdotfile(ps.realpath) || (iszfs() && _ishidden_zfs(ps))
_ishidden_unix(ps::PathStruct) =
_isdotfile(ps.realpath) || (iszfs() && _ishidden_zfs(ps))

@static if Sys.isbsd() # BDS-related; this is true for macOS as well
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/chflags.2.html
Expand All @@ -76,7 +74,8 @@ include("path.jl")
# https://github.com/davidkaya/corefx/blob/4fd3d39f831f3e14f311b0cdc0a33d662e684a9c/src/System.IO.FileSystem/src/System/IO/FileStatus.Unix.cs#L88
_isinvisible(f::AbstractString) = (_st_flags(f) & UF_HIDDEN) == UF_HIDDEN

_ishidden_bsd_related(ps::PathStruct) = _ishidden_unix(ps) || _isinvisible(ps.realpath)
_ishidden_bsd_related(ps::PathStruct) =
_ishidden_unix(ps) || _isinvisible(ps.realpath)
end

@static if Sys.isapple() # macOS/Darwin
Expand All @@ -92,7 +91,6 @@ include("path.jl")
# in this category are the `.` and `..` directories, which are references to the
# current and parent directories respectively. This case is handled by _ishidden_unix


#=== Case 2: UNIX-specific directories ===#
# The directories in this category are inherited from traditional UNIX installations.
# They are an important part of the system’s BSD layer but are more useful to
Expand All @@ -112,7 +110,6 @@ include("path.jl")
# TODO
_issystemfile(f::AbstractString) = false


#=== Case 3: Explicitly hidden files and directories ===#
# The Finder may hide specific files or directories that should not be accessed
# directly by the user. The most notable example of this is the /Volumes directory,
Expand All @@ -122,7 +119,6 @@ include("path.jl")
# `~/Library` directory—that is, the `Library` directory located in the user’s
# home directory. This case is handled by `_isinvisible`.


#=== Case 4: Packages and bundles ===#
# Packages and bundles are directories that the Finder presents to the user as if
# they were files. Bundles hide the internal workings of executables such as apps
Expand All @@ -139,7 +135,9 @@ include("path.jl")
# https://github.com/osquery/osquery/blob/598983db97459f858e7a9cc5c731409ffc089b48/osquery/tables/system/darwin/extended_attributes.cpp#L111-L144
# https://github.com/objective-see/ProcInfo/blob/ec51090fcf741a9e045dd3e5119cb5cc8750efd3/procInfo/Binary.m#L121-L172
# NOTE: this function will fail if you give it f as "/"
function _k_mditem_content_type_tree(f::AbstractString, str_encoding::Unsigned = CF_STRING_ENCODING)
function _k_mditem_content_type_tree(
f::AbstractString, str_encoding::Unsigned = CF_STRING_ENCODING
)
cfstr = _cfstring_create_with_cstring(f, str_encoding)
mditem = _mditem_create(cfstr)
mdattrs = _mditem_copy_attribute(mditem, K_MDITEM_CONTENT_TYPE_TREE)
Expand All @@ -159,8 +157,11 @@ include("path.jl")
# https://stackoverflow.com/a/12233785
# Bundles: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html
# Packages: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/DocumentPackages/DocumentPackages.html
const PKG_BUNDLE_TYPES = ("com.apple.package", "com.apple.bundle", "com.apple.application-bundle")
_ispackage_or_bundle(f::AbstractString) = any(t PKG_BUNDLE_TYPES for t in _k_mditem_content_type_tree(f))
const PKG_BUNDLE_TYPES = (
"com.apple.package", "com.apple.bundle", "com.apple.application-bundle"
)
_ispackage_or_bundle(f::AbstractString) =
any(t PKG_BUNDLE_TYPES for t in _k_mditem_content_type_tree(f))

# If a file or directory exists inside a package or bundle, then it is hidden.
# Packages or bundles themselves are not necessarily hidden.
Expand All @@ -181,9 +182,11 @@ include("path.jl")
return false
end


#=== All macOS cases ===#
_ishidden_macos(ps::PathStruct) = _ishidden_bsd_related(ps) || _issystemfile(ps.path) || _exists_inside_package_or_bundle(ps.realpath)
_ishidden_macos(ps::PathStruct) =
_ishidden_bsd_related(ps) ||
_issystemfile(ps.path) ||
_exists_inside_package_or_bundle(ps.realpath)
_ishidden = _ishidden_macos
elseif Sys.isbsd() # BSD; this excludes macOS through control flow (as macOS is checked for first)
_ishidden_bsd(ps::PathStruct) = _ishidden_bsd_related(ps)
Expand Down Expand Up @@ -212,11 +215,9 @@ else
_ishidden(f::AbstractString) = error("hidden files for this OS need to be defined")
end


# Check if the file is actually a directory reference to the current or parent directory
_isdirref(f::AbstractString) = basename(f) (".", "..") # see issue #24


# Each OS branch defines its own _ishidden function. In the main ishidden function,
# we check construct our PathStruct object to pass around to the branch's _ishidden
# function to use as the function necessitates
Expand All @@ -225,5 +226,4 @@ function ishidden(f::AbstractString)
return _isdirref(ps.path) || _ishidden(ps)
end


end # end module
5 changes: 0 additions & 5 deletions src/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ iszfs
"This function is not yet implemented"
_ishidden_zfs


### General Unix ###

"""
Expand Down Expand Up @@ -49,7 +48,6 @@ See also: [`_isdotfile`](@ref), [`_ishidden_zfs`](@ref).
"""
_ishidden_unix


### macOS/BSD ###

"""
Expand Down Expand Up @@ -113,7 +111,6 @@ See also: [`_ishidden_unix`](@ref), [`isinvisible`](@ref).
"""
_ishidden_bsd_related


### macOS ###

"""
Expand Down Expand Up @@ -191,7 +188,6 @@ See also: [`_ishidden_unix`](@ref), [`_ishidden_bsd_related`](@ref), [`_isinvisi
"""
_ishidden_macos


### BSD ###

"""
Expand All @@ -210,7 +206,6 @@ See also: [`_ishidden_unix`](@ref), [`_ishidden_bsd_related`](@ref), [`_isinvisi
"""
_ishidden_bsd


### Windows ###

"""
Expand Down
55 changes: 35 additions & 20 deletions src/utils/darwin.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# https://opensource.apple.com/source/CF/CF-635/CFString.h.auto.html
# https://developer.apple.com/documentation/corefoundation/cfstringbuiltinencodings
const K_CF_STRING_ENCODING_MAC_ROMAN = 0x0
const K_CF_STRING_ENCODING_MAC_ROMAN = 0x0
const K_CF_STRING_ENCODING_WINDOWS_LATIN_1 = 0x0500 # ANSI codepage 1252
const K_CF_STRING_ENCODING_ISO_LATIN_1 = 0x0201 # ISO 8859-1
const K_CF_STRING_ENCODING_ISO_LATIN_1 = 0x0201 # ISO 8859-1
const K_CF_STRING_ENCODING_NEXT_STEP_LATIN = 0x0B01 # NextStep encoding
const K_CF_STRING_ENCODING_ASCII = 0x0600 # 0..127 (in creating CFString, values greater than 0x7F are treated as corresponding Unicode value)
const K_CF_STRING_ENCODING_UNICODE = 0x0100 # kTextEncodingUnicodeDefault + kTextEncodingDefaultFormat (aka kUnicode16BitFormat)
const K_CF_STRING_ENCODING_UTF8 = 0x08000100 # kTextEncodingUnicodeDefault + kUnicodeUTF8Format
const K_CF_STRING_ENCODING_ASCII = 0x0600 # 0..127 (in creating CFString, values greater than 0x7F are treated as corresponding Unicode value)
const K_CF_STRING_ENCODING_UNICODE = 0x0100 # kTextEncodingUnicodeDefault + kTextEncodingDefaultFormat (aka kUnicode16BitFormat)
const K_CF_STRING_ENCODING_UTF8 = 0x08000100 # kTextEncodingUnicodeDefault + kUnicodeUTF8Format
const K_CF_STRING_ENCODING_NON_LOSSY_ASCII = 0x0BFF # 7bit Unicode variants used by Cocoa & Java
const K_CF_STRING_ENCODING_UTF16 = 0x0100 # kTextEncodingUnicodeDefault + kUnicodeUTF16Format (alias of kCFStringEncodingUnicode)
const K_CF_STRING_ENCODING_UTF16BE = 0x10000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16BEFormat
const K_CF_STRING_ENCODING_UTF16LE = 0x14000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16LEFormat
const K_CF_STRING_ENCODING_UTF32 = 0x0c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32Format
const K_CF_STRING_ENCODING_UTF32BE = 0x18000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32BEFormat
const K_CF_STRING_ENCODING_UTF32LE = 0x1c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32LEFormat
const K_CF_STRING_ENCODING_UTF16 = 0x0100 # kTextEncodingUnicodeDefault + kUnicodeUTF16Format (alias of kCFStringEncodingUnicode)
const K_CF_STRING_ENCODING_UTF16BE = 0x10000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16BEFormat
const K_CF_STRING_ENCODING_UTF16LE = 0x14000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16LEFormat
const K_CF_STRING_ENCODING_UTF32 = 0x0c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32Format
const K_CF_STRING_ENCODING_UTF32BE = 0x18000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32BEFormat
const K_CF_STRING_ENCODING_UTF32LE = 0x1c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32LEFormat

# This will be out main/default string encoding
"""
Expand Down Expand Up @@ -41,13 +41,21 @@ See also: [`_string_from_cf_string`](@ref).
[1]: https://developer.apple.com/documentation/corefoundation/1542942-cfstringcreatewithcstring
"""
function _cfstring_create_with_cstring(s::AbstractString, encoding::Unsigned = CF_STRING_ENCODING)
function _cfstring_create_with_cstring(
s::AbstractString, encoding::Unsigned = CF_STRING_ENCODING
)
# https://developer.apple.com/documentation/corefoundation/1542942-cfstringcreatewithcstring
# CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
cfstr = ccall(:CFStringCreateWithCString, Cstring,
(Ptr{Cvoid}, Cstring, UInt32),
C_NULL, s, encoding)
cfstr == C_NULL && error("Cannot create CF String for $(repr(s)) using encoding $(repr(encoding))")
cfstr = ccall(
:CFStringCreateWithCString,
Cstring,
(Ptr{Cvoid}, Cstring, UInt32),
C_NULL,
s,
encoding,
)
cfstr == C_NULL &&
error("Cannot create CF String for $(repr(s)) using encoding $(repr(encoding))")
return cfstr
end

Expand Down Expand Up @@ -90,8 +98,11 @@ See also: [`_mditem_create`](@ref), [`_k_mditem_content_type_tree](@ref).
function _mditem_copy_attribute(mditem::Ptr{UInt32}, cfstr_attr_name::Cstring)
# https://developer.apple.com/documentation/coreservices/1427080-mditemcopyattribute
# CFTypeRef MDItemCopyAttribute(MDItemRef item, CFStringRef name);
ptr = ccall(:MDItemCopyAttribute, Ptr{UInt32}, (Ptr{UInt32}, Cstring), mditem, cfstr_attr_name)
ptr == C_NULL && error("Cannot copy MD Item attribute $(repr(cfstr_attr_name)); this attribute name might not exist")
ptr = ccall(
:MDItemCopyAttribute, Ptr{UInt32}, (Ptr{UInt32}, Cstring), mditem, cfstr_attr_name
)
ptr == C_NULL &&
error("Cannot copy MD Item attribute $(repr(cfstr_attr_name)); this attribute name might not exist")
return ptr
end

Expand Down Expand Up @@ -154,10 +165,14 @@ Given the length of a string and its encoding type, return the maximum length of
[1]: https://developer.apple.com/documentation/corefoundation/1542143-cfstringgetmaximumsizeforencodin
"""
function _cfstring_get_maximum_size_for_encoding(strlen::T, encoding::Unsigned = CF_STRING_ENCODING) where {T <: Integer}
function _cfstring_get_maximum_size_for_encoding(
strlen::T, encoding::Unsigned = CF_STRING_ENCODING
) where {T <: Integer}
# https://developer.apple.com/documentation/corefoundation/1542143-cfstringgetmaximumsizeforencodin
# CFIndex CFStringGetMaximumSizeForEncoding(CFIndex length, CFStringEncoding encoding);
return ccall(:CFStringGetMaximumSizeForEncoding, Int32, (Int32, UInt32), strlen, encoding)
return ccall(
:CFStringGetMaximumSizeForEncoding, Int32, (Int32, UInt32), strlen, encoding
)
end

"""
Expand Down
1 change: 0 additions & 1 deletion src/utils/zfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ function iszfs()
# TODO
return false
end

20 changes: 14 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ using Test
cfstr_nonexistent = HiddenFiles._cfstring_create_with_cstring(f)
@test_throws Exception HiddenFiles._mditem_create(cfstr_nonexistent)
encoding_mode_nonexistent = 0x1c000101 # this encoding mode should not exist
@test_throws Exception HiddenFiles._cfstring_create_with_cstring("Julia", encoding_mode_nonexistent)
@test_throws Exception HiddenFiles._cfstring_create_with_cstring(
"Julia", encoding_mode_nonexistent
)
cfstr = HiddenFiles._cfstring_create_with_cstring(@__FILE__)
mditem = HiddenFiles._mditem_create(cfstr)
cfattr_nonexistent = HiddenFiles._cfstring_create_with_cstring("kMDItemNonexistentAttributeName")
@test_throws Exception HiddenFiles._mditem_copy_attribute(mditem, cfattr_nonexistent)
@test_throws Exception HiddenFiles._mditem_copy_attribute(
mditem, cfattr_nonexistent
)
end
elseif Sys.isbsd()
# TODO: should we not only support FreeBSD? Are we testing on other BSD systems? OpenBSD?
Expand Down Expand Up @@ -86,7 +90,8 @@ using Test
end
end

rm(p); rm(p′)
rm(p)
rm(p′)
elseif Sys.iswindows()
@testset "HiddenFiles.jl—Windows" begin
@test !ishidden("C:\\Windows\\system32\\")
Expand All @@ -104,21 +109,24 @@ using Test
end
end


@testset "HiddenFiles.jl—Path Handling (PathStruct)" begin
@static if Sys.isunix()
bin_rp = Sys.islinux() ? "/usr/bin" : "/bin"

@test HiddenFiles.PathStruct("/bin", bin_rp) isa HiddenFiles.PathStruct
@test HiddenFiles.PathStruct("/../bin", bin_rp) isa HiddenFiles.PathStruct
@test_throws HiddenFiles.InvalidRealPathError HiddenFiles.PathStruct("/bin", "/../bin")
@test_throws HiddenFiles.InvalidRealPathError HiddenFiles.PathStruct(
"/bin", "/../bin"
)
@test HiddenFiles.PathStruct("/../bin").realpath == bin_rp
@test HiddenFiles.PathStruct(".").path == "."

elseif Sys.iswindows()
@test HiddenFiles.PathStruct("C:\\", "C:\\") isa HiddenFiles.PathStruct
@test HiddenFiles.PathStruct("C:\\..\\", "C:\\") isa HiddenFiles.PathStruct
@test_throws HiddenFiles.InvalidRealPathError HiddenFiles.PathStruct("C:\\", "C:\\..\\")
@test_throws HiddenFiles.InvalidRealPathError HiddenFiles.PathStruct(
"C:\\", "C:\\..\\"
)
else
# TODO
@test false
Expand Down

0 comments on commit d7f6acc

Please sign in to comment.