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 inf
= TextIO.openIn
"/etc/hostname"
89 case TextIO.inputLine inf
of
90 NONE
=> (TextIO.closeIn inf
; raise Fail
"No line in /etc/hostname")
91 | SOME line
=> (TextIO.closeIn inf
; String.substring (line
, 0, size line
- 1))
94 fun concatTo p fname
=
98 val dir
= Posix
.FileSys
.opendir dname
101 case Posix
.FileSys
.readdir dir
of
102 NONE
=> Posix
.FileSys
.closedir dir
105 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
107 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
109 else if p fname
' then
110 shellF ([Config
.cat
, " ", path
, " >>", fname
],
111 fn cl
=> "Error concatenating: " ^ cl
)
120 TextIO.closeOut (TextIO.openOut fname
);
121 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()})
124 fun enumerateTo p sep fname
=
126 val outf
= TextIO.openOut fname
129 val baseLen
= length (String.fields (fn ch
=> ch
= #
"/") Config
.resultRoot
) + 1
133 val dir
= Posix
.FileSys
.opendir dname
136 case Posix
.FileSys
.readdir dir
of
137 NONE
=> Posix
.FileSys
.closedir dir
140 val path
= OS
.Path
.joinDirFile
{dir
= dname
, file
= fname
'}
142 if Posix
.FileSys
.ST
.isDir (Posix
.FileSys
.stat path
) then
144 else if p fname
' then
146 val toks
= String.fields (fn ch
=> ch
= #
"/") dname
147 val toks
= List.drop (toks
, baseLen
)
148 val dom
= String.concatWith
"." (rev toks
)
153 TextIO.output (outf
, sep
);
154 TextIO.output (outf
, dom
)
164 visitDir (OS
.Path
.joinDirFile
{dir
= Config
.resultRoot
, file
= hostname ()});
170 val inf
= TextIO.openIn fname
173 case TextIO.inputLine inf
of
175 | SOME line
=> loop (String.substring (line
, 0, size line
- 1) :: acc
)
178 before TextIO.closeIn inf
181 fun writeList (fname
, ls
) =
183 val outf
= TextIO.openOut fname
185 app (fn s
=> (TextIO.output (outf
, s
);
186 TextIO.output1 (outf
, #
"\n"))) ls
;
190 fun lineInFile fname line
=
192 val inf
= TextIO.openIn fname
193 val line
' = line ^
"\n"
196 case TextIO.inputLine inf
of
198 | SOME line
=> line
= line
' orelse loop ()
201 before TextIO.closeIn inf
202 end handle IO
.Io _
=> false
204 fun inGroup
{user
, group
} =
205 List.exists (fn x
=> x
= user
)
206 (Posix
.SysDB
.Group
.members (Posix
.SysDB
.getgrnam group
))
207 handle OS
.SysErr _
=> false
209 fun mkDirAll dir
= ignore (OS
.Process
.system ("mkdir -p " ^ dir
))
211 fun remove (ls
, x
) = List.filter (fn y
=> y
<> x
) ls
212 fun removeDups ls
= List.foldr (fn (x
, ls
) =>
213 if List.exists (fn y
=> y
= x
) ls
then
218 fun moveDirCreate
{from
, to
} =
220 if Posix
.FileSys
.access (from
, []) then
221 (ignore (OS
.Process
.system ("rm -rf " ^ to
));
222 ignore (OS
.Process
.system ("cp -r " ^ from ^
" " ^ to
));
223 ignore (OS
.Process
.system ("rm -rf " ^ from
)))