forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.jl
110 lines (94 loc) · 3.17 KB
/
stat.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
type Stat
device :: Uint
inode :: Uint
mode :: Uint
nlink :: Int
uid :: Uint
gid :: Uint
rdev :: Uint
size :: Int
blksize :: Int
blocks :: Int
mtime :: Float64
ctime :: Float64
end
Stat(buf::Vector{Uint8}) = Stat(
ccall(:jl_stat_dev, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_ino, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_mode, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_nlink, Int, (Ptr{Uint8},), buf),
ccall(:jl_stat_uid, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_uid, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_rdev, Uint, (Ptr{Uint8},), buf),
ccall(:jl_stat_size, Int, (Ptr{Uint8},), buf),
ccall(:jl_stat_blksize, Int, (Ptr{Uint8},), buf),
ccall(:jl_stat_blocks, Int, (Ptr{Uint8},), buf),
ccall(:jl_stat_mtime, Float64, (Ptr{Uint8},), buf),
ccall(:jl_stat_ctime, Float64, (Ptr{Uint8},), buf),
)
show(io::IO, st::Stat) = printf(f"Stat(mode=%06o,size=%i)\n", st.mode, st.size)
# stat & lstat functions
const _jl_stat_buf = Array(Uint8, ccall(:jl_sizeof_stat, Int, ()))
macro _jl_stat_call(sym,arg)
quote
fill!(_jl_stat_buf,0)
r = ccall($(expr(:quote,sym)), Int32, (Ptr{Uint8},Ptr{Uint8}), $arg, _jl_stat_buf)
system_error("stat", r!=0 && errno()!=ENOENT)
st = Stat(_jl_stat_buf)
if ispath(st) != (r==0)
error("WTF: stat returned zero type for a valid path!?")
end
st
end
end
stat(path::String) = @_jl_stat_call jl_stat path
stat(fd::Integer) = @_jl_stat_call jl_fstat fd
lstat(path::String) = @_jl_stat_call jl_lstat path
# mode type predicates
ispath(mode::Unsigned) = mode & 0xf000 != 0x0000
isfifo(mode::Unsigned) = mode & 0xf000 == 0x1000
ischardev(mode::Unsigned) = mode & 0xf000 == 0x2000
isdir(mode::Unsigned) = mode & 0xf000 == 0x4000
isblockdev(mode::Unsigned) = mode & 0xf000 == 0x6000
isfile(mode::Unsigned) = mode & 0xf000 == 0x8000
islink(mode::Unsigned) = mode & 0xf000 == 0xa000
issocket(mode::Unsigned) = mode & 0xf000 == 0xc000
# mode permission predicates
issetuid(mode::Unsigned) = (mode & 0x800) > 0
issetgid(mode::Unsigned) = (mode & 0x400) > 0
issticky(mode::Unsigned) = (mode & 0x200) > 0
isreadable(mode::Unsigned) = (mode & 0x124) > 0
iswriteable(mode::Unsigned) = (mode & 0x092) > 0
isexecutable(mode::Unsigned) = (mode & 0x049) > 0
uperm(mode::Unsigned) = uint8(mode >> 6) & 0x7
gperm(mode::Unsigned) = uint8(mode >> 3) & 0x7
operm(mode::Unsigned) = uint8(mode ) & 0x7
# mode predicate methods for file names & stat objects
for f in {
:ispath
:isfifo
:ischardev
:isdir
:isblockdev
:isfile
:islink
:issocket
:issetuid
:issetgid
:issticky
:isreadable
:iswriteable
:isexecutable
:uperm
:gperm
:operm
}
@eval ($f)(st::Stat) = ($f)(st.mode)
@eval ($f)(path::String) = ($f)(stat(path))
end
islink(path::String) = islink(lstat(path))
# some convenience functions
filemode(path::String) = stat(path).mode
filesize(path::String) = stat(path).size
mtime(path::String) = stat(path).mtime
ctime(path::String) = stat(path).ctime