Trellis branch of vsys
[vsys.git] / frontend.ml
1 (* frontend.ml: Routines that implement frontend actions, such as creating directories in a slice, creating pipes etc. *)
2
3 open Printf
4 open Unix
5 open Globals
6 open Directfifowatcher
7
8 (** frontendhandler class: Methods to create and unlink pipes and directories 
9   @param root_dir vsys directory inside a slice
10   @param slice_name actual slice name - set with care, since the acl functionality refers to these names *)
11 class frontendHandler (root_dir,slice_name) = 
12 object(this)
13   method get_slice_name () = slice_name
14
15   (** A new script was copied into the backend, make a corresponding entry in
16     the frontend.
17     @param rp Relative path of the entry in the backend
18     @param abspath Absolute path of the entry
19     @param perm Permissions of the entry at the frontend *)
20   method mkentry (rp:relpath) abspath perm = 
21     let realperm = perm land (lnot 0o111) in
22       match rp with Relpath(rel) ->
23         let fqp = String.concat "/" [root_dir;rel] in
24         let res = Directfifowatcher.mkentry fqp abspath realperm slice_name in
25           match res with 
26             | Success ->
27                 Directfifowatcher.openentry root_dir fqp (abspath,slice_name)
28             | _ -> ()
29
30   (** A new directory was created at the backend, make a corresponding directory
31     at the frontend. Refer to mkentry for parameters *)
32   method mkdir rp perm =
33     match rp with Relpath(rel) ->
34       let fqp = String.concat "/" [root_dir;rel] in
35         try 
36           let s = Unix.stat fqp in
37             if (s.st_kind<>S_DIR) then
38               begin
39                 Unix.unlink fqp;
40                 Unix.mkdir fqp perm
41               end
42             else if (s.st_perm <> perm) then
43               begin
44                 Unix.rmdir fqp;
45                 Unix.mkdir fqp perm
46               end;
47         with Unix.Unix_error(_,_,_) ->
48           Unix.mkdir fqp perm;
49         Directfifowatcher.add_dir_watch fqp
50
51
52
53             
54
55   (** Functions corresponding to file deletion/directory removal *)
56
57   (** *)
58   method unlink rp =
59     match rp with Relpath(rel) ->
60       let fqp = String.concat "/" [root_dir;rel] in
61       let fqp_in = String.concat "." [fqp;"in"] in
62       let fqp_out = String.concat "." [fqp;"out"] in
63         Directfifowatcher.closeentry fqp;
64         try 
65           Unix.unlink fqp_in;
66           Unix.unlink fqp_out
67         with _ ->
68           fprintf logfd "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ());flush logfd
69
70   method rmdir rp =
71     match rp with Relpath(rel) ->
72       let fqp = String.concat "/" [root_dir;rel] in
73         Directfifowatcher.del_dir_watch fqp;
74         try
75           Unix.rmdir fqp
76         with _ ->
77           fprintf logfd "Hm. %s disappeared or not empty. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ());flush logfd
78
79   initializer 
80           (
81         try 
82           let s = Unix.stat root_dir in
83             if (s.st_kind<>S_DIR) then
84               begin
85                 Unix.unlink root_dir;
86                 Unix.mkdir root_dir 0o700
87               end
88             else if (s.st_perm <> 0o700) then
89               begin
90                 Unix.rmdir root_dir;
91                 Unix.mkdir root_dir 0o700
92               end;
93         with Unix.Unix_error(_,_,_) ->
94           begin
95           try 
96           Unix.mkdir root_dir 0o700;
97           with _ -> ();
98           end);
99         Directfifowatcher.add_dir_watch root_dir
100 end