copy files from factory->factory/exec, from where they are installed
[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   (** Functions corresponding to file deletion/directory removal *)
52
53   (** *)
54   method unlink rp =
55     match rp with Relpath(rel) ->
56       let fqp = String.concat "/" [root_dir;rel] in
57       let fqp_in = String.concat "." [fqp;"in"] in
58       let fqp_out = String.concat "." [fqp;"out"] in
59         Directfifowatcher.closeentry fqp;
60         try 
61           Unix.unlink fqp_in;
62           Unix.unlink fqp_out
63         with _ ->
64           logprint "Hm. %s disappeared. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
65
66   method rmdir rp =
67     match rp with Relpath(rel) ->
68       let fqp = String.concat "/" [root_dir;rel] in
69         Directfifowatcher.del_dir_watch fqp;
70         try
71           Unix.rmdir fqp
72         with _ ->
73           logprint "Hm. %s disappeared or not empty. Looks like slice %s shot itself in the foot\n" fqp (this#get_slice_name ())
74
75   initializer 
76     (
77       try 
78         let s = Unix.stat root_dir in
79           if (s.st_kind<>S_DIR) then
80             begin
81               Unix.unlink root_dir;
82               Unix.mkdir root_dir 0o700
83             end
84           else if (s.st_perm <> 0o700) then
85             begin
86               Unix.rmdir root_dir;
87               Unix.mkdir root_dir 0o700
88             end;
89       with Unix.Unix_error(_,_,_) ->
90         begin
91           try 
92             Unix.mkdir root_dir 0o700;
93           with _ -> ();
94         end);
95           Directfifowatcher.add_dir_watch root_dir
96 end