Initial commit
Some checks failed
Build Kernel / Build all affected Kernels (push) Has been cancelled
Build all core packages / Build all core packages for selected target (push) Has been cancelled
Build and Push prebuilt tools container / Build and Push all prebuilt containers (push) Has been cancelled
Build Toolchains / Build Toolchains for each target (push) Has been cancelled
Build host tools / Build host tools for linux and macos based systems (push) Has been cancelled
Coverity scan build / Coverity x86/64 build (push) Has been cancelled

This commit is contained in:
domenico
2025-06-24 14:35:53 +02:00
commit c06fb25d1f
9263 changed files with 1750214 additions and 0 deletions

28
tools/gnulib/Makefile Normal file
View File

@@ -0,0 +1,28 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=gnulib
PKG_CPE_ID:=cpe:/a:gnu:$(PKG_NAME)
PKG_VERSION:=c99c8d491850dc3a6e0b8604a2729d8bc5c0eff1# # stable-202401
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://git.savannah.gnu.org/cgit/$(PKG_NAME).git/snapshot
PKG_HASH:=8e6f4a907d9677b55fd452e1340a3e030a6f530b138d420c11975da33f086b1e
include $(INCLUDE_DIR)/host-build.mk
define Host/Configure
endef
define Host/Install
$(INSTALL_DIR) $(1)/share/aclocal
$(INSTALL_DATA) $(HOST_BUILD_DIR)/m4/*.m4 $(1)/share/aclocal/
$(INSTALL_DIR) $(1)/share/gnulib
$(CP) $(HOST_BUILD_DIR)/* $(1)/share/gnulib/
ln -sf ../share/gnulib/gnulib-tool $(STAGING_DIR_HOST)/bin/gnulib-tool
endef
define Host/Clean
rm -rf $(STAGING_DIR_HOST)/bin/gnulib-tool $(STAGING_DIR_HOST)/share/gnulib
endef
$(eval $(call HostBuild))

View File

@@ -0,0 +1,54 @@
--- a/build-aux/bootstrap
+++ b/build-aux/bootstrap
@@ -237,14 +237,14 @@ test -r "$conffile" && . "$conffile"
check_exists() {
if test "$1" = "--verbose"; then
- ($2 --version </dev/null) >/dev/null 2>&1
+ ($2 $3 $4 --version </dev/null) >/dev/null 2>&1
if test $? -ge 126; then
# If not found, run with diagnostics as one may be
# presented with env variables to set to find the right version
- ($2 --version </dev/null)
+ ($2 $3 $4 --version </dev/null)
fi
else
- ($1 --version </dev/null) >/dev/null 2>&1
+ ($@ --version </dev/null) >/dev/null 2>&1
fi
test $? -lt 126
@@ -309,7 +309,7 @@ p
q'
get_version() {
- app=$1
+ app="$@"
$app --version >/dev/null 2>&1 || { $app --version; return 1; }
@@ -366,13 +366,13 @@ check_versions() {
if [ "$req_ver" = "-" ]; then
# Merely require app to exist; not all prereq apps are well-behaved
# so we have to rely on $? rather than get_version.
- if ! check_exists --verbose $app; then
+ if ! check_exists --verbose "$app"; then
warn_ "Error: '$app' not found"
ret=1
fi
else
# Require app to produce a new enough version string.
- inst_ver=$(get_version $app)
+ inst_ver=$(get_version "$app")
if [ ! "$inst_ver" ]; then
warn_ "Error: '$app' not found"
ret=1
@@ -1157,7 +1157,7 @@ autogen()
# two just-pre-run programs.
# Import from gettext.
- with_gettext=yes
+ with_gettext=no
grep '^[ ]*AM_GNU_GETTEXT_VERSION(' configure.ac >/dev/null || \
with_gettext=no

View File

@@ -0,0 +1,47 @@
--- a/gnulib-tool
+++ b/gnulib-tool
@@ -346,6 +346,34 @@ Options for --import, --add/remove-impor
Report bugs to <bug-gnulib@gnu.org>."
}
+get_version_sed='
+# Move version to start of line.
+s/.*[v ]\([0-9]\)/\1/
+
+# Skip lines that do not start with version.
+/^[0-9]/!d
+
+# Remove characters after the version.
+s/[^.a-z0-9-].*//
+
+# The first component must be digits only.
+s/^\([0-9]*\)[a-z-].*/\1/
+
+# The following essentially does s/5.005/5.5/
+s/\.0*\([1-9]\)/.\1/g
+p
+q'
+
+# get_version
+# copied from build-aux/bootstrap
+get_version () {
+ app=$1
+
+ $app --version >/dev/null 2>&1 || { $app --version; return 1; }
+
+ $app --version 2>&1 | sed -n "$get_version_sed"
+}
+
# func_version
# outputs to stdout the --version message.
func_version ()
@@ -1620,6 +1648,9 @@ func_determine_path_separator
fi
case "$autoconf_minversion" in
1.* | 2.[0-5]* | 2.6[0-3]*)
+ # if the version of autoconf in use is high enough, do not error
+ case "$(get_version autoconf)" in 1.* | 2.[0-5]* | 2.6[0-3]*) false ;; esac && \
+ func_warning "gnulib requires a newer version of autoconf than configure.ac ( $DEFAULT_AUTOCONF_MINVERSION > AC_PREREQ([$autoconf_minversion]) )" || \
func_fatal_error "minimum supported autoconf version is 2.64. Try adding AC_PREREQ([$DEFAULT_AUTOCONF_MINVERSION]) to your configure.ac." ;;
esac

