memcmp-stat.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Distilled from issue found with tar and symlinks.
  2. * Make sure that the whole stat struct between runs
  3. * is agreeable.
  4. */
  5. #ifndef _GNU_SOURCE
  6. # define _GNU_SOURCE
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <time.h>
  18. void show_stat(struct stat *st)
  19. {
  20. printf(
  21. "------------------\n"
  22. "st_dev = %li\n"
  23. "st_ino = %li\n"
  24. "st_mode = %li\n"
  25. "st_nlink = %li\n"
  26. "st_uid = %li\n"
  27. "st_gid = %li\n"
  28. "st_rdev = %li\n"
  29. "st_size = %li\n"
  30. "st_blksize = %li\n"
  31. "st_blocks = %li\n"
  32. "st_atime = %li\n"
  33. "st_ansec = %li\n"
  34. "st_mtime = %li\n"
  35. "st_mnsec = %li\n"
  36. "st_ctime = %li\n"
  37. "st_cnsec = %li\n",
  38. (long int)st->st_dev,
  39. (long int)st->st_ino,
  40. (long int)st->st_mode,
  41. (long int)st->st_nlink,
  42. (long int)st->st_uid,
  43. (long int)st->st_gid,
  44. (long int)st->st_rdev,
  45. (long int)st->st_size,
  46. (long int)st->st_blksize,
  47. (long int)st->st_blocks,
  48. #ifndef __UCLIBC__
  49. (long int)st->st_atime,
  50. (long int)st->st_atim.tv_nsec,
  51. (long int)st->st_mtime,
  52. (long int)st->st_mtim.tv_nsec,
  53. (long int)st->st_ctime,
  54. (long int)st->st_ctim.tv_nsec
  55. #else
  56. (long int)st->st_atime,
  57. (long int)st->st_atimensec,
  58. (long int)st->st_mtime,
  59. (long int)st->st_mtimensec,
  60. (long int)st->st_ctime,
  61. (long int)st->st_ctimensec
  62. #endif
  63. );
  64. }
  65. int main()
  66. {
  67. int ret;
  68. int fd;
  69. struct stat fst, st;
  70. memset(&fst, 0xAA, sizeof(fst));
  71. memset(&st, 0x55, sizeof(st));
  72. unlink(".testfile");
  73. fd = open(".testfile", O_WRONLY | O_CREAT | O_EXCL, 0);
  74. if (fd < 0) {
  75. perror("open(.testfile) failed");
  76. return 1;
  77. }
  78. ret = fstat(fd, &fst);
  79. if (ret != 0) {
  80. perror("fstat(.testfile) failed");
  81. return 1;
  82. }
  83. close(fd);
  84. ret = stat(".testfile", &st);
  85. if (ret != 0) {
  86. perror("stat(.testfile) failed");
  87. return 1;
  88. }
  89. ret = memcmp(&fst, &st, sizeof(fst));
  90. if (ret != 0) {
  91. printf("FAILED: memcmp() = %i\n", ret);
  92. show_stat(&fst);
  93. show_stat(&st);
  94. }
  95. unlink(".testfile");
  96. return ret;
  97. }