hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 10ef469fe78e7fc0dc20da9cbce97eabc32ddf17
parent e7dbe3a9bad36fbfe29eb18267df73aa6036c6d5
Author: Ajay R <ar324@protonmail.com>
Date:   Thu, 24 Feb 2022 03:19:34 +0000

fs: (move) remove new file if error occurs

Signed-off-by: Ajay R <ar324@protonmail.com>

Diffstat:
Mfs/fs.ha | 12++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/fs.ha b/fs/fs.ha @@ -112,15 +112,19 @@ export fn move(fs: *fs, oldpath: str, newpath: str) (void | error) = { return; // Success }; // TODO: - // - If an error occurs, remove the new file. // - Move non-regular files let st = stat(fs, oldpath)?; assert(isfile(st.mode), "TODO: move non-regular files"); - let new = create(fs, newpath, st.mode)?; - defer io::close(new); let old = open(fs, oldpath)?; defer io::close(old); - io::copy(new, old)?; + let new = create(fs, newpath, st.mode)?; + defer io::close(new); + match (io::copy(new, old)) { + case let err: io::error => + remove(fs, newpath)?; + return err; + case size => void; + }; remove(fs, oldpath)?; };