This change is the result of a code audit. The changes are not drastic, but should...
[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                 (* We don't want to get triggered when the .in descriptor is
28                  * opened *)
29                 Directfifowatcher.openentry rp root_dir fqp (abspath,slice_name);
30             | _ -> ()
31
32   (** A new directory was created at the backend, make a corresponding directory
33     at the frontend. Refer to mkentry for parameters *)
34   method mkdir rp perm =
35     match rp with Relpath(rel) ->
36       let fqp = String.concat "/" [root_dir;rel] in
37         try 
38           let s = Unix.stat fqp in
39             if (s.st_kind<>S_DIR) then
40               begin
41                 Unix.unlink fqp;
42                 Unix.mkdir fqp perm
43               end
44             else if (s.st_perm <> perm) then
45               begin
46                 Unix.rmdir fqp;
47                 Unix.mkdir fqp perm
48               end;
49         with Unix.Unix_error(_,_,_) ->
50           Unix.mkdir fqp perm;
51           Directfifowatcher.add_dir_watch fqp
52
53   (** Functions corresponding to file deletion/directory removal *)
54
55   (** *)
56   method unlink rp =
57     match rp with Relpath(rel) ->
58       let fqp = String.concat "/" [root_dir;rel] in
59       let fqp_in = String.concat "." [fqp;"in"] in
60       let fqp_out = String.concat "." [fqp;"out"] in
61         Directfifowatcher.closeentry fqp;
62         try 
63           Unix.unlink fqp_in;
64           Unix.unlink fqp_out
65         with _ ->
66           fprintf logfd "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ());flush logfd
67
68   method rmdir rp =
69     match rp with Relpath(rel) ->
70       let fqp = String.concat "/" [root_dir;rel] in
71         Directfifowatcher.del_dir_watch fqp;
72         try
73           Unix.rmdir fqp
74         with _ ->
75           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
76
77   initializer 
78     (
79       try 
80         let s = Unix.stat root_dir in
81           if (s.st_kind<>S_DIR) then
82             begin
83               Unix.unlink root_dir;
84               Unix.mkdir root_dir 0o700
85             end
86           else if (s.st_perm <> 0o700) then
87             begin
88               Unix.rmdir root_dir;
89               Unix.mkdir root_dir 0o700
90             end;
91       with Unix.Unix_error(_,_,_) ->
92         begin
93           try 
94             Unix.mkdir root_dir 0o700;
95           with _ -> ();
96         end);
97           Directfifowatcher.add_dir_watch root_dir
98 end