1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
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
.
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
.
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
.
19 (* Code for receiving
and executing configuration files
*)
21 structure Slave
:> SLAVE
= struct
23 datatype file_action
=
28 fun isDelete (Delete _
) = true
31 type file_status
= {action
: file_action
,
36 val fileHandler
= ref (fn _
: file_status
=> ())
37 val preHandler
= ref (fn () => ())
38 val postHandler
= ref (fn () => ())
40 fun registerFileHandler handler
=
42 val old
= !fileHandler
44 fileHandler
:= (fn x
=> (handler x
; old x
))
47 fun registerPreHandler handler
=
51 preHandler
:= (fn () => (handler (); old ()))
54 fun registerPostHandler handler
=
56 val old
= !postHandler
58 postHandler
:= (fn () => (handler (); old ()))
61 fun handleChanges fs
= (!preHandler ();
62 app (fn recd
as {action
, file
, ...} =>
66 if b
andalso Posix
.FileSys
.access (file
, []) then
67 OS
.FileSys
.remove file
73 fun shell ss
= OS
.Process
.isSuccess (OS
.Process
.system (String.concat ss
))
75 fun shellF (ss
, msg
) =
77 val s
= String.concat ss
79 if OS
.Process
.isSuccess (OS
.Process
.system s
) then
82 ErrorMsg
.error
NONE (msg s
)
87 val proc
= Unix
.execute ("/bin/bash", ["-c", String.concat ss ^
" 2>&1"])
88 val inf
= Unix
.textInstreamOf proc
91 case TextIO.inputLine inf
of
92 NONE
=> String.concat (rev out
)
93 | SOME line
=> loop (line
:: out
)
98 if OS
.Process
.isSuccess (Unix
.reap proc
) then
106 val inf
= TextIO.openIn
"/etc/hostname"
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))
113 fun concatTo p fname
=
117 val dir
= Posix
.FileSys
.opendir dname
120 case Posix
.FileSys
.readdir dir
of
121 NONE
=> Posix
.FileSys
.closedir dir
124 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
126 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
128 else if p fname
' then
129 shellF ([Config
.cat
, " ", path
, " >>", fname
],
130 fn cl
=> "Error concatenating: " ^ cl
)
139 TextIO.closeOut (TextIO.openOut fname
);
140 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()})
143 fun enumerateTo p sep fname
=
145 val outf
= TextIO.openOut fname
148 val baseLen
= length (String.fields (fn ch
=> ch
= #
"/") Config
.resultRoot
) + 1
152 val dir
= Posix
.FileSys
.opendir dname
155 case Posix
.FileSys
.readdir dir
of
156 NONE
=> Posix
.FileSys
.closedir dir
159 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
161 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
163 else if p fname
' then
165 val toks
= String.fields (fn ch
=> ch
= #
"/") dname
166 val toks
= List.drop (toks
, baseLen
)
167 val dom
= String.concatWith
"." (rev toks
)
172 TextIO.output (outf
, sep
);
173 TextIO.output (outf
, dom
)
183 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()});
189 val inf
= TextIO.openIn fname
192 case TextIO.inputLine inf
of
194 | SOME line
=> loop (String.substring (line
, 0, size line
- 1) :: acc
)
197 before TextIO.closeIn inf
200 fun writeList (fname
, ls
) =
202 val outf
= TextIO.openOut fname
204 app (fn s
=> (TextIO.output (outf
, s
);
205 TextIO.output1 (outf
, #
"\n"))) ls
;
209 fun lineInFile fname line
=
211 val inf
= TextIO.openIn fname
212 val line
' = line ^
"\n"
215 case TextIO.inputLine inf
of
217 | SOME line
=> line
= line
' orelse loop ()
220 before TextIO.closeIn inf
221 end handle IO
.Io _
=> false
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
228 fun mkDirAll dir
= ignore (OS
.Process
.system ("mkdir -p " ^ dir
))
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
237 fun moveDirCreate
{from
, 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
)))