[PATCH v2 1/2] tools/nolibc: add ftruncate()

Jordan Richards posted 2 patches 2 days, 3 hours ago
[PATCH v2 1/2] tools/nolibc: add ftruncate()
Posted by Jordan Richards 2 days, 3 hours ago
---
 tools/include/nolibc/unistd.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index bb5e80f3f05d..845d10f48fd0 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -48,6 +48,17 @@ int access(const char *path, int amode)
 	return faccessat(AT_FDCWD, path, amode, 0);
 }
 
+static __attribute__((unused))
+int sys_ftruncate(int fd, off_t length)
+{
+	return my_syscall2(__NR_ftruncate, fd, length);
+}
+
+static __attribute__((unused))
+int ftruncate(int fd, off_t length)
+{
+	return __sysret(sys_ftruncate(fd, length));
+}
 
 static __attribute__((unused))
 int msleep(unsigned int msecs)
-- 
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v2 1/2] tools/nolibc: add ftruncate()
Posted by Thomas Weißschuh 1 day, 3 hours ago
Hi Jordan,

(please Cc nolibc maintainers on nolibc changes, I only saw this by
chance)

On 2026-02-05 22:23:28+0000, Jordan Richards wrote:
> ---
>  tools/include/nolibc/unistd.h | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
> index bb5e80f3f05d..845d10f48fd0 100644
> --- a/tools/include/nolibc/unistd.h
> +++ b/tools/include/nolibc/unistd.h
> @@ -48,6 +48,17 @@ int access(const char *path, int amode)
>  	return faccessat(AT_FDCWD, path, amode, 0);
>  }
>  
> +static __attribute__((unused))
> +int sys_ftruncate(int fd, off_t length)
> +{
> +	return my_syscall2(__NR_ftruncate, fd, length);
> +}

off_t is now always 64bit on nolibc. On 32-bit architectures however
__NR_ftruncate only takes a 32bit value, leading to the wrong kind of
truncation. Use __NR_ftruncate64 instead where available.

Please also add a simple test to
tools/testing/selftests/nolibc/nolibc-test.c

> +
> +static __attribute__((unused))
> +int ftruncate(int fd, off_t length)
> +{
> +	return __sysret(sys_ftruncate(fd, length));
> +}
>  
>  static __attribute__((unused))
>  int msleep(unsigned int msecs)
> -- 
> 2.53.0.rc2.204.g2597b5adb4-goog
>