Skip to content

Commit 9f9fc5d

Browse files
authored
Merge pull request #6910 from NathanReb/remove-chdir
Remove unnecessary use of chdir
2 parents b7cde31 + 9f0d7a2 commit 9f9fc5d

19 files changed

Lines changed: 188 additions & 213 deletions

master_changes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ users)
1515

1616
## Global CLI
1717
* Update Kate's email address [#6808 @kit-ty-kate]
18+
* Remove unnecessary uses of `chdir` [#6910 @NathanReb]
1819

1920
## Plugins
2021

@@ -247,6 +248,7 @@ users)
247248
* `OpamPathName` was added [#6917 @rjbou]
248249
* `OpamRepositoryPathName` was added [#6917 @rjbou]
249250
* `OpamRepositoryPath` was moved from `opam-repository` [#6917 @rjbou]
251+
* `OpamFilter.expand_interpolations_in_file`: changed argument type from `basename` to `filename` [#6910 @NathanReb]
250252

251253
## opam-core
252254
* `OpamCmdliner` was added. It is accessible through a new `opam-core.cmdliner` sub-library [#6755 @kit-ty-kate]
@@ -263,3 +265,8 @@ users)
263265
* `OpamFilename`: add `is_dir_read_only` [#6489 @rjbou]
264266
* `OpamFilename.might_escape`: ensure / is detected as a file separator when called with `~sep:Unspecified` on Windows [#6897 @kit-ty-kate]
265267
* `OpamFilename.Unix` was added abstracting over `/` separated paths regardless of the current system [#6914 @rjbou @kit-ty-kate]
268+
* `OpamFilename.in_dir`: removed [#6910 @NathanReb]
269+
* `OpamSystem.in_tmp_dir`: removed [#6910 @NathanReb]
270+
* `OpamSystem.in_dir`: removed [#6910 @NathanReb]
271+
* `OpamSystem.chdir`: removed [#6910 @NathanReb]
272+
* `OpamSystem.{command,commands,read_command_output}`: add a `?dir: dirname` optional arg to launch the command in a specific directory [#6910 @NathanReb]

src/client/opamAction.ml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ let preprocess_dot_install_t st nv build_dir =
168168
List.map (fun (src, dst) ->
169169
let file = file_wo_prefix dst in
170170
let inst warning =
171-
let src_file = OpamFilename.create (OpamFilename.cwd ()) src.c in
171+
let src_file = OpamFilename.create build_dir src.c in
172172
if OpamFilename.exists dst
173173
&& OpamConsole.confirm "Overwriting %s?" (OpamFilename.to_string dst) then
174174
OpamFilename.install ~warning ~src:src_file ~dst ()
@@ -211,7 +211,6 @@ let preprocess_dot_install st nv build_dir =
211211
else
212212
(OpamSystem.default_install_warning, (fun () -> false))
213213
in
214-
OpamFilename.in_dir build_dir @@ fun () ->
215214
log "Installing %s.\n" (OpamPackage.to_string nv);
216215
let warnings =
217216
List.filter_map (fun install -> install warning) installs
@@ -396,26 +395,22 @@ let prepare_package_build env opam nv dir =
396395
None)
397396
else
398397
let subst_errs =
399-
OpamFilename.in_dir dir @@ fun () ->
400398
List.fold_left (fun errs f ->
401399
try
402400
print_subst f;
403-
OpamFilter.expand_interpolations_in_file env f;
401+
let file = OpamFilename.create dir f in
402+
OpamFilter.expand_interpolations_in_file env file;
404403
errs
405404
with e -> (f, e)::errs)
406405
[] subst_patches
407406
in
408407
let patching_errors = apply_patches () in
409-
(* Substitute the configuration files. We should be in the right
410-
directory to get the correct absolute path for the
411-
substitution files (see [OpamFilter.expand_interpolations_in_file] and
412-
[OpamFilename.of_basename]. *)
413408
let subst_errs =
414-
OpamFilename.in_dir dir @@ fun () ->
415409
List.fold_left (fun errs f ->
416410
try
417411
print_subst f;
418-
OpamFilter.expand_interpolations_in_file env f;
412+
let file = OpamFilename.create dir f in
413+
OpamFilter.expand_interpolations_in_file env file;
419414
errs
420415
with e -> (f, e)::errs)
421416
subst_errs subst_others

src/client/opamAuxCommands.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,8 @@ let check_and_revert_sandboxing root config =
588588
Array.append [| "OPAM_SWITCH_PREFIX=/dev/null" |] (Unix.environment ())
589589
in
590590
try
591-
(* Don't assume that we can mount the CWD *)
592-
OpamSystem.in_tmp_dir @@ fun () ->
593-
OpamSystem.read_command_output ~env ~allow_stdin:false (cmd @ test_cmd)
591+
OpamSystem.with_tmp_dir @@ fun dir ->
592+
OpamSystem.read_command_output ~env ~dir ~allow_stdin:false (cmd @ test_cmd)
594593
= ["SUCCESS"]
595594
with e ->
596595
(OpamConsole.error "Sandboxing is not working on your platform%s:\n%s"

src/client/opamConfigCommand.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,11 @@ let env gt switch ?(set_opamroot=false) ?(set_opamswitch=false)
377377
let subst gt fs =
378378
log "config-substitute";
379379
OpamSwitchState.with_ `Lock_none gt @@ fun st ->
380+
let env = OpamPackageVar.resolve st in
380381
List.iter
381-
(OpamFilter.expand_interpolations_in_file (OpamPackageVar.resolve st))
382+
(fun b ->
383+
let file = OpamFilename.of_basename b in
384+
OpamFilter.expand_interpolations_in_file env file)
382385
fs
383386

384387
let expand gt str =

src/core/opamFilename.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,16 @@ let dirs d =
106106
let dir_is_empty d =
107107
OpamSystem.dir_is_empty (Dir.to_string d)
108108

109-
let in_dir dirname fn = OpamSystem.in_dir dirname fn
110-
111109
let is_dir_read_only dirname =
112110
OpamSystem.is_dir_read_only (Dir.to_string dirname)
113111

114112
let env_of_list l = Array.of_list (List.rev_map (fun (k,v) -> k^"="^v) l)
115113

116-
let exec dirname ?env ?name ?metadata ?keep_going cmds =
114+
let exec dir ?env ?name ?metadata ?keep_going cmds =
117115
let env = match env with
118116
| None -> None
119117
| Some l -> Some (env_of_list l) in
120-
in_dir dirname
121-
(fun () -> OpamSystem.commands ?env ?name ?metadata ?keep_going cmds)
118+
OpamSystem.commands ~dir ?env ?name ?metadata ?keep_going cmds
122119

123120
let move_dir ~src ~dst =
124121
OpamSystem.mv (Dir.to_string src) (Dir.to_string dst)

src/core/opamFilename.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ val is_dir_read_only: Dir.t -> bool
6060
(** List the sub-directory (do not recurse) *)
6161
val dirs: Dir.t -> Dir.t list
6262

63-
(** Evaluate a function in a given directory *)
64-
val in_dir: Dir.t -> (unit -> 'a) -> 'a
65-
6663
(** Turns an assoc list into an array suitable to be provided as environment *)
6764
val env_of_list: (string * string) list -> string array
6865

src/core/opamSystem.ml

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -337,36 +337,17 @@ let copy_file_aux ?chmod ~src ~dst () =
337337
(try Unix.unlink dst with Unix.Unix_error _ -> ());
338338
internal_error "Cannot copy %s to %s (%s)." src dst (Printexc.to_string e)
339339
340-
let chdir dir =
341-
try Unix.chdir dir
342-
with Unix.Unix_error _ -> raise (File_not_found dir)
343-
344-
let in_dir dir fn =
345-
let reset_cwd =
346-
let cwd =
347-
try Some (Sys.getcwd ())
348-
with Sys_error _ -> None in
349-
fun () ->
350-
match cwd with
351-
| None -> ()
352-
| Some cwd -> try chdir cwd with File_not_found _ -> () in
353-
chdir dir;
354-
try
355-
let r = fn () in
356-
reset_cwd ();
357-
r
358-
with e ->
359-
OpamStd.Exn.finalise e reset_cwd
360-
361340
let list kind dir =
362-
try
363-
in_dir dir (fun () ->
364-
let d = Sys.readdir (Sys.getcwd ()) in
365-
let d = Array.to_list d in
366-
let l = List.filter kind d in
367-
List.map (Filename.concat dir) (List.sort compare l)
368-
)
369-
with File_not_found _ -> []
341+
let d = try Sys.readdir dir with Sys_error _ -> [||] in
342+
let filtered =
343+
Array.fold_left
344+
(fun acc base ->
345+
let path = Filename.concat dir base in
346+
if kind path then path::acc else acc)
347+
[]
348+
d
349+
in
350+
List.sort String.compare filtered
370351
371352
let ls dir = list (fun _ -> true) dir
372353
@@ -433,10 +414,6 @@ let with_tmp_dir fn =
433414
OpamStd.Exn.finalise e @@ fun () ->
434415
remove_dir dir
435416
436-
let in_tmp_dir fn =
437-
with_tmp_dir @@ fun dir ->
438-
in_dir dir fn
439-
440417
let with_tmp_dir_job fjob =
441418
let dir = mk_temp_dir () in
442419
mkdir dir;
@@ -573,7 +550,7 @@ let make_command
573550
| `Denied -> permission_denied cmd
574551
575552
let run_process
576-
?verbose ?env ~name ?metadata ?stdout ?allow_stdin command =
553+
?verbose ?env ~name ?metadata ?dir ?stdout ?allow_stdin command =
577554
let env = match env with None -> OpamProcess.default_env () | Some e -> e in
578555
let chrono = OpamConsole.timer () in
579556
match command with
@@ -588,7 +565,7 @@ let run_process
588565
let r =
589566
OpamProcess.run
590567
(OpamProcess.command
591-
~env ~name ~verbose ?metadata ?allow_stdin ?stdout
568+
~env ~name ~verbose ?metadata ?dir ?allow_stdin ?stdout
592569
full_cmd args)
593570
in
594571
let str = String.concat " " (cmd :: args) in
@@ -599,15 +576,15 @@ let run_process
599576
| `Not_found -> command_not_found cmd
600577
| `Denied -> permission_denied cmd
601578
602-
let command ?verbose ?env ?name ?metadata ?allow_stdin cmd =
579+
let command ?verbose ?env ?name ?metadata ?dir ?allow_stdin cmd =
603580
let name = log_file name in
604-
let r = run_process ?verbose ?env ~name ?metadata ?allow_stdin cmd in
581+
let r = run_process ?verbose ?env ~name ?metadata ?dir ?allow_stdin cmd in
605582
OpamProcess.cleanup r;
606583
raise_on_process_error r
607584
608-
let commands ?verbose ?env ?name ?metadata ?(keep_going=false) commands =
585+
let commands ?verbose ?env ?name ?metadata ?dir ?(keep_going=false) commands =
609586
let name = log_file name in
610-
let run = run_process ?verbose ?env ~name ?metadata in
587+
let run = run_process ?verbose ?env ~name ?metadata ?dir in
611588
let command r0 c =
612589
match r0, keep_going with
613590
| (`Error _ | `Exception _), false -> r0
@@ -625,12 +602,12 @@ let commands ?verbose ?env ?name ?metadata ?(keep_going=false) commands =
625602
| `Error e -> process_error e
626603
| `Exception e -> raise e
627604
628-
let read_command_output ?verbose ?env ?metadata ?allow_stdin
605+
let read_command_output ?verbose ?env ?metadata ?dir ?allow_stdin
629606
?(ignore_stderr=false) cmd =
630607
let name = log_file None in
631608
let stdout = name ^ (if ignore_stderr then ".stdout" else ".out") in
632609
let r =
633-
run_process ?verbose ?env ~name ?metadata ?allow_stdin
610+
run_process ?verbose ?env ~name ?metadata ?dir ?allow_stdin
634611
~stdout
635612
cmd
636613
in

src/core/opamSystem.mli

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ val internal_error: ('a, unit, string, 'b) format4 -> 'a
3636
passes its name to [fn]. The directory is always removed on completion. *)
3737
val with_tmp_dir: (string -> 'a) -> 'a
3838

39-
(** [in_tmp_dir fn] executes [fn] in a temporary directory. *)
40-
val in_tmp_dir: (unit -> 'a) -> 'a
41-
4239
(** Runs a job with a temp dir that is cleaned up afterwards *)
4340
val with_tmp_dir_job: (string -> 'a OpamProcess.job) -> 'a OpamProcess.job
4441

@@ -164,12 +161,6 @@ val rmdir_cleanup : string -> unit
164161
exists and is not a symlink. Returns [false] otherwise. *)
165162
val is_reg_dir: string -> bool
166163

167-
(** Change the current working directory *)
168-
val chdir: string -> unit
169-
170-
(** [in_dir dir fn] evaluates [fn] in the directory [dir] *)
171-
val in_dir: string -> (unit -> 'a) -> 'a
172-
173164
(** Returns the list of files and directories in the given directory (full
174165
names) *)
175166
val ls: string -> string list
@@ -251,14 +242,15 @@ val apply_cygpath: string -> string
251242
(** [command cmd] executes the command [cmd] in the correct OPAM
252243
environment. *)
253244
val command: ?verbose:bool -> ?env:string array -> ?name:string ->
254-
?metadata:(string * string) list -> ?allow_stdin:bool ->
245+
?metadata:(string * string) list -> ?dir: string -> ?allow_stdin:bool ->
255246
command -> unit
256247

257248
(** [commands cmds] executes the commands [cmds] in the correct OPAM
258249
environment. It stops whenever one command fails unless [keep_going] is set
259250
to [true]. In this case, the first error is re-raised at the end. *)
260251
val commands: ?verbose:bool -> ?env:string array -> ?name:string ->
261-
?metadata:(string * string) list -> ?keep_going:bool -> command list -> unit
252+
?metadata:(string * string) list -> ?dir: string -> ?keep_going:bool ->
253+
command list -> unit
262254

263255
(** [read_command_output cmd] executes the command [cmd] in the
264256
correct OPAM environment and return the lines from output if the command
@@ -267,7 +259,7 @@ val commands: ?verbose:bool -> ?env:string array -> ?name:string ->
267259
It returns stdout and stder combiend, unless [ignore_stderr] is st to true.
268260
*)
269261
val read_command_output: ?verbose:bool -> ?env:string array ->
270-
?metadata:(string * string) list -> ?allow_stdin:bool ->
262+
?metadata:(string * string) list -> ?dir: string -> ?allow_stdin:bool ->
271263
?ignore_stderr:bool -> command -> string list
272264

273265
(** END *)

src/format/opamFilter.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,9 @@ let expand_interpolations_in_file_full env ~src ~dst =
461461
close_out oc
462462

463463
(* Substitute the file contents *)
464-
let expand_interpolations_in_file env file =
465-
let file = OpamFilename.of_basename file in
466-
let src = OpamFilename.add_extension file OpamPathName.subst_ext in
467-
expand_interpolations_in_file_full env ~src ~dst:file
464+
let expand_interpolations_in_file env dst =
465+
let src = OpamFilename.add_extension dst OpamPathName.subst_ext in
466+
expand_interpolations_in_file_full env ~src ~dst
468467

469468
(* Apply filters and interpolations to package commands *)
470469

src/format/opamFilter.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ val expand_interpolations_in_file_full: env -> src:filename -> dst:filename -> u
143143
(** Same as {!expand_interpolations_in_file} but allows to set the source [src] and
144144
destination [dst] files independently instead of implying [src] = [dst].in *)
145145

146-
(** Rewrites [basename.in] to [basename], expanding interpolations.
146+
(** Rewrites [filename.in] to [filename], expanding interpolations.
147147
If the first line begins ["opam-version:"], assumes that expansion of
148148
variables within strings should be properly escaped. In particular, this
149149
means that Windows paths should expand correctly when generating .config
150150
files. *)
151-
val expand_interpolations_in_file: env -> basename -> unit
151+
val expand_interpolations_in_file: env -> filename -> unit
152152

153153

154154
(** Processes filters evaluation in a command list: parameter expansion and

0 commit comments

Comments
 (0)