2
0

futimens.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <fcntl.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/statvfs.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. int main(int argc, char** argv) {
  11. char temp[] = "/tmp/stattest-XXXXXX";
  12. const char file[] = "/mkfifo_fifo";
  13. int len = sizeof(temp) + sizeof(int);
  14. char* path = calloc(len, sizeof(char));
  15. if (path == NULL) {
  16. fprintf(stderr, "Could not allocate: %s\n", strerror(errno));
  17. exit(1);
  18. }
  19. strncat(path, mktemp(temp), sizeof(temp));
  20. strncat(path, file, sizeof(file));
  21. if (mkdir(temp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
  22. fprintf(stderr, "mkdir %s: %s\n", temp, strerror(errno));
  23. exit(1);
  24. }
  25. int tmp = open(path, O_CREAT | O_CLOEXEC | O_RDONLY | S_IRWXU | S_IRWXG | S_IRWXO);
  26. if (tmp == -1) {
  27. fprintf(stderr, "touch %s: %s\n", path, strerror(errno));
  28. exit(1);
  29. }
  30. if (close(tmp) == -1) {
  31. fprintf(stderr, "close %s: %s\n", path, strerror(errno));
  32. exit(1);
  33. }
  34. int fd = open(path, 0, 0);
  35. if (fd == -1) {
  36. fprintf(stderr, "open %s: %s\n", path, strerror(errno));
  37. exit(1);
  38. }
  39. const struct timespec times[] = { { .tv_sec = 10 }, { .tv_sec = 20 } };
  40. if (futimens(fd, times) == -1) {
  41. fprintf(stderr, "futimens: %s\n", strerror(errno));
  42. exit(1);
  43. }
  44. struct stat sb;
  45. if (stat(path, &sb) != 0) {
  46. fprintf(stderr, "stat: %s\n", strerror(errno));
  47. exit(1);
  48. }
  49. if (sb.st_mtim.tv_sec != 20 || sb.st_mtim.tv_nsec != 0) {
  50. fprintf(stderr, "Wrong modified time: %d.%d\n", sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
  51. exit(1);
  52. }
  53. // Access times are not flushed to disk, so this check can't be (currently) performed
  54. /*
  55. * if (sb.st_atim.tv_sec != 10 || sb.st_atim.tv_nsec != 0) {
  56. * fprintf(stderr, "Wrong accessed time: %d.%d\n", sb.st_atim.tv_sec, sb.st_atim.tv_nsec);
  57. * exit(1);
  58. * }
  59. */
  60. }