Skip to content

Commit f18e586

Browse files
felixhandtehswong3i
authored andcommitted
Use utimensat() on FreeBSD
FreeBSD only claims to support POSIX 2001 [0]. But they do in fact support `utimensat()`. This adds a specific check to opt them in to using it. This value was selected by consulting [1]. See discussion on facebook#3952. Further addresses facebook#3748. [0] https://github.com/freebsd/freebsd-src/blob/937a0055858a098027f464abf0b2b1ec5d36748f/sys/sys/unistd.h#L96 [1] https://docs.freebsd.org/en/books/porters-handbook/versions/
1 parent 2670c95 commit f18e586

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

programs/util.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,27 @@ extern "C" {
2323
#include <errno.h>
2424
#include <assert.h>
2525

26+
#if defined(__FreeBSD__)
27+
#include <sys/param.h> /* __FreeBSD_version */
28+
#endif /* #ifdef __FreeBSD__ */
29+
2630
#if defined(_WIN32)
2731
# include <sys/utime.h> /* utime */
2832
# include <io.h> /* _chmod */
33+
# define ZSTD_USE_UTIMENSAT 0
2934
#else
3035
# include <unistd.h> /* chown, stat */
31-
# if PLATFORM_POSIX_VERSION < 200809L || !defined(st_mtime)
32-
# include <utime.h> /* utime */
36+
# include <sys/stat.h> /* utimensat, st_mtime */
37+
# if (PLATFORM_POSIX_VERSION >= 200809L && defined(st_mtime)) \
38+
|| (defined(__FreeBSD__) && __FreeBSD_version >= 1100056)
39+
# define ZSTD_USE_UTIMENSAT 1
3340
# else
41+
# define ZSTD_USE_UTIMENSAT 0
42+
# endif
43+
# if ZSTD_USE_UTIMENSAT
3444
# include <fcntl.h> /* AT_FDCWD */
35-
# include <sys/stat.h> /* utimensat */
45+
# else
46+
# include <utime.h> /* utime */
3647
# endif
3748
#endif
3849

@@ -259,7 +270,12 @@ int UTIL_utime(const char* filename, const stat_t *statbuf)
259270
* that struct stat has a struct timespec st_mtim member. We need this
260271
* check because there are some platforms that claim to be POSIX 2008
261272
* compliant but which do not have st_mtim... */
262-
#if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime)
273+
/* FreeBSD has implemented POSIX 2008 for a long time but still only
274+
* advertises support for POSIX 2001. They have a version macro that
275+
* lets us safely gate them in.
276+
* See https://docs.freebsd.org/en/books/porters-handbook/versions/.
277+
*/
278+
#if ZSTD_USE_UTIMENSAT
263279
{
264280
/* (atime, mtime) */
265281
struct timespec timebuf[2] = { {0, UTIME_NOW} };
@@ -1546,7 +1562,6 @@ int UTIL_countCores(int logical)
15461562

15471563
#elif defined(__FreeBSD__)
15481564

1549-
#include <sys/param.h>
15501565
#include <sys/sysctl.h>
15511566

15521567
/* Use physical core sysctl when available

0 commit comments

Comments
 (0)