[Files.save_as]: just write directly to special files#60
Merged
Conversation
Also, if [name] is a symbolic link, resolve it first.
rr0gi
requested changes
May 7, 2026
Comment on lines
+85
to
+93
| let save_as name ?mode f = | ||
| let name = | ||
| match (Unix.lstat name).st_kind with | ||
| | Unix.S_LNK -> Unix.realpath name | ||
| | (exception Unix.Unix_error (Unix.ENOENT, _, _)) | _ -> name | ||
| in | ||
| match (Unix.stat name).st_kind with | ||
| | Unix.S_REG | (exception Unix.Unix_error (Unix.ENOENT, _, _)) -> save_as_regular name ?mode f | ||
| | _ -> Out_channel.with_open_gen [ Open_wronly ] 0 name f |
Contributor
There was a problem hiding this comment.
doesn't matter perf-wise but lets not do two stats in the most common case of regular file, call save_as recursively in case of S_LNK
Contributor
Author
There was a problem hiding this comment.
Please see the latest, recursive version. Like the old version, I noticed (or CC rather) that the function will throw an error on broken symlinks. That seems like reasonable behavior to me but I clarified it in the mli. I got a bunch of vibe coded tests as well, see latest commit. Do you want to keep them?
Co-authored-by: rr0gi <igor@ahrefs.com>
Co-authored-by: rr0gi <igor@ahrefs.com>
rr0gi
approved these changes
May 11, 2026
Comment on lines
+87
to
+88
| | Unix.S_LNK -> save_as (Unix.realpath name) ?mode f | ||
| | Unix.S_REG | (exception Unix.Unix_error (Unix.ENOENT, _, _)) -> save_as_regular name ?mode f |
Contributor
There was a problem hiding this comment.
Suggested change
| | Unix.S_LNK -> save_as (Unix.realpath name) ?mode f | |
| | Unix.S_REG | (exception Unix.Unix_error (Unix.ENOENT, _, _)) -> save_as_regular name ?mode f | |
| | S_LNK -> save_as (Unix.realpath name) ?mode f | |
| | S_REG | (exception Unix.Unix_error (ENOENT, _, _)) -> save_as_regular name ?mode f |
| let () = test "Files.save_as no temp file left on failure" @@ fun () -> | ||
| with_temp_path "test_save_as_fail.txt" @@ fun path -> | ||
| (try Files.save_as path (fun _oc -> failwith "boom") with Failure _ -> ()); | ||
| let temp = Printf.sprintf "%s.save.%d.tmp" path (U.gettid ()) in |
Contributor
There was a problem hiding this comment.
this test is kind of fragile but i guess can fix when if ever it breaks
raphael-proust
added a commit
that referenced
this pull request
May 12, 2026
* master: [Files.save_as]: just write directly to special files (#60) web: expose http_request_k (#58) structured logging (#59) log: add critical due to error level inflation ci: use ocaml 5.4 files: add mkdir_p (#57) [Action.config_lines] document and test behavior of commented lines (#56) update to trace 0.12 and OTEL main web: track content-type fix: avoid curl.ml crash web: track more information about request/response in span
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This is a backport of ahrefs/passage#28
In [Files.save_as]: just write directly to special files instead of doing the temp-file dance. Also, if [name] is a symbolic link, resolve it first.