From: David Scott Date: Fri, 2 Oct 2009 17:22:52 +0000 (+0100) Subject: CA-28788: fix the 'atomic_write_to_file' function which always ended up setting the... X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=a7d310583e449225ff6a96d437ec4fc247192c43;p=xcp%2Fxen-api-libs.git CA-28788: fix the 'atomic_write_to_file' function which always ended up setting the perms to 0o0 despite the face that it tries to set them to 0o644. 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 --- diff --git a/stdext/unixext.ml b/stdext/unixext.ml index a74d83b..18c198f 100644 --- a/stdext/unixext.ml +++ b/stdext/unixext.ml @@ -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!")) diff --git a/stdext/unixext.mli b/stdext/unixext.mli index 527173f..5dec075 100644 --- a/stdext/unixext.mli +++ b/stdext/unixext.mli @@ -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