tst-atime.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. #include <sys/statvfs.h>
  9. static int do_test (void);
  10. #define TEST_FUNCTION do_test ()
  11. #include <test-skeleton.c>
  12. static int
  13. do_test (void)
  14. {
  15. char *buf;
  16. int fd;
  17. FILE *fp;
  18. int ch;
  19. struct stat st1;
  20. struct stat st2;
  21. buf = (char *) malloc (strlen (test_dir) + sizeof "/tst-atime.XXXXXX");
  22. if (buf == NULL)
  23. {
  24. printf ("cannot allocate memory: %m\n");
  25. return 1;
  26. }
  27. stpcpy (stpcpy (buf, test_dir), "/tst-atime.XXXXXX");
  28. fd = mkstemp (buf);
  29. if (fd == -1)
  30. {
  31. printf ("cannot open temporary file: %m\n");
  32. return 1;
  33. }
  34. #ifdef ST_NOATIME
  35. /* Make sure the filesystem doesn't have the noatime option set. If
  36. statvfs is not available just continue. */
  37. struct statvfs sv;
  38. int e = fstatvfs (fd, &sv);
  39. if (e != ENOSYS)
  40. {
  41. if (e != 0)
  42. {
  43. printf ("cannot statvfs '%s': %m\n", buf);
  44. return 1;
  45. }
  46. if ((sv.f_flag & ST_NOATIME) != 0)
  47. {
  48. puts ("Bah! The filesystem is mounted with noatime");
  49. return 0;
  50. }
  51. }
  52. #endif
  53. /* Make sure it gets removed. */
  54. add_temp_file (buf);
  55. if (write (fd, "some string\n", 12) != 12)
  56. {
  57. printf ("cannot write temporary file: %m\n");
  58. return 1;
  59. }
  60. if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
  61. {
  62. printf ("cannot reposition temporary file: %m\n");
  63. return 1;
  64. }
  65. fp = fdopen (fd, "r");
  66. if (fp == NULL)
  67. {
  68. printf ("cannot create stream: %m\n");
  69. return 1;
  70. }
  71. if (fstat (fd, &st1) == -1)
  72. {
  73. printf ("first stat failed: %m\n");
  74. return 1;
  75. }
  76. sleep (2);
  77. ch = fgetc (fp);
  78. if (ch != 's')
  79. {
  80. printf ("did not read correct character: got '%c', expected 's'\n", ch);
  81. return 1;
  82. }
  83. if (fstat (fd, &st2) == -1)
  84. {
  85. printf ("second stat failed: %m\n");
  86. return 1;
  87. }
  88. if (st1.st_atime > st2.st_atime)
  89. {
  90. puts ("second atime smaller");
  91. return 1;
  92. }
  93. else if (st1.st_atime == st2.st_atime)
  94. {
  95. puts ("atime has not changed");
  96. return 1;
  97. }
  98. fclose (fp);
  99. return 0;
  100. }