bsdtar_platform.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * $FreeBSD: src/usr.bin/tar/bsdtar_platform.h,v 1.26 2008/12/06 07:37:14 kientzle Exp $
  26. */
  27. /*
  28. * This header is the first thing included in any of the bsdtar
  29. * source files. As far as possible, platform-specific issues should
  30. * be dealt with here and not within individual source files.
  31. */
  32. #ifndef BSDTAR_PLATFORM_H_INCLUDED
  33. #define BSDTAR_PLATFORM_H_INCLUDED
  34. #if defined(PLATFORM_CONFIG_H)
  35. /* Use hand-built config.h in environments that need it. */
  36. #include PLATFORM_CONFIG_H
  37. #elif defined(HAVE_CONFIG_H)
  38. /* Most POSIX platforms use the 'configure' script to build config.h */
  39. #include "config.h"
  40. #else
  41. /* Warn if bsdtar hasn't been (automatically or manually) configured. */
  42. #error Oops: No config.h and no built-in configuration in bsdtar_platform.h.
  43. #endif /* !HAVE_CONFIG_H */
  44. /* No non-FreeBSD platform will have __FBSDID, so just define it here. */
  45. #ifdef __FreeBSD__
  46. #include <sys/cdefs.h> /* For __FBSDID */
  47. #else
  48. /* Just leaving this macro replacement empty leads to a dangling semicolon. */
  49. #define __FBSDID(a) struct _undefined_hack
  50. #endif
  51. #ifdef HAVE_LIBARCHIVE
  52. /* If we're using the platform libarchive, include system headers. */
  53. #include <archive.h>
  54. #include <archive_entry.h>
  55. #else
  56. /* Otherwise, include user headers. */
  57. #include "archive.h"
  58. #include "archive_entry.h"
  59. #endif
  60. /*
  61. * Does this platform have complete-looking POSIX-style ACL support,
  62. * including some variant of the acl_get_perm() function (which was
  63. * omitted from the POSIX.1e draft)?
  64. */
  65. #if HAVE_SYS_ACL_H && HAVE_ACL_PERMSET_T && HAVE_ACL_USER
  66. #if HAVE_ACL_GET_PERM || HAVE_ACL_GET_PERM_NP
  67. #define HAVE_POSIX_ACL 1
  68. #endif
  69. #endif
  70. #ifdef HAVE_LIBACL
  71. #include <acl/libacl.h>
  72. #endif
  73. #if HAVE_ACL_GET_PERM
  74. #define ACL_GET_PERM acl_get_perm
  75. #else
  76. #if HAVE_ACL_GET_PERM_NP
  77. #define ACL_GET_PERM acl_get_perm_np
  78. #endif
  79. #endif
  80. /*
  81. * Include "dirent.h" (or its equivalent on several different platforms).
  82. *
  83. * This is slightly modified from the GNU autoconf recipe.
  84. * In particular, FreeBSD includes d_namlen in its dirent structure,
  85. * so my configure script includes an explicit test for the d_namlen
  86. * field.
  87. */
  88. #if HAVE_DIRENT_H
  89. # include <dirent.h>
  90. # if HAVE_DIRENT_D_NAMLEN
  91. # define DIRENT_NAMLEN(dirent) (dirent)->d_namlen
  92. # else
  93. # define DIRENT_NAMLEN(dirent) strlen((dirent)->d_name)
  94. # endif
  95. #else
  96. # define dirent direct
  97. # define DIRENT_NAMLEN(dirent) (dirent)->d_namlen
  98. # if HAVE_SYS_NDIR_H
  99. # include <sys/ndir.h>
  100. # endif
  101. # if HAVE_SYS_DIR_H
  102. # include <sys/dir.h>
  103. # endif
  104. # if HAVE_NDIR_H
  105. # include <ndir.h>
  106. # endif
  107. #endif
  108. /*
  109. * We need to be able to display a filesize using printf(). The type
  110. * and format string here must be compatible with one another and
  111. * large enough for any file.
  112. */
  113. #if HAVE_UINTMAX_T
  114. #define BSDTAR_FILESIZE_TYPE uintmax_t
  115. #define BSDTAR_FILESIZE_PRINTF "%ju"
  116. #else
  117. #if HAVE_UNSIGNED_LONG_LONG
  118. #define BSDTAR_FILESIZE_TYPE unsigned long long
  119. #define BSDTAR_FILESIZE_PRINTF "%llu"
  120. #else
  121. #define BSDTAR_FILESIZE_TYPE unsigned long
  122. #define BSDTAR_FILESIZE_PRINTF "%lu"
  123. #endif
  124. #endif
  125. #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
  126. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctimespec.tv_nsec
  127. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtimespec.tv_nsec
  128. #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  129. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctim.tv_nsec
  130. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtim.tv_nsec
  131. #elif HAVE_STRUCT_STAT_ST_MTIME_N
  132. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctime_n
  133. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtime_n
  134. #elif HAVE_STRUCT_STAT_ST_UMTIME
  135. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_uctime * 1000
  136. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_umtime * 1000
  137. #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
  138. #define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctime_usec * 1000
  139. #define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtime_usec * 1000
  140. #else
  141. #define ARCHIVE_STAT_CTIME_NANOS(st) (0)
  142. #define ARCHIVE_STAT_MTIME_NANOS(st) (0)
  143. #endif
  144. /* If asprintf isn't provided by the OS, use our local version. */
  145. #ifndef HAVE_ASPRINTF
  146. #define asprintf tarsnap_asprintf
  147. #include "asprintf.h"
  148. #endif
  149. /* How to mark functions that don't return. */
  150. /* This facilitates use of some newer static code analysis tools. */
  151. #undef __LA_DEAD
  152. #if defined(__GNUC__) && (__GNUC__ > 2 || \
  153. (__GNUC__ == 2 && __GNUC_MINOR__ >= 5))
  154. #define __LA_DEAD __attribute__((__noreturn__))
  155. #else
  156. #define __LA_DEAD
  157. #endif
  158. #if defined(__CYGWIN__)
  159. #include "bsdtar_cygwin.h"
  160. #elif defined(_WIN32) /* && !__CYGWIN__ */
  161. #include "bsdtar_windows.h"
  162. #else
  163. #define bsdtar_is_privileged(bsdtar) (bsdtar->user_uid == 0)
  164. #endif
  165. #endif /* !BSDTAR_PLATFORM_H_INCLUDED */