Allow some of a user's config to survive regen, even when some doesn't type-check
[hcoop/domtool2.git] / src / main.sml
index ec795e3..d769a61 100644 (file)
@@ -883,13 +883,70 @@ fun requestDescribe dom =
          | SOME m =>
            case m of
                MsgDescription s => print s
-             | MsgError s => print ("Describe failed: " ^ s ^ "\n")
+             | MsgError s => print ("Description failed: " ^ s ^ "\n")
              | _ => print "Unexpected server reply.\n";
        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 +958,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
  
@@ -957,9 +1018,11 @@ fun regenerateEither tc checker context =
                    in
                        if !ErrorMsg.anyErrors then
                            (ErrorMsg.reset ();
-                            print ("User " ^ user ^ "'s configuration has errors!\n"))
+                            print ("User " ^ user ^ "'s configuration has errors!\n");
+                            ok := false)
                        else
-                           app checker files
+                           ();
+                       app checker files
                    end
                else
                    ()
@@ -978,7 +1041,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
 
@@ -1433,13 +1511,17 @@ fun service () =
                                      (fn () => ())
 
                               | MsgDescribe dom =>
-                                doIt (fn () => (if Domain.validDomain dom then
-                                                    (Msg.send (bio, MsgDescription (Domain.describe dom));
-                                                     ("Requested description of domain " ^ dom,
-                                                      NONE))
-                                                else
-                                                    ("Requested description of invalid domain " ^ dom,
-                                                     SOME "Invalid domain name")))
+                                doIt (fn () => if not (Domain.validDomain dom) then
+                                                   ("Requested description of invalid domain " ^ dom,
+                                                    SOME "Invalid domain name")
+                                               else if not (Domain.yourDomain dom
+                                                            orelse Acl.query {user = user, class = "priv", value = "all"}) then
+                                                   ("Requested description of " ^ dom ^ ", but not allowed access",
+                                                    SOME "Access denied")
+                                               else
+                                                   (Msg.send (bio, MsgDescription (Domain.describe dom));
+                                                    ("Sent description of domain " ^ dom,
+                                                     NONE)))
                                      (fn () => ())
 
                               | _ =>