]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
Add a minimal implementation of utimensat().
authorDarren Tucker <dtucker@dtucker.net>
Thu, 17 Jan 2019 23:11:42 +0000 (12:11 +1300)
committerDarren Tucker <dtucker@dtucker.net>
Thu, 17 Jan 2019 23:16:11 +0000 (10:16 +1100)
Some systems (eg older OS X) do not have utimensat, so provide minimal
implementation in compat layer.  Fixes build on at least El Capitan.

configure.ac
openbsd-compat/bsd-misc.c
openbsd-compat/bsd-misc.h
openbsd-compat/regress/Makefile.in
openbsd-compat/regress/utimensattest.c [new file with mode: 0644]

index c1427247e69e405d1f1928673122a16209b66595..2d1dafdee136b96ff08b0a105f3d2fc50186cc7e 100644 (file)
@@ -1812,6 +1812,7 @@ AC_CHECK_FUNCS([ \
        truncate \
        unsetenv \
        updwtmpx \
+       utimensat \
        user_from_uid \
        usleep \
        vasprintf \
index 5d7540a70f4b6e264445ec9fefa7a72728839002..4bae96548270ee5ef191d071cfd28b2779af1f3a 100644 (file)
@@ -25,6 +25,7 @@
 # include <sys/time.h>
 #endif
 
+#include <fcntl.h>
 #include <string.h>
 #include <signal.h>
 #include <stdlib.h>
@@ -117,6 +118,42 @@ int utimes(char *filename, struct timeval *tvp)
 }
 #endif
 
+#ifndef HAVE_UTIMENSAT
+/*
+ * A limited implementation of utimensat() that only implements the
+ * functionality used by OpenSSH, currently only AT_FDCWD and
+ * AT_SYMLINK_NOFOLLOW.
+ */
+int
+utimensat(int fd, const char *path, const struct timespec times[2],
+    int flag)
+{
+       struct timeval tv[2];
+       int ret, oflags = O_WRONLY;
+
+       tv[0].tv_sec = times[0].tv_sec;
+       tv[0].tv_usec = times[0].tv_nsec / 1000;
+       tv[1].tv_sec = times[1].tv_sec;
+       tv[1].tv_usec = times[1].tv_nsec / 1000;
+
+       if (fd != AT_FDCWD) {
+               errno = ENOSYS;
+               return -1;
+       }
+# ifndef HAVE_FUTIMES
+       return utimes(path, tv);
+# else
+       if (flag & AT_SYMLINK_NOFOLLOW)
+               oflags |= O_NOFOLLOW;
+       if ((fd = open(path, oflags)) == -1)
+               return -1;
+       ret = futimes(fd, tv);
+       close(fd);
+       return ret;
+# endif
+}
+#endif
+
 #ifndef HAVE_TRUNCATE
 int truncate(const char *path, off_t length)
 {
index 52ec528538b589f1323a570c663eabc53e14cadb..584c2b5ef5e5a1d5c7cf0d19a738d0e7ee3d4063 100644 (file)
@@ -64,6 +64,14 @@ struct timeval {
 int utimes(char *, struct timeval *);
 #endif /* HAVE_UTIMES */
 
+#ifndef HAVE_UTIMENSAT
+/* start with the high bits and work down to minimise risk of overlap */
+# ifndef AT_SYMLINK_NOFOLLOW
+#  define AT_SYMLINK_NOFOLLOW 0x80000000
+# endif
+int utimensat(int, const char *, const struct timespec[2], int);
+#endif
+
 #ifndef HAVE_TRUNCATE
 int truncate (const char *, off_t);
 #endif /* HAVE_TRUNCATE */
index 529331be5c26eb80f6957f549cd79856a5813ea1..c5aae61e2a42f427c36f8257bbd771567c0bb3b5 100644 (file)
@@ -14,7 +14,7 @@ LIBS=@LIBS@
 LDFLAGS=@LDFLAGS@ $(LIBCOMPAT)
 
 TESTPROGS=closefromtest$(EXEEXT) snprintftest$(EXEEXT) strduptest$(EXEEXT) \
-       strtonumtest$(EXEEXT) opensslvertest$(EXEEXT)
+       strtonumtest$(EXEEXT) opensslvertest$(EXEEXT) utimensattest$(EXEEXT)
 
 all:   t-exec ${OTHERTESTS}
 
diff --git a/openbsd-compat/regress/utimensattest.c b/openbsd-compat/regress/utimensattest.c
new file mode 100644 (file)
index 0000000..a7bc763
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2019 Darren Tucker
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define TMPFILE "utimensat.tmp"
+#define TMPFILE2 "utimensat.tmp2"
+
+#ifndef AT_SYMLINK_NOFOLLOW
+# define AT_SYMLINK_NOFOLLOW 0x80000000
+#endif
+
+int utimensat(int, const char *, const struct timespec[2], int);
+
+void
+fail(char *msg, long expect, long got)
+{
+       int saved_errno = errno;
+
+       if (expect == got && got == 0)
+               fprintf(stderr, "utimensat: %s: %s\n", msg,
+                    strerror(saved_errno));
+       else
+               fprintf(stderr, "utimensat: %s: expected %ld got %ld\n",
+                    msg, expect, got);
+       exit(1);
+}
+
+int
+main(void)
+{
+       int fd;
+       struct stat sb;
+       struct timespec ts[2];
+
+       if ((fd = open(TMPFILE, O_CREAT, 0600)) == -1)
+               fail("open", 0, 0);
+       close(fd);
+
+       ts[0].tv_sec = 12345678;
+       ts[0].tv_nsec = 23456789;
+       ts[1].tv_sec = 34567890;
+       ts[1].tv_nsec = 45678901;
+       if (utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW) == -1)
+               fail("utimensat", 0, 0);
+
+       if (stat(TMPFILE, &sb) == -1)
+               fail("stat", 0, 0 );
+       if (sb.st_atime != 12345678)
+               fail("st_atime", 0, 0 );
+       if (sb.st_mtime != 34567890)
+               fail("st_mtime", 0, 0 );
+#if 0
+       /*
+        * Results expected to be rounded to the nearest microsecond.
+        * Depends on timestamp precision in kernel and filesystem so
+        * disabled by default.
+        */
+       if (sb.st_atim.tv_nsec != 23456000)
+               fail("atim.tv_nsec", 23456000, sb.st_atim.tv_nsec);
+       if (sb.st_mtim.tv_nsec != 45678000)
+               fail("mtim.tv_nsec", 45678000, sb.st_mtim.tv_nsec);
+#endif
+
+       if (rename(TMPFILE, TMPFILE2) == -1)
+               fail("rename", 0, 0);
+       if (symlink(TMPFILE2, TMPFILE) == -1)
+               fail("symlink", 0, 0);
+
+       if (utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW) != -1)
+               fail("utimensat followed symlink", 0, 0);
+
+       if (!(unlink(TMPFILE) == 0 && unlink(TMPFILE2) == 0))
+               fail("unlink", 0, 0);
+       exit(0);
+}