-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinotify.adb
More file actions
172 lines (152 loc) · 6.28 KB
/
inotify.adb
File metadata and controls
172 lines (152 loc) · 6.28 KB
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
package body inotify is
function mask_in_mask (a, b : mask_t) return boolean is
use type interfaces.unsigned_32;
begin
return (a and b) = a;
end mask_in_mask;
function "*" (a, b : mask_t) return boolean is
begin
return mask_in_mask(a, b);
end "*";
function mask_to_mask (a, b : mask_t) return mask_t is
use type interfaces.unsigned_32;
begin
return a or b;
end mask_to_mask;
function "+" (a, b : mask_t) return mask_t is
begin
return mask_to_mask(a, b);
end "+";
function "=" (a, b : descriptor_t) return boolean is
use type interfaces.c_streams.files;
begin
return a.file = b.file;
end "=";
function get_event (handle : descriptor_t) return event_t is
use type ada.strings.unbounded.unbounded_string;
use type system.crtl.size_t;
size : system.crtl.size_t;
e : event;
name : interfaces.c.char_array(0..MAXFILENAME);
result : event_t;
begin
size := interfaces.c_streams.fread(
header_buf'address, system.crtl.size_t(event_io.buffer_size), 1, handle.file);
if size /= 1 then
if interfaces.c_streams.ferror(handle.file) /= 0 then
ada.exceptions.raise_exception(error_badf'identity, "[Read]");
end if;
return event_null;
end if;
event_io.read(header_buf, e);
size := interfaces.c_streams.fread(
name'address, 1, system.crtl.size_t(e.length), handle.file);
if size /= system.crtl.size_t(e.length) then
if interfaces.c_streams.ferror(handle.file) /= 0 then
ada.exceptions.raise_exception(error_badf'identity, "[Read]");
end if;
return event_null;
end if;
result.wd := watch_descriptor_t(e.watch_descriptor);
result.mask := mask_t(e.mask);
result.cookie := cookie_t(e.cookie);
result.name := ada.strings.unbounded.to_unbounded_string(
interfaces.c.strings.value(interfaces.c.strings.new_char_array(name), interfaces.c.size_t(e.length)));
return result;
end get_event;
function add_watch (handle : descriptor_t; path : string; mask : mask_t) return watch_descriptor_t is
wd : watch_descriptor_t;
error : error_t;
begin
if path'length > natural(MAXFILENAME) then
ada.exceptions.raise_exception(error_nametoolong'identity, "[Rm] name too long. code: " & ENAMETOOLONG'img);
end if;
wd := inotify_add_watch(
interfaces.c_streams.fileno(handle.file),
interfaces.c.strings.new_string(path), mask);
if wd = -1 then
error := error_t(gnat.os_lib.errno);
if error = EACCES then
ada.exceptions.raise_exception(error_acces'identity, "[Add] not permitted. code: " & error'img);
elsif error = EBADF then
ada.exceptions.raise_exception(error_badf'identity, "[Add] bad fd. code: " & error'img);
elsif error = EFAULT then
ada.exceptions.raise_exception(error_fault'identity, "[Add] pathname outside. code: " & error'img);
elsif error = EINVAL then
ada.exceptions.raise_exception(error_inval'identity, "[Add] no valid mask. code: " & error'img);
elsif error = ENAMETOOLONG then
ada.exceptions.raise_exception(error_nametoolong'identity, "[Add] name too long. code: " & error'img);
elsif error = ENOENT then
ada.exceptions.raise_exception(error_noent'identity, "[Add] does not exist or is a symlink. code: " & error'img);
elsif error = ENOMEM then
ada.exceptions.raise_exception(error_nomem'identity, "[Add] memory. code: " & error'img);
elsif error = ENOSPC then
ada.exceptions.raise_exception(error_nospc'identity, "[Add] limit. code: " & error'img);
else
ada.exceptions.raise_exception(error_unknown'identity, "[Add] unknown. code: " & error'img);
end if;
end if;
return wd;
end add_watch;
procedure rm_watch (handle : descriptor_t; wd : watch_descriptor_t) is
error : error_t;
begin
if inotify_rm_watch(interfaces.c_streams.fileno(handle.file), wd) = -1 then
error := error_t(gnat.os_lib.errno);
if error = EINVAL then
ada.exceptions.raise_exception(error_inval'identity, "[Rm] bad wd. code: " & error'img);
elsif error = EBADF then
ada.exceptions.raise_exception(error_badf'identity, "[Rm] bad fd. code: " & error'img);
else
ada.exceptions.raise_exception(error_unknown'identity, "[Init] unknown. code: " & error'img);
end if;
end if;
end rm_watch;
function init (nonblock : boolean := false; cloexec : boolean := false) return descriptor_t is
use type interfaces.c_streams.files;
fd : integer;
mode : string := "r";
handle : descriptor_t;
flags : mask_t := 0;
error : error_t;
begin
if nonblock then
flags := flags + IN_NONBLOCK;
end if;
if cloexec then
flags := flags + IN_CLOEXEC;
end if;
fd := inotify_init1(mask_t(flags));
if fd = -1 then
error := error_t(gnat.os_lib.errno);
if error = EINVAL then
ada.exceptions.raise_exception(error_inval'identity, "[Init] bad flags. code: " & error'img);
elsif error = EMFILE then
ada.exceptions.raise_exception(error_mfile'identity, "[Init] user limit. code: " & error'img);
elsif error = ENFILE then
ada.exceptions.raise_exception(error_nfile'identity, "[Init] system limit. code: " & error'img);
elsif error = ENOMEM then
ada.exceptions.raise_exception(error_nomem'identity, "[Init] memory. code: " & error'img);
else
ada.exceptions.raise_exception(error_unknown'identity, "[Init] unknown. code: " & error'img);
end if;
end if;
handle.file := interfaces.c_streams.fdopen(fd, mode'address);
if handle.file = interfaces.c_streams.NULL_stream then
error := error_t(gnat.os_lib.errno);
if error = EINVAL then
ada.exceptions.raise_exception(error_inval'identity, "[Init] bad flags. code: " & error'img);
else
ada.exceptions.raise_exception(error_unknown'identity, "[Init] unknown. code: " & error'img);
end if;
end if;
return handle;
end init;
procedure close (handle : descriptor_t) is
begin
if interfaces.c_streams.fclose(handle.file) /= 0 then
ada.exceptions.raise_exception(error_close'identity, "[Close] stream. code: " & error_t(gnat.os_lib.errno)'img);
end if;
gnat.os_lib.close(gnat.os_lib.file_descriptor(interfaces.c_streams.fileno(handle.file)));
end close;
end inotify;