From 33b0056f5db298388df8fd66b72cd46e5839bf23 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Wed, 6 Jul 2005 12:10:22 +0000 Subject: [PATCH] - bugfix in cache handling for remote resources - more informative Resource_not_found exception (include method's name) - catch http error exceptions --- helm/ocaml/getter/http_getter_storage.ml | 34 ++++++++++++++--------- helm/ocaml/getter/http_getter_storage.mli | 11 ++++++-- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/helm/ocaml/getter/http_getter_storage.ml b/helm/ocaml/getter/http_getter_storage.ml index 8266c9611..01a5a521e 100644 --- a/helm/ocaml/getter/http_getter_storage.ml +++ b/helm/ocaml/getter/http_getter_storage.ml @@ -29,7 +29,7 @@ open Http_getter_misc open Http_getter_types exception Not_found' -exception Resource_not_found of string (** uri *) +exception Resource_not_found of string * string (** method, uri *) let index_fname = "INDEX" @@ -124,8 +124,10 @@ let ls_file_single _ path_prefix = with Unix.Unix_error (_, "opendir", _) -> [] let ls_http_single _ url_prefix = - let index = Http_getter_wget.get (normalize_dir url_prefix ^ index_fname) in - Pcre.split ~rex:newline_RE index + try + let index = Http_getter_wget.get (normalize_dir url_prefix ^ index_fname) in + Pcre.split ~rex:newline_RE index + with Http_client_error _ -> raise Not_found' let get_file _ path = if Sys.file_exists (path ^ gz_suffix) then @@ -141,20 +143,25 @@ let get_http uri url = | [scheme; path] -> scheme, path | _ -> assert false in - let cache_dest = + let cache_name = sprintf "%s%s/%s" (Lazy.force Http_getter_env.cache_dir) scheme path in - if not (Sys.file_exists cache_dest) then begin (* fill cache *) - Http_getter_misc.mkdir ~parents:true (Filename.dirname cache_dest); + if Sys.file_exists (cache_name ^ gz_suffix) then + cache_name ^ gz_suffix + else if Sys.file_exists cache_name then + cache_name + else begin (* fill cache *) + Http_getter_misc.mkdir ~parents:true (Filename.dirname cache_name); (try - Http_getter_wget.get_and_save (url ^ gz_suffix) (cache_dest ^ gz_suffix) - with Http_user_agent.Http_error _ -> + Http_getter_wget.get_and_save (url ^ gz_suffix) (cache_name ^ gz_suffix); + cache_name ^ gz_suffix + with Http_client_error _ -> (try - Http_getter_wget.get_and_save url cache_dest - with Http_user_agent.Http_error _ -> + Http_getter_wget.get_and_save url cache_name; + cache_name + with Http_client_error _ -> raise Not_found')) - end; - cache_dest + end let remove_file _ path = if Sys.file_exists (path ^ gz_suffix) then Sys.remove (path ^ gz_suffix); @@ -186,7 +193,7 @@ let dispatch storage_method uri = storage_method.http uri url else raise (Unsupported_scheme url) - with Not_found' -> raise (Resource_not_found uri) + with Not_found' -> raise (Resource_not_found (storage_method.name, uri)) let exists = dispatch { name = "exists"; file = exists_file; http = exists_http } @@ -205,6 +212,7 @@ let filename = get * for example: when doing "ls cic:/" we would like to see the "cic:/matita" * directory *) let ls uri_prefix = +(* prerr_endline ("Http_getter_storage.ls " ^ uri_prefix); *) let direct_results = ls_single uri_prefix in List.fold_left (fun results (_, uri_prefix', _) -> diff --git a/helm/ocaml/getter/http_getter_storage.mli b/helm/ocaml/getter/http_getter_storage.mli index 396510af8..f14c3b635 100644 --- a/helm/ocaml/getter/http_getter_storage.mli +++ b/helm/ocaml/getter/http_getter_storage.mli @@ -40,15 +40,20 @@ * holds: that exception is raised iff exists return false on a given resource * *) -exception Resource_not_found of string (** uri *) +exception Resource_not_found of string * string (** method, uri *) (** @return a list of string where dir are returned with a trailing "/" *) val ls: string -> string list (** @return the filename of the resource corresponding to a given uri. Handle - * download and caching for remote resources. *) -val filename: string -> string + * download and caching for remote resources. + * @param find if set to true all matching prefixes will be searched for the + * asked resource, if not only the best matching prefix will be used. Note + * that the search is performed only if the asked resource is not found in + * cache (i.e. to perform the find again you need to clean the cache). + * Defaults to false *) +val filename: (* ?find:bool -> *) string -> string (** only works for local resources * if both compressed and non-compressed versions of a resource exist, both of -- 2.39.2