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
)
85 fun run (program
, argv
) =
87 val proc
= Unix
.execute (program
, argv
)
90 case TextIO.inputLine inf
of
92 | SOME line
=> loop inf
93 (* Programs that output will fail unless we eat their output
*)
94 val () = loop (Unix
.textInstreamOf proc
)
96 OS
.Process
.isSuccess (Unix
.reap proc
)
101 val proc
= Unix
.execute ("/bin/bash", ["-c", String.concat ss ^
" 2>&1"])
102 val inf
= Unix
.textInstreamOf proc
105 case TextIO.inputLine inf
of
106 NONE
=> String.concat (rev out
)
107 | SOME line
=> loop (line
:: out
)
112 if OS
.Process
.isSuccess (Unix
.reap proc
) then
120 val inf
= TextIO.openIn
"/etc/hostname"
122 case TextIO.inputLine inf
of
123 NONE
=> (TextIO.closeIn inf
; raise Fail
"No line in /etc/hostname")
124 | SOME line
=> (TextIO.closeIn inf
; String.substring (line
, 0, size line
- 1))
127 fun concatTo p fname
=
131 val dir
= Posix
.FileSys
.opendir dname
134 case Posix
.FileSys
.readdir dir
of
135 NONE
=> Posix
.FileSys
.closedir dir
138 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
140 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
142 else if p fname
' then
143 shellF ([Config
.cat
, " ", path
, " >>", fname
],
144 fn cl
=> "Error concatenating: " ^ cl
)
153 TextIO.closeOut (TextIO.openOut fname
);
154 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()})
157 fun enumerateTo p sep fname
=
159 val outf
= TextIO.openOut fname
162 val baseLen
= length (String.fields (fn ch
=> ch
= #
"/") Config
.resultRoot
) + 1
166 val dir
= Posix
.FileSys
.opendir dname
169 case Posix
.FileSys
.readdir dir
of
170 NONE
=> Posix
.FileSys
.closedir dir
173 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
175 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
177 else if p fname
' then
179 val toks
= String.fields (fn ch
=> ch
= #
"/") dname
180 val toks
= List.drop (toks
, baseLen
)
181 val dom
= String.concatWith
"." (rev toks
)
186 TextIO.output (outf
, sep
);
187 TextIO.output (outf
, dom
)
197 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()});
203 val inf
= TextIO.openIn fname
206 case TextIO.inputLine inf
of
208 | SOME line
=> loop (String.substring (line
, 0, size line
- 1) :: acc
)
211 before TextIO.closeIn inf
214 fun writeList (fname
, ls
) =
216 val outf
= TextIO.openOut fname
218 app (fn s
=> (TextIO.output (outf
, s
);
219 TextIO.output1 (outf
, #
"\n"))) ls
;
223 fun lineInFile fname line
=
225 val inf
= TextIO.openIn fname
226 val line
' = line ^
"\n"
229 case TextIO.inputLine inf
of
231 | SOME line
=> line
= line
' orelse loop ()
234 before TextIO.closeIn inf
235 end handle IO
.Io _
=> false
237 fun inGroup
{user
, group
} =
238 List.exists (fn x
=> x
= user
)
239 (Posix
.SysDB
.Group
.members (Posix
.SysDB
.getgrnam group
))
240 handle OS
.SysErr _
=> false
242 fun mkDirAll dir
= ignore (OS
.Process
.system ("mkdir -p " ^ dir
))
244 fun remove (ls
, x
) = List.filter (fn y
=> y
<> x
) ls
245 fun removeDups ls
= List.foldr (fn (x
, ls
) =>
246 if List.exists (fn y
=> y
= x
) ls
then
251 fun moveDirCreate
{from
, to
} =
253 if Posix
.FileSys
.access (from
, []) then
254 (ignore (OS
.Process
.system ("rm -rf " ^ to
));
255 ignore (OS
.Process
.system ("cp -r " ^ from ^
" " ^ to
));
256 ignore (OS
.Process
.system ("rm -rf " ^ from
)))