[Libguestfs] [PATCH 1/2] mllib: curl: add optional tmpdir parameter

Pino Toscano ptoscano at redhat.com
Mon Oct 24 16:29:54 UTC 2016


Add a new optional parameter for the Curl ADT, so temporary files can be
created in a specified directory (which is supposed to be temporary, and
disposed only when the application quits).
---
 mllib/curl.ml  | 16 +++++++++++-----
 mllib/curl.mli |  2 +-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/mllib/curl.ml b/mllib/curl.ml
index 376406e..7d07125 100644
--- a/mllib/curl.ml
+++ b/mllib/curl.ml
@@ -25,6 +25,7 @@ let quote = Filename.quote
 type t = {
   curl : string;
   args : args;
+  tmpdir : string option;
 }
 and args = (string * string option) list
 
@@ -40,12 +41,13 @@ let args_of_proxy = function
   | SystemProxy ->     []
   | ForcedProxy url -> [ "proxy", Some url; "noproxy", Some "" ]
 
-let create ?(curl = "curl") ?(proxy = SystemProxy) args =
+let create ?(curl = "curl") ?(proxy = SystemProxy) ?tmpdir args =
   let args = safe_args @ args_of_proxy proxy @ args in
-  { curl = curl; args = args }
+  { curl = curl; args = args; tmpdir = tmpdir }
 
-let run { curl = curl; args = args } =
-  let config_file, chan = Filename.open_temp_file "guestfscurl" ".conf" in
+let run { curl = curl; args = args; tmpdir = tmpdir } =
+  let config_file, chan = Filename.open_temp_file ?temp_dir:tmpdir
+    "guestfscurl" ".conf" in
   List.iter (
     function
     | name, None -> fprintf chan "%s\n" name
@@ -71,7 +73,11 @@ let run { curl = curl; args = args } =
 
   let cmd = sprintf "%s -q --config %s" (quote curl) (quote config_file) in
   let lines = external_command ~echo_cmd:false cmd in
-  Unix.unlink config_file;
+  (* Remove the temporary configuration only when not created under
+   * a proper temporary directory.
+   *)
+  if tmpdir = None then
+    Unix.unlink config_file;
   lines
 
 let to_string { curl = curl; args = args } =
diff --git a/mllib/curl.mli b/mllib/curl.mli
index f045572..c0c2fb0 100644
--- a/mllib/curl.mli
+++ b/mllib/curl.mli
@@ -27,7 +27,7 @@ type proxy =
   | SystemProxy           (** Use the system settings. *)
   | ForcedProxy of string (** The proxy is forced to the specified URL. *)
 
-val create : ?curl:string -> ?proxy:proxy -> args -> t
+val create : ?curl:string -> ?proxy:proxy -> ?tmpdir:string -> args -> t
 (** Create a curl command handle.
 
     The curl arguments are a list of key, value pairs corresponding
-- 
2.7.4




More information about the Libguestfs mailing list