View File

@@ -0,0 +1,19 @@
--- /dev/null
+++ b/lib/fts.h
@@ -0,0 +1,6 @@
+#ifdef __APPLE__
+# define _FTS_H_ 1
+# include <fts_.h>
+#else
+# include_next <fts.h>
+#endif
--- a/modules/fts
+++ b/modules/fts
@@ -2,6 +2,7 @@ Description:
Traverse a file hierarchy.
Files:
+lib/fts.h
lib/fts_.h
lib/fts.c
lib/fts-cycle.c

View File

@@ -0,0 +1,193 @@
--- a/lib/search.in.h
+++ b/lib/search.in.h
@@ -112,6 +112,11 @@ _GL_CXXALIASWARN (lsearch);
# define twalk rpl_twalk
# endif
# endif
+# if @REPLACE_TDESTROY@
+# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+# define tdestroy rpl_tdestroy
+# endif
+# endif
/* See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/search.h.html>
<https://pubs.opengroup.org/onlinepubs/9699919799/functions/tsearch.html>
@@ -137,6 +142,7 @@ extern "C" {
# if !GNULIB_defined_search_fn_types
typedef int (*_gl_search_compar_fn) (const void *, const void *);
typedef void (*_gl_search_action_fn) (const void *, VISIT, int);
+typedef void (*_gl_search_free_fn) (void *);
# define GNULIB_defined_search_fn_types 1
# endif
# ifdef __cplusplus
@@ -252,9 +258,36 @@ _GL_CXXALIAS_SYS (twalk, void,
_GL_CXXALIASWARN (twalk);
# endif
+/* Removes the whole tree pointed to by root,
+ freeing all resources allocated by the tsearch() function.
+ The FREE_NODE function is called:
+ - For the data in each tree node.
+ - Even when no such work is necessary, to a function doing nothing
+ The arguments passed to FREE_NODE are:
+ 1. The pointer to the data. */
+# if @REPLACE_TDESTROY@
+_GL_FUNCDECL_RPL (tdestroy, void,
+ (void *vroot, _gl_search_free_fn freefct)
+ _GL_ARG_NONNULL ((2)));
+_GL_CXXALIAS_RPL (tdestroy, void,
+ (void *vroot, _gl_search_free_fn freefct));
+# else
+# if !@HAVE_TDESTROY@
+_GL_FUNCDECL_SYS (tdestroy, void,
+ (void *vroot, _gl_search_free_fn freefct)
+ _GL_ARG_NONNULL ((2)));
+# endif
+_GL_CXXALIAS_SYS (tdestroy, void,
+ (void *vroot, _gl_search_free_fn freefct));
+# endif
+# if __GLIBC__ >= 2
+_GL_CXXALIASWARN (tdestroy);
+# endif
+
/* Flags used by tsearch.c. */
# define GNULIB_defined_tsearch (@REPLACE_TSEARCH@ || !@HAVE_TSEARCH@)
# define GNULIB_defined_twalk (@REPLACE_TWALK@ || !@HAVE_TWALK@)
+# define GNULIB_defined_tdestroy (@REPLACE_TDESTROY@ || !@HAVE_TDESTROY@)
#elif defined GNULIB_POSIXCHECK
# undef tsearch
@@ -277,6 +310,11 @@ _GL_WARN_ON_USE (tdelete, "tdelete is un
_GL_WARN_ON_USE (twalk, "twalk is unportable - "
"use gnulib module tsearch for portability");
# endif
+# undef tdestroy
+# if HAVE_RAW_DECL_TDESTROY
+_GL_WARN_ON_USE (tdestroy, "tdestroy is unportable - "
+ "use gnulib module tsearch for portability");
+# endif
#endif
--- a/lib/tsearch.c
+++ b/lib/tsearch.c
@@ -98,12 +98,14 @@
typedef int (*__compar_fn_t) (const void *, const void *);
typedef void (*__action_fn_t) (const void *, VISIT, int);
+typedef void (*__free_fn_t) (void *);
#ifndef weak_alias
# define __tsearch tsearch
# define __tfind tfind
# define __tdelete tdelete
# define __twalk twalk
+# define __tdestroy tdestroy
#endif
#ifndef internal_function
@@ -656,7 +658,7 @@ weak_alias (__twalk, twalk)
#endif /* GNULIB_defined_twalk */
-#ifdef _LIBC
+#if defined(_LIBC) || GNULIB_defined_tdestroy
/* The standardized functions miss an important functionality: the
tree cannot be removed easily. We provide a function to do this. */
@@ -683,6 +685,8 @@ __tdestroy (void *vroot, __free_fn_t fre
if (root != NULL)
tdestroy_recurse (root, freefct);
}
+#ifdef weak_alias
weak_alias (__tdestroy, tdestroy)
+#endif
-#endif /* _LIBC */
+#endif /* defined(_LIBC) || GNULIB_defined_tdestroy */
--- a/m4/search_h.m4
+++ b/m4/search_h.m4
@@ -39,7 +39,7 @@ AC_DEFUN_ONCE([gl_SEARCH_H],
dnl Check for declarations of anything we want to poison if the
dnl corresponding gnulib module is not in use.
gl_WARN_ON_USE_PREPARE([[#include <search.h>
- ]], [tdelete tfind tsearch twalk])
+ ]], [tdelete tfind tsearch twalk tdestroy])
AC_REQUIRE([AC_C_RESTRICT])
])
@@ -75,8 +75,10 @@ AC_DEFUN([gl_SEARCH_H_DEFAULTS],
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_LFIND], [1])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_LSEARCH], [1])
dnl Assume proper GNU behavior unless another module says otherwise.
- HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH])
- HAVE_TWALK=1; AC_SUBST([HAVE_TWALK])
- REPLACE_TSEARCH=0; AC_SUBST([REPLACE_TSEARCH])
- REPLACE_TWALK=0; AC_SUBST([REPLACE_TWALK])
+ HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH])
+ HAVE_TWALK=1; AC_SUBST([HAVE_TWALK])
+ HAVE_TDESTROY=1; AC_SUBST([HAVE_TDESTROY])
+ REPLACE_TSEARCH=0; AC_SUBST([REPLACE_TSEARCH])
+ REPLACE_TWALK=0; AC_SUBST([REPLACE_TWALK])
+ REPLACE_TDESTROY=0; AC_SUBST([REPLACE_TDESTROY])
])
--- a/m4/tsearch.m4
+++ b/m4/tsearch.m4
@@ -9,6 +9,7 @@ AC_DEFUN([gl_FUNC_TSEARCH],
AC_REQUIRE([gl_SEARCH_H_DEFAULTS])
gl_CHECK_FUNCS_ANDROID([tsearch], [[#include <search.h>]])
gl_CHECK_FUNCS_ANDROID([twalk], [[#include <search.h>]])
+ gl_CHECK_FUNCS_ANDROID([tdestroy], [[#include <search.h>]])
if test $ac_cv_func_tsearch = yes; then
dnl On OpenBSD 4.0, the return value of tdelete() is incorrect.
AC_REQUIRE([AC_PROG_CC])
@@ -50,6 +51,7 @@ main ()
*no)
REPLACE_TSEARCH=1
REPLACE_TWALK=1
+ REPLACE_TDESTROY=1
;;
esac
else
@@ -64,6 +66,12 @@ main ()
future*) REPLACE_TWALK=1 ;;
esac
fi
+ if test $ac_cv_func_tdestroy != yes; then
+ HAVE_TDESTROY=0
+ case "$gl_cv_onwards_func_tdestroy" in
+ future*) REPLACE_TDESTROY=1 ;;
+ esac
+ fi
])
# Prerequisites of lib/tsearch.c.
--- a/modules/search
+++ b/modules/search
@@ -37,8 +37,10 @@ search.h: search.in.h $(top_builddir)/co
-e 's/@''GNULIB_MDA_LSEARCH''@/$(GNULIB_MDA_LSEARCH)/g' \
-e 's|@''HAVE_TSEARCH''@|$(HAVE_TSEARCH)|g' \
-e 's|@''HAVE_TWALK''@|$(HAVE_TWALK)|g' \
+ -e 's|@''HAVE_TDESTROY''@|$(HAVE_TDESTROY)|g' \
-e 's|@''REPLACE_TSEARCH''@|$(REPLACE_TSEARCH)|g' \
-e 's|@''REPLACE_TWALK''@|$(REPLACE_TWALK)|g' \
+ -e 's|@''REPLACE_TDESTROY''@|$(REPLACE_TDESTROY)|g' \
-e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
-e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
-e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
--- a/modules/tsearch
+++ b/modules/tsearch
@@ -11,7 +11,12 @@ search
configure.ac:
gl_FUNC_TSEARCH
gl_CONDITIONAL([GL_COND_OBJ_TSEARCH],
- [test $HAVE_TSEARCH = 0 || test $HAVE_TWALK = 0 || test $REPLACE_TSEARCH = 1 || test $REPLACE_TWALK = 1])
+ [test $HAVE_TSEARCH = 0 ||
+ test $HAVE_TWALK = 0 ||
+ test $HAVE_TDESTROY = 0 ||
+ test $REPLACE_TSEARCH = 1 ||
+ test $REPLACE_TWALK = 1 ||
+ test $REPLACE_TDESTROY = 1])
AM_COND_IF([GL_COND_OBJ_TSEARCH], [
gl_PREREQ_TSEARCH
])

View File

@@ -0,0 +1,115 @@
--- a/lib/ialloc.h
+++ b/lib/ialloc.h
@@ -106,6 +106,8 @@ icalloc (idx_t n, idx_t s)
return calloc (n, s);
}
+#if GNULIB_REALLOCARRAY
+
/* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size).
It returns a non-NULL pointer to num * size bytes of memory.
Upon failure, it returns NULL with errno set. */
@@ -131,6 +133,8 @@ ireallocarray (void *p, idx_t n, idx_t s
return _gl_alloc_nomem ();
}
+#endif /* GNULIB_REALLOCARRAY */
+
#ifdef __cplusplus
}
#endif
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -51,12 +51,16 @@ ximalloc (idx_t s)
return nonnull (imalloc (s));
}
+#if GNULIB_REALLOCARRAY
+
char *
xcharalloc (size_t n)
{
return XNMALLOC (n, char);
}
+#endif /* GNULIB_REALLOCARRAY */
+
/* Change the size of an allocated block of memory P to S bytes,
with error checking. */
@@ -75,6 +79,8 @@ xirealloc (void *p, idx_t s)
return nonnull (irealloc (p, s));
}
+#if GNULIB_REALLOCARRAY
+
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. */
@@ -205,6 +211,8 @@ x2nrealloc (void *p, size_t *pn, size_t
return p;
}
+#endif /* GNULIB_REALLOCARRAY */
+
/* Grow PA, which points to an array of *PN items, and return the
location of the reallocated array, updating *PN to reflect its
new size. The new array will contain at least N_INCR_MIN more
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -129,6 +129,7 @@ char *xstrdup (char const *str)
# define XCALLOC(n, t) \
((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
+# if GNULIB_REALLOCARRAY
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
@@ -156,6 +157,8 @@ char *xcharalloc (size_t n)
_GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
_GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
+# endif /* GNULIB_REALLOCARRAY */
+
#endif /* GNULIB_XALLOC */
--- a/lib/safe-alloc.h
+++ b/lib/safe-alloc.h
@@ -36,6 +36,8 @@ _GL_INLINE_HEADER_BEGIN
# define SAFE_ALLOC_INLINE _GL_INLINE
#endif
+#if GNULIB_REALLOCARRAY
+
/* Don't call these directly - use the macros below. */
SAFE_ALLOC_INLINE void *
safe_alloc_realloc_n (void *ptr, size_t count, size_t size)
@@ -51,6 +53,9 @@ safe_alloc_realloc_n (void *ptr, size_t
#endif
return ptr;
}
+
+#endif /* GNULIB_REALLOCARRAY */
+
_GL_ATTRIBUTE_NODISCARD SAFE_ALLOC_INLINE int
safe_alloc_check (void *ptr)
{
@@ -84,6 +89,8 @@ safe_alloc_check (void *ptr)
#define ALLOC_N(ptr, count) \
safe_alloc_check ((ptr) = calloc (count, sizeof *(ptr)))
+#if GNULIB_REALLOCARRAY
+
/**
* ALLOC_N_UNINITIALIZED:
* @ptr: pointer to allocated memory
@@ -112,6 +119,8 @@ safe_alloc_check (void *ptr)
#define REALLOC_N(ptr, count) \
safe_alloc_check ((ptr) = safe_alloc_realloc_n (ptr, count, sizeof *(ptr)))
+#endif /* GNULIB_REALLOCARRAY */
+
/**
* FREE:
* @ptr: pointer holding address to be freed

View File

@@ -0,0 +1,56 @@
--- a/lib/fcntl.c
+++ b/lib/fcntl.c
@@ -198,6 +198,8 @@ static int klibc_fcntl (int fd, int acti
FD_CLOEXEC is portable, but other flags may be present); otherwise
return -1 and set errno. */
+#if (GNULIB_defined_fcntl || GNULIB_defined_rpl_fcntl)
+
int
fcntl (int fd, int action, /* arg */...)
#undef fcntl
@@ -443,6 +445,8 @@ fcntl (int fd, int action, /* arg */...)
return result;
}
+#endif /* (GNULIB_defined_fcntl || GNULIB_defined_rpl_fcntl) */
+
static int
rpl_fcntl_DUPFD (int fd, int target)
{
--- a/lib/stdlib.in.h
+++ b/lib/stdlib.in.h
@@ -1447,10 +1447,16 @@ _GL_FUNCDECL_RPL (reallocarray, void *,
(void *ptr, size_t nmemb, size_t size));
_GL_CXXALIAS_RPL (reallocarray, void *,
(void *ptr, size_t nmemb, size_t size));
+# if !GNULIB_defined_rpl_reallocarray
+# define GNULIB_defined_rpl_reallocarray 1
+# endif
# else
# if ! @HAVE_REALLOCARRAY@
_GL_FUNCDECL_SYS (reallocarray, void *,
(void *ptr, size_t nmemb, size_t size));
+# if !GNULIB_defined_reallocarray
+# define GNULIB_defined_reallocarray 1
+# endif
# endif
_GL_CXXALIAS_SYS (reallocarray, void *,
(void *ptr, size_t nmemb, size_t size));
--- a/lib/reallocarray.c
+++ b/lib/reallocarray.c
@@ -23,6 +23,8 @@
#include <stdlib.h>
#include <errno.h>
+#if (GNULIB_defined_reallocarray || GNULIB_defined_rpl_reallocarray)
+
void *
reallocarray (void *ptr, size_t nmemb, size_t size)
{
@@ -36,3 +38,5 @@ reallocarray (void *ptr, size_t nmemb, s
/* Rely on the semantics of GNU realloc. */
return realloc (ptr, nbytes);
}
+
+#endif /* (GNULIB_defined_reallocarray || GNULIB_defined_rpl_reallocarray) */

View File

@@ -0,0 +1,326 @@
--- /dev/null
+++ b/modules/fallocate-posix
@@ -0,0 +1,43 @@
+Description:
+posix_fallocate function that is glibc compatible.
+
+Files:
+lib/posix_fallocate.c
+m4/fcntl_h.m4
+m4/posix_fallocate.m4
+
+Depends-on:
+errno [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+fcntl [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+fstat [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+ftruncate [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+pread [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+pwrite [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+stdint [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+sys_stat [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+unistd [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1]
+fcntl-h
+
+configure.ac:
+gl_FUNC_POSIX_FALLOCATE
+gl_CONDITIONAL([GL_COND_OBJ_POSIX_FALLOCATE],
+ [test $HAVE_FALLOCATE_POSIX = 0 || test $REPLACE_FALLOCATE_POSIX = 1])
+AM_COND_IF([GL_COND_OBJ_POSIX_FALLOCATE], [
+ gl_PREREQ_POSIX_FALLOCATE
+])
+gl_MODULE_INDICATOR([fallocate-posix])
+gl_FCNTL_MODULE_INDICATOR([fallocate-posix])
+
+Makefile.am:
+if GL_COND_OBJ_POSIX_FALLOCATE
+lib_SOURCES += posix_fallocate.c
+endif
+
+Include:
+<fcntl.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
--- /dev/null
+++ b/m4/posix_fallocate.m4
@@ -0,0 +1,20 @@
+# posix_fallocate.m4 serial 1
+dnl Copyright (C) 2024 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_POSIX_FALLOCATE],
+[
+ AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
+ gl_CHECK_FUNCS_ANDROID([posix_fallocate], [[#include <fcntl.h>]])
+ if test "$ac_cv_func_posix_fallocate" = no; then
+ HAVE_FALLOCATE_POSIX=0
+ case "$gl_cv_onwards_func_posix_fallocate" in
+ future*) REPLACE_FALLOCATE_POSIX=1 ;;
+ esac
+ fi
+])
+
+# Prerequisites of lib/posix_fallocate.c.
+AC_DEFUN([gl_PREREQ_POSIX_FALLOCATE], [:])
--- a/m4/fcntl_h.m4
+++ b/m4/fcntl_h.m4
@@ -23,7 +23,7 @@ AC_DEFUN_ONCE([gl_FCNTL_H],
dnl corresponding gnulib module is not in use, if it is not common
dnl enough to be declared everywhere.
gl_WARN_ON_USE_PREPARE([[#include <fcntl.h>
- ]], [fcntl openat])
+ ]], [fcntl openat posix_fallocate])
])
# gl_FCNTL_MODULE_INDICATOR([modulename])
@@ -50,6 +50,7 @@ AC_DEFUN([gl_FCNTL_H_REQUIRE_DEFAULTS],
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_NONBLOCKING])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OPEN])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OPENAT])
+ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FALLOCATE_POSIX])
dnl Support Microsoft deprecated alias function names by default.
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_CREAT], [1])
gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_OPEN], [1])
@@ -61,10 +62,12 @@ AC_DEFUN([gl_FCNTL_H_REQUIRE_DEFAULTS],
AC_DEFUN([gl_FCNTL_H_DEFAULTS],
[
dnl Assume proper GNU behavior unless another module says otherwise.
- HAVE_FCNTL=1; AC_SUBST([HAVE_FCNTL])
- HAVE_OPENAT=1; AC_SUBST([HAVE_OPENAT])
- REPLACE_CREAT=0; AC_SUBST([REPLACE_CREAT])
- REPLACE_FCNTL=0; AC_SUBST([REPLACE_FCNTL])
- REPLACE_OPEN=0; AC_SUBST([REPLACE_OPEN])
- REPLACE_OPENAT=0; AC_SUBST([REPLACE_OPENAT])
+ HAVE_FCNTL=1; AC_SUBST([HAVE_FCNTL])
+ HAVE_OPENAT=1; AC_SUBST([HAVE_OPENAT])
+ HAVE_FALLOCATE_POSIX=1; AC_SUBST([HAVE_FALLOCATE_POSIX])
+ REPLACE_CREAT=0; AC_SUBST([REPLACE_CREAT])
+ REPLACE_FCNTL=0; AC_SUBST([REPLACE_FCNTL])
+ REPLACE_OPEN=0; AC_SUBST([REPLACE_OPEN])
+ REPLACE_OPENAT=0; AC_SUBST([REPLACE_OPENAT])
+ REPLACE_FALLOCATE_POSIX=0; AC_SUBST([REPLACE_FALLOCATE_POSIX])
])
--- a/modules/fcntl-h
+++ b/modules/fcntl-h
@@ -40,14 +40,17 @@ fcntl.h: fcntl.in.h $(top_builddir)/conf
-e 's/@''GNULIB_NONBLOCKING''@/$(GNULIB_NONBLOCKING)/g' \
-e 's/@''GNULIB_OPEN''@/$(GNULIB_OPEN)/g' \
-e 's/@''GNULIB_OPENAT''@/$(GNULIB_OPENAT)/g' \
+ -e 's/@''GNULIB_FALLOCATE_POSIX''@/$(GNULIB_FALLOCATE_POSIX)/g' \
-e 's/@''GNULIB_MDA_CREAT''@/$(GNULIB_MDA_CREAT)/g' \
-e 's/@''GNULIB_MDA_OPEN''@/$(GNULIB_MDA_OPEN)/g' \
-e 's|@''HAVE_FCNTL''@|$(HAVE_FCNTL)|g' \
-e 's|@''HAVE_OPENAT''@|$(HAVE_OPENAT)|g' \
+ -e 's|@''HAVE_FALLOCATE_POSIX''@|$(HAVE_FALLOCATE_POSIX)|g' \
-e 's|@''REPLACE_CREAT''@|$(REPLACE_CREAT)|g' \
-e 's|@''REPLACE_FCNTL''@|$(REPLACE_FCNTL)|g' \
-e 's|@''REPLACE_OPEN''@|$(REPLACE_OPEN)|g' \
-e 's|@''REPLACE_OPENAT''@|$(REPLACE_OPENAT)|g' \
+ -e 's|@''REPLACE_FALLOCATE_POSIX''@|$(REPLACE_FALLOCATE_POSIX)|g' \
-e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
-e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
-e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
--- a/lib/fcntl.in.h
+++ b/lib/fcntl.in.h
@@ -238,6 +238,33 @@ _GL_WARN_ON_USE (openat, "openat is not
# endif
#endif
+#if @GNULIB_FALLOCATE_POSIX@
+# if @REPLACE_FALLOCATE_POSIX@
+# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+# undef posix_fallocate
+# define posix_fallocate rpl_posix_fallocate
+# endif
+_GL_FUNCDECL_RPL (posix_fallocate, int,
+ (int fd, off_t offset, off_t len));
+_GL_CXXALIAS_RPL (posix_fallocate, int,
+ (int fd, off_t offset, off_t len));
+# else
+# if !@HAVE_FALLOCATE_POSIX@
+_GL_FUNCDECL_SYS (posix_fallocate, int,
+ (int fd, off_t offset, off_t len));
+# endif
+_GL_CXXALIAS_SYS (posix_fallocate, int,
+ (int fd, off_t offset, off_t len));
+# endif
+_GL_CXXALIASWARN (posix_fallocate);
+#elif defined GNULIB_POSIXCHECK
+# undef posix_fallocate
+# if HAVE_RAW_DECL_POSIX_FALLOCATE
+_GL_WARN_ON_USE (posix_fallocate, "posix_fallocate is not portable - "
+ "use gnulib module fallocate-posix for portability");
+# endif
+#endif
+
/* Fix up the FD_* macros, only known to be missing on mingw. */
--- /dev/null
+++ b/lib/posix_fallocate.c
@@ -0,0 +1,150 @@
+/* posix_fallocate function that is glibc compatible.
+
+ Copyright (C) 2024 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <sys/fcntl.h>
+#include <sys/stat.h>
+
+#ifdef __APPLE__
+# include <sys/param.h>
+# include <sys/mount.h>
+#else
+# include <sys/statfs.h>
+#endif
+
+/* Reserve storage for the data of the file associated with FD. This
+ emulation is far from perfect, but the kernel cannot do not much
+ better for network file systems, either. */
+
+int
+posix_fallocate (int fd, off_t offset, off_t len)
+{
+ int ret;
+ struct stat st;
+
+ if (fd < 0 || offset < 0 || len < 0)
+ return EINVAL;
+
+ /* Perform overflow check. The outer cast relies on a GCC
+ extension. */
+ if ((off_t) ((uint64_t) offset + (uint64_t) len) < 0)
+ return EFBIG;
+
+ /* pwrite below will not do the right thing in O_APPEND mode. */
+ {
+ int flags = fcntl (fd, F_GETFL, 0);
+ if (flags < 0 || (flags & O_APPEND) != 0)
+ return EBADF;
+ }
+
+ /* We have to make sure that this is really a regular file. */
+ if (fstat (fd, &st) != 0)
+ return EBADF;
+ if (S_ISFIFO (st.st_mode))
+ return ESPIPE;
+ if (! S_ISREG (st.st_mode))
+ return ENODEV;
+
+ if (len == 0)
+ {
+ /* This is racy, but there is no good way to satisfy a
+ zero-length allocation request. */
+ if (st.st_size < offset)
+ {
+ ret = ftruncate (fd, offset);
+
+ if (ret != 0)
+ ret = errno;
+ return ret;
+ }
+ return ret;
+ }
+
+#ifdef __APPLE__
+ fstore_t sto = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, offset + len, 0};
+ /* allocate continuous */
+ ret = fcntl (fd, F_PREALLOCATE, &sto);
+ if (ret < 0)
+ {
+ /* allocate non-continuous */
+ sto.fst_flags = F_ALLOCATEALL;
+ ret = fcntl (fd, F_PREALLOCATE, &sto);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
+ ret = ftruncate(fd, offset + len);
+#else
+
+ /* Minimize data transfer for network file systems, by issuing
+ single-byte write requests spaced by the file system block size.
+ (Most local file systems have fallocate support, so this fallback
+ code is not used there.) */
+
+ unsigned increment;
+ {
+ struct statfs f;
+
+ if (fstatfs (fd, &f) != 0)
+ return errno;
+ if (f.f_bsize == 0)
+ increment = 512;
+ else if (f.f_bsize < 4096)
+ increment = f.f_bsize;
+ else
+ /* NFS does not propagate the block size of the underlying
+ storage and may report a much larger value which would still
+ leave holes after the loop below, so we cap the increment at
+ 4096. */
+ increment = 4096;
+ }
+
+ /* Write a null byte to every block. This is racy; we currently
+ lack a better option. Compare-and-swap against a file mapping
+ might additional local races, but requires interposition of a
+ signal handler to catch SIGBUS. */
+ for (offset += (len - 1) % increment; len > 0; offset += increment)
+ {
+ len -= increment;
+
+ if (offset < st.st_size)
+ {
+ unsigned char c;
+ ssize_t rsize = pread (fd, &c, 1, offset);
+
+ if (rsize < 0)
+ return errno;
+ /* If there is a non-zero byte, the block must have been
+ allocated already. */
+ else if (rsize == 1 && c != 0)
+ continue;
+ }
+
+ if (pwrite (fd, "", 1, offset) != 1)
+ return errno;
+ }
+
+#endif /* __APPLE__ */
+
+ return ret;
+}
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2552,6 +2552,7 @@ func_all_modules ()
func_module execve
func_module execvp
func_module execvpe
+ func_module fallocate-posix
func_module fchdir
func_module fclose
func_module fcntl-h