Report when users' ~/.domtool directories are unreadable
[hcoop/domtool2.git] / src / main.sml
index 8cc5a3a..eaee4fc 100644 (file)
@@ -33,7 +33,8 @@ fun check' G fname =
        if !ErrorMsg.anyErrors then
            G
        else
-           Tycheck.checkFile G (Defaults.tInit ()) prog
+           (Option.app (Unused.check G) (#3 prog);
+            Tycheck.checkFile G (Defaults.tInit ()) prog)
     end
 
 fun basis () =
@@ -87,7 +88,8 @@ fun check fname =
                        if !ErrorMsg.anyErrors then
                            raise ErrorMsg.Error
                        else
-                           (G', #3 prog)
+                           (Option.app (Unused.check b) (#3 prog);
+                            (G', #3 prog))
                    end
            end
     end
@@ -176,7 +178,7 @@ fun eval fname =
            raise ErrorMsg.Error
        else
            Eval.exec (Defaults.eInit ()) body'
-      | NONE => raise ErrorMsg.Error
+      | NONE => ()
 
 fun eval' fname =
     case reduce fname of
@@ -185,7 +187,7 @@ fun eval' fname =
            raise ErrorMsg.Error
        else
            ignore (Eval.exec' (Defaults.eInit ()) body')
-      | NONE => raise ErrorMsg.Error
+      | NONE => ()
 
 val dispatcher =
     Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
@@ -288,7 +290,7 @@ fun requestDir dname =
        val (_, files) = Order.order (SOME b) files
 
        val _ = if !ErrorMsg.anyErrors then
-                   raise ErrorMsg.Error
+                   (print "J\n";raise ErrorMsg.Error)
                else
                    ()
 
@@ -888,8 +890,65 @@ fun requestDescribe dom =
        OpenSSL.close bio
     end
 
+structure SS = StringSet
+
+fun domainList dname =
+    let
+       val dir = Posix.FileSys.opendir dname
+
+       fun visitNode dset =
+           case Posix.FileSys.readdir dir of
+               NONE => dset
+             | SOME node =>
+               let
+                   val path = OS.Path.joinDirFile {dir = dname,
+                                                   file = node}
+
+                   fun visitDomains (path, bfor, dset) =
+                       let
+                           val dir = Posix.FileSys.opendir path
+
+                           fun loop dset =
+                               case Posix.FileSys.readdir dir of
+                                   NONE => dset
+                                 | SOME dname =>
+                                   let
+                                       val path = OS.Path.joinDirFile {dir = path,
+                                                                       file = dname}
+                                   in
+                                       if Posix.FileSys.ST.isDir (Posix.FileSys.stat path) then
+                                           let
+                                               val bfor = dname :: bfor
+                                           in
+                                               loop (visitDomains (path, bfor,
+                                                                   SS.add (dset,
+                                                                           String.concatWith "." bfor)))
+                                           end
+                                       else
+                                           loop dset
+                                   end
+                       in
+                           loop dset
+                           before Posix.FileSys.closedir dir
+                       end
+               in
+                   visitNode (visitDomains (path, [], dset))
+               end
+    in
+       visitNode SS.empty
+       before Posix.FileSys.closedir dir
+    end
+
 fun regenerateEither tc checker context =
     let
+       val () = print "Starting regeneration....\n"
+
+       val domainsBefore =
+           if tc then
+               SS.empty
+           else
+               domainList Config.resultRoot
+
        fun ifReal f =
            if tc then
                ()
@@ -901,7 +960,11 @@ fun regenerateEither tc checker context =
        val b = basis ()
        val () = Tycheck.disallowExterns ()
 
-       val () = ifReal Domain.resetGlobal
+       val () = ifReal (fn () =>
+                           (ignore (OS.Process.system ("rm -rf " ^ Config.oldResultRoot ^ "/*"));
+                            ignore (OS.Process.system ("cp -r " ^ Config.resultRoot
+                                                       ^ "/* " ^ Config.oldResultRoot ^ "/"));
+                            Domain.resetGlobal ()))
 
        val ok = ref true
  
@@ -960,10 +1023,14 @@ fun regenerateEither tc checker context =
                             print ("User " ^ user ^ "'s configuration has errors!\n");
                             ok := false)
                        else
-                           app checker files
+                           ();
+                       app checker files
                    end
+               else if String.isSuffix "_admin" user then
+                   ()    
                else
-                   ()
+                   (print ("Couldn't access " ^ user ^ "'s ~/.domtool directory.\n");
+                    ok := false)
            end
            handle IO.Io {name, function, ...} =>
                   (print ("IO error processing user " ^ user ^ ": " ^ function ^ ": " ^ name ^ "\n");
@@ -979,7 +1046,22 @@ fun regenerateEither tc checker context =
        ifReal (fn () => (app contactNode Config.nodeIps;
                          Env.pre ()));
        app doUser (Acl.users ());
-       ifReal Env.post;
+       ifReal (fn () =>
+                  let
+                      val domainsAfter = domainList Config.resultRoot
+                      val domainsGone = SS.difference (domainsBefore, domainsAfter)
+                  in
+                      if SS.isEmpty domainsGone then
+                          ()
+                      else
+                          (print "Domains to kill:";
+                           SS.app (fn s => (print " "; print s)) domainsGone;
+                           print "\n";
+
+                           Domain.rmdom' Config.oldResultRoot (SS.listItems domainsGone));
+                      
+                      Env.post ()
+                  end);
        !ok
     end
 
@@ -1559,7 +1641,7 @@ fun slave () =
                          | _ => (OpenSSL.close bio;
                                  loop ())
                end handle OpenSSL.OpenSSL s =>
-                          (print ("OpenSSL error: "^ s ^ "\n");
+                          (print ("OpenSSL error: " ^ s ^ "\n");
                            OpenSSL.close bio
                                          handle OpenSSL.OpenSSL _ => ();
                            loop ())
@@ -1569,6 +1651,17 @@ fun slave () =
                            OpenSSL.close bio
                            handle OpenSSL.OpenSSL _ => ();
                            loop ())
+                        | IO.Io {function, name, ...} =>
+                          (print ("IO error: " ^ function ^ ": " ^ name ^ "\n");
+                           OpenSSL.close bio
+                           handle OpenSSL.OpenSSL _ => ();
+                           loop ())
+                        | e =>
+                          (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
+                           print "Uncaught exception!\n";
+                           OpenSSL.close bio
+                           handle OpenSSL.OpenSSL _ => ();
+                           loop ())
     in
        loop ();
        OpenSSL.shutdown sock