-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
info.cr
146 lines (123 loc) · 3.57 KB
/
info.cr
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require "crystal/system/file_info"
class File
# Represents the various behaviour-altering flags which can be set on files.
# Not all flags will be supported on all platforms.
@[::Flags]
enum Flags : UInt8
SetUser
SetGroup
Sticky
end
# Represents the type of a file. Not all types will be supported on all
# platforms.
enum Type : UInt8
File
Directory
Symlink
Socket
Pipe
CharacterDevice
BlockDevice
Unknown
# Returns true if `self` is a `CharacterDevice` or a `BlockDevice`.
def device?
character_device? || block_device?
end
end
# Represents a set of access permissions for a file. Not all permission sets
# will be supported on all platforms.
#
# The binary representation of this enum is defined to be same representation
# as the permission bits of a unix `st_mode` field. `File::Permissions`
# can also be compared to its underlying bitset, for example
# `File::Permissions::All == 0o777` will always be `true`.
#
# On windows, only the `OwnerWrite` bit is effective. All file permissions
# will either be `0o444` for read-only files or `0o666` for read-write files.
# Directories are always mode `0o555` for read-only or `0o777`.
@[::Flags]
enum Permissions : Int16
OtherExecute = 0o001
OtherWrite = 0o002
OtherRead = 0o004
OtherAll = 0o007
GroupExecute = 0o010
GroupWrite = 0o020
GroupRead = 0o040
GroupAll = 0o070
OwnerExecute = 0o100
OwnerWrite = 0o200
OwnerRead = 0o400
OwnerAll = 0o700
def self.new(int : Int)
new(int.to_i16)
end
def to_s(io : IO) : Nil
io << (owner_read? ? 'r' : '-')
io << (owner_write? ? 'w' : '-')
io << (owner_execute? ? 'x' : '-')
io << (group_read? ? 'r' : '-')
io << (group_write? ? 'w' : '-')
io << (group_execute? ? 'x' : '-')
io << (other_read? ? 'r' : '-')
io << (other_write? ? 'w' : '-')
io << (other_execute? ? 'x' : '-')
io << " (0o" << self.to_i.to_s(8) << ')'
end
end
# A `File::Info` contains metadata regarding a file.
# It is returned by `File.info`, `File#info` and `File.info?`.
struct Info
include Crystal::System::FileInfo
# Size of the file, in bytes.
def size : Int64
system_size
end
# The permissions of the file.
def permissions : Permissions
system_permissions
end
# The type of the file.
def type : Type
system_type
end
# The special flags this file has set.
def flags : Flags
system_flags
end
# The last time this file was modified.
def modification_time : Time
system_modification_time
end
# The user ID that the file belongs to.
def owner_id : String
system_owner_id
end
# The group ID that the file belongs to.
def group_id : String
system_group_id
end
# Returns true if this `Info` and *other* are of the same file.
#
# On Unix-like systems, this compares device and inode fields, and will
# compare equal for hard linked files.
def same_file?(other : self) : Bool
system_same_file?(other)
end
# Returns true if this `Info` represents a standard file. Shortcut for
# `type.file?`.
def file?
type.file?
end
# Returns true if this `Info` represents a directory. Shortcut for
# `type.directory?`.
def directory?
type.directory?
end
# Returns true if this `Info` represents a symbolic link to another file.
# Shortcut for `type.symlink?`.
def symlink?
type.symlink?
end
end
end