Fix regeneration of multi-file dependencies
[hcoop/domtool2.git] / src / slave.sml
1 (* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19 (* Code for receiving and executing configuration files *)
20
21 structure Slave :> SLAVE = struct
22
23 datatype file_action =
24 Add
25 | Delete of bool
26 | Modify
27
28 fun isDelete (Delete _) = true
29 | isDelete _ = false
30
31 type file_status = {action : file_action,
32 domain : string,
33 dir : string,
34 file : string}
35
36 val fileHandler = ref (fn _ : file_status => ())
37 val preHandler = ref (fn () => ())
38 val postHandler = ref (fn () => ())
39
40 fun registerFileHandler handler =
41 let
42 val old = !fileHandler
43 in
44 fileHandler := (fn x => (handler x; old x))
45 end
46
47 fun registerPreHandler handler =
48 let
49 val old = !preHandler
50 in
51 preHandler := (fn () => (handler (); old ()))
52 end
53
54 fun registerPostHandler handler =
55 let
56 val old = !postHandler
57 in
58 postHandler := (fn () => (handler (); old ()))
59 end
60
61 fun handleChanges fs = (!preHandler ();
62 app (fn recd as {action, file, ...} =>
63 (!fileHandler recd;
64 case action of
65 Delete b =>
66 if b andalso Posix.FileSys.access (file, []) then
67 OS.FileSys.remove file
68 else
69 ()
70 | _ => ())) fs;
71 !postHandler ())
72
73 fun shell ss = OS.Process.isSuccess (OS.Process.system (String.concat ss))
74
75 fun shellF (ss, msg) =
76 let
77 val s = String.concat ss
78 in
79 if OS.Process.isSuccess (OS.Process.system s) then
80 ()
81 else
82 ErrorMsg.error NONE (msg s)
83 end
84
85 fun shellOutput ss =
86 let
87 val proc = Unix.execute ("/bin/bash", ["-c", String.concat ss ^ " 2>&1"])
88 val inf = Unix.textInstreamOf proc
89
90 fun loop out =
91 case TextIO.inputLine inf of
92 NONE => String.concat (rev out)
93 | SOME line => loop (line :: out)
94
95 val lines = loop []
96 in
97 print lines;
98 if OS.Process.isSuccess (Unix.reap proc) then
99 NONE
100 else
101 SOME lines
102 end
103
104 fun hostname () =
105 let
106 val inf = TextIO.openIn "/etc/hostname"
107 in
108 case TextIO.inputLine inf of
109 NONE => (TextIO.closeIn inf; raise Fail "No line in /etc/hostname")
110 | SOME line => (TextIO.closeIn inf; String.substring (line, 0, size line - 1))
111 end
112
113 fun concatTo p fname =
114 let
115 fun visitDir dname =
116 let
117 val dir = Posix.FileSys.opendir dname
118
119 fun loop () =
120 case Posix.FileSys.readdir dir of
121 NONE => Posix.FileSys.closedir dir
122 | SOME fname' =>
123 let
124 val path = OS.Path.joinDirFile {dir = dname, file = fname'}
125 in
126 if Posix.FileSys.ST.isDir (Posix.FileSys.stat path) then
127 visitDir path
128 else if p fname' then
129 shellF ([Config.cat, " ", path, " >>", fname],
130 fn cl => "Error concatenating: " ^ cl)
131 else
132 ();
133 loop ()
134 end
135 in
136 loop ()
137 end
138 in
139 TextIO.closeOut (TextIO.openOut fname);
140 visitDir (OS.Path.joinDirFile {dir = Config.resultRoot, file = hostname ()})
141 end
142
143 fun enumerateTo p sep fname =
144 let
145 val outf = TextIO.openOut fname
146
147 val first = ref true
148 val baseLen = length (String.fields (fn ch => ch = #"/") Config.resultRoot) + 1
149
150 fun visitDir dname =
151 let
152 val dir = Posix.FileSys.opendir dname
153
154 fun loop () =
155 case Posix.FileSys.readdir dir of
156 NONE => Posix.FileSys.closedir dir
157 | SOME fname' =>
158 let
159 val path = OS.Path.joinDirFile {dir = dname, file = fname'}
160 in
161 if Posix.FileSys.ST.isDir (Posix.FileSys.stat path) then
162 visitDir path
163 else if p fname' then
164 let
165 val toks = String.fields (fn ch => ch = #"/") dname
166 val toks = List.drop (toks, baseLen)
167 val dom = String.concatWith "." (rev toks)
168 in
169 if !first then
170 first := false
171 else
172 TextIO.output (outf, sep);
173 TextIO.output (outf, dom)
174 end
175 else
176 ();
177 loop ()
178 end
179 in
180 loop ()
181 end
182 in
183 visitDir (OS.Path.joinDirFile {dir = Config.resultRoot, file = hostname ()});
184 TextIO.closeOut outf
185 end
186
187 fun readList fname =
188 let
189 val inf = TextIO.openIn fname
190
191 fun loop acc =
192 case TextIO.inputLine inf of
193 NONE => rev acc
194 | SOME line => loop (String.substring (line, 0, size line - 1) :: acc)
195 in
196 loop []
197 before TextIO.closeIn inf
198 end
199
200 fun writeList (fname, ls) =
201 let
202 val outf = TextIO.openOut fname
203 in
204 app (fn s => (TextIO.output (outf, s);
205 TextIO.output1 (outf, #"\n"))) ls;
206 TextIO.closeOut outf
207 end
208
209 fun lineInFile fname line =
210 let
211 val inf = TextIO.openIn fname
212 val line' = line ^ "\n"
213
214 fun loop () =
215 case TextIO.inputLine inf of
216 NONE => false
217 | SOME line => line = line' orelse loop ()
218 in
219 loop ()
220 before TextIO.closeIn inf
221 end handle IO.Io _ => false
222
223 fun inGroup {user, group} =
224 List.exists (fn x => x = user)
225 (Posix.SysDB.Group.members (Posix.SysDB.getgrnam group))
226 handle OS.SysErr _ => false
227
228 fun mkDirAll dir = ignore (OS.Process.system ("mkdir -p " ^ dir))
229
230 fun remove (ls, x) = List.filter (fn y => y <> x) ls
231 fun removeDups ls = List.foldr (fn (x, ls) =>
232 if List.exists (fn y => y = x) ls then
233 ls
234 else
235 x :: ls) [] ls
236
237 fun moveDirCreate {from, to} =
238 (mkDirAll to;
239 if Posix.FileSys.access (from, []) then
240 (ignore (OS.Process.system ("rm -rf " ^ to));
241 ignore (OS.Process.system ("cp -r " ^ from ^ " " ^ to));
242 ignore (OS.Process.system ("rm -rf " ^ from)))
243 else
244 ())
245
246 end