File b40c69380f724933c462ede4b926e3c4f4182d09.patch of Package ocaml-obuild
From b5d17ea03601af1519d2f7a51d9fcee28283e0dc Mon Sep 17 00:00:00 2001
From: Andy Li <andy@onthewings.net>
Date: Tue, 19 Mar 2019 22:39:11 +0800
Subject: [PATCH] Always sort readdir results in order to get reproducible
builds. (#175)
---
ext/filesystem.ml | 30 ++++++------------------------
obuild/project.ml | 2 +-
2 files changed, 7 insertions(+), 25 deletions(-)
diff --git a/ext/filesystem.ml b/ext/filesystem.ml
index 1a9412a..5914d8c 100644
--- a/ext/filesystem.ml
+++ b/ext/filesystem.ml
@@ -33,17 +33,9 @@ let removeDirContent wpath =
let removeDir path = removeDirContent path; Unix.rmdir (fp_to_string path); ()
let iterate f path =
- let dirhandle = Unix.opendir (fp_to_string path) in
- (try
- while true do
- let ent = Unix.readdir dirhandle in
- if ent <> ".." && ent <> "."
- then f (fn ent)
- done;
- with End_of_file ->
- ()
- );
- Unix.closedir dirhandle;
+ let entries = Sys.readdir (fp_to_string path) in
+ Array.fast_sort String.compare entries;
+ Array.iter (fun ent -> f (fn ent)) entries;
()
(* list directory entry with a map function included for efficiency *)
@@ -62,19 +54,9 @@ let list_dir_pred (p : filename -> bool) path : filename list =
let list_dir = list_dir_pred (const true)
let list_dir_path_pred p path =
- let accum = ref [] in
- let dirhandle = Unix.opendir (fp_to_string path) in
- (try
- while true do
- let ent = Unix.readdir dirhandle in
- if ent <> ".." && p ent
- then accum := (path </> fn ent) :: !accum
- done;
- with End_of_file ->
- ()
- );
- Unix.closedir dirhandle;
- !accum
+ let entries = List.filter p (Array.to_list (Sys.readdir (fp_to_string path))) in
+ let sorted = List.fast_sort String.compare entries in
+ List.map (fun ent -> path </> fn ent) sorted
let list_dir_path = list_dir_path_pred (const true)
diff --git a/obuild/project.ml b/obuild/project.ml
index acec79d..a2d3744 100644
--- a/obuild/project.ml
+++ b/obuild/project.ml
@@ -440,7 +440,7 @@ let make = {
}
let findPath () =
- let ents = Array.to_list (Sys.readdir ".") in
+ let ents = List.fast_sort String.compare (Array.to_list (Sys.readdir ".")) in
match List.find_all (fun ent -> not (string_startswith "." ent) && string_endswith ".obuild" ent) ents with
| [] -> raise NoConfFile
| [x] -> fp x