Browse Source

find_file: check if path is absolute before scanning a dir (#10001)

Aleksandr Kuzmenko 4 years ago
parent
commit
cd73cf4d0a
2 changed files with 16 additions and 8 deletions
  1. 6 8
      src/context/common.ml
  2. 10 0
      src/core/path.ml

+ 6 - 8
src/context/common.ml

@@ -932,8 +932,12 @@ let find_file ctx f =
 		match Hashtbl.find ctx.file_lookup_cache f with
 		match Hashtbl.find ctx.file_lookup_cache f with
 		| None -> raise Exit
 		| None -> raise Exit
 		| Some f -> f
 		| Some f -> f
-	with Exit ->
+	with
+	| Exit ->
 		raise Not_found
 		raise Not_found
+	| Not_found when Path.is_absolute_path f ->
+		Hashtbl.add ctx.file_lookup_cache f (Some f);
+		f
 	| Not_found ->
 	| Not_found ->
 		let f_dir = Filename.dirname f in
 		let f_dir = Filename.dirname f in
 		let rec loop had_empty = function
 		let rec loop had_empty = function
@@ -958,13 +962,7 @@ let find_file ctx f =
 						loop (had_empty || p = "") l
 						loop (had_empty || p = "") l
 				end
 				end
 		in
 		in
-		let r =
-			try
-				Some (loop false ctx.class_path)
-			with Not_found ->
-				if Sys.file_exists f then Some f
-				else None
-		in
+		let r = try Some (loop false ctx.class_path) with Not_found -> None in
 		Hashtbl.add ctx.file_lookup_cache f r;
 		Hashtbl.add ctx.file_lookup_cache f r;
 		match r with
 		match r with
 		| None -> raise Not_found
 		| None -> raise Not_found

+ 10 - 0
src/core/path.ml

@@ -84,6 +84,16 @@ let normalize_path path =
 
 
 let path_sep = if Globals.is_windows then "\\" else "/"
 let path_sep = if Globals.is_windows then "\\" else "/"
 
 
+let is_absolute_path f =
+	try
+		match f.[0] with
+		| '/' -> true
+		| 'A'..'Z' | 'a'..'z' -> Globals.is_windows && f.[1] = ':'
+		| '\\' -> Globals.is_windows
+		| _ -> false
+	with _ ->
+		false
+
 (**
 (**
 	Returns absolute path.
 	Returns absolute path.
 	Resolves `.`, `..`, double slashes and trailing slashesw.
 	Resolves `.`, `..`, double slashes and trailing slashesw.