]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CA-28788: fix the 'atomic_write_to_file' function which always ended up setting the...
authorDavid Scott <david.scott@xensource.com>
Fri, 2 Oct 2009 17:22:52 +0000 (18:22 +0100)
committerDavid Scott <david.scott@xensource.com>
Fri, 2 Oct 2009 17:22:52 +0000 (18:22 +0100)
As AndyP points out, the 'temp_file_in_dir' function creates the file with 0o0 and Unix.open only uses the given permissions if it actually creates a file. Therefore we add a Unix.chmod to set the permissions we want.

Signed-off-by: David Scott <dave.scott@eu.citrix.com>
stdext/unixext.ml
stdext/unixext.mli

index a74d83b0e2d002c79cd3bfda215af87833e5f46e..18c198fe5758118c7e33bdb9ce39e9678f1a69f7 100644 (file)
@@ -138,11 +138,12 @@ let read_whole_file_to_string fname =
 
 (** Opens a temp file, applies the fd to the function, when the function completes, renames the file
     as required. *)
-let atomic_write_to_file fname f =
+let atomic_write_to_file fname perms f =
   let tmp = Filenameext.temp_file_in_dir fname in
+  Unix.chmod tmp perms;
   Pervasiveext.finally
     (fun () ->
-      let fd = Unix.openfile tmp [Unix.O_WRONLY; Unix.O_CREAT] 0o644 in
+      let fd = Unix.openfile tmp [Unix.O_WRONLY; Unix.O_CREAT] perms (* ignored since the file exists *) in
       let result = Pervasiveext.finally
        (fun () -> f fd)
        (fun () -> Unix.close fd) in
@@ -153,7 +154,7 @@ let atomic_write_to_file fname f =
 
 (** Atomically write a string to a file *)
 let write_string_to_file fname s =
-  atomic_write_to_file fname (fun fd ->
+  atomic_write_to_file fname 0o644 (fun fd ->
     let len = String.length s in
     let written = Unix.write fd s 0 len in
     if written <> len then (failwith "Short write occured!"))
index 527173f73d304c926e05370b05cdc142ad5bbf9b..5dec0751bc3fef71e32555bb7e7e3ec320312d67 100644 (file)
@@ -23,7 +23,7 @@ val with_directory : string -> (Unix.dir_handle -> 'a) -> 'a
 val readfile_line : (string -> 'a) -> string -> unit
 val read_whole_file : int -> int -> Unix.file_descr -> string
 val read_whole_file_to_string : string -> string
-val atomic_write_to_file : string -> (Unix.file_descr -> 'a) -> 'a
+val atomic_write_to_file : string -> Unix.file_perm -> (Unix.file_descr -> 'a) -> 'a
 val write_string_to_file : string -> string -> unit
 val execv_get_output : string -> string array -> int * Unix.file_descr
 val copy_file : ?limit:int64 -> Unix.file_descr -> Unix.file_descr -> int64