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