56d1d9f61aa616b5136fdab430fed37a1daf4889
[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
14   (** regex indicating that the script passes fds around *)
15   val fd_regex = Str.regexp "^fd_"
16
17   method is_fd_passer fname = Str.string_match fd_regex fname 0
18   method get_slice_name () = slice_name
19   (** A new script was copied into the backend, make a corresponding entry in
20     the frontend.
21     @param rp Relative path of the entry in the backend
22     @param abspath Absolute path of the entry
23     @param perm Permissions of the entry at the frontend *)
24   method mkentry (rp:relpath) abspath perm = 
25     let realperm = perm land (lnot 0o111) in
26       match rp with Relpath(rel) ->
27         let fqp = String.concat "/" [root_dir;rel] in
28           if (is_fd_passer rel) then
29             let res = Unixsocketwatcher.mkentry fqp abspath realperm slice_name in
30               begin
31                 match res with
32                   | Success ->
33                       Unixsocketwatcher.openentry root_dir fqp (abspath, slice_name)
34                   | _ -> 
35                       logprint "Could not create entry %s" abspath
36               end
37               else
38                 let res = Directfifowatcher.mkentry fqp abspath realperm slice_name in
39                   begin
40                     match res with 
41                       | Success ->
42                           Directfifowatcher.openentry root_dir fqp (abspath,slice_name)
43                       | _ -> 
44                           logprint "Could not create entry %s" abspath
45                   end
46           
47
48
49
50   (** A new directory was created at the backend, make a corresponding directory
51     at the frontend. Refer to mkentry for parameters *)
52   method mkdir rp perm =
53     match rp with Relpath(rel) ->
54       let fqp = String.concat "/" [root_dir;rel] in
55         try 
56           let s = Unix.stat fqp in
57             if (s.st_kind<>S_DIR) then
58               begin
59                 Unix.unlink fqp;
60                 Unix.mkdir fqp perm
61               end
62             else if (s.st_perm <> perm) then
63               begin
64                 Unix.rmdir fqp;
65                 Unix.mkdir fqp perm
66               end;
67         with Unix.Unix_error(_,_,_) ->
68           Unix.mkdir fqp perm;
69           Directfifowatcher.add_dir_watch fqp
70
71   (** Functions corresponding to file deletion/directory removal *)
72
73   (** *)
74   method unlink rp =
75     match rp with Relpath(rel) ->
76       let fqp = String.concat "/" [root_dir;rel] in
77       let fqp_in = String.concat "." [fqp;"in"] in
78       let fqp_out = String.concat "." [fqp;"out"] in
79         Directfifowatcher.closeentry fqp;
80         try 
81           Unix.unlink fqp_in;
82           Unix.unlink fqp_out
83         with _ ->
84           logprint "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
85
86   method rmdir rp =
87     match rp with Relpath(rel) ->
88       let fqp = String.concat "/" [root_dir;rel] in
89         Directfifowatcher.del_dir_watch fqp;
90         try
91           Unix.rmdir fqp
92         with _ ->
93           logprint "Hm. %s disappeared or not empty. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
94
95   initializer 
96     (
97       try 
98         let s = Unix.stat root_dir in
99           if (s.st_kind<>S_DIR) then
100             begin
101               Unix.unlink root_dir;
102               Unix.mkdir root_dir 0o700
103             end
104           else if (s.st_perm <> 0o700) then
105             begin
106               Unix.rmdir root_dir;
107               Unix.mkdir root_dir 0o700
108             end;
109       with Unix.Unix_error(_,_,_) ->
110         begin
111           try 
112             Unix.mkdir root_dir 0o700;
113           with _ -> ();
114         end);
115           Directfifowatcher.add_dir_watch root_dir
116 end