Signed-off-by: Autumn! <autumnull@posteo.net>
---
accidentally did plagiarism badly
fs/fs.ha | 23 +++++++++++++++++++++++
os/os.ha | 5 +++++
2 files changed, 28 insertions(+)
diff --git a/fs/fs.ha b/fs/fs.ha
index af630261..c7335bb7 100644
--- a/fs/fs.ha
+++ b/fs/fs.ha
@@ -102,6 +102,29 @@ export fn rename(fs: *fs, oldpath: str, newpath: str) (void | error) = {
};
};
+// Copy a file from oldpath to newpath. Preserves the file permissions.
+export fn copy(fs: *fs, oldpath: str, newpath: str) (void | error) = {
+ // TODO: copy non-regular files
+ let st = stat(fs, oldpath)?;
+ assert(isfile(st.mode), "TODO: copy non-regular files");
+ let old = open(fs, oldpath)?;
+ let new = match (create(fs, newpath, st.mode)) {
+ case let h: io::handle =>
+ yield h;
+ case let err: error =>
+ io::close(old): void;
+ return err;
+ };
+ match (io::copy(new, old)) {
+ case let err: io::error =>
+ io::close(new): void;
+ io::close(old): void;
+ remove(fs, newpath)?;
+ return err;
+ case size => void;
+ };
+};
+
// Moves a file. This will use [[rename]] if possible, and will fall back to
// copy and remove if necessary.
export fn move(fs: *fs, oldpath: str, newpath: str) (void | error) = {
diff --git a/os/os.ha b/os/os.ha
index 69e6ae77..4cb76275 100644
--- a/os/os.ha
+++ b/os/os.ha
@@ -19,6 +19,11 @@ export fn remove(path: str) (void | fs::error) = fs::remove(cwd, path);
export fn rename(oldpath: str, newpath: str) (void | fs::error) =
fs::rename(cwd, oldpath, newpath);
+
+// Copies a file from oldpath to newpath. Preserves the permissions.
+export fn copy(oldpath: str, newpath: str) (void | fs::error) =
+ fs::copy(cwd, oldpath, newpath);
+
// Moves a file. This will use [[rename]] if possible, and will fall back to
// copy and remove if necessary.
export fn move(oldpath: str, newpath: str) (void | fs::error) =
--
2.40.1
hare/patches: SUCCESS in 1m42s
[fs,os: add copy()][0] v2 from [Autumn!][1]
[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/41097
[1]: mailto:autumnull@posteo.net
✓ #988948 SUCCESS hare/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/988948
✓ #988949 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/988949
TOCTOU. Also maybe out of scope?