commit 4535c99aadc6b6765007242abd877580bfe3f47e
parent 311c41956e32a43afc1c21d9addbfbbab7c59514
Author: Autumn! <autumnull@posteo.net>
Date: Tue, 9 May 2023 16:46:07 +0000
io: implement trunc()
Signed-off-by: Autumn! <autumnull@posteo.net>
Diffstat:
6 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/io/+freebsd/platform_trunc.ha b/io/+freebsd/platform_trunc.ha
@@ -0,0 +1,10 @@
+// License: MPL-2.0
+use errors;
+use rt;
+
+ fn platform_trunc(fd: file, ln: size) (void | error) = {
+ match (rt::ftruncate(fd: int, ln: rt::off_t)) {
+ case void => void;
+ case let e: rt::errno => return errors::errno(e);
+ };
+ };
diff --git a/io/+linux/platform_trunc.ha b/io/+linux/platform_trunc.ha
@@ -0,0 +1,10 @@
+// License: MPL-2.0
+use errors;
+use rt;
+
+ fn platform_trunc(fd: file, ln: size) (void | error) = {
+ match (rt::ftruncate(fd: int, ln: rt::off_t)) {
+ case void => void;
+ case let e: rt::errno => return errors::errno(e);
+ };
+ };
diff --git a/io/trunc.ha b/io/trunc.ha
@@ -0,0 +1,4 @@
+// License: MPL-2.0
+
+// Truncate a file to a specified length. The file must be open for writing.
+export fn trunc(fd: file, ln: size) (void | error) = platform_trunc(fd, ln);
diff --git a/rt/+freebsd/syscalls.ha b/rt/+freebsd/syscalls.ha
@@ -72,6 +72,10 @@ export fn lseek(fd: int, off: i64, whence: int) (i64 | errno) = {
fd: u64, off: u64, whence: u64))?: i64;
};
+export fn ftruncate(fd: int, ln: off_t) (void | errno) = {
+ wrap_return(syscall2(SYS_ftruncate, fd: u64, ln: u32))?;
+};
+
export fn pipe2(pipefd: *[2]int, flags: int) (void | errno) = {
wrap_return(syscall2(SYS_pipe2, pipefd: uintptr: u64, flags: u64))?;
};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -893,6 +893,7 @@ gensrcs_io() {
+linux/file.ha \
+linux/mmap.ha \
+linux/platform_lock.ha \
+ +linux/platform_trunc.ha \
+linux/vector.ha \
copy.ha \
drain.ha \
@@ -902,6 +903,7 @@ gensrcs_io() {
lock.ha \
stream.ha \
tee.ha \
+ trunc.ha \
types.ha \
util.ha \
zero.ha \
@@ -911,6 +913,7 @@ gensrcs_io() {
+freebsd/file.ha \
+freebsd/mmap.ha \
+freebsd/platform_lock.ha \
+ +freebsd/platform_trunc.ha \
+freebsd/vector.ha \
copy.ha \
drain.ha \
@@ -920,6 +923,7 @@ gensrcs_io() {
lock.ha \
stream.ha \
tee.ha \
+ trunc.ha \
types.ha \
util.ha \
zero.ha \
diff --git a/stdlib.mk b/stdlib.mk
@@ -1463,6 +1463,7 @@ stdlib_io_linux_srcs = \
$(STDLIB)/io/+linux/file.ha \
$(STDLIB)/io/+linux/mmap.ha \
$(STDLIB)/io/+linux/platform_lock.ha \
+ $(STDLIB)/io/+linux/platform_trunc.ha \
$(STDLIB)/io/+linux/vector.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
@@ -1472,6 +1473,7 @@ stdlib_io_linux_srcs = \
$(STDLIB)/io/lock.ha \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
+ $(STDLIB)/io/trunc.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
$(STDLIB)/io/zero.ha
@@ -1482,6 +1484,7 @@ stdlib_io_freebsd_srcs = \
$(STDLIB)/io/+freebsd/file.ha \
$(STDLIB)/io/+freebsd/mmap.ha \
$(STDLIB)/io/+freebsd/platform_lock.ha \
+ $(STDLIB)/io/+freebsd/platform_trunc.ha \
$(STDLIB)/io/+freebsd/vector.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
@@ -1491,6 +1494,7 @@ stdlib_io_freebsd_srcs = \
$(STDLIB)/io/lock.ha \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
+ $(STDLIB)/io/trunc.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
$(STDLIB)/io/zero.ha
@@ -3730,6 +3734,7 @@ testlib_io_linux_srcs = \
$(STDLIB)/io/+linux/file.ha \
$(STDLIB)/io/+linux/mmap.ha \
$(STDLIB)/io/+linux/platform_lock.ha \
+ $(STDLIB)/io/+linux/platform_trunc.ha \
$(STDLIB)/io/+linux/vector.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
@@ -3739,6 +3744,7 @@ testlib_io_linux_srcs = \
$(STDLIB)/io/lock.ha \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
+ $(STDLIB)/io/trunc.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
$(STDLIB)/io/zero.ha \
@@ -3751,6 +3757,7 @@ testlib_io_freebsd_srcs = \
$(STDLIB)/io/+freebsd/file.ha \
$(STDLIB)/io/+freebsd/mmap.ha \
$(STDLIB)/io/+freebsd/platform_lock.ha \
+ $(STDLIB)/io/+freebsd/platform_trunc.ha \
$(STDLIB)/io/+freebsd/vector.ha \
$(STDLIB)/io/copy.ha \
$(STDLIB)/io/drain.ha \
@@ -3760,6 +3767,7 @@ testlib_io_freebsd_srcs = \
$(STDLIB)/io/lock.ha \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
+ $(STDLIB)/io/trunc.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
$(STDLIB)/io/zero.ha \