archive_entry_stat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry_stat.c,v 1.2 2008/09/30 03:53:03 kientzle Exp $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #include "archive_entry.h"
  34. #include "archive_entry_private.h"
  35. const struct stat *
  36. archive_entry_stat(struct archive_entry *entry)
  37. {
  38. struct stat *st;
  39. if (entry->stat == NULL) {
  40. entry->stat = malloc(sizeof(*st));
  41. if (entry->stat == NULL)
  42. return (NULL);
  43. entry->stat_valid = 0;
  44. }
  45. /*
  46. * If none of the underlying fields have been changed, we
  47. * don't need to regenerate. In theory, we could use a bitmap
  48. * here to flag only those items that have changed, but the
  49. * extra complexity probably isn't worth it. It will be very
  50. * rare for anyone to change just one field then request a new
  51. * stat structure.
  52. */
  53. if (entry->stat_valid)
  54. return (entry->stat);
  55. st = entry->stat;
  56. /*
  57. * Use the public interfaces to extract items, so that
  58. * the appropriate conversions get invoked.
  59. */
  60. st->st_atime = archive_entry_atime(entry);
  61. #if HAVE_STRUCT_STAT_ST_BIRTHTIME
  62. st->st_birthtime = archive_entry_birthtime(entry);
  63. #endif
  64. st->st_ctime = archive_entry_ctime(entry);
  65. st->st_mtime = archive_entry_mtime(entry);
  66. st->st_dev = archive_entry_dev(entry);
  67. st->st_gid = archive_entry_gid(entry);
  68. st->st_uid = archive_entry_uid(entry);
  69. st->st_ino = archive_entry_ino(entry);
  70. st->st_nlink = archive_entry_nlink(entry);
  71. st->st_rdev = archive_entry_rdev(entry);
  72. st->st_size = archive_entry_size(entry);
  73. st->st_mode = archive_entry_mode(entry);
  74. /*
  75. * On systems that support high-res timestamps, copy that
  76. * information into struct stat.
  77. */
  78. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  79. st->st_atimespec.tv_nsec = archive_entry_atime_nsec(entry);
  80. st->st_ctimespec.tv_nsec = archive_entry_ctime_nsec(entry);
  81. st->st_mtimespec.tv_nsec = archive_entry_mtime_nsec(entry);
  82. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  83. st->st_atim.tv_nsec = archive_entry_atime_nsec(entry);
  84. st->st_ctim.tv_nsec = archive_entry_ctime_nsec(entry);
  85. st->st_mtim.tv_nsec = archive_entry_mtime_nsec(entry);
  86. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  87. st->st_atime_n = archive_entry_atime_nsec(entry);
  88. st->st_ctime_n = archive_entry_ctime_nsec(entry);
  89. st->st_mtime_n = archive_entry_mtime_nsec(entry);
  90. #elif HAVE_STRUCT_STAT_ST_UMTIME
  91. st->st_uatime = archive_entry_atime_nsec(entry) / 1000;
  92. st->st_uctime = archive_entry_ctime_nsec(entry) / 1000;
  93. st->st_umtime = archive_entry_mtime_nsec(entry) / 1000;
  94. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  95. st->st_atime_usec = archive_entry_atime_nsec(entry) / 1000;
  96. st->st_ctime_usec = archive_entry_ctime_nsec(entry) / 1000;
  97. st->st_mtime_usec = archive_entry_mtime_nsec(entry) / 1000;
  98. #endif
  99. #if HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
  100. st->st_birthtimespec.tv_nsec = archive_entry_birthtime_nsec(entry);
  101. #endif
  102. /*
  103. * TODO: On Linux, store 32 or 64 here depending on whether
  104. * the cached stat structure is a stat32 or a stat64. This
  105. * will allow us to support both variants interchangably.
  106. */
  107. entry->stat_valid = 1;
  108. return (st);
  109. }