]> matita.cs.unibo.it Git - helm.git/commitdiff
- bugfix in cache handling for remote resources
authorStefano Zacchiroli <zack@upsilon.cc>
Wed, 6 Jul 2005 12:10:22 +0000 (12:10 +0000)
committerStefano Zacchiroli <zack@upsilon.cc>
Wed, 6 Jul 2005 12:10:22 +0000 (12:10 +0000)
- more informative Resource_not_found exception (include method's name)
- catch http error exceptions

helm/ocaml/getter/http_getter_storage.ml
helm/ocaml/getter/http_getter_storage.mli

index 8266c96112118cf769a13adc8d0d072b8b446e28..01a5a521ecc11529f4a071563e4435c1b7cacc22 100644 (file)
@@ -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', _) ->
index 396510af800a6e2b799f37aaa51bf782951cde0b..f14c3b6351243ea0e4fcb5bd2d9763e325746076 100644 (file)
  * 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