utimensattest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2019 Darren Tucker
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #define TMPFILE "utimensat.tmp"
  25. #define TMPFILE2 "utimensat.tmp2"
  26. #ifndef AT_SYMLINK_NOFOLLOW
  27. # define AT_SYMLINK_NOFOLLOW 0x80000000
  28. #endif
  29. int utimensat(int, const char *, const struct timespec[2], int);
  30. static void
  31. cleanup(void)
  32. {
  33. (void)unlink(TMPFILE);
  34. (void)unlink(TMPFILE2);
  35. }
  36. static void
  37. fail(char *msg, long expect, long got)
  38. {
  39. int saved_errno = errno;
  40. if (expect == got && got == 0)
  41. fprintf(stderr, "utimensat: %s: %s\n", msg,
  42. strerror(saved_errno));
  43. else
  44. fprintf(stderr, "utimensat: %s: expected %ld got %ld\n",
  45. msg, expect, got);
  46. cleanup();
  47. exit(1);
  48. }
  49. int
  50. main(void)
  51. {
  52. int fd;
  53. struct stat sb;
  54. struct timespec ts[2];
  55. cleanup();
  56. if ((fd = open(TMPFILE, O_CREAT, 0600)) == -1)
  57. fail("open", 0, 0);
  58. close(fd);
  59. ts[0].tv_sec = 12345678;
  60. ts[0].tv_nsec = 23456789;
  61. ts[1].tv_sec = 34567890;
  62. ts[1].tv_nsec = 45678901;
  63. if (utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW) == -1)
  64. fail("utimensat", 0, 0);
  65. if (stat(TMPFILE, &sb) == -1)
  66. fail("stat", 0, 0 );
  67. if (sb.st_atime != 12345678)
  68. fail("st_atime", 0, 0 );
  69. if (sb.st_mtime != 34567890)
  70. fail("st_mtime", 0, 0 );
  71. #if 0
  72. /*
  73. * Results expected to be rounded to the nearest microsecond.
  74. * Depends on timestamp precision in kernel and filesystem so
  75. * disabled by default.
  76. */
  77. if (sb.st_atim.tv_nsec != 23456000)
  78. fail("atim.tv_nsec", 23456000, sb.st_atim.tv_nsec);
  79. if (sb.st_mtim.tv_nsec != 45678000)
  80. fail("mtim.tv_nsec", 45678000, sb.st_mtim.tv_nsec);
  81. #endif
  82. /*
  83. * POSIX specifies that when given a symlink, AT_SYMLINK_NOFOLLOW
  84. * should update the symlink and not the destination. The compat
  85. * code doesn't have a way to do this, so where possible it fails
  86. * with instead of following a symlink when explicitly asked not to.
  87. * Here we just test that it does not update the destination.
  88. */
  89. if (rename(TMPFILE, TMPFILE2) == -1)
  90. fail("rename", 0, 0);
  91. if (symlink(TMPFILE2, TMPFILE) == -1)
  92. fail("symlink", 0, 0);
  93. ts[0].tv_sec = 11223344;
  94. ts[1].tv_sec = 55667788;
  95. (void)utimensat(AT_FDCWD, TMPFILE, ts, AT_SYMLINK_NOFOLLOW);
  96. if (stat(TMPFILE2, &sb) == -1)
  97. fail("stat", 0, 0 );
  98. if (sb.st_atime == 11223344)
  99. fail("utimensat symlink st_atime", 0, 0 );
  100. if (sb.st_mtime == 55667788)
  101. fail("utimensat symlink st_mtime", 0, 0 );
  102. cleanup();
  103. exit(0);
  104. }