Message ID | 20211122004913.20052-10-wwcohen@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | 9p: Add support for darwin | expand |
On Montag, 22. November 2021 01:49:11 CET Will Cohen wrote: > From: Keno Fischer <keno@juliacomputing.com> > > This function is new in Mac OS 10.13. Provide a fallback implementation > when building against older SDKs. The complication in the definition comes > having to separately handle the used SDK version and the target OS version. > > - If the SDK version is too low (__MAC_10_13 not defined), utimensat is not > defined in the header, so we must not try to use it (doing so would > error). - Otherwise, if the targetted OS version is at least 10.13, we know > this function is available, so we can unconditionally call it. AFAIK QEMU officially only supports "the last two released versions" of macOS, and Peter periodically wipes backward compatibility code for older macOS versions with new release cycles: commit 483644c25b932360018d15818d8bcd8c85ba70b8 Author: Peter Maydell <peter.maydell@linaro.org> Date: Sat Feb 1 17:05:34 2020 +0000 ui/cocoa: Drop workarounds for pre-10.12 OSX Our official OSX support policy covers the last two released versions. Currently that is 10.14 and 10.15. We also may work on older versions, but don't guarantee it. In commit 50290c002c045280f8d in mid-2019 we introduced some uses of CLOCK_MONOTONIC which incidentally broke compilation for pre-10.12 OSX versions (see LP:1861551). We don't intend to fix that, so we might as well drop the code in ui/cocoa.m which caters for pre-10.12 versions as well. (For reference, 10.11 fell out of Apple extended security support in September 2018.) Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Message-Id: <20200201170534.22123-1-peter.maydell@linaro.org> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> And this series would not go into a QEMU release prior early next year, so probably dropping this macOS backward compatibility code right from the start? > - Lastly, we check for the availability of the __builtin_available macro to > potentially insert a dynamic check for this OS version. However, > __builtin_available is only available with sufficiently recent versions of > clang and while all Apple clang versions that ship with Xcode versions that > support the 10.13 SDK support with builtin, we want to allow building with > compilers other than Apple clang that may not support this builtin. > > Signed-off-by: Keno Fischer <keno@juliacomputing.com> > Signed-off-by: Michael Roitzsch <reactorcontrol@icloud.com> > Signed-off-by: Will Cohen <wwcohen@gmail.com> > --- > hw/9pfs/9p-local.c | 2 +- > hw/9pfs/9p-util-darwin.c | 96 ++++++++++++++++++++++++++++++++++++++++ > hw/9pfs/9p-util-linux.c | 6 +++ > hw/9pfs/9p-util.h | 8 ++++ > hw/9pfs/9p.c | 1 + > 5 files changed, 112 insertions(+), 1 deletion(-) > > diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c > index 2bfff79b12..4268703d05 100644 > --- a/hw/9pfs/9p-local.c > +++ b/hw/9pfs/9p-local.c > @@ -1076,7 +1076,7 @@ static int local_utimensat(FsContext *s, V9fsPath > *fs_path, goto out; > } > > - ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); > + ret = utimensat_nofollow(dirfd, name, buf); > close_preserve_errno(dirfd); > out: > g_free(dirpath); > diff --git a/hw/9pfs/9p-util-darwin.c b/hw/9pfs/9p-util-darwin.c > index cdb4c9e24c..ac414bcbfd 100644 > --- a/hw/9pfs/9p-util-darwin.c > +++ b/hw/9pfs/9p-util-darwin.c > @@ -62,3 +62,99 @@ int fsetxattrat_nofollow(int dirfd, const char *filename, > const char *name, close_preserve_errno(fd); > return ret; > } > + > +#ifndef __has_builtin > +#define __has_builtin(x) 0 > +#endif > + > +static int update_times_from_stat(int fd, struct timespec times[2], > + int update0, int update1) > +{ > + struct stat buf; > + int ret = fstat(fd, &buf); > + if (ret == -1) { > + return ret; > + } > + if (update0) { > + times[0] = buf.st_atimespec; > + } > + if (update1) { > + times[1] = buf.st_mtimespec; > + } > + return 0; > +} > + > +int utimensat_nofollow(int dirfd, const char *filename, > + const struct timespec times_in[2]) > +{ > + int ret, fd; > + int special0, special1; > + struct timeval futimes_buf[2]; > + struct timespec times[2]; > + memcpy(times, times_in, 2 * sizeof(struct timespec)); > + > +/* Check whether we have an SDK version that defines utimensat */ > +#if defined(__MAC_10_13) > +# if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_13 > +# define UTIMENSAT_AVAILABLE 1 > +# elif __has_builtin(__builtin_available) > +# define UTIMENSAT_AVAILABLE __builtin_available(macos 10.13, *) > +# else > +# define UTIMENSAT_AVAILABLE 0 > +# endif > + if (UTIMENSAT_AVAILABLE) { > + return utimensat(dirfd, filename, times, AT_SYMLINK_NOFOLLOW); > + } > +#endif > + > + /* utimensat not available. Use futimes. */ > + fd = openat_file(dirfd, filename, O_PATH_9P_UTIL | O_NOFOLLOW, 0); > + if (fd == -1) { > + return -1; > + } > + > + special0 = times[0].tv_nsec == UTIME_OMIT; > + special1 = times[1].tv_nsec == UTIME_OMIT; > + if (special0 || special1) { > + /* If both are set, nothing to do */ > + if (special0 && special1) { > + ret = 0; > + goto done; > + } > + > + ret = update_times_from_stat(fd, times, special0, special1); > + if (ret < 0) { > + goto done; > + } > + } > + > + special0 = times[0].tv_nsec == UTIME_NOW; > + special1 = times[1].tv_nsec == UTIME_NOW; > + if (special0 || special1) { > + ret = futimes(fd, NULL); > + if (ret < 0) { > + goto done; > + } > + > + /* If both are set, we are done */ > + if (special0 && special1) { > + ret = 0; > + goto done; > + } > + > + ret = update_times_from_stat(fd, times, special0, special1); > + if (ret < 0) { > + goto done; > + } > + } > + > + futimes_buf[0].tv_sec = times[0].tv_sec; > + futimes_buf[0].tv_usec = times[0].tv_nsec / 1000; > + futimes_buf[1].tv_sec = times[1].tv_sec; > + futimes_buf[1].tv_usec = times[1].tv_nsec / 1000; > + ret = futimes(fd, futimes_buf); > + > +done: > + close_preserve_errno(fd); > + return ret; > +} > diff --git a/hw/9pfs/9p-util-linux.c b/hw/9pfs/9p-util-linux.c > index 398614a5d0..d54bf57a59 100644 > --- a/hw/9pfs/9p-util-linux.c > +++ b/hw/9pfs/9p-util-linux.c > @@ -62,3 +62,9 @@ int fsetxattrat_nofollow(int dirfd, const char *filename, > const char *name, g_free(proc_path); > return ret; > } > + > +int utimensat_nofollow(int dirfd, const char *filename, > + const struct timespec times[2]) > +{ > + return utimensat(dirfd, filename, times, AT_SYMLINK_NOFOLLOW); > +} > diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h > index 38ef8b289d..1c477a0e66 100644 > --- a/hw/9pfs/9p-util.h > +++ b/hw/9pfs/9p-util.h > @@ -36,6 +36,12 @@ static inline int qemu_lsetxattr(const char *path, const > char *name, #define qemu_lsetxattr lsetxattr > #endif > > +/* Compatibility with old SDK Versions for Darwin */ > +#if defined(CONFIG_DARWIN) && !defined(UTIME_NOW) > +#define UTIME_NOW -1 > +#define UTIME_OMIT -2 > +#endif > + > static inline void close_preserve_errno(int fd) > { > int serrno = errno; > @@ -96,5 +102,7 @@ ssize_t flistxattrat_nofollow(int dirfd, const char > *filename, char *list, size_t size); > ssize_t fremovexattrat_nofollow(int dirfd, const char *filename, > const char *name); > +int utimensat_nofollow(int dirfd, const char *filename, > + const struct timespec times[2]); > > #endif > diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c > index d671995aa4..3d676405c7 100644 > --- a/hw/9pfs/9p.c > +++ b/hw/9pfs/9p.c > @@ -27,6 +27,7 @@ > #include "virtio-9p.h" > #include "fsdev/qemu-fsdev.h" > #include "9p-xattr.h" > +#include "9p-util.h" > #include "coth.h" > #include "trace.h" > #include "migration/blocker.h"
diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 2bfff79b12..4268703d05 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -1076,7 +1076,7 @@ static int local_utimensat(FsContext *s, V9fsPath *fs_path, goto out; } - ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); + ret = utimensat_nofollow(dirfd, name, buf); close_preserve_errno(dirfd); out: g_free(dirpath); diff --git a/hw/9pfs/9p-util-darwin.c b/hw/9pfs/9p-util-darwin.c index cdb4c9e24c..ac414bcbfd 100644 --- a/hw/9pfs/9p-util-darwin.c +++ b/hw/9pfs/9p-util-darwin.c @@ -62,3 +62,99 @@ int fsetxattrat_nofollow(int dirfd, const char *filename, const char *name, close_preserve_errno(fd); return ret; } + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +static int update_times_from_stat(int fd, struct timespec times[2], + int update0, int update1) +{ + struct stat buf; + int ret = fstat(fd, &buf); + if (ret == -1) { + return ret; + } + if (update0) { + times[0] = buf.st_atimespec; + } + if (update1) { + times[1] = buf.st_mtimespec; + } + return 0; +} + +int utimensat_nofollow(int dirfd, const char *filename, + const struct timespec times_in[2]) +{ + int ret, fd; + int special0, special1; + struct timeval futimes_buf[2]; + struct timespec times[2]; + memcpy(times, times_in, 2 * sizeof(struct timespec)); + +/* Check whether we have an SDK version that defines utimensat */ +#if defined(__MAC_10_13) +# if __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_13 +# define UTIMENSAT_AVAILABLE 1 +# elif __has_builtin(__builtin_available) +# define UTIMENSAT_AVAILABLE __builtin_available(macos 10.13, *) +# else +# define UTIMENSAT_AVAILABLE 0 +# endif + if (UTIMENSAT_AVAILABLE) { + return utimensat(dirfd, filename, times, AT_SYMLINK_NOFOLLOW); + } +#endif + + /* utimensat not available. Use futimes. */ + fd = openat_file(dirfd, filename, O_PATH_9P_UTIL | O_NOFOLLOW, 0); + if (fd == -1) { + return -1; + } + + special0 = times[0].tv_nsec == UTIME_OMIT; + special1 = times[1].tv_nsec == UTIME_OMIT; + if (special0 || special1) { + /* If both are set, nothing to do */ + if (special0 && special1) { + ret = 0; + goto done; + } + + ret = update_times_from_stat(fd, times, special0, special1); + if (ret < 0) { + goto done; + } + } + + special0 = times[0].tv_nsec == UTIME_NOW; + special1 = times[1].tv_nsec == UTIME_NOW; + if (special0 || special1) { + ret = futimes(fd, NULL); + if (ret < 0) { + goto done; + } + + /* If both are set, we are done */ + if (special0 && special1) { + ret = 0; + goto done; + } + + ret = update_times_from_stat(fd, times, special0, special1); + if (ret < 0) { + goto done; + } + } + + futimes_buf[0].tv_sec = times[0].tv_sec; + futimes_buf[0].tv_usec = times[0].tv_nsec / 1000; + futimes_buf[1].tv_sec = times[1].tv_sec; + futimes_buf[1].tv_usec = times[1].tv_nsec / 1000; + ret = futimes(fd, futimes_buf); + +done: + close_preserve_errno(fd); + return ret; +} diff --git a/hw/9pfs/9p-util-linux.c b/hw/9pfs/9p-util-linux.c index 398614a5d0..d54bf57a59 100644 --- a/hw/9pfs/9p-util-linux.c +++ b/hw/9pfs/9p-util-linux.c @@ -62,3 +62,9 @@ int fsetxattrat_nofollow(int dirfd, const char *filename, const char *name, g_free(proc_path); return ret; } + +int utimensat_nofollow(int dirfd, const char *filename, + const struct timespec times[2]) +{ + return utimensat(dirfd, filename, times, AT_SYMLINK_NOFOLLOW); +} diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h index 38ef8b289d..1c477a0e66 100644 --- a/hw/9pfs/9p-util.h +++ b/hw/9pfs/9p-util.h @@ -36,6 +36,12 @@ static inline int qemu_lsetxattr(const char *path, const char *name, #define qemu_lsetxattr lsetxattr #endif +/* Compatibility with old SDK Versions for Darwin */ +#if defined(CONFIG_DARWIN) && !defined(UTIME_NOW) +#define UTIME_NOW -1 +#define UTIME_OMIT -2 +#endif + static inline void close_preserve_errno(int fd) { int serrno = errno; @@ -96,5 +102,7 @@ ssize_t flistxattrat_nofollow(int dirfd, const char *filename, char *list, size_t size); ssize_t fremovexattrat_nofollow(int dirfd, const char *filename, const char *name); +int utimensat_nofollow(int dirfd, const char *filename, + const struct timespec times[2]); #endif diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index d671995aa4..3d676405c7 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -27,6 +27,7 @@ #include "virtio-9p.h" #include "fsdev/qemu-fsdev.h" #include "9p-xattr.h" +#include "9p-util.h" #include "coth.h" #include "trace.h" #include "migration/blocker.h"