-
Notifications
You must be signed in to change notification settings - Fork 54
/
posix.stat.m
253 lines (210 loc) · 6.36 KB
/
posix.stat.m
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2001, 2004, 2007 The University of Melbourne.
% Copyright (C) 2018-2019 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% Module: posix.stat.
% Main author: Michael Day <miked@lendtech.com.au>
%
%-----------------------------------------------------------------------------%
:- module posix.stat.
:- interface.
:- import_module string.
:- import_module time.
%-----------------------------------------------------------------------------%
:- type file_type
---> file
; directory
; symbolic_link
; character_device
; block_device
; fifo
; unknown.
:- type stat.
:- func dev(stat) = dev_t.
:- func ino(stat) = ino_t.
:- func mode(stat) = mode_t.
:- func file_type(stat) = posix.stat.file_type.
:- func nlink(stat) = nlink_t.
:- func uid(stat) = uid_t.
:- func gid(stat) = gid_t.
:- func rdev(stat) = dev_t.
:- func size(stat) = off_t.
:- func blksize(stat) = blksize_t.
:- func blocks(stat) = blkcnt_t.
:- func atime(stat) = time_t.
:- func mtime(stat) = time_t.
:- func ctime(stat) = time_t.
:- pred stat(string::in, posix.result(stat)::out, io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- pragma foreign_decl("C", "
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
").
%-----------------------------------------------------------------------------%
:- interface.
:- pragma foreign_type("C", stat, "struct stat *", [can_pass_as_mercury_type]).
%-----------------------------------------------------------------------------%
:- implementation.
%-----------------------------------------------------------------------------%
stat(Path, Result, !IO) :-
stat0(Path, Res, Stat, !IO),
( if Res = 0 then
Result = ok(Stat)
else
errno(Err, !IO),
Result = error(Err)
).
:- pred stat0(string::in, int::out, stat::out, io::di, io::uo) is det.
:- pragma foreign_proc("C",
stat0(Path::in, Res::out, Stat::out, _IO0::di, _IO::uo),
[promise_pure, will_not_call_mercury, thread_safe, tabled_for_io],
"
Stat = MR_GC_NEW(struct stat);
do {
Res = stat(Path, Stat);
} while (Res == -1 && MR_is_eintr(errno));
").
%-----------------------------------------------------------------------------%
file_type(Stat) =
( if is_slnk(Mode) then symbolic_link
else if is_reg(Mode) then file
else if is_dir(Mode) then directory
else if is_chr(Mode) then character_device
else if is_blk(Mode) then block_device
else if is_fifo(Mode) then fifo
else unknown ) :- Mode = Stat ^ (mode).
:- pred is_slnk(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_slnk(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISLNK(Mode);
").
:- pred is_reg(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_reg(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISREG(Mode);
").
:- pred is_dir(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_dir(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISDIR(Mode);
").
:- pred is_chr(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_chr(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISCHR(Mode);
").
:- pred is_blk(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_blk(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISBLK(Mode);
").
:- pred is_fifo(mode_t::in) is semidet.
:- pragma foreign_proc("C",
is_fifo(Mode::in),
[promise_pure, will_not_call_mercury, thread_safe],
"
SUCCESS_INDICATOR = S_ISFIFO(Mode);
").
%-----------------------------------------------------------------------------%
:- pragma foreign_proc("C",
dev(S::in) = (Dev::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Dev = S->st_dev;
").
:- pragma foreign_proc("C",
ino(S::in) = (Ino::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Ino = S->st_ino;
").
:- pragma foreign_proc("C",
mode(S::in) = (Mode::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Mode = S->st_mode;
").
:- pragma foreign_proc("C",
nlink(S::in) = (Nlink::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Nlink = S->st_nlink;
").
:- pragma foreign_proc("C",
uid(S::in) = (Uid::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Uid = S->st_uid;
").
:- pragma foreign_proc("C",
gid(S::in) = (Gid::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Gid = S->st_gid;
").
:- pragma foreign_proc("C",
rdev(S::in) = (Rdev::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Rdev = S->st_rdev;
").
size(S) = off(integer(size0(S))).
:- func size0(stat) = int.
:- pragma foreign_proc("C",
size0(S::in) = (Size::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Size = S->st_size;
").
:- pragma foreign_proc("C",
blksize(S::in) = (Blksize::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Blksize = S->st_blksize;
").
blocks(S) = blkcnt(integer(blocks0(S))).
:- func blocks0(stat) = int.
:- pragma foreign_proc("C",
blocks0(S::in) = (Blocks::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Blocks = S->st_blocks;
").
:- pragma foreign_proc("C",
atime(S::in) = (Atime::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Atime = S->st_atime;
").
:- pragma foreign_proc("C",
mtime(S::in) = (Mtime::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Mtime = S->st_mtime;
").
:- pragma foreign_proc("C",
ctime(S::in) = (Ctime::out),
[promise_pure, will_not_call_mercury, thread_safe],
"
Ctime = S->st_ctime;
").
%------------------------------------------------------------------------------%
:- end_module posix.stat.
%------------------------------------------------------------------------------%