- case findVhostUser (#file fs) of
- NONE => print ("Can't find user in " ^ #file fs ^ "! Taking no action.\n")
- | SOME user =>
- let
- val spl = OS.Path.splitDirFile (#file fs)
- in
- if String.isSuffix ".vhost" (#file spl)
- orelse String.isSuffix ".vhost_ssl" (#file spl) then
- (vhostsChanged := true;
- case #action fs of
- Slave.Delete =>
- let
- val {base, ...} = OS.Path.splitBaseExt (#file spl)
-
- val logname = OS.Path.joinDirFile
- {dir = Config.Apache.logDir,
- file = user}
- in
- ignore (OS.Process.system (Config.rm
- ^ " -rf "
- ^ Config.Apache.confDir
- ^ "/"
- ^ #file spl));
- Slave.writeList (logname,
- List.filter (fn s => s <> base)
- (Slave.readList logname))
- end
-
- | Slave.Add =>
- let
- val _ =
- OS.Process.system (Config.cp
- ^ " "
- ^ #file fs
- ^ " "
- ^ Config.Apache.confDir
- ^ "/"
- ^ #file spl)
-
- val {base, ...} = OS.Path.splitBaseExt (#file spl)
-
- val logname = OS.Path.joinDirFile
- {dir = Config.Apache.logDir,
- file = user}
-
- val outf = TextIO.openAppend logname
- in
- TextIO.output (outf, base);
- TextIO.output1 (outf, #"\n");
- TextIO.closeOut outf
- end
-
- | _ =>
- ignore (OS.Process.system (Config.cp
- ^ " "
- ^ #file fs
- ^ " "
- ^ Config.Apache.confDir
- ^ "/"
- ^ #file spl)))
- else
- ()
- end)
+ let
+ val spl = OS.Path.splitDirFile (#file fs)
+ in
+ if String.isSuffix ".vhost" (#file spl)
+ orelse String.isSuffix ".vhost_ssl" (#file spl) then let
+ val realVhostFile = OS.Path.joinDirFile
+ {dir = Config.Apache.confDir,
+ file = #file spl}
+
+ val user = findVhostUser (#file fs)
+ val oldUser = case #action fs of
+ Slave.Delete false => user
+ | _ => findVhostUser realVhostFile
+ in
+ if (oldUser = NONE andalso #action fs <> Slave.Add)
+ orelse (user = NONE andalso not (Slave.isDelete (#action fs))) then
+ print ("Can't find user in " ^ #file fs ^ " or " ^ realVhostFile ^ "! Taking no action.\n")
+ else
+ let
+ val vhostId = if OS.Path.ext (#file spl) = SOME "vhost_ssl" then
+ OS.Path.base (#file spl) ^ ".ssl"
+ else
+ OS.Path.base (#file spl)
+
+ fun realLogDir user =
+ logDir {user = valOf user,
+ node = Slave.hostname (),
+ vhostId = vhostId}
+
+ fun backupLogs () =
+ OS.Path.joinDirFile
+ {dir = Config.Apache.backupLogDirOf
+ (isVersion1 (Slave.hostname ())),
+ file = vhostId}
+ in
+ vhostsChanged := true;
+ case #action fs of
+ Slave.Delete _ =>
+ let
+ val ldir = realLogDir oldUser
+ in
+ if !logDeleted then
+ ()
+ else
+ (ignore (OS.Process.system (down ()));
+ ignore (OS.Process.system (fixperms ()));
+ logDeleted := true);
+ ignore (OS.Process.system (Config.rm
+ ^ " -rf "
+ ^ realVhostFile));
+ Slave.moveDirCreate {from = ldir,
+ to = backupLogs ()}
+ end
+ | Slave.Add =>
+ let
+ val rld = realLogDir user
+ in
+ ignore (OS.Process.system (Config.cp
+ ^ " "
+ ^ #file fs
+ ^ " "
+ ^ realVhostFile));
+ if Posix.FileSys.access (rld, []) then
+ ()
+ else
+ Slave.moveDirCreate {from = backupLogs (),
+ to = rld}
+ end
+
+ | _ =>
+ (ignore (OS.Process.system (Config.cp
+ ^ " "
+ ^ #file fs
+ ^ " "
+ ^ realVhostFile));
+ if user <> oldUser then
+ let
+ val old = realLogDir oldUser
+ val rld = realLogDir user
+ in
+ if !logDeleted then
+ ()
+ else
+ (ignore (OS.Process.system (down ()));
+ logDeleted := true);
+ ignore (OS.Process.system (Config.rm
+ ^ " -rf "
+ ^ realLogDir oldUser));
+ if Posix.FileSys.access (rld, []) then
+ ()
+ else
+ Slave.mkDirAll rld
+ end
+ else
+ ())
+ end
+ end
+ else
+ ()
+ end)