hare

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

commit 113cdf3fd568feaa6ddbc42fb0df5a2d03ac2fb9
parent 8cfe3937787f075c757640a48ddcf0c3271a65cc
Author: Sebastian <sebastian@sebsite.pw>
Date:   Wed, 31 May 2023 01:09:03 -0400

temp: require mode param for "file" and "named"

Instead of abusing variadism to create an optional parameter

References: https://todo.sr.ht/~sircmpwn/hare/841
Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mcmd/hare/release.ha | 6++++--
Mtemp/+freebsd.ha | 19++++---------------
Mtemp/+linux.ha | 21++++-----------------
3 files changed, 12 insertions(+), 34 deletions(-)

diff --git a/cmd/hare/release.ha b/cmd/hare/release.ha @@ -107,7 +107,8 @@ fn do_release( const name = path::basename(os::getcwd()); const dir = temp::dir(); defer os::rmdirall(dir)!; - const (clfile, changelog) = temp::named(os::cwd, dir, io::mode::WRITE)?; + const (clfile, changelog) = temp::named(os::cwd, + dir, io::mode::WRITE, 0o644)?; defer io::close(clfile)!; fmt::fprintfln(clfile, changelog_template, lasttag, name, newtag)?; shortlog(clfile, range)?; @@ -137,7 +138,8 @@ fn do_initial_release(ver: (modversion | increment)) (void | release_error) = { const name = path::basename(os::getcwd()); const dir = temp::dir(); defer os::rmdirall(dir)!; - const (clfile, changelog) = temp::named(os::cwd, dir, io::mode::WRITE)?; + const (clfile, changelog) = temp::named(os::cwd, + dir, io::mode::WRITE, 0o644)?; defer io::close(clfile)!; fmt::fprintfln(clfile, initial_template, name, newtag)?; diff --git a/temp/+freebsd.ha b/temp/+freebsd.ha @@ -21,15 +21,9 @@ fn get_tmpdir() str = os::tryenv("TMPDIR", "/tmp"); // when the stream is closed. // // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. -// -// Only one variadic argument may be provided, if at all, to specify the mode of -// the new file. The default is 0o644. -export fn file( - iomode: io::mode, - mode: fs::mode... -) (io::file | fs::error) = { +export fn file(iomode: io::mode, mode: fs::mode) (io::file | fs::error) = { // TODO: Add a custom "close" function which removes the named file - let file = named(os::cwd, get_tmpdir(), iomode, mode...)?; + let file = named(os::cwd, get_tmpdir(), iomode, mode)?; free(file.1); return file.0; }; @@ -40,19 +34,14 @@ export fn file( // overwritten on subsequent calls. // // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. -// -// Only one variadic argument may be provided, if at all, to specify the mode of -// the new file. The default is 0o644. export fn named( fs: *fs::fs, path: str, iomode: io::mode, - mode: fs::mode... + mode: fs::mode, ) ((io::file, str) | fs::error) = { assert(iomode == io::mode::WRITE || iomode == io::mode::RDWR); - assert(len(mode) == 0 || len(mode) == 1); - let fmode = if (len(mode) != 0) mode[0] else 0o644: fs::mode; let oflags = fs::flag::EXCL; if (iomode == io::mode::RDWR) { oflags |= fs::flag::RDWR; @@ -70,7 +59,7 @@ export fn named( const name = fmt::bsprintf(namebuf, "temp.{}", id); const path = path::set(&pathbuf, path, name)!; - match (fs::create_file(fs, path, fmode, oflags)) { + match (fs::create_file(fs, path, mode, oflags)) { case errors::exists => continue; case let err: fs::error => diff --git a/temp/+linux.ha b/temp/+linux.ha @@ -21,16 +21,8 @@ fn get_tmpdir() str = os::tryenv("TMPDIR", "/tmp"); // when the stream is closed. // // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. -// -// Only one variadic argument may be provided, if at all, to specify the mode of -// the new file. The default is 0o644. -export fn file( - iomode: io::mode, - mode: fs::mode... -) (io::file | fs::error) = { +export fn file(iomode: io::mode, mode: fs::mode) (io::file | fs::error) = { assert(iomode == io::mode::WRITE || iomode == io::mode::RDWR); - assert(len(mode) == 0 || len(mode) == 1); - let fmode = if (len(mode) != 0) mode[0] else 0o644: fs::mode; let oflags = fs::flag::TMPFILE | fs::flag::EXCL; if (iomode == io::mode::RDWR) { oflags |= fs::flag::RDWR; @@ -38,7 +30,7 @@ export fn file( oflags |= fs::flag::WRONLY; }; // TODO: Add a custom "close" function which removes the named file - match (os::create(get_tmpdir(), fmode, oflags)) { + match (os::create(get_tmpdir(), mode, oflags)) { case let err: fs::error => let file = named(os::cwd, get_tmpdir(), iomode, mode...)?; free(file.1); @@ -54,19 +46,14 @@ export fn file( // overwritten on subsequent calls. // // The I/O mode must be either [[io::mode::WRITE]] or [[io::mode::RDWR]]. -// -// Only one variadic argument may be provided, if at all, to specify the mode of -// the new file. The default is 0o644. export fn named( fs: *fs::fs, path: str, iomode: io::mode, - mode: fs::mode... + mode: fs::mode, ) ((io::file, str) | fs::error) = { assert(iomode == io::mode::WRITE || iomode == io::mode::RDWR); - assert(len(mode) == 0 || len(mode) == 1); - let fmode = if (len(mode) != 0) mode[0] else 0o644: fs::mode; let oflags = fs::flag::EXCL; if (iomode == io::mode::RDWR) { oflags |= fs::flag::RDWR; @@ -84,7 +71,7 @@ export fn named( const name = fmt::bsprintf(namebuf, "temp.{}", id); const path = path::set(&pathbuf, path, name)!; - match (fs::create_file(fs, path, fmode, oflags)) { + match (fs::create_file(fs, path, mode, oflags)) { case errors::exists => continue; case let err: fs::error =>