@papyrus-sdk/engine-native 0.2.22-beta.1 → 0.2.22-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/android/build.gradle +1 -0
  2. package/android/src/main/cpp/CMakeLists.txt +137 -0
  3. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  5. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  6. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +353 -0
  7. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +17 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +298 -34
  10. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  12. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +24 -0
  13. package/android/src/main/java/com/papyrus/engine/PapyrusRenderMath.java +9 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +439 -0
  15. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +43 -0
  16. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  17. package/dist/index.d.mts +439 -3
  18. package/dist/index.d.ts +439 -3
  19. package/dist/index.js +412 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +404 -3
  22. package/dist/index.mjs.map +1 -1
  23. package/ios/PapyrusComicDocumentView.h +21 -0
  24. package/ios/PapyrusComicDocumentView.m +374 -0
  25. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  26. package/ios/PapyrusEngineStore.m +4 -0
  27. package/ios/PapyrusNativeEngine.m +664 -0
  28. package/ios/PapyrusNativeEngine.podspec +26 -1
  29. package/ios/PapyrusPageRotationRegistry.h +29 -0
  30. package/ios/PapyrusPageRotationRegistry.m +179 -0
  31. package/ios/PapyrusPageView.m +38 -11
  32. package/ios/PapyrusPdfDocumentView.h +2 -0
  33. package/ios/PapyrusPdfDocumentView.m +121 -15
  34. package/ios/PapyrusPdfDocumentViewManager.m +2 -0
  35. package/ios/PapyrusPdfPageTheme.h +15 -0
  36. package/ios/PapyrusPdfPageTheme.m +218 -0
  37. package/ios/PapyrusTextDocumentView.h +27 -0
  38. package/ios/PapyrusTextDocumentView.m +300 -0
  39. package/ios/PapyrusTextDocumentViewManager.m +29 -0
  40. package/ios/PapyrusTextStore.h +13 -0
  41. package/ios/PapyrusTextStore.m +73 -0
  42. package/package.json +5 -3
  43. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  44. package/third_party_licenses/libarchive-COPYING +65 -0
  45. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  46. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  47. package/vendor/libarchive/README.papyrus.md +15 -0
  48. package/vendor/libarchive/android.h +192 -0
  49. package/vendor/libarchive/config/PapyrusArchiveConfig.h +65 -0
  50. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  51. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  52. package/vendor/libarchive/libarchive/archive.h +1266 -0
  53. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  54. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  55. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  56. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  57. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  58. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  59. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  60. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  61. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  62. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  63. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  64. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  65. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  66. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  67. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  68. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  69. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  70. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  71. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  72. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  73. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  74. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  75. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  76. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  77. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  78. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  79. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  80. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  81. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  82. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  83. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  84. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  85. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  86. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  87. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  88. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  89. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  90. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  91. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  92. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  93. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  94. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  95. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  96. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  97. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  98. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  99. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  100. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  101. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  102. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  103. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  104. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  105. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  106. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  107. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  108. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  109. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  110. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  111. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  112. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  113. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  114. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  115. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  116. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  117. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  118. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  119. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  120. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  121. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  122. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  123. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  124. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  125. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  126. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  127. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  128. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  129. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  130. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  131. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  132. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  133. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  134. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  135. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  136. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  137. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  138. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  139. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  140. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  141. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  142. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  143. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  144. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  145. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  146. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  147. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  148. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  149. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  150. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  151. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  152. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  153. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  154. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  155. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  156. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  180. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  181. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  182. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  183. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  184. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  185. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  186. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  187. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  188. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  189. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  190. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  191. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  192. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  193. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  194. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  195. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  196. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  197. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  198. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  199. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  200. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  201. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  202. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  209. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  210. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  211. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  212. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  213. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  214. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  215. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  216. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  217. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  218. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  219. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  220. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  221. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  222. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  223. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  224. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  225. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  226. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  227. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  228. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  229. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  230. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  231. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  232. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  233. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  234. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  235. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  236. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  253. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  254. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  255. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  256. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  257. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  258. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  259. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  260. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  261. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  262. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  263. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  264. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  265. package/vendor/libarchive/libarchive/tar.5 +947 -0
  266. package/vendor/libarchive/libarchive/xxhash.c +531 -0
@@ -0,0 +1,4789 @@
1
+ /*-
2
+ * Copyright (c) 2003-2010 Tim Kientzle
3
+ * Copyright (c) 2012 Michihiro NAKAJIMA
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer
11
+ * in this position and unchanged.
12
+ * 2. Redistributions in binary form must reproduce the above copyright
13
+ * notice, this list of conditions and the following disclaimer in the
14
+ * documentation and/or other materials provided with the distribution.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include "archive_platform.h"
29
+
30
+ #if !defined(_WIN32) || defined(__CYGWIN__)
31
+
32
+ #ifdef HAVE_SYS_TYPES_H
33
+ #include <sys/types.h>
34
+ #endif
35
+ #ifdef HAVE_SYS_ACL_H
36
+ #include <sys/acl.h>
37
+ #endif
38
+ #ifdef HAVE_SYS_EXTATTR_H
39
+ #include <sys/extattr.h>
40
+ #endif
41
+ #if HAVE_SYS_XATTR_H
42
+ #include <sys/xattr.h>
43
+ #elif HAVE_ATTR_XATTR_H
44
+ #include <attr/xattr.h>
45
+ #endif
46
+ #ifdef HAVE_SYS_EA_H
47
+ #include <sys/ea.h>
48
+ #endif
49
+ #ifdef HAVE_SYS_IOCTL_H
50
+ #include <sys/ioctl.h>
51
+ #endif
52
+ #ifdef HAVE_SYS_STAT_H
53
+ #include <sys/stat.h>
54
+ #endif
55
+ #ifdef HAVE_SYS_TIME_H
56
+ #include <sys/time.h>
57
+ #endif
58
+ #ifdef HAVE_SYS_UTIME_H
59
+ #include <sys/utime.h>
60
+ #endif
61
+ #ifdef HAVE_COPYFILE_H
62
+ #include <copyfile.h>
63
+ #endif
64
+ #ifdef HAVE_ERRNO_H
65
+ #include <errno.h>
66
+ #endif
67
+ #ifdef HAVE_FCNTL_H
68
+ #include <fcntl.h>
69
+ #endif
70
+ #ifdef HAVE_GRP_H
71
+ #include <grp.h>
72
+ #endif
73
+ #ifdef HAVE_LANGINFO_H
74
+ #include <langinfo.h>
75
+ #endif
76
+ #ifdef HAVE_LINUX_FS_H
77
+ #include <linux/fs.h> /* for Linux file flags */
78
+ #endif
79
+ /*
80
+ * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
81
+ * As the include guards don't agree, the order of include is important.
82
+ */
83
+ #ifdef HAVE_LINUX_EXT2_FS_H
84
+ #include <linux/ext2_fs.h> /* for Linux file flags */
85
+ #endif
86
+ #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
87
+ #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
88
+ #endif
89
+ #ifdef HAVE_LIMITS_H
90
+ #include <limits.h>
91
+ #endif
92
+ #ifdef HAVE_PWD_H
93
+ #include <pwd.h>
94
+ #endif
95
+ #include <stdio.h>
96
+ #ifdef HAVE_STDLIB_H
97
+ #include <stdlib.h>
98
+ #endif
99
+ #ifdef HAVE_STRING_H
100
+ #include <string.h>
101
+ #endif
102
+ #ifdef HAVE_UNISTD_H
103
+ #include <unistd.h>
104
+ #endif
105
+ #ifdef HAVE_UTIME_H
106
+ #include <utime.h>
107
+ #endif
108
+ #ifdef F_GETTIMES /* Tru64 specific */
109
+ #include <sys/fcntl1.h>
110
+ #endif
111
+
112
+ /*
113
+ * Macro to cast st_mtime and time_t to an int64 so that 2 numbers can reliably be compared.
114
+ *
115
+ * It assumes that the input is an integer type of no more than 64 bits.
116
+ * If the number is less than zero, t must be a signed type, so it fits in
117
+ * int64_t. Otherwise, it's a nonnegative value so we can cast it to uint64_t
118
+ * without loss. But it could be a large unsigned value, so we have to clip it
119
+ * to INT64_MAX.*
120
+ */
121
+ #define to_int64_time(t) \
122
+ ((t) < 0 ? (int64_t)(t) : (uint64_t)(t) > (uint64_t)INT64_MAX ? INT64_MAX : (int64_t)(t))
123
+
124
+ #if __APPLE__
125
+ #include <TargetConditionals.h>
126
+ #if TARGET_OS_MAC && !TARGET_OS_EMBEDDED && HAVE_QUARANTINE_H
127
+ #include <quarantine.h>
128
+ #define HAVE_QUARANTINE 1
129
+ #endif
130
+ #endif
131
+
132
+ #ifdef HAVE_ZLIB_H
133
+ #include <zlib.h>
134
+ #endif
135
+
136
+ /* TODO: Support Mac OS 'quarantine' feature. This is really just a
137
+ * standard tag to mark files that have been downloaded as "tainted".
138
+ * On Mac OS, we should mark the extracted files as tainted if the
139
+ * archive being read was tainted. Windows has a similar feature; we
140
+ * should investigate ways to support this generically. */
141
+
142
+ #include "archive.h"
143
+ #include "archive_acl_private.h"
144
+ #include "archive_string.h"
145
+ #include "archive_endian.h"
146
+ #include "archive_entry.h"
147
+ #include "archive_private.h"
148
+ #include "archive_write_disk_private.h"
149
+
150
+ #ifndef O_BINARY
151
+ #define O_BINARY 0
152
+ #endif
153
+ #ifndef O_CLOEXEC
154
+ #define O_CLOEXEC 0
155
+ #endif
156
+
157
+ /* Ignore non-int O_NOFOLLOW constant. */
158
+ /* gnulib's fcntl.h does this on AIX, but it seems practical everywhere */
159
+ #if defined O_NOFOLLOW && !(INT_MIN <= O_NOFOLLOW && O_NOFOLLOW <= INT_MAX)
160
+ #undef O_NOFOLLOW
161
+ #endif
162
+
163
+ #ifndef O_NOFOLLOW
164
+ #define O_NOFOLLOW 0
165
+ #endif
166
+
167
+ #ifndef AT_FDCWD
168
+ #define AT_FDCWD -100
169
+ #endif
170
+
171
+ struct fixup_entry {
172
+ struct fixup_entry *next;
173
+ struct archive_acl acl;
174
+ mode_t mode;
175
+ __LA_MODE_T filetype;
176
+ int64_t atime;
177
+ int64_t birthtime;
178
+ int64_t mtime;
179
+ int64_t ctime;
180
+ unsigned long atime_nanos;
181
+ unsigned long birthtime_nanos;
182
+ unsigned long mtime_nanos;
183
+ unsigned long ctime_nanos;
184
+ unsigned long fflags_set;
185
+ size_t mac_metadata_size;
186
+ void *mac_metadata;
187
+ int fixup; /* bitmask of what needs fixing */
188
+ char *name;
189
+ };
190
+
191
+ /*
192
+ * We use a bitmask to track which operations remain to be done for
193
+ * this file. In particular, this helps us avoid unnecessary
194
+ * operations when it's possible to take care of one step as a
195
+ * side-effect of another. For example, mkdir() can specify the mode
196
+ * for the newly-created object but symlink() cannot. This means we
197
+ * can skip chmod() if mkdir() succeeded, but we must explicitly
198
+ * chmod() if we're trying to create a directory that already exists
199
+ * (mkdir() failed) or if we're restoring a symlink. Similarly, we
200
+ * need to verify UID/GID before trying to restore SUID/SGID bits;
201
+ * that verification can occur explicitly through a stat() call or
202
+ * implicitly because of a successful chown() call.
203
+ */
204
+ #define TODO_MODE_FORCE 0x40000000
205
+ #define TODO_MODE_BASE 0x20000000
206
+ #define TODO_SUID 0x10000000
207
+ #define TODO_SUID_CHECK 0x08000000
208
+ #define TODO_SGID 0x04000000
209
+ #define TODO_SGID_CHECK 0x02000000
210
+ #define TODO_APPLEDOUBLE 0x01000000
211
+ #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
212
+ #define TODO_TIMES ARCHIVE_EXTRACT_TIME
213
+ #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
214
+ #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
215
+ #define TODO_ACLS ARCHIVE_EXTRACT_ACL
216
+ #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
217
+ #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
218
+ #define TODO_HFS_COMPRESSION ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED
219
+
220
+ struct archive_write_disk {
221
+ struct archive archive;
222
+
223
+ mode_t user_umask;
224
+ struct fixup_entry *fixup_list;
225
+ struct fixup_entry *current_fixup;
226
+ int64_t user_uid;
227
+ int skip_file_set;
228
+ int64_t skip_file_dev;
229
+ int64_t skip_file_ino;
230
+ time_t start_time;
231
+
232
+ int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
233
+ void (*cleanup_gid)(void *private);
234
+ void *lookup_gid_data;
235
+ int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
236
+ void (*cleanup_uid)(void *private);
237
+ void *lookup_uid_data;
238
+
239
+ /*
240
+ * Full path of last file to satisfy symlink checks.
241
+ */
242
+ struct archive_string path_safe;
243
+
244
+ /*
245
+ * Cached stat data from disk for the current entry.
246
+ * If this is valid, pst points to st. Otherwise,
247
+ * pst is null.
248
+ */
249
+ struct stat st;
250
+ struct stat *pst;
251
+
252
+ /* Information about the object being restored right now. */
253
+ struct archive_entry *entry; /* Entry being extracted. */
254
+ char *name; /* Name of entry, possibly edited. */
255
+ struct archive_string _name_data; /* backing store for 'name' */
256
+ char *tmpname; /* Temporary name * */
257
+ struct archive_string _tmpname_data; /* backing store for 'tmpname' */
258
+ /* Tasks remaining for this object. */
259
+ int todo;
260
+ /* Tasks deferred until end-of-archive. */
261
+ int deferred;
262
+ /* Options requested by the client. */
263
+ int flags;
264
+ /* Handle for the file we're restoring. */
265
+ int fd;
266
+ /* Current offset for writing data to the file. */
267
+ int64_t offset;
268
+ /* Last offset actually written to disk. */
269
+ int64_t fd_offset;
270
+ /* Total bytes actually written to files. */
271
+ int64_t total_bytes_written;
272
+ /* Maximum size of file, -1 if unknown. */
273
+ int64_t filesize;
274
+ /* Dir we were in before this restore; only for deep paths. */
275
+ int restore_pwd;
276
+ /* Mode we should use for this entry; affected by _PERM and umask. */
277
+ mode_t mode;
278
+ /* UID/GID to use in restoring this entry. */
279
+ int64_t uid;
280
+ int64_t gid;
281
+ /*
282
+ * HFS+ Compression.
283
+ */
284
+ /* Xattr "com.apple.decmpfs". */
285
+ uint32_t decmpfs_attr_size;
286
+ unsigned char *decmpfs_header_p;
287
+ /* ResourceFork set options used for fsetxattr. */
288
+ int rsrc_xattr_options;
289
+ /* Xattr "com.apple.ResourceFork". */
290
+ unsigned char *resource_fork;
291
+ size_t resource_fork_allocated_size;
292
+ unsigned int decmpfs_block_count;
293
+ uint32_t *decmpfs_block_info;
294
+ /* Buffer for compressed data. */
295
+ unsigned char *compressed_buffer;
296
+ size_t compressed_buffer_size;
297
+ size_t compressed_buffer_remaining;
298
+ /* The offset of the ResourceFork where compressed data will
299
+ * be placed. */
300
+ uint32_t compressed_rsrc_position;
301
+ uint32_t compressed_rsrc_position_v;
302
+ /* Buffer for uncompressed data. */
303
+ unsigned char *uncompressed_buffer;
304
+ size_t block_remaining_bytes;
305
+ size_t file_remaining_bytes;
306
+ #ifdef HAVE_ZLIB_H
307
+ z_stream stream;
308
+ int stream_valid;
309
+ int decmpfs_compression_level;
310
+ #endif
311
+ #if !(ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX ||\
312
+ ARCHIVE_XATTR_FREEBSD)
313
+ int warning_done;
314
+ #endif
315
+ };
316
+
317
+ /*
318
+ * Default mode for dirs created automatically (will be modified by umask).
319
+ * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
320
+ * by the process' file creation mask."
321
+ */
322
+ #define DEFAULT_DIR_MODE 0777
323
+ /*
324
+ * Dir modes are restored in two steps: During the extraction, the permissions
325
+ * in the archive are modified to match the following limits. During
326
+ * the post-extract fixup pass, the permissions from the archive are
327
+ * applied.
328
+ */
329
+ #define MINIMUM_DIR_MODE 0700
330
+ #define MAXIMUM_DIR_MODE 0775
331
+
332
+ /*
333
+ * Maximum uncompressed size of a decmpfs block.
334
+ */
335
+ #define MAX_DECMPFS_BLOCK_SIZE (64 * 1024)
336
+ /*
337
+ * HFS+ compression type.
338
+ */
339
+ #define CMP_XATTR 3/* Compressed data in xattr. */
340
+ #define CMP_RESOURCE_FORK 4/* Compressed data in resource fork. */
341
+ /*
342
+ * HFS+ compression resource fork.
343
+ */
344
+ #define RSRC_H_SIZE 260 /* Base size of Resource fork header. */
345
+ #define RSRC_F_SIZE 50 /* Size of Resource fork footer. */
346
+ /* Size to write compressed data to resource fork. */
347
+ #define COMPRESSED_W_SIZE (64 * 1024)
348
+ /* decmpfs definitions. */
349
+ #define MAX_DECMPFS_XATTR_SIZE 3802
350
+ #ifndef DECMPFS_XATTR_NAME
351
+ #define DECMPFS_XATTR_NAME "com.apple.decmpfs"
352
+ #endif
353
+ #define DECMPFS_MAGIC 0x636d7066
354
+ #define DECMPFS_COMPRESSION_MAGIC 0
355
+ #define DECMPFS_COMPRESSION_TYPE 4
356
+ #define DECMPFS_UNCOMPRESSED_SIZE 8
357
+ #define DECMPFS_HEADER_SIZE 16
358
+
359
+ #define HFS_BLOCKS(s) ((s) >> 12)
360
+
361
+
362
+ static int la_opendirat(int, const char *);
363
+ static int la_mktemp(struct archive_write_disk *);
364
+ static int la_verify_filetype(mode_t, __LA_MODE_T);
365
+ static void fsobj_error(int *, struct archive_string *, int, const char *,
366
+ const char *);
367
+ static int check_symlinks_fsobj(char *, int *, struct archive_string *,
368
+ int, int);
369
+ static int check_symlinks(struct archive_write_disk *);
370
+ static int create_filesystem_object(struct archive_write_disk *);
371
+ static struct fixup_entry *current_fixup(struct archive_write_disk *,
372
+ const char *pathname);
373
+ #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
374
+ static void edit_deep_directories(struct archive_write_disk *ad);
375
+ #endif
376
+ static int cleanup_pathname_fsobj(char *, int *, struct archive_string *,
377
+ int);
378
+ static int cleanup_pathname(struct archive_write_disk *);
379
+ static int create_dir(struct archive_write_disk *, char *);
380
+ static int create_parent_dir(struct archive_write_disk *, char *);
381
+ static ssize_t hfs_write_data_block(struct archive_write_disk *,
382
+ const char *, size_t);
383
+ static int fixup_appledouble(struct archive_write_disk *, const char *);
384
+ static int older(struct stat *, struct archive_entry *);
385
+ static int restore_entry(struct archive_write_disk *);
386
+ static int set_mac_metadata(struct archive_write_disk *, const char *,
387
+ const void *, size_t);
388
+ static int set_xattrs(struct archive_write_disk *);
389
+ static int clear_nochange_fflags(struct archive_write_disk *);
390
+ static int set_fflags(struct archive_write_disk *);
391
+ static int set_fflags_platform(struct archive_write_disk *, int fd,
392
+ const char *name, mode_t mode,
393
+ unsigned long fflags_set, unsigned long fflags_clear);
394
+ static int set_ownership(struct archive_write_disk *);
395
+ static int set_mode(struct archive_write_disk *, int mode);
396
+ static int set_time(int, int, const char *, time_t, long, time_t, long);
397
+ static int set_times(struct archive_write_disk *, int, int, const char *,
398
+ time_t, long, time_t, long, time_t, long, time_t, long);
399
+ static int set_times_from_entry(struct archive_write_disk *);
400
+ static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
401
+ static ssize_t write_data_block(struct archive_write_disk *,
402
+ const char *, size_t);
403
+ static void close_file_descriptor(struct archive_write_disk *);
404
+
405
+ static int _archive_write_disk_close(struct archive *);
406
+ static int _archive_write_disk_free(struct archive *);
407
+ static int _archive_write_disk_header(struct archive *,
408
+ struct archive_entry *);
409
+ static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
410
+ static int _archive_write_disk_finish_entry(struct archive *);
411
+ static ssize_t _archive_write_disk_data(struct archive *, const void *,
412
+ size_t);
413
+ static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
414
+ size_t, int64_t);
415
+
416
+ static int
417
+ la_mktemp(struct archive_write_disk *a)
418
+ {
419
+ struct archive_string *tmp = &a->_tmpname_data;
420
+ int oerrno, fd;
421
+ mode_t mode;
422
+
423
+ archive_strcpy(tmp, a->name);
424
+ archive_string_dirname(tmp);
425
+ archive_strcat(tmp, "/tar.XXXXXXXX");
426
+ a->tmpname = tmp->s;
427
+
428
+ fd = __archive_mkstemp(a->tmpname);
429
+ if (fd == -1)
430
+ return -1;
431
+
432
+ mode = a->mode & 0777 & ~a->user_umask;
433
+ if (fchmod(fd, mode) == -1) {
434
+ oerrno = errno;
435
+ close(fd);
436
+ errno = oerrno;
437
+ return -1;
438
+ }
439
+ return fd;
440
+ }
441
+
442
+ static int
443
+ la_opendirat(int fd, const char *path) {
444
+ const int flags = O_CLOEXEC
445
+ #if defined(O_BINARY)
446
+ | O_BINARY
447
+ #endif
448
+ #if defined(O_DIRECTORY)
449
+ | O_DIRECTORY
450
+ #endif
451
+ #if defined(O_PATH)
452
+ | O_PATH
453
+ #elif defined(O_SEARCH)
454
+ | O_SEARCH
455
+ #elif defined(__FreeBSD__) && defined(O_EXEC)
456
+ | O_EXEC
457
+ #else
458
+ | O_RDONLY
459
+ #endif
460
+ ;
461
+
462
+ #if !defined(HAVE_OPENAT)
463
+ if (fd != AT_FDCWD) {
464
+ errno = ENOTSUP;
465
+ return (-1);
466
+ } else
467
+ return (open(path, flags));
468
+ #else
469
+ return (openat(fd, path, flags));
470
+ #endif
471
+ }
472
+
473
+ static int
474
+ la_verify_filetype(mode_t mode, __LA_MODE_T filetype) {
475
+ int ret = 0;
476
+
477
+ switch (filetype) {
478
+ case AE_IFREG:
479
+ ret = (S_ISREG(mode));
480
+ break;
481
+ case AE_IFDIR:
482
+ ret = (S_ISDIR(mode));
483
+ break;
484
+ case AE_IFLNK:
485
+ ret = (S_ISLNK(mode));
486
+ break;
487
+ #ifdef S_ISSOCK
488
+ case AE_IFSOCK:
489
+ ret = (S_ISSOCK(mode));
490
+ break;
491
+ #endif
492
+ case AE_IFCHR:
493
+ ret = (S_ISCHR(mode));
494
+ break;
495
+ case AE_IFBLK:
496
+ ret = (S_ISBLK(mode));
497
+ break;
498
+ case AE_IFIFO:
499
+ ret = (S_ISFIFO(mode));
500
+ break;
501
+ default:
502
+ break;
503
+ }
504
+
505
+ return (ret);
506
+ }
507
+
508
+ static int
509
+ lazy_stat(struct archive_write_disk *a)
510
+ {
511
+ if (a->pst != NULL) {
512
+ /* Already have stat() data available. */
513
+ return (ARCHIVE_OK);
514
+ }
515
+ #ifdef HAVE_FSTAT
516
+ if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
517
+ a->pst = &a->st;
518
+ return (ARCHIVE_OK);
519
+ }
520
+ #endif
521
+ /*
522
+ * XXX At this point, symlinks should not be hit, otherwise
523
+ * XXX a race occurred. Do we want to check explicitly for that?
524
+ */
525
+ #ifdef HAVE_LSTAT
526
+ if (lstat(a->name, &a->st) == 0)
527
+ #else
528
+ if (la_stat(a->name, &a->st) == 0)
529
+ #endif
530
+ {
531
+ a->pst = &a->st;
532
+ return (ARCHIVE_OK);
533
+ }
534
+ archive_set_error(&a->archive, errno, "Couldn't stat file");
535
+ return (ARCHIVE_WARN);
536
+ }
537
+
538
+ static const struct archive_vtable
539
+ archive_write_disk_vtable = {
540
+ .archive_close = _archive_write_disk_close,
541
+ .archive_filter_bytes = _archive_write_disk_filter_bytes,
542
+ .archive_free = _archive_write_disk_free,
543
+ .archive_write_header = _archive_write_disk_header,
544
+ .archive_write_finish_entry = _archive_write_disk_finish_entry,
545
+ .archive_write_data = _archive_write_disk_data,
546
+ .archive_write_data_block = _archive_write_disk_data_block,
547
+ };
548
+
549
+ static int64_t
550
+ _archive_write_disk_filter_bytes(struct archive *_a, int n)
551
+ {
552
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
553
+ (void)n; /* UNUSED */
554
+ if (n == -1 || n == 0)
555
+ return (a->total_bytes_written);
556
+ return (-1);
557
+ }
558
+
559
+
560
+ int
561
+ archive_write_disk_set_options(struct archive *_a, int flags)
562
+ {
563
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
564
+
565
+ a->flags = flags;
566
+ return (ARCHIVE_OK);
567
+ }
568
+
569
+
570
+ /*
571
+ * Extract this entry to disk.
572
+ *
573
+ * TODO: Validate hardlinks. According to the standards, we're
574
+ * supposed to check each extracted hardlink and squawk if it refers
575
+ * to a file that we didn't restore. I'm not entirely convinced this
576
+ * is a good idea, but more importantly: Is there any way to validate
577
+ * hardlinks without keeping a complete list of filenames from the
578
+ * entire archive?? Ugh.
579
+ *
580
+ */
581
+ static int
582
+ _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
583
+ {
584
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
585
+ struct fixup_entry *fe;
586
+ const char *linkname;
587
+ int ret, r;
588
+
589
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
590
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
591
+ "archive_write_disk_header");
592
+ archive_clear_error(&a->archive);
593
+ if (a->archive.state & ARCHIVE_STATE_DATA) {
594
+ r = _archive_write_disk_finish_entry(&a->archive);
595
+ if (r == ARCHIVE_FATAL)
596
+ return (r);
597
+ }
598
+
599
+ /* Set up for this particular entry. */
600
+ a->pst = NULL;
601
+ a->current_fixup = NULL;
602
+ a->deferred = 0;
603
+ archive_entry_free(a->entry);
604
+ a->entry = archive_entry_clone(entry);
605
+ if (a->entry == NULL) {
606
+ archive_set_error(&a->archive, ENOMEM, "Out of memory");
607
+ return (ARCHIVE_FATAL);
608
+ }
609
+ a->fd = -1;
610
+ a->fd_offset = 0;
611
+ a->offset = 0;
612
+ a->restore_pwd = -1;
613
+ a->uid = a->user_uid;
614
+ a->mode = archive_entry_mode(a->entry);
615
+ if (archive_entry_size_is_set(a->entry))
616
+ a->filesize = archive_entry_size(a->entry);
617
+ else
618
+ a->filesize = -1;
619
+ archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
620
+ a->name = a->_name_data.s;
621
+ archive_clear_error(&a->archive);
622
+
623
+ /*
624
+ * Clean up the requested path. This is necessary for correct
625
+ * dir restores; the dir restore logic otherwise gets messed
626
+ * up by nonsense like "dir/.".
627
+ */
628
+ ret = cleanup_pathname(a);
629
+ if (ret != ARCHIVE_OK)
630
+ return (ret);
631
+
632
+ /*
633
+ * Check if we have a hardlink that points to itself.
634
+ */
635
+ linkname = archive_entry_hardlink(a->entry);
636
+ if (linkname != NULL && strcmp(a->name, linkname) == 0) {
637
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
638
+ "Skipping hardlink pointing to itself: %s",
639
+ a->name);
640
+ return (ARCHIVE_WARN);
641
+ }
642
+
643
+ /*
644
+ * Query the umask so we get predictable mode settings.
645
+ * This gets done on every call to _write_header in case the
646
+ * user edits their umask during the extraction for some
647
+ * reason.
648
+ */
649
+ umask(a->user_umask = umask(0));
650
+
651
+ /* Figure out what we need to do for this entry. */
652
+ a->todo = TODO_MODE_BASE;
653
+ if (a->flags & ARCHIVE_EXTRACT_PERM) {
654
+ a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
655
+ /*
656
+ * SGID requires an extra "check" step because we
657
+ * cannot easily predict the GID that the system will
658
+ * assign. (Different systems assign GIDs to files
659
+ * based on a variety of criteria, including process
660
+ * credentials and the gid of the enclosing
661
+ * directory.) We can only restore the SGID bit if
662
+ * the file has the right GID, and we only know the
663
+ * GID if we either set it (see set_ownership) or if
664
+ * we've actually called stat() on the file after it
665
+ * was restored. Since there are several places at
666
+ * which we might verify the GID, we need a TODO bit
667
+ * to keep track.
668
+ */
669
+ if (a->mode & S_ISGID)
670
+ a->todo |= TODO_SGID | TODO_SGID_CHECK;
671
+ /*
672
+ * Verifying the SUID is simpler, but can still be
673
+ * done in multiple ways, hence the separate "check" bit.
674
+ */
675
+ if (a->mode & S_ISUID)
676
+ a->todo |= TODO_SUID | TODO_SUID_CHECK;
677
+ } else {
678
+ /*
679
+ * User didn't request full permissions, so don't
680
+ * restore SUID, SGID bits and obey umask.
681
+ */
682
+ a->mode &= ~S_ISUID;
683
+ a->mode &= ~S_ISGID;
684
+ a->mode &= ~S_ISVTX;
685
+ a->mode &= ~a->user_umask;
686
+ }
687
+ if (a->flags & ARCHIVE_EXTRACT_OWNER)
688
+ a->todo |= TODO_OWNER;
689
+ if (a->flags & ARCHIVE_EXTRACT_TIME)
690
+ a->todo |= TODO_TIMES;
691
+ if (a->flags & ARCHIVE_EXTRACT_ACL) {
692
+ #if ARCHIVE_ACL_DARWIN
693
+ /*
694
+ * On MacOS, platform ACLs get stored in mac_metadata, too.
695
+ * If we intend to extract mac_metadata and it is present
696
+ * we skip extracting libarchive NFSv4 ACLs.
697
+ */
698
+ size_t metadata_size;
699
+
700
+ if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
701
+ archive_entry_mac_metadata(a->entry,
702
+ &metadata_size) == NULL || metadata_size == 0)
703
+ #endif
704
+ #if ARCHIVE_ACL_LIBRICHACL
705
+ /*
706
+ * RichACLs are stored in an extended attribute.
707
+ * If we intend to extract extended attributes and have this
708
+ * attribute we skip extracting libarchive NFSv4 ACLs.
709
+ */
710
+ short extract_acls = 1;
711
+ if (a->flags & ARCHIVE_EXTRACT_XATTR && (
712
+ archive_entry_acl_types(a->entry) &
713
+ ARCHIVE_ENTRY_ACL_TYPE_NFS4)) {
714
+ const char *attr_name;
715
+ const void *attr_value;
716
+ size_t attr_size;
717
+ int i = archive_entry_xattr_reset(a->entry);
718
+ while (i--) {
719
+ archive_entry_xattr_next(a->entry, &attr_name,
720
+ &attr_value, &attr_size);
721
+ if (attr_name != NULL && attr_value != NULL &&
722
+ attr_size > 0 && strcmp(attr_name,
723
+ "trusted.richacl") == 0) {
724
+ extract_acls = 0;
725
+ break;
726
+ }
727
+ }
728
+ }
729
+ if (extract_acls)
730
+ #endif
731
+ #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
732
+ {
733
+ #endif
734
+ if (archive_entry_filetype(a->entry) == AE_IFDIR)
735
+ a->deferred |= TODO_ACLS;
736
+ else
737
+ a->todo |= TODO_ACLS;
738
+ #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
739
+ }
740
+ #endif
741
+ }
742
+ if (a->flags & ARCHIVE_EXTRACT_MAC_METADATA) {
743
+ if (archive_entry_filetype(a->entry) == AE_IFDIR)
744
+ a->deferred |= TODO_MAC_METADATA;
745
+ else
746
+ a->todo |= TODO_MAC_METADATA;
747
+ }
748
+ #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
749
+ if ((a->flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) == 0) {
750
+ unsigned long set, clear;
751
+ archive_entry_fflags(a->entry, &set, &clear);
752
+ if ((set & ~clear) & UF_COMPRESSED) {
753
+ a->todo |= TODO_HFS_COMPRESSION;
754
+ a->decmpfs_block_count = (unsigned)-1;
755
+ }
756
+ }
757
+ if ((a->flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) != 0 &&
758
+ (a->mode & AE_IFMT) == AE_IFREG && a->filesize > 0) {
759
+ a->todo |= TODO_HFS_COMPRESSION;
760
+ a->decmpfs_block_count = (unsigned)-1;
761
+ }
762
+ {
763
+ const char *p;
764
+
765
+ /* Check if the current file name is a type of the
766
+ * resource fork file. */
767
+ p = strrchr(a->name, '/');
768
+ if (p == NULL)
769
+ p = a->name;
770
+ else
771
+ p++;
772
+ if (p[0] == '.' && p[1] == '_') {
773
+ /* Do not compress "._XXX" files. */
774
+ a->todo &= ~TODO_HFS_COMPRESSION;
775
+ if (a->filesize > 0)
776
+ a->todo |= TODO_APPLEDOUBLE;
777
+ }
778
+ }
779
+ #endif
780
+
781
+ if (a->flags & ARCHIVE_EXTRACT_XATTR) {
782
+ #if ARCHIVE_XATTR_DARWIN
783
+ /*
784
+ * On MacOS, extended attributes get stored in mac_metadata,
785
+ * too. If we intend to extract mac_metadata and it is present
786
+ * we skip extracting extended attributes.
787
+ */
788
+ size_t metadata_size;
789
+
790
+ if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
791
+ archive_entry_mac_metadata(a->entry,
792
+ &metadata_size) == NULL || metadata_size == 0)
793
+ #endif
794
+ a->todo |= TODO_XATTR;
795
+ }
796
+ if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
797
+ a->todo |= TODO_FFLAGS;
798
+ if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
799
+ ret = check_symlinks(a);
800
+ if (ret != ARCHIVE_OK)
801
+ return (ret);
802
+ }
803
+ #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
804
+ /* If path exceeds PATH_MAX, shorten the path. */
805
+ edit_deep_directories(a);
806
+ #endif
807
+
808
+ ret = restore_entry(a);
809
+
810
+ #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
811
+ /*
812
+ * Check if the filesystem the file is restoring on supports
813
+ * HFS+ Compression. If not, cancel HFS+ Compression.
814
+ */
815
+ if (a->todo | TODO_HFS_COMPRESSION) {
816
+ /*
817
+ * NOTE: UF_COMPRESSED is ignored even if the filesystem
818
+ * supports HFS+ Compression because the file should
819
+ * have at least an extended attribute "com.apple.decmpfs"
820
+ * before the flag is set to indicate that the file have
821
+ * been compressed. If the filesystem does not support
822
+ * HFS+ Compression the system call will fail.
823
+ */
824
+ if (a->fd < 0 || fchflags(a->fd, UF_COMPRESSED) != 0)
825
+ a->todo &= ~TODO_HFS_COMPRESSION;
826
+ }
827
+ #endif
828
+
829
+ /*
830
+ * TODO: There are rumours that some extended attributes must
831
+ * be restored before file data is written. If this is true,
832
+ * then we either need to write all extended attributes both
833
+ * before and after restoring the data, or find some rule for
834
+ * determining which must go first and which last. Due to the
835
+ * many ways people are using xattrs, this may prove to be an
836
+ * intractable problem.
837
+ */
838
+
839
+ #ifdef HAVE_FCHDIR
840
+ /* If we changed directory above, restore it here. */
841
+ if (a->restore_pwd >= 0) {
842
+ r = fchdir(a->restore_pwd);
843
+ if (r != 0) {
844
+ archive_set_error(&a->archive, errno,
845
+ "chdir() failure");
846
+ ret = ARCHIVE_FATAL;
847
+ }
848
+ close(a->restore_pwd);
849
+ a->restore_pwd = -1;
850
+ }
851
+ #endif
852
+
853
+ /*
854
+ * Fixup uses the unedited pathname from archive_entry_pathname(),
855
+ * because it is relative to the base dir and the edited path
856
+ * might be relative to some intermediate dir as a result of the
857
+ * deep restore logic.
858
+ */
859
+ if (a->deferred & TODO_MODE) {
860
+ fe = current_fixup(a, archive_entry_pathname(entry));
861
+ if (fe == NULL)
862
+ return (ARCHIVE_FATAL);
863
+ fe->filetype = archive_entry_filetype(entry);
864
+ fe->fixup |= TODO_MODE_BASE;
865
+ fe->mode = a->mode;
866
+ }
867
+
868
+ if ((a->deferred & TODO_TIMES)
869
+ && (archive_entry_mtime_is_set(entry)
870
+ || archive_entry_atime_is_set(entry))) {
871
+ fe = current_fixup(a, archive_entry_pathname(entry));
872
+ if (fe == NULL)
873
+ return (ARCHIVE_FATAL);
874
+ fe->filetype = archive_entry_filetype(entry);
875
+ fe->mode = a->mode;
876
+ fe->fixup |= TODO_TIMES;
877
+ if (archive_entry_atime_is_set(entry)) {
878
+ fe->atime = archive_entry_atime(entry);
879
+ fe->atime_nanos = archive_entry_atime_nsec(entry);
880
+ } else {
881
+ /* If atime is unset, use start time. */
882
+ fe->atime = a->start_time;
883
+ fe->atime_nanos = 0;
884
+ }
885
+ if (archive_entry_mtime_is_set(entry)) {
886
+ fe->mtime = archive_entry_mtime(entry);
887
+ fe->mtime_nanos = archive_entry_mtime_nsec(entry);
888
+ } else {
889
+ /* If mtime is unset, use start time. */
890
+ fe->mtime = a->start_time;
891
+ fe->mtime_nanos = 0;
892
+ }
893
+ if (archive_entry_birthtime_is_set(entry)) {
894
+ fe->birthtime = archive_entry_birthtime(entry);
895
+ fe->birthtime_nanos = archive_entry_birthtime_nsec(
896
+ entry);
897
+ } else {
898
+ /* If birthtime is unset, use mtime. */
899
+ fe->birthtime = fe->mtime;
900
+ fe->birthtime_nanos = fe->mtime_nanos;
901
+ }
902
+ }
903
+
904
+ if (a->deferred & TODO_ACLS) {
905
+ fe = current_fixup(a, archive_entry_pathname(entry));
906
+ if (fe == NULL)
907
+ return (ARCHIVE_FATAL);
908
+ fe->filetype = archive_entry_filetype(entry);
909
+ fe->fixup |= TODO_ACLS;
910
+ archive_acl_copy(&fe->acl, archive_entry_acl(entry));
911
+ }
912
+
913
+ if (a->deferred & TODO_MAC_METADATA) {
914
+ const void *metadata;
915
+ size_t metadata_size;
916
+ metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
917
+ if (metadata != NULL && metadata_size > 0) {
918
+ fe = current_fixup(a, archive_entry_pathname(entry));
919
+ if (fe == NULL)
920
+ return (ARCHIVE_FATAL);
921
+ fe->filetype = archive_entry_filetype(entry);
922
+ fe->mac_metadata = malloc(metadata_size);
923
+ if (fe->mac_metadata != NULL) {
924
+ memcpy(fe->mac_metadata, metadata,
925
+ metadata_size);
926
+ fe->mac_metadata_size = metadata_size;
927
+ fe->fixup |= TODO_MAC_METADATA;
928
+ }
929
+ }
930
+ }
931
+
932
+ if (a->deferred & TODO_FFLAGS) {
933
+ fe = current_fixup(a, archive_entry_pathname(entry));
934
+ if (fe == NULL)
935
+ return (ARCHIVE_FATAL);
936
+ fe->filetype = archive_entry_filetype(entry);
937
+ fe->fixup |= TODO_FFLAGS;
938
+ /* TODO: Complete this.. defer fflags from below. */
939
+ }
940
+
941
+ /* We've created the object and are ready to pour data into it. */
942
+ if (ret >= ARCHIVE_WARN)
943
+ a->archive.state = ARCHIVE_STATE_DATA;
944
+ /*
945
+ * If it's not open, tell our client not to try writing.
946
+ * In particular, dirs, links, etc, don't get written to.
947
+ */
948
+ if (a->fd < 0) {
949
+ archive_entry_set_size(entry, 0);
950
+ a->filesize = 0;
951
+ }
952
+
953
+ return (ret);
954
+ }
955
+
956
+ int
957
+ archive_write_disk_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i)
958
+ {
959
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
960
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
961
+ ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
962
+ a->skip_file_set = 1;
963
+ a->skip_file_dev = d;
964
+ a->skip_file_ino = i;
965
+ return (ARCHIVE_OK);
966
+ }
967
+
968
+ static ssize_t
969
+ write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
970
+ {
971
+ uint64_t start_size = size;
972
+ ssize_t bytes_written = 0;
973
+ ssize_t block_size = 0, bytes_to_write;
974
+
975
+ if (size == 0)
976
+ return (ARCHIVE_OK);
977
+
978
+ if (a->filesize == 0 || a->fd < 0) {
979
+ archive_set_error(&a->archive, EIO,
980
+ "Attempt to write to an empty file");
981
+ return (ARCHIVE_WARN);
982
+ }
983
+
984
+ if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
985
+ #if HAVE_STRUCT_STAT_ST_BLKSIZE
986
+ int r;
987
+ if ((r = lazy_stat(a)) != ARCHIVE_OK)
988
+ return (r);
989
+ block_size = a->pst->st_blksize;
990
+ #else
991
+ /* XXX TODO XXX Is there a more appropriate choice here ? */
992
+ /* This needn't match the filesystem allocation size. */
993
+ block_size = 16*1024;
994
+ #endif
995
+ }
996
+
997
+ /* If this write would run beyond the file size, truncate it. */
998
+ if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
999
+ start_size = size = (size_t)(a->filesize - a->offset);
1000
+
1001
+ /* Write the data. */
1002
+ while (size > 0) {
1003
+ if (block_size == 0) {
1004
+ bytes_to_write = size;
1005
+ } else {
1006
+ /* We're sparsifying the file. */
1007
+ const char *p, *end;
1008
+ int64_t block_end;
1009
+
1010
+ /* Skip leading zero bytes. */
1011
+ for (p = buff, end = buff + size; p < end; ++p) {
1012
+ if (*p != '\0')
1013
+ break;
1014
+ }
1015
+ a->offset += p - buff;
1016
+ size -= p - buff;
1017
+ buff = p;
1018
+ if (size == 0)
1019
+ break;
1020
+
1021
+ /* Calculate next block boundary after offset. */
1022
+ block_end
1023
+ = (a->offset / block_size + 1) * block_size;
1024
+
1025
+ /* If the adjusted write would cross block boundary,
1026
+ * truncate it to the block boundary. */
1027
+ bytes_to_write = size;
1028
+ if (a->offset + bytes_to_write > block_end)
1029
+ bytes_to_write = block_end - a->offset;
1030
+ }
1031
+ /* Seek if necessary to the specified offset. */
1032
+ if (a->offset != a->fd_offset) {
1033
+ if (lseek(a->fd, a->offset, SEEK_SET) < 0) {
1034
+ archive_set_error(&a->archive, errno,
1035
+ "Seek failed");
1036
+ return (ARCHIVE_FATAL);
1037
+ }
1038
+ a->fd_offset = a->offset;
1039
+ }
1040
+ bytes_written = write(a->fd, buff, bytes_to_write);
1041
+ if (bytes_written < 0) {
1042
+ archive_set_error(&a->archive, errno, "Write failed");
1043
+ return (ARCHIVE_WARN);
1044
+ }
1045
+ buff += bytes_written;
1046
+ size -= bytes_written;
1047
+ a->total_bytes_written += bytes_written;
1048
+ a->offset += bytes_written;
1049
+ a->fd_offset = a->offset;
1050
+ }
1051
+ return (start_size - size);
1052
+ }
1053
+
1054
+ #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
1055
+ && defined(HAVE_ZLIB_H)
1056
+
1057
+ /*
1058
+ * Set UF_COMPRESSED file flag.
1059
+ * This have to be called after hfs_write_decmpfs() because if the
1060
+ * file does not have "com.apple.decmpfs" xattr the flag is ignored.
1061
+ */
1062
+ static int
1063
+ hfs_set_compressed_fflag(struct archive_write_disk *a)
1064
+ {
1065
+ int r;
1066
+
1067
+ if ((r = lazy_stat(a)) != ARCHIVE_OK)
1068
+ return (r);
1069
+
1070
+ a->st.st_flags |= UF_COMPRESSED;
1071
+ if (fchflags(a->fd, a->st.st_flags) != 0) {
1072
+ archive_set_error(&a->archive, errno,
1073
+ "Failed to set UF_COMPRESSED file flag");
1074
+ return (ARCHIVE_WARN);
1075
+ }
1076
+ return (ARCHIVE_OK);
1077
+ }
1078
+
1079
+ /*
1080
+ * HFS+ Compression decmpfs
1081
+ *
1082
+ * +------------------------------+ +0
1083
+ * | Magic(LE 4 bytes) |
1084
+ * +------------------------------+
1085
+ * | Type(LE 4 bytes) |
1086
+ * +------------------------------+
1087
+ * | Uncompressed size(LE 8 bytes)|
1088
+ * +------------------------------+ +16
1089
+ * | |
1090
+ * | Compressed data |
1091
+ * | (Placed only if Type == 3) |
1092
+ * | |
1093
+ * +------------------------------+ +3802 = MAX_DECMPFS_XATTR_SIZE
1094
+ *
1095
+ * Type is 3: decmpfs has compressed data.
1096
+ * Type is 4: Resource Fork has compressed data.
1097
+ */
1098
+ /*
1099
+ * Write "com.apple.decmpfs"
1100
+ */
1101
+ static int
1102
+ hfs_write_decmpfs(struct archive_write_disk *a)
1103
+ {
1104
+ int r;
1105
+ uint32_t compression_type;
1106
+
1107
+ r = fsetxattr(a->fd, DECMPFS_XATTR_NAME, a->decmpfs_header_p,
1108
+ a->decmpfs_attr_size, 0, 0);
1109
+ if (r < 0) {
1110
+ archive_set_error(&a->archive, errno,
1111
+ "Cannot restore xattr:%s", DECMPFS_XATTR_NAME);
1112
+ compression_type = archive_le32dec(
1113
+ &a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE]);
1114
+ if (compression_type == CMP_RESOURCE_FORK)
1115
+ fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME,
1116
+ XATTR_SHOWCOMPRESSION);
1117
+ return (ARCHIVE_WARN);
1118
+ }
1119
+ return (ARCHIVE_OK);
1120
+ }
1121
+
1122
+ /*
1123
+ * HFS+ Compression Resource Fork
1124
+ *
1125
+ * +-----------------------------+
1126
+ * | Header(260 bytes) |
1127
+ * +-----------------------------+
1128
+ * | Block count(LE 4 bytes) |
1129
+ * +-----------------------------+ --+
1130
+ * +-- | Offset (LE 4 bytes) | |
1131
+ * | | [distance from Block count] | | Block 0
1132
+ * | +-----------------------------+ |
1133
+ * | | Compressed size(LE 4 bytes) | |
1134
+ * | +-----------------------------+ --+
1135
+ * | | |
1136
+ * | | .................. |
1137
+ * | | |
1138
+ * | +-----------------------------+ --+
1139
+ * | | Offset (LE 4 bytes) | |
1140
+ * | +-----------------------------+ | Block (Block count -1)
1141
+ * | | Compressed size(LE 4 bytes) | |
1142
+ * +-> +-----------------------------+ --+
1143
+ * | Compressed data(n bytes) | Block 0
1144
+ * +-----------------------------+
1145
+ * | |
1146
+ * | .................. |
1147
+ * | |
1148
+ * +-----------------------------+
1149
+ * | Compressed data(n bytes) | Block (Block count -1)
1150
+ * +-----------------------------+
1151
+ * | Footer(50 bytes) |
1152
+ * +-----------------------------+
1153
+ *
1154
+ */
1155
+ /*
1156
+ * Write the header of "com.apple.ResourceFork"
1157
+ */
1158
+ static int
1159
+ hfs_write_resource_fork(struct archive_write_disk *a, unsigned char *buff,
1160
+ size_t bytes, uint32_t position)
1161
+ {
1162
+ int ret;
1163
+
1164
+ ret = fsetxattr(a->fd, XATTR_RESOURCEFORK_NAME, buff, bytes,
1165
+ position, a->rsrc_xattr_options);
1166
+ if (ret < 0) {
1167
+ archive_set_error(&a->archive, errno,
1168
+ "Cannot restore xattr: %s at %u pos %u bytes",
1169
+ XATTR_RESOURCEFORK_NAME,
1170
+ (unsigned)position,
1171
+ (unsigned)bytes);
1172
+ return (ARCHIVE_WARN);
1173
+ }
1174
+ a->rsrc_xattr_options &= ~XATTR_CREATE;
1175
+ return (ARCHIVE_OK);
1176
+ }
1177
+
1178
+ static int
1179
+ hfs_write_compressed_data(struct archive_write_disk *a, size_t bytes_compressed)
1180
+ {
1181
+ int ret;
1182
+
1183
+ ret = hfs_write_resource_fork(a, a->compressed_buffer,
1184
+ bytes_compressed, a->compressed_rsrc_position);
1185
+ if (ret == ARCHIVE_OK)
1186
+ a->compressed_rsrc_position += bytes_compressed;
1187
+ return (ret);
1188
+ }
1189
+
1190
+ static int
1191
+ hfs_write_resource_fork_header(struct archive_write_disk *a)
1192
+ {
1193
+ unsigned char *buff;
1194
+ uint32_t rsrc_bytes;
1195
+ uint32_t rsrc_header_bytes;
1196
+
1197
+ /*
1198
+ * Write resource fork header + block info.
1199
+ */
1200
+ buff = a->resource_fork;
1201
+ rsrc_bytes = a->compressed_rsrc_position - RSRC_F_SIZE;
1202
+ rsrc_header_bytes =
1203
+ RSRC_H_SIZE + /* Header base size. */
1204
+ 4 + /* Block count. */
1205
+ (a->decmpfs_block_count * 8);/* Block info */
1206
+ archive_be32enc(buff, 0x100);
1207
+ archive_be32enc(buff + 4, rsrc_bytes);
1208
+ archive_be32enc(buff + 8, rsrc_bytes - 256);
1209
+ archive_be32enc(buff + 12, 0x32);
1210
+ memset(buff + 16, 0, 240);
1211
+ archive_be32enc(buff + 256, rsrc_bytes - 260);
1212
+ return hfs_write_resource_fork(a, buff, rsrc_header_bytes, 0);
1213
+ }
1214
+
1215
+ static size_t
1216
+ hfs_set_resource_fork_footer(unsigned char *buff, size_t buff_size)
1217
+ {
1218
+ static const char rsrc_footer[RSRC_F_SIZE] = {
1219
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1220
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1221
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1222
+ 0x00, 0x1c, 0x00, 0x32, 0x00, 0x00, 'c', 'm',
1223
+ 'p', 'f', 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01,
1224
+ 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1225
+ 0x00, 0x00
1226
+ };
1227
+ if (buff_size < sizeof(rsrc_footer))
1228
+ return (0);
1229
+ memcpy(buff, rsrc_footer, sizeof(rsrc_footer));
1230
+ return (sizeof(rsrc_footer));
1231
+ }
1232
+
1233
+ static int
1234
+ hfs_reset_compressor(struct archive_write_disk *a)
1235
+ {
1236
+ int ret;
1237
+
1238
+ if (a->stream_valid)
1239
+ ret = deflateReset(&a->stream);
1240
+ else
1241
+ ret = deflateInit(&a->stream, a->decmpfs_compression_level);
1242
+
1243
+ if (ret != Z_OK) {
1244
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1245
+ "Failed to initialize compressor");
1246
+ return (ARCHIVE_FATAL);
1247
+ } else
1248
+ a->stream_valid = 1;
1249
+
1250
+ return (ARCHIVE_OK);
1251
+ }
1252
+
1253
+ static int
1254
+ hfs_decompress(struct archive_write_disk *a)
1255
+ {
1256
+ uint32_t *block_info;
1257
+ uint32_t block_count;
1258
+ uint32_t data_pos, data_size;
1259
+ ssize_t r;
1260
+ ssize_t bytes_written, bytes_to_write;
1261
+ unsigned char *b;
1262
+
1263
+ block_info = (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
1264
+ block_count = archive_le32dec(block_info++);
1265
+ while (block_count--) {
1266
+ data_pos = RSRC_H_SIZE + archive_le32dec(block_info++);
1267
+ data_size = archive_le32dec(block_info++);
1268
+ r = fgetxattr(a->fd, XATTR_RESOURCEFORK_NAME,
1269
+ a->compressed_buffer, data_size, data_pos, 0);
1270
+ if (r != data_size) {
1271
+ archive_set_error(&a->archive,
1272
+ (r < 0)?errno:ARCHIVE_ERRNO_MISC,
1273
+ "Failed to read resource fork");
1274
+ return (ARCHIVE_WARN);
1275
+ }
1276
+ if (a->compressed_buffer[0] == 0xff) {
1277
+ bytes_to_write = data_size -1;
1278
+ b = a->compressed_buffer + 1;
1279
+ } else {
1280
+ size_t dest_len = MAX_DECMPFS_BLOCK_SIZE;
1281
+ int zr;
1282
+
1283
+ zr = uncompress(a->uncompressed_buffer,
1284
+ &dest_len, a->compressed_buffer, data_size);
1285
+ if (zr != Z_OK) {
1286
+ archive_set_error(&a->archive,
1287
+ ARCHIVE_ERRNO_MISC,
1288
+ "Failed to decompress resource fork");
1289
+ return (ARCHIVE_WARN);
1290
+ }
1291
+ bytes_to_write = dest_len;
1292
+ b = a->uncompressed_buffer;
1293
+ }
1294
+ do {
1295
+ bytes_written = write(a->fd, b, bytes_to_write);
1296
+ if (bytes_written < 0) {
1297
+ archive_set_error(&a->archive, errno,
1298
+ "Write failed");
1299
+ return (ARCHIVE_WARN);
1300
+ }
1301
+ bytes_to_write -= bytes_written;
1302
+ b += bytes_written;
1303
+ } while (bytes_to_write > 0);
1304
+ }
1305
+ r = fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME, 0);
1306
+ if (r == -1) {
1307
+ archive_set_error(&a->archive, errno,
1308
+ "Failed to remove resource fork");
1309
+ return (ARCHIVE_WARN);
1310
+ }
1311
+ return (ARCHIVE_OK);
1312
+ }
1313
+
1314
+ static int
1315
+ hfs_drive_compressor(struct archive_write_disk *a, const char *buff,
1316
+ size_t size)
1317
+ {
1318
+ unsigned char *buffer_compressed;
1319
+ size_t bytes_compressed;
1320
+ size_t bytes_used;
1321
+ int ret;
1322
+
1323
+ ret = hfs_reset_compressor(a);
1324
+ if (ret != ARCHIVE_OK)
1325
+ return (ret);
1326
+
1327
+ if (a->compressed_buffer == NULL) {
1328
+ size_t block_size;
1329
+
1330
+ block_size = COMPRESSED_W_SIZE + RSRC_F_SIZE +
1331
+ + compressBound(MAX_DECMPFS_BLOCK_SIZE);
1332
+ a->compressed_buffer = malloc(block_size);
1333
+ if (a->compressed_buffer == NULL) {
1334
+ archive_set_error(&a->archive, ENOMEM,
1335
+ "Can't allocate memory for Resource Fork");
1336
+ return (ARCHIVE_FATAL);
1337
+ }
1338
+ a->compressed_buffer_size = block_size;
1339
+ a->compressed_buffer_remaining = block_size;
1340
+ }
1341
+
1342
+ buffer_compressed = a->compressed_buffer +
1343
+ a->compressed_buffer_size - a->compressed_buffer_remaining;
1344
+ a->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff;
1345
+ a->stream.avail_in = size;
1346
+ a->stream.next_out = buffer_compressed;
1347
+ a->stream.avail_out = a->compressed_buffer_remaining;
1348
+ do {
1349
+ ret = deflate(&a->stream, Z_FINISH);
1350
+ switch (ret) {
1351
+ case Z_OK:
1352
+ case Z_STREAM_END:
1353
+ break;
1354
+ default:
1355
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1356
+ "Failed to compress data");
1357
+ return (ARCHIVE_FAILED);
1358
+ }
1359
+ } while (ret == Z_OK);
1360
+ bytes_compressed = a->compressed_buffer_remaining - a->stream.avail_out;
1361
+
1362
+ /*
1363
+ * If the compressed size is larger than the original size,
1364
+ * throw away compressed data, use uncompressed data instead.
1365
+ */
1366
+ if (bytes_compressed > size) {
1367
+ buffer_compressed[0] = 0xFF;/* uncompressed marker. */
1368
+ memcpy(buffer_compressed + 1, buff, size);
1369
+ bytes_compressed = size + 1;
1370
+ }
1371
+ a->compressed_buffer_remaining -= bytes_compressed;
1372
+
1373
+ /*
1374
+ * If the compressed size is smaller than MAX_DECMPFS_XATTR_SIZE
1375
+ * and the block count in the file is only one, store compressed
1376
+ * data to decmpfs xattr instead of the resource fork.
1377
+ */
1378
+ if (a->decmpfs_block_count == 1 &&
1379
+ (a->decmpfs_attr_size + bytes_compressed)
1380
+ <= MAX_DECMPFS_XATTR_SIZE) {
1381
+ archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
1382
+ CMP_XATTR);
1383
+ memcpy(a->decmpfs_header_p + DECMPFS_HEADER_SIZE,
1384
+ buffer_compressed, bytes_compressed);
1385
+ a->decmpfs_attr_size += bytes_compressed;
1386
+ a->compressed_buffer_remaining = a->compressed_buffer_size;
1387
+ /*
1388
+ * Finish HFS+ Compression.
1389
+ * - Write the decmpfs xattr.
1390
+ * - Set the UF_COMPRESSED file flag.
1391
+ */
1392
+ ret = hfs_write_decmpfs(a);
1393
+ if (ret == ARCHIVE_OK)
1394
+ ret = hfs_set_compressed_fflag(a);
1395
+ return (ret);
1396
+ }
1397
+
1398
+ /* Update block info. */
1399
+ archive_le32enc(a->decmpfs_block_info++,
1400
+ a->compressed_rsrc_position_v - RSRC_H_SIZE);
1401
+ archive_le32enc(a->decmpfs_block_info++, bytes_compressed);
1402
+ a->compressed_rsrc_position_v += bytes_compressed;
1403
+
1404
+ /*
1405
+ * Write the compressed data to the resource fork.
1406
+ */
1407
+ bytes_used = a->compressed_buffer_size - a->compressed_buffer_remaining;
1408
+ while (bytes_used >= COMPRESSED_W_SIZE) {
1409
+ ret = hfs_write_compressed_data(a, COMPRESSED_W_SIZE);
1410
+ if (ret != ARCHIVE_OK)
1411
+ return (ret);
1412
+ bytes_used -= COMPRESSED_W_SIZE;
1413
+ if (bytes_used > COMPRESSED_W_SIZE)
1414
+ memmove(a->compressed_buffer,
1415
+ a->compressed_buffer + COMPRESSED_W_SIZE,
1416
+ bytes_used);
1417
+ else
1418
+ memcpy(a->compressed_buffer,
1419
+ a->compressed_buffer + COMPRESSED_W_SIZE,
1420
+ bytes_used);
1421
+ }
1422
+ a->compressed_buffer_remaining = a->compressed_buffer_size - bytes_used;
1423
+
1424
+ /*
1425
+ * If the current block is the last block, write the remaining
1426
+ * compressed data and the resource fork footer.
1427
+ */
1428
+ if (a->file_remaining_bytes == 0) {
1429
+ size_t rsrc_size;
1430
+ int64_t bk;
1431
+
1432
+ /* Append the resource footer. */
1433
+ rsrc_size = hfs_set_resource_fork_footer(
1434
+ a->compressed_buffer + bytes_used,
1435
+ a->compressed_buffer_remaining);
1436
+ ret = hfs_write_compressed_data(a, bytes_used + rsrc_size);
1437
+ a->compressed_buffer_remaining = a->compressed_buffer_size;
1438
+
1439
+ /* If the compressed size is not enough smaller than
1440
+ * the uncompressed size. cancel HFS+ compression.
1441
+ * TODO: study a behavior of ditto utility and improve
1442
+ * the condition to fall back into no HFS+ compression. */
1443
+ bk = HFS_BLOCKS(a->compressed_rsrc_position);
1444
+ bk += bk >> 7;
1445
+ if (bk > HFS_BLOCKS(a->filesize))
1446
+ return hfs_decompress(a);
1447
+ /*
1448
+ * Write the resourcefork header.
1449
+ */
1450
+ if (ret == ARCHIVE_OK)
1451
+ ret = hfs_write_resource_fork_header(a);
1452
+ /*
1453
+ * Finish HFS+ Compression.
1454
+ * - Write the decmpfs xattr.
1455
+ * - Set the UF_COMPRESSED file flag.
1456
+ */
1457
+ if (ret == ARCHIVE_OK)
1458
+ ret = hfs_write_decmpfs(a);
1459
+ if (ret == ARCHIVE_OK)
1460
+ ret = hfs_set_compressed_fflag(a);
1461
+ }
1462
+ return (ret);
1463
+ }
1464
+
1465
+ static ssize_t
1466
+ hfs_write_decmpfs_block(struct archive_write_disk *a, const char *buff,
1467
+ size_t size)
1468
+ {
1469
+ const char *buffer_to_write;
1470
+ size_t bytes_to_write;
1471
+ int ret;
1472
+
1473
+ if (a->decmpfs_block_count == (unsigned)-1) {
1474
+ void *new_block;
1475
+ size_t block_count, new_size;
1476
+
1477
+ if (a->decmpfs_header_p == NULL) {
1478
+ new_block = malloc(MAX_DECMPFS_XATTR_SIZE
1479
+ + sizeof(uint32_t));
1480
+ if (new_block == NULL) {
1481
+ archive_set_error(&a->archive, ENOMEM,
1482
+ "Can't allocate memory for decmpfs");
1483
+ return (ARCHIVE_FATAL);
1484
+ }
1485
+ a->decmpfs_header_p = new_block;
1486
+ }
1487
+ a->decmpfs_attr_size = DECMPFS_HEADER_SIZE;
1488
+ archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_MAGIC],
1489
+ DECMPFS_MAGIC);
1490
+ archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
1491
+ CMP_RESOURCE_FORK);
1492
+ archive_le64enc(&a->decmpfs_header_p[DECMPFS_UNCOMPRESSED_SIZE],
1493
+ a->filesize);
1494
+
1495
+ /*
1496
+ * Calculate block count for the file.
1497
+ */
1498
+ block_count = (a->filesize + MAX_DECMPFS_BLOCK_SIZE - 1) /
1499
+ MAX_DECMPFS_BLOCK_SIZE;
1500
+ if (block_count > (SIZE_MAX - RSRC_H_SIZE - 4 - RSRC_F_SIZE) /
1501
+ (sizeof(uint32_t) * 2) || block_count > UINT32_MAX) {
1502
+ archive_set_error(&a->archive, EFBIG, "File too big");
1503
+ return (ARCHIVE_FATAL);
1504
+ }
1505
+
1506
+ /*
1507
+ * Allocate buffer for resource fork.
1508
+ * Set up related pointers.
1509
+ */
1510
+ new_size =
1511
+ RSRC_H_SIZE + /* header */
1512
+ 4 + /* Block count */
1513
+ (block_count * sizeof(uint32_t) * 2) +
1514
+ RSRC_F_SIZE; /* footer */
1515
+ if (new_size > a->resource_fork_allocated_size) {
1516
+ new_block = realloc(a->resource_fork, new_size);
1517
+ if (new_block == NULL) {
1518
+ archive_set_error(&a->archive, ENOMEM,
1519
+ "Can't allocate memory for ResourceFork");
1520
+ return (ARCHIVE_FATAL);
1521
+ }
1522
+ a->resource_fork_allocated_size = new_size;
1523
+ a->resource_fork = new_block;
1524
+ }
1525
+
1526
+ /* Allocate uncompressed buffer */
1527
+ if (a->uncompressed_buffer == NULL) {
1528
+ new_block = malloc(MAX_DECMPFS_BLOCK_SIZE);
1529
+ if (new_block == NULL) {
1530
+ archive_set_error(&a->archive, ENOMEM,
1531
+ "Can't allocate memory for decmpfs");
1532
+ return (ARCHIVE_FATAL);
1533
+ }
1534
+ a->uncompressed_buffer = new_block;
1535
+ }
1536
+ a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
1537
+ a->file_remaining_bytes = a->filesize;
1538
+ a->compressed_buffer_remaining = a->compressed_buffer_size;
1539
+
1540
+ /*
1541
+ * Set up a resource fork.
1542
+ */
1543
+ a->rsrc_xattr_options = XATTR_CREATE;
1544
+ /* Get the position where we are going to set a bunch
1545
+ * of block info. */
1546
+ a->decmpfs_block_info =
1547
+ (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
1548
+ /* Set the block count to the resource fork. */
1549
+ archive_le32enc(a->decmpfs_block_info++, (uint32_t)block_count);
1550
+ /* Get the position where we are going to set compressed
1551
+ * data. */
1552
+ a->compressed_rsrc_position =
1553
+ RSRC_H_SIZE + 4 + (block_count * 8);
1554
+ a->compressed_rsrc_position_v = a->compressed_rsrc_position;
1555
+ a->decmpfs_block_count = block_count;
1556
+ }
1557
+
1558
+ /* Ignore redundant bytes. */
1559
+ if (a->file_remaining_bytes == 0)
1560
+ return ((ssize_t)size);
1561
+
1562
+ /* Do not overrun a block size. */
1563
+ if (size > a->block_remaining_bytes)
1564
+ bytes_to_write = a->block_remaining_bytes;
1565
+ else
1566
+ bytes_to_write = size;
1567
+ /* Do not overrun the file size. */
1568
+ if (bytes_to_write > a->file_remaining_bytes)
1569
+ bytes_to_write = a->file_remaining_bytes;
1570
+
1571
+ /* For efficiency, if a copy length is full of the uncompressed
1572
+ * buffer size, do not copy writing data to it. */
1573
+ if (bytes_to_write == MAX_DECMPFS_BLOCK_SIZE)
1574
+ buffer_to_write = buff;
1575
+ else {
1576
+ memcpy(a->uncompressed_buffer +
1577
+ MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes,
1578
+ buff, bytes_to_write);
1579
+ buffer_to_write = a->uncompressed_buffer;
1580
+ }
1581
+ a->block_remaining_bytes -= bytes_to_write;
1582
+ a->file_remaining_bytes -= bytes_to_write;
1583
+
1584
+ if (a->block_remaining_bytes == 0 || a->file_remaining_bytes == 0) {
1585
+ ret = hfs_drive_compressor(a, buffer_to_write,
1586
+ MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes);
1587
+ if (ret < 0)
1588
+ return (ret);
1589
+ a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
1590
+ }
1591
+ /* Ignore redundant bytes. */
1592
+ if (a->file_remaining_bytes == 0)
1593
+ return ((ssize_t)size);
1594
+ return (bytes_to_write);
1595
+ }
1596
+
1597
+ static ssize_t
1598
+ hfs_write_data_block(struct archive_write_disk *a, const char *buff,
1599
+ size_t size)
1600
+ {
1601
+ uint64_t start_size = size;
1602
+ ssize_t bytes_written = 0;
1603
+ ssize_t bytes_to_write;
1604
+
1605
+ if (size == 0)
1606
+ return (ARCHIVE_OK);
1607
+
1608
+ if (a->filesize == 0 || a->fd < 0) {
1609
+ archive_set_error(&a->archive, EIO,
1610
+ "Attempt to write to an empty file");
1611
+ return (ARCHIVE_WARN);
1612
+ }
1613
+
1614
+ /* If this write would run beyond the file size, truncate it. */
1615
+ if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
1616
+ start_size = size = (size_t)(a->filesize - a->offset);
1617
+
1618
+ /* Write the data. */
1619
+ while (size > 0) {
1620
+ bytes_to_write = size;
1621
+ /* Seek if necessary to the specified offset. */
1622
+ if (a->offset < a->fd_offset) {
1623
+ /* Can't support backward move. */
1624
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1625
+ "Seek failed");
1626
+ return (ARCHIVE_FATAL);
1627
+ } else if (a->offset > a->fd_offset) {
1628
+ uint64_t skip = a->offset - a->fd_offset;
1629
+ char nullblock[1024];
1630
+
1631
+ memset(nullblock, 0, sizeof(nullblock));
1632
+ while (skip > 0) {
1633
+ if (skip > sizeof(nullblock))
1634
+ bytes_written = hfs_write_decmpfs_block(
1635
+ a, nullblock, sizeof(nullblock));
1636
+ else
1637
+ bytes_written = hfs_write_decmpfs_block(
1638
+ a, nullblock, skip);
1639
+ if (bytes_written < 0) {
1640
+ archive_set_error(&a->archive, errno,
1641
+ "Write failed");
1642
+ return (ARCHIVE_WARN);
1643
+ }
1644
+ skip -= bytes_written;
1645
+ }
1646
+
1647
+ a->fd_offset = a->offset;
1648
+ }
1649
+ bytes_written =
1650
+ hfs_write_decmpfs_block(a, buff, bytes_to_write);
1651
+ if (bytes_written < 0)
1652
+ return (bytes_written);
1653
+ buff += bytes_written;
1654
+ size -= bytes_written;
1655
+ a->total_bytes_written += bytes_written;
1656
+ a->offset += bytes_written;
1657
+ a->fd_offset = a->offset;
1658
+ }
1659
+ return (start_size - size);
1660
+ }
1661
+ #else
1662
+ static ssize_t
1663
+ hfs_write_data_block(struct archive_write_disk *a, const char *buff,
1664
+ size_t size)
1665
+ {
1666
+ return (write_data_block(a, buff, size));
1667
+ }
1668
+ #endif
1669
+
1670
+ static ssize_t
1671
+ _archive_write_disk_data_block(struct archive *_a,
1672
+ const void *buff, size_t size, int64_t offset)
1673
+ {
1674
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1675
+ ssize_t r;
1676
+
1677
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1678
+ ARCHIVE_STATE_DATA, "archive_write_data_block");
1679
+
1680
+ a->offset = offset;
1681
+ if (a->todo & TODO_HFS_COMPRESSION)
1682
+ r = hfs_write_data_block(a, buff, size);
1683
+ else
1684
+ r = write_data_block(a, buff, size);
1685
+ if (r < ARCHIVE_OK)
1686
+ return (r);
1687
+ if ((size_t)r < size) {
1688
+ archive_set_error(&a->archive, EOVERFLOW,
1689
+ "Too much data: Truncating file at %ju bytes",
1690
+ (uintmax_t)a->filesize);
1691
+ return (ARCHIVE_WARN);
1692
+ }
1693
+ #if ARCHIVE_VERSION_NUMBER < 3999000
1694
+ return (ARCHIVE_OK);
1695
+ #else
1696
+ return (size);
1697
+ #endif
1698
+ }
1699
+
1700
+ static ssize_t
1701
+ _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
1702
+ {
1703
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1704
+
1705
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1706
+ ARCHIVE_STATE_DATA, "archive_write_data");
1707
+
1708
+ if (a->todo & TODO_HFS_COMPRESSION)
1709
+ return (hfs_write_data_block(a, buff, size));
1710
+ return (write_data_block(a, buff, size));
1711
+ }
1712
+
1713
+ static int
1714
+ _archive_write_disk_finish_entry(struct archive *_a)
1715
+ {
1716
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1717
+ int ret = ARCHIVE_OK;
1718
+
1719
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1720
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1721
+ "archive_write_finish_entry");
1722
+ if (a->archive.state & ARCHIVE_STATE_HEADER)
1723
+ return (ARCHIVE_OK);
1724
+ archive_clear_error(&a->archive);
1725
+
1726
+ /* Pad or truncate file to the right size. */
1727
+ if (a->fd < 0) {
1728
+ /* There's no file. */
1729
+ } else if (a->filesize < 0) {
1730
+ /* File size is unknown, so we can't set the size. */
1731
+ } else if (a->fd_offset == a->filesize) {
1732
+ /* Last write ended at exactly the filesize; we're done. */
1733
+ /* Hopefully, this is the common case. */
1734
+ #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
1735
+ } else if (a->todo & TODO_HFS_COMPRESSION) {
1736
+ char null_d[1024];
1737
+ ssize_t r;
1738
+
1739
+ if (a->file_remaining_bytes)
1740
+ memset(null_d, 0, sizeof(null_d));
1741
+ while (a->file_remaining_bytes) {
1742
+ if (a->file_remaining_bytes > sizeof(null_d))
1743
+ r = hfs_write_data_block(
1744
+ a, null_d, sizeof(null_d));
1745
+ else
1746
+ r = hfs_write_data_block(
1747
+ a, null_d, a->file_remaining_bytes);
1748
+ if (r < 0) {
1749
+ close_file_descriptor(a);
1750
+ return ((int)r);
1751
+ }
1752
+ }
1753
+ #endif
1754
+ } else {
1755
+ #if HAVE_FTRUNCATE
1756
+ if (ftruncate(a->fd, a->filesize) == -1 &&
1757
+ a->filesize == 0) {
1758
+ archive_set_error(&a->archive, errno,
1759
+ "File size could not be restored");
1760
+ close_file_descriptor(a);
1761
+ return (ARCHIVE_FAILED);
1762
+ }
1763
+ #endif
1764
+ /*
1765
+ * Not all platforms implement the XSI option to
1766
+ * extend files via ftruncate. Stat() the file again
1767
+ * to see what happened.
1768
+ */
1769
+ a->pst = NULL;
1770
+ if ((ret = lazy_stat(a)) != ARCHIVE_OK) {
1771
+ close_file_descriptor(a);
1772
+ return (ret);
1773
+ }
1774
+ /* We can use lseek()/write() to extend the file if
1775
+ * ftruncate didn't work or isn't available. */
1776
+ if (a->st.st_size < a->filesize) {
1777
+ const char nul = '\0';
1778
+ if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) {
1779
+ archive_set_error(&a->archive, errno,
1780
+ "Seek failed");
1781
+ close_file_descriptor(a);
1782
+ return (ARCHIVE_FATAL);
1783
+ }
1784
+ if (write(a->fd, &nul, 1) < 0) {
1785
+ archive_set_error(&a->archive, errno,
1786
+ "Write to restore size failed");
1787
+ close_file_descriptor(a);
1788
+ return (ARCHIVE_FATAL);
1789
+ }
1790
+ a->pst = NULL;
1791
+ }
1792
+ }
1793
+
1794
+ /* Restore metadata. */
1795
+
1796
+ /*
1797
+ * This is specific to Mac OS X.
1798
+ * If the current file is an AppleDouble file, it should be
1799
+ * linked with the data fork file and remove it.
1800
+ */
1801
+ if (a->todo & TODO_APPLEDOUBLE) {
1802
+ int r2 = fixup_appledouble(a, a->name);
1803
+ if (r2 == ARCHIVE_EOF) {
1804
+ /* The current file has been successfully linked
1805
+ * with the data fork file and removed. So there
1806
+ * is nothing to do on the current file. */
1807
+ goto finish_metadata;
1808
+ }
1809
+ if (r2 < ret) ret = r2;
1810
+ }
1811
+
1812
+ /*
1813
+ * Look up the "real" UID only if we're going to need it.
1814
+ * TODO: the TODO_SGID condition can be dropped here, can't it?
1815
+ */
1816
+ if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
1817
+ a->uid = archive_write_disk_uid(&a->archive,
1818
+ archive_entry_uname(a->entry),
1819
+ archive_entry_uid(a->entry));
1820
+ }
1821
+ /* Look up the "real" GID only if we're going to need it. */
1822
+ /* TODO: the TODO_SUID condition can be dropped here, can't it? */
1823
+ if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
1824
+ a->gid = archive_write_disk_gid(&a->archive,
1825
+ archive_entry_gname(a->entry),
1826
+ archive_entry_gid(a->entry));
1827
+ }
1828
+
1829
+ /*
1830
+ * Restore ownership before set_mode tries to restore suid/sgid
1831
+ * bits. If we set the owner, we know what it is and can skip
1832
+ * a stat() call to examine the ownership of the file on disk.
1833
+ */
1834
+ if (a->todo & TODO_OWNER) {
1835
+ int r2 = set_ownership(a);
1836
+ if (r2 < ret) ret = r2;
1837
+ }
1838
+
1839
+ /*
1840
+ * HYPOTHESIS:
1841
+ * If we're not root, we won't be setting any security
1842
+ * attributes that may be wiped by the set_mode() routine
1843
+ * below. We also can't set xattr on non-owner-writable files,
1844
+ * which may be the state after set_mode(). Perform
1845
+ * set_xattrs() first based on these constraints.
1846
+ */
1847
+ if (a->user_uid != 0 &&
1848
+ (a->todo & TODO_XATTR)) {
1849
+ int r2 = set_xattrs(a);
1850
+ if (r2 < ret) ret = r2;
1851
+ }
1852
+
1853
+ /*
1854
+ * set_mode must precede ACLs on systems such as Solaris and
1855
+ * FreeBSD where setting the mode implicitly clears extended ACLs
1856
+ */
1857
+ if (a->todo & TODO_MODE) {
1858
+ int r2 = set_mode(a, a->mode);
1859
+ if (r2 < ret) ret = r2;
1860
+ }
1861
+
1862
+ /*
1863
+ * Security-related extended attributes (such as
1864
+ * security.capability on Linux) have to be restored last,
1865
+ * since they're implicitly removed by other file changes.
1866
+ * We do this last only when root.
1867
+ */
1868
+ if (a->user_uid == 0 &&
1869
+ (a->todo & TODO_XATTR)) {
1870
+ int r2 = set_xattrs(a);
1871
+ if (r2 < ret) ret = r2;
1872
+ }
1873
+
1874
+ /*
1875
+ * Some flags prevent file modification; they must be restored after
1876
+ * file contents are written.
1877
+ */
1878
+ if (a->todo & TODO_FFLAGS) {
1879
+ int r2 = set_fflags(a);
1880
+ if (r2 < ret) ret = r2;
1881
+ }
1882
+
1883
+ /*
1884
+ * Time must follow most other metadata;
1885
+ * otherwise atime will get changed.
1886
+ */
1887
+ if (a->todo & TODO_TIMES) {
1888
+ int r2 = set_times_from_entry(a);
1889
+ if (r2 < ret) ret = r2;
1890
+ }
1891
+
1892
+ /*
1893
+ * Mac extended metadata includes ACLs.
1894
+ */
1895
+ if (a->todo & TODO_MAC_METADATA) {
1896
+ const void *metadata;
1897
+ size_t metadata_size;
1898
+ metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
1899
+ if (metadata != NULL && metadata_size > 0) {
1900
+ int r2 = set_mac_metadata(a, archive_entry_pathname(
1901
+ a->entry), metadata, metadata_size);
1902
+ if (r2 < ret) ret = r2;
1903
+ }
1904
+ }
1905
+
1906
+ /*
1907
+ * ACLs must be restored after timestamps because there are
1908
+ * ACLs that prevent attribute changes (including time).
1909
+ */
1910
+ if (a->todo & TODO_ACLS) {
1911
+ int r2;
1912
+ r2 = archive_write_disk_set_acls(&a->archive, a->fd,
1913
+ archive_entry_pathname(a->entry),
1914
+ archive_entry_acl(a->entry),
1915
+ archive_entry_mode(a->entry));
1916
+ if (r2 < ret) ret = r2;
1917
+ }
1918
+
1919
+ finish_metadata:
1920
+ /* If there's an fd, we can close it now. */
1921
+ if (a->fd >= 0) {
1922
+ close(a->fd);
1923
+ a->fd = -1;
1924
+ if (a->tmpname) {
1925
+ if (rename(a->tmpname, a->name) == -1) {
1926
+ archive_set_error(&a->archive, errno,
1927
+ "Failed to rename temporary file");
1928
+ ret = ARCHIVE_FAILED;
1929
+ unlink(a->tmpname);
1930
+ }
1931
+ a->tmpname = NULL;
1932
+ }
1933
+ }
1934
+ /* If there's an entry, we can release it now. */
1935
+ archive_entry_free(a->entry);
1936
+ a->entry = NULL;
1937
+ a->archive.state = ARCHIVE_STATE_HEADER;
1938
+ return (ret);
1939
+ }
1940
+
1941
+ int
1942
+ archive_write_disk_set_group_lookup(struct archive *_a,
1943
+ void *private_data,
1944
+ la_int64_t (*lookup_gid)(void *private, const char *gname, la_int64_t gid),
1945
+ void (*cleanup_gid)(void *private))
1946
+ {
1947
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1948
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1949
+ ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
1950
+
1951
+ if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1952
+ (a->cleanup_gid)(a->lookup_gid_data);
1953
+
1954
+ a->lookup_gid = lookup_gid;
1955
+ a->cleanup_gid = cleanup_gid;
1956
+ a->lookup_gid_data = private_data;
1957
+ return (ARCHIVE_OK);
1958
+ }
1959
+
1960
+ int
1961
+ archive_write_disk_set_user_lookup(struct archive *_a,
1962
+ void *private_data,
1963
+ int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
1964
+ void (*cleanup_uid)(void *private))
1965
+ {
1966
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1967
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1968
+ ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
1969
+
1970
+ if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1971
+ (a->cleanup_uid)(a->lookup_uid_data);
1972
+
1973
+ a->lookup_uid = lookup_uid;
1974
+ a->cleanup_uid = cleanup_uid;
1975
+ a->lookup_uid_data = private_data;
1976
+ return (ARCHIVE_OK);
1977
+ }
1978
+
1979
+ int64_t
1980
+ archive_write_disk_gid(struct archive *_a, const char *name, la_int64_t id)
1981
+ {
1982
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1983
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1984
+ ARCHIVE_STATE_ANY, "archive_write_disk_gid");
1985
+ if (a->lookup_gid)
1986
+ return (a->lookup_gid)(a->lookup_gid_data, name, id);
1987
+ return (id);
1988
+ }
1989
+
1990
+ int64_t
1991
+ archive_write_disk_uid(struct archive *_a, const char *name, la_int64_t id)
1992
+ {
1993
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
1994
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1995
+ ARCHIVE_STATE_ANY, "archive_write_disk_uid");
1996
+ if (a->lookup_uid)
1997
+ return (a->lookup_uid)(a->lookup_uid_data, name, id);
1998
+ return (id);
1999
+ }
2000
+
2001
+ /*
2002
+ * Create a new archive_write_disk object and initialize it with global state.
2003
+ */
2004
+ struct archive *
2005
+ archive_write_disk_new(void)
2006
+ {
2007
+ struct archive_write_disk *a;
2008
+
2009
+ a = calloc(1, sizeof(*a));
2010
+ if (a == NULL)
2011
+ return (NULL);
2012
+ a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
2013
+ /* We're ready to write a header immediately. */
2014
+ a->archive.state = ARCHIVE_STATE_HEADER;
2015
+ a->archive.vtable = &archive_write_disk_vtable;
2016
+ a->start_time = time(NULL);
2017
+ /* Query and restore the umask. */
2018
+ umask(a->user_umask = umask(0));
2019
+ #ifdef HAVE_GETEUID
2020
+ a->user_uid = geteuid();
2021
+ #endif /* HAVE_GETEUID */
2022
+ if (archive_string_ensure(&a->path_safe, 512) == NULL) {
2023
+ free(a);
2024
+ return (NULL);
2025
+ }
2026
+ a->path_safe.s[0] = 0;
2027
+
2028
+ #ifdef HAVE_ZLIB_H
2029
+ a->decmpfs_compression_level = 5;
2030
+ #endif
2031
+ return (&a->archive);
2032
+ }
2033
+
2034
+
2035
+ /*
2036
+ * If pathname is longer than PATH_MAX, chdir to a suitable
2037
+ * intermediate dir and edit the path down to a shorter suffix. Note
2038
+ * that this routine never returns an error; if the chdir() attempt
2039
+ * fails for any reason, we just go ahead with the long pathname. The
2040
+ * object creation is likely to fail, but any error will get handled
2041
+ * at that time.
2042
+ */
2043
+ #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
2044
+ static void
2045
+ edit_deep_directories(struct archive_write_disk *a)
2046
+ {
2047
+ int ret;
2048
+ char *tail = a->name;
2049
+
2050
+ /* If path is short, avoid the open() below. */
2051
+ if (strlen(tail) < PATH_MAX)
2052
+ return;
2053
+
2054
+ /* Try to record our starting dir. */
2055
+ a->restore_pwd = la_opendirat(AT_FDCWD, ".");
2056
+ __archive_ensure_cloexec_flag(a->restore_pwd);
2057
+ if (a->restore_pwd < 0)
2058
+ return;
2059
+
2060
+ /* As long as the path is too long... */
2061
+ while (strlen(tail) >= PATH_MAX) {
2062
+ /* Locate a dir prefix shorter than PATH_MAX. */
2063
+ tail += PATH_MAX - 8;
2064
+ while (tail > a->name && *tail != '/')
2065
+ tail--;
2066
+ /* Exit if we find a too-long path component. */
2067
+ if (tail <= a->name)
2068
+ return;
2069
+ /* Create the intermediate dir and chdir to it. */
2070
+ *tail = '\0'; /* Terminate dir portion */
2071
+ ret = create_dir(a, a->name);
2072
+ if (ret == ARCHIVE_OK && chdir(a->name) != 0)
2073
+ ret = ARCHIVE_FAILED;
2074
+ *tail = '/'; /* Restore the / we removed. */
2075
+ if (ret != ARCHIVE_OK)
2076
+ return;
2077
+ tail++;
2078
+ /* The chdir() succeeded; we've now shortened the path. */
2079
+ a->name = tail;
2080
+ }
2081
+ return;
2082
+ }
2083
+ #endif
2084
+
2085
+ /*
2086
+ * The main restore function.
2087
+ */
2088
+ static int
2089
+ restore_entry(struct archive_write_disk *a)
2090
+ {
2091
+ int ret = ARCHIVE_OK, en;
2092
+
2093
+ if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
2094
+ /*
2095
+ * TODO: Fix this. Apparently, there are platforms
2096
+ * that still allow root to hose the entire filesystem
2097
+ * by unlinking a dir. The S_ISDIR() test above
2098
+ * prevents us from using unlink() here if the new
2099
+ * object is a dir, but that doesn't mean the old
2100
+ * object isn't a dir.
2101
+ */
2102
+ if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
2103
+ (void)clear_nochange_fflags(a);
2104
+ if (unlink(a->name) == 0) {
2105
+ /* We removed it, reset cached stat. */
2106
+ a->pst = NULL;
2107
+ } else if (errno == ENOENT) {
2108
+ /* File didn't exist, that's just as good. */
2109
+ } else if (rmdir(a->name) == 0) {
2110
+ /* It was a dir, but now it's gone. */
2111
+ a->pst = NULL;
2112
+ } else {
2113
+ /* We tried, but couldn't get rid of it. */
2114
+ archive_set_error(&a->archive, errno,
2115
+ "Could not unlink");
2116
+ return(ARCHIVE_FAILED);
2117
+ }
2118
+ }
2119
+
2120
+ /* Try creating it first; if this fails, we'll try to recover. */
2121
+ en = create_filesystem_object(a);
2122
+
2123
+ if ((en == ENOTDIR || en == ENOENT)
2124
+ && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
2125
+ /* If the parent dir doesn't exist, try creating it. */
2126
+ create_parent_dir(a, a->name);
2127
+ /* Now try to create the object again. */
2128
+ en = create_filesystem_object(a);
2129
+ }
2130
+
2131
+ if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) {
2132
+ archive_set_error(&a->archive, en,
2133
+ "Hard-link target '%s' does not exist",
2134
+ archive_entry_hardlink(a->entry));
2135
+ return (ARCHIVE_FAILED);
2136
+ }
2137
+
2138
+ if ((en == EISDIR || en == EEXIST)
2139
+ && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
2140
+ /* If we're not overwriting, we're done. */
2141
+ if (S_ISDIR(a->mode)) {
2142
+ /* Don't overwrite any settings on existing directories. */
2143
+ a->todo = 0;
2144
+ }
2145
+ archive_entry_unset_size(a->entry);
2146
+ return (ARCHIVE_OK);
2147
+ }
2148
+
2149
+ /*
2150
+ * Some platforms return EISDIR if you call
2151
+ * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
2152
+ * return EEXIST. POSIX is ambiguous, requiring EISDIR
2153
+ * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
2154
+ * on an existing item.
2155
+ */
2156
+ if (en == EISDIR) {
2157
+ /* A dir is in the way of a non-dir, rmdir it. */
2158
+ if (rmdir(a->name) != 0) {
2159
+ archive_set_error(&a->archive, errno,
2160
+ "Can't remove already-existing dir");
2161
+ return (ARCHIVE_FAILED);
2162
+ }
2163
+ a->pst = NULL;
2164
+ /* Try again. */
2165
+ en = create_filesystem_object(a);
2166
+ } else if (en == EEXIST) {
2167
+ /*
2168
+ * We know something is in the way, but we don't know what;
2169
+ * we need to find out before we go any further.
2170
+ */
2171
+ int r = 0;
2172
+ /*
2173
+ * The SECURE_SYMLINKS logic has already removed a
2174
+ * symlink to a dir if the client wants that. So
2175
+ * follow the symlink if we're creating a dir.
2176
+ */
2177
+ if (S_ISDIR(a->mode))
2178
+ r = la_stat(a->name, &a->st);
2179
+ /*
2180
+ * If it's not a dir (or it's a broken symlink),
2181
+ * then don't follow it.
2182
+ */
2183
+ if (r != 0 || !S_ISDIR(a->mode))
2184
+ #ifdef HAVE_LSTAT
2185
+ r = lstat(a->name, &a->st);
2186
+ #else
2187
+ r = la_stat(a->name, &a->st);
2188
+ #endif
2189
+ if (r != 0) {
2190
+ archive_set_error(&a->archive, errno,
2191
+ "Can't stat existing object");
2192
+ return (ARCHIVE_FAILED);
2193
+ }
2194
+
2195
+ /*
2196
+ * NO_OVERWRITE_NEWER doesn't apply to directories.
2197
+ */
2198
+ if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
2199
+ && !S_ISDIR(a->st.st_mode)) {
2200
+ if (!older(&(a->st), a->entry)) {
2201
+ archive_entry_unset_size(a->entry);
2202
+ return (ARCHIVE_OK);
2203
+ }
2204
+ }
2205
+
2206
+ /* If it's our archive, we're done. */
2207
+ if (a->skip_file_set &&
2208
+ a->st.st_dev == (dev_t)a->skip_file_dev &&
2209
+ a->st.st_ino == (ino_t)a->skip_file_ino) {
2210
+ archive_set_error(&a->archive, EIO,
2211
+ "Refusing to overwrite archive");
2212
+ return (ARCHIVE_FAILED);
2213
+ }
2214
+
2215
+ if (!S_ISDIR(a->st.st_mode)) {
2216
+ if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
2217
+ (void)clear_nochange_fflags(a);
2218
+
2219
+ if ((a->flags & ARCHIVE_EXTRACT_SAFE_WRITES) &&
2220
+ S_ISREG(a->mode)) {
2221
+ /* Use a temporary file to extract */
2222
+ if ((a->fd = la_mktemp(a)) == -1) {
2223
+ archive_set_error(&a->archive, errno,
2224
+ "Can't create temporary file");
2225
+ return ARCHIVE_FAILED;
2226
+ }
2227
+ a->pst = NULL;
2228
+ en = 0;
2229
+ } else {
2230
+ /* A non-dir is in the way, unlink it. */
2231
+ if (unlink(a->name) != 0) {
2232
+ archive_set_error(&a->archive, errno,
2233
+ "Can't unlink already-existing "
2234
+ "object");
2235
+ return (ARCHIVE_FAILED);
2236
+ }
2237
+ a->pst = NULL;
2238
+ /* Try again. */
2239
+ en = create_filesystem_object(a);
2240
+ }
2241
+ } else if (!S_ISDIR(a->mode)) {
2242
+ /* A dir is in the way of a non-dir, rmdir it. */
2243
+ if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
2244
+ (void)clear_nochange_fflags(a);
2245
+ if (rmdir(a->name) != 0) {
2246
+ archive_set_error(&a->archive, errno,
2247
+ "Can't replace existing directory with non-directory");
2248
+ return (ARCHIVE_FAILED);
2249
+ }
2250
+ /* Try again. */
2251
+ en = create_filesystem_object(a);
2252
+ } else {
2253
+ /*
2254
+ * There's a dir in the way of a dir. Don't
2255
+ * waste time with rmdir()/mkdir(), just fix
2256
+ * up the permissions on the existing dir.
2257
+ * Note that we don't change perms on existing
2258
+ * dirs unless _EXTRACT_PERM is specified.
2259
+ */
2260
+ if ((a->mode != a->st.st_mode)
2261
+ && (a->todo & TODO_MODE_FORCE))
2262
+ a->deferred |= (a->todo & TODO_MODE);
2263
+ /* Ownership doesn't need deferred fixup. */
2264
+ en = 0; /* Forget the EEXIST. */
2265
+ }
2266
+ }
2267
+
2268
+ if (en) {
2269
+ /* Everything failed; give up here. */
2270
+ if ((&a->archive)->error == NULL)
2271
+ archive_set_error(&a->archive, en, "Can't create '%s'",
2272
+ a->name);
2273
+ return (ARCHIVE_FAILED);
2274
+ }
2275
+
2276
+ a->pst = NULL; /* Cached stat data no longer valid. */
2277
+ return (ret);
2278
+ }
2279
+
2280
+ /*
2281
+ * Returns 0 if creation succeeds, or else returns errno value from
2282
+ * the failed system call. Note: This function should only ever perform
2283
+ * a single system call.
2284
+ */
2285
+ static int
2286
+ create_filesystem_object(struct archive_write_disk *a)
2287
+ {
2288
+ /* Create the entry. */
2289
+ const char *linkname;
2290
+ mode_t final_mode, mode;
2291
+ int r;
2292
+ /* these for check_symlinks_fsobj */
2293
+ char *linkname_copy; /* non-const copy of linkname */
2294
+ struct stat st;
2295
+ struct archive_string error_string;
2296
+ int error_number;
2297
+
2298
+ /* We identify hard/symlinks according to the link names. */
2299
+ /* Since link(2) and symlink(2) don't handle modes, we're done here. */
2300
+ linkname = archive_entry_hardlink(a->entry);
2301
+ if (linkname != NULL) {
2302
+ #if !HAVE_LINK
2303
+ return (EPERM);
2304
+ #else
2305
+ archive_string_init(&error_string);
2306
+ linkname_copy = strdup(linkname);
2307
+ if (linkname_copy == NULL) {
2308
+ return (EPERM);
2309
+ }
2310
+ /*
2311
+ * TODO: consider using the cleaned-up path as the link
2312
+ * target?
2313
+ */
2314
+ r = cleanup_pathname_fsobj(linkname_copy, &error_number,
2315
+ &error_string, a->flags);
2316
+ if (r != ARCHIVE_OK) {
2317
+ archive_set_error(&a->archive, error_number, "%s",
2318
+ error_string.s);
2319
+ free(linkname_copy);
2320
+ archive_string_free(&error_string);
2321
+ /*
2322
+ * EPERM is more appropriate than error_number for our
2323
+ * callers
2324
+ */
2325
+ return (EPERM);
2326
+ }
2327
+ r = check_symlinks_fsobj(linkname_copy, &error_number,
2328
+ &error_string, a->flags, 1);
2329
+ if (r != ARCHIVE_OK) {
2330
+ archive_set_error(&a->archive, error_number, "%s",
2331
+ error_string.s);
2332
+ free(linkname_copy);
2333
+ archive_string_free(&error_string);
2334
+ /*
2335
+ * EPERM is more appropriate than error_number for our
2336
+ * callers
2337
+ */
2338
+ return (EPERM);
2339
+ }
2340
+ free(linkname_copy);
2341
+ archive_string_free(&error_string);
2342
+ /*
2343
+ * Unlinking and linking here is really not atomic,
2344
+ * but doing it right, would require us to construct
2345
+ * an mktemplink() function, and then use rename(2).
2346
+ */
2347
+ if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES)
2348
+ unlink(a->name);
2349
+ #ifdef HAVE_LINKAT
2350
+ r = linkat(AT_FDCWD, linkname, AT_FDCWD, a->name,
2351
+ 0) ? errno : 0;
2352
+ #else
2353
+ r = link(linkname, a->name) ? errno : 0;
2354
+ #endif
2355
+ /*
2356
+ * New cpio and pax formats allow hardlink entries
2357
+ * to carry data, so we may have to open the file
2358
+ * for hardlink entries.
2359
+ *
2360
+ * If the hardlink was successfully created and
2361
+ * the archive doesn't have carry data for it,
2362
+ * consider it to be non-authoritative for meta data.
2363
+ * This is consistent with GNU tar and BSD pax.
2364
+ * If the hardlink does carry data, let the last
2365
+ * archive entry decide ownership.
2366
+ */
2367
+ if (r == 0 && a->filesize <= 0) {
2368
+ a->todo = 0;
2369
+ a->deferred = 0;
2370
+ } else if (r == 0 && a->filesize > 0) {
2371
+ #ifdef HAVE_LSTAT
2372
+ r = lstat(a->name, &st);
2373
+ #else
2374
+ r = la_stat(a->name, &st);
2375
+ #endif
2376
+ if (r != 0)
2377
+ r = errno;
2378
+ else if (S_ISREG(st.st_mode)) {
2379
+ a->fd = open(a->name, O_WRONLY | O_TRUNC |
2380
+ O_BINARY | O_CLOEXEC | O_NOFOLLOW);
2381
+ __archive_ensure_cloexec_flag(a->fd);
2382
+ if (a->fd < 0)
2383
+ r = errno;
2384
+ }
2385
+ }
2386
+ return (r);
2387
+ #endif
2388
+ }
2389
+ linkname = archive_entry_symlink(a->entry);
2390
+ if (linkname != NULL) {
2391
+ #if HAVE_SYMLINK
2392
+ /*
2393
+ * Unlinking and linking here is really not atomic,
2394
+ * but doing it right, would require us to construct
2395
+ * an mktempsymlink() function, and then use rename(2).
2396
+ */
2397
+ if (a->flags & ARCHIVE_EXTRACT_SAFE_WRITES)
2398
+ unlink(a->name);
2399
+ return symlink(linkname, a->name) ? errno : 0;
2400
+ #else
2401
+ return (EPERM);
2402
+ #endif
2403
+ }
2404
+
2405
+ /*
2406
+ * The remaining system calls all set permissions, so let's
2407
+ * try to take advantage of that to avoid an extra chmod()
2408
+ * call. (Recall that umask is set to zero right now!)
2409
+ */
2410
+
2411
+ /* Mode we want for the final restored object (w/o file type bits). */
2412
+ final_mode = a->mode & 07777;
2413
+ /*
2414
+ * The mode that will actually be restored in this step. Note
2415
+ * that SUID, SGID, etc, require additional work to ensure
2416
+ * security, so we never restore them at this point.
2417
+ */
2418
+ mode = final_mode & 0777 & ~a->user_umask;
2419
+
2420
+ /*
2421
+ * Always create writable such that [f]setxattr() works if we're not
2422
+ * root.
2423
+ */
2424
+ if (a->user_uid != 0 &&
2425
+ a->todo & (TODO_HFS_COMPRESSION | TODO_XATTR)) {
2426
+ mode |= 0200;
2427
+ }
2428
+
2429
+ switch (a->mode & AE_IFMT) {
2430
+ default:
2431
+ /* POSIX requires that we fall through here. */
2432
+ /* FALLTHROUGH */
2433
+ case AE_IFREG:
2434
+ a->tmpname = NULL;
2435
+ a->fd = open(a->name,
2436
+ O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, mode);
2437
+ __archive_ensure_cloexec_flag(a->fd);
2438
+ r = (a->fd < 0);
2439
+ break;
2440
+ case AE_IFCHR:
2441
+ #ifdef HAVE_MKNOD
2442
+ /* Note: we use AE_IFCHR for the case label, and
2443
+ * S_IFCHR for the mknod() call. This is correct. */
2444
+ r = mknod(a->name, mode | S_IFCHR,
2445
+ archive_entry_rdev(a->entry));
2446
+ break;
2447
+ #else
2448
+ /* TODO: Find a better way to warn about our inability
2449
+ * to restore a char device node. */
2450
+ return (EINVAL);
2451
+ #endif /* HAVE_MKNOD */
2452
+ case AE_IFBLK:
2453
+ #ifdef HAVE_MKNOD
2454
+ r = mknod(a->name, mode | S_IFBLK,
2455
+ archive_entry_rdev(a->entry));
2456
+ break;
2457
+ #else
2458
+ /* TODO: Find a better way to warn about our inability
2459
+ * to restore a block device node. */
2460
+ return (EINVAL);
2461
+ #endif /* HAVE_MKNOD */
2462
+ case AE_IFDIR:
2463
+ mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
2464
+ r = mkdir(a->name, mode);
2465
+ if (r == 0) {
2466
+ /* Defer setting dir times. */
2467
+ a->deferred |= (a->todo & TODO_TIMES);
2468
+ a->todo &= ~TODO_TIMES;
2469
+ /* Never use an immediate chmod(). */
2470
+ /* We can't avoid the chmod() entirely if EXTRACT_PERM
2471
+ * because of SysV SGID inheritance. */
2472
+ if ((mode != final_mode)
2473
+ || (a->flags & ARCHIVE_EXTRACT_PERM))
2474
+ a->deferred |= (a->todo & TODO_MODE);
2475
+ a->todo &= ~TODO_MODE;
2476
+ }
2477
+ break;
2478
+ case AE_IFIFO:
2479
+ #ifdef HAVE_MKFIFO
2480
+ r = mkfifo(a->name, mode);
2481
+ break;
2482
+ #else
2483
+ /* TODO: Find a better way to warn about our inability
2484
+ * to restore a fifo. */
2485
+ return (EINVAL);
2486
+ #endif /* HAVE_MKFIFO */
2487
+ }
2488
+
2489
+ /* All the system calls above set errno on failure. */
2490
+ if (r)
2491
+ return (errno);
2492
+
2493
+ /* If we managed to set the final mode, we've avoided a chmod(). */
2494
+ if (mode == final_mode)
2495
+ a->todo &= ~TODO_MODE;
2496
+ return (0);
2497
+ }
2498
+
2499
+ /*
2500
+ * Cleanup function for archive_extract. Mostly, this involves processing
2501
+ * the fixup list, which is used to address a number of problems:
2502
+ * * Dir permissions might prevent us from restoring a file in that
2503
+ * dir, so we restore the dir with minimum 0700 permissions first,
2504
+ * then correct the mode at the end.
2505
+ * * Similarly, the act of restoring a file touches the directory
2506
+ * and changes the timestamp on the dir, so we have to touch-up dir
2507
+ * timestamps at the end as well.
2508
+ * * Some file flags can interfere with the restore by, for example,
2509
+ * preventing the creation of hardlinks to those files.
2510
+ * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
2511
+ *
2512
+ * Note that tar/cpio do not require that archives be in a particular
2513
+ * order; there is no way to know when the last file has been restored
2514
+ * within a directory, so there's no way to optimize the memory usage
2515
+ * here by fixing up the directory any earlier than the
2516
+ * end-of-archive.
2517
+ *
2518
+ * XXX TODO: Directory ACLs should be restored here, for the same
2519
+ * reason we set directory perms here. XXX
2520
+ */
2521
+ static int
2522
+ _archive_write_disk_close(struct archive *_a)
2523
+ {
2524
+ struct archive_write_disk *a = (struct archive_write_disk *)_a;
2525
+ struct fixup_entry *next, *p;
2526
+ struct stat st;
2527
+ char *c;
2528
+ int fd, ret, openflags;
2529
+
2530
+ archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
2531
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
2532
+ "archive_write_disk_close");
2533
+ ret = _archive_write_disk_finish_entry(&a->archive);
2534
+
2535
+ /* Sort dir list so directories are fixed up in depth-first order. */
2536
+ p = sort_dir_list(a->fixup_list);
2537
+
2538
+ while (p != NULL) {
2539
+ fd = -1;
2540
+ a->pst = NULL; /* Mark stat cache as out-of-date. */
2541
+
2542
+ /* We must strip trailing slashes from the path to avoid
2543
+ dereferencing symbolic links to directories */
2544
+ c = p->name;
2545
+ while (*c != '\0')
2546
+ c++;
2547
+ while (c != p->name && *(c - 1) == '/') {
2548
+ c--;
2549
+ *c = '\0';
2550
+ }
2551
+
2552
+ if (p->fixup == 0)
2553
+ goto skip_fixup_entry;
2554
+ else {
2555
+ /*
2556
+ * We need to verify if the type of the file
2557
+ * we are going to open matches the file type
2558
+ * of the fixup entry.
2559
+ */
2560
+ openflags = O_BINARY | O_NOFOLLOW | O_RDONLY
2561
+ | O_CLOEXEC;
2562
+ #if defined(O_DIRECTORY)
2563
+ if (p->filetype == AE_IFDIR)
2564
+ openflags |= O_DIRECTORY;
2565
+ #endif
2566
+ fd = open(p->name, openflags);
2567
+
2568
+ #if defined(O_DIRECTORY)
2569
+ /*
2570
+ * If we support O_DIRECTORY and open was
2571
+ * successful we can skip the file type check
2572
+ * for directories. For other file types
2573
+ * we need to verify via fstat() or lstat()
2574
+ */
2575
+ if (fd == -1 || p->filetype != AE_IFDIR) {
2576
+ #if HAVE_FSTAT
2577
+ if (fd > 0 && (
2578
+ fstat(fd, &st) != 0 ||
2579
+ la_verify_filetype(st.st_mode,
2580
+ p->filetype) == 0)) {
2581
+ goto skip_fixup_entry;
2582
+ } else
2583
+ #endif
2584
+ if (
2585
+ #ifdef HAVE_LSTAT
2586
+ lstat(p->name, &st) != 0 ||
2587
+ #else
2588
+ la_stat(p->name, &st) != 0 ||
2589
+ #endif
2590
+ la_verify_filetype(st.st_mode,
2591
+ p->filetype) == 0) {
2592
+ goto skip_fixup_entry;
2593
+ }
2594
+ }
2595
+ #else
2596
+ #if HAVE_FSTAT
2597
+ if (fd > 0 && (
2598
+ fstat(fd, &st) != 0 ||
2599
+ la_verify_filetype(st.st_mode,
2600
+ p->filetype) == 0)) {
2601
+ goto skip_fixup_entry;
2602
+ } else
2603
+ #endif
2604
+ if (
2605
+ #ifdef HAVE_LSTAT
2606
+ lstat(p->name, &st) != 0 ||
2607
+ #else
2608
+ la_stat(p->name, &st) != 0 ||
2609
+ #endif
2610
+ la_verify_filetype(st.st_mode,
2611
+ p->filetype) == 0) {
2612
+ goto skip_fixup_entry;
2613
+ }
2614
+ #endif
2615
+ }
2616
+ if (p->fixup & TODO_TIMES) {
2617
+ set_times(a, fd, p->mode, p->name,
2618
+ p->atime, p->atime_nanos,
2619
+ p->birthtime, p->birthtime_nanos,
2620
+ p->mtime, p->mtime_nanos,
2621
+ p->ctime, p->ctime_nanos);
2622
+ }
2623
+ if (p->fixup & TODO_MODE_BASE) {
2624
+ #ifdef HAVE_FCHMOD
2625
+ if (fd >= 0)
2626
+ fchmod(fd, p->mode & 07777);
2627
+ else
2628
+ #endif
2629
+ #ifdef HAVE_LCHMOD
2630
+ lchmod(p->name, p->mode & 07777);
2631
+ #else
2632
+ chmod(p->name, p->mode & 07777);
2633
+ #endif
2634
+ }
2635
+ if (p->fixup & TODO_ACLS)
2636
+ archive_write_disk_set_acls(&a->archive, fd,
2637
+ p->name, &p->acl, p->mode);
2638
+ if (p->fixup & TODO_FFLAGS)
2639
+ set_fflags_platform(a, fd, p->name,
2640
+ p->mode, p->fflags_set, 0);
2641
+ if (p->fixup & TODO_MAC_METADATA)
2642
+ set_mac_metadata(a, p->name, p->mac_metadata,
2643
+ p->mac_metadata_size);
2644
+ skip_fixup_entry:
2645
+ next = p->next;
2646
+ archive_acl_clear(&p->acl);
2647
+ free(p->mac_metadata);
2648
+ free(p->name);
2649
+ if (fd >= 0)
2650
+ close(fd);
2651
+ free(p);
2652
+ p = next;
2653
+ }
2654
+ a->fixup_list = NULL;
2655
+ return (ret);
2656
+ }
2657
+
2658
+ static int
2659
+ _archive_write_disk_free(struct archive *_a)
2660
+ {
2661
+ struct archive_write_disk *a;
2662
+ int ret;
2663
+ if (_a == NULL)
2664
+ return (ARCHIVE_OK);
2665
+ archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
2666
+ ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
2667
+ a = (struct archive_write_disk *)_a;
2668
+ ret = _archive_write_disk_close(&a->archive);
2669
+ archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
2670
+ archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
2671
+ archive_entry_free(a->entry);
2672
+ archive_string_free(&a->_name_data);
2673
+ archive_string_free(&a->_tmpname_data);
2674
+ archive_string_free(&a->archive.error_string);
2675
+ archive_string_free(&a->path_safe);
2676
+ a->archive.magic = 0;
2677
+ __archive_clean(&a->archive);
2678
+ free(a->decmpfs_header_p);
2679
+ free(a->resource_fork);
2680
+ free(a->compressed_buffer);
2681
+ free(a->uncompressed_buffer);
2682
+ #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
2683
+ && defined(HAVE_ZLIB_H)
2684
+ if (a->stream_valid) {
2685
+ switch (deflateEnd(&a->stream)) {
2686
+ case Z_OK:
2687
+ break;
2688
+ default:
2689
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2690
+ "Failed to clean up compressor");
2691
+ ret = ARCHIVE_FATAL;
2692
+ break;
2693
+ }
2694
+ }
2695
+ #endif
2696
+ free(a);
2697
+ return (ret);
2698
+ }
2699
+
2700
+ /*
2701
+ * Simple O(n log n) merge sort to order the fixup list. In
2702
+ * particular, we want to restore dir timestamps depth-first.
2703
+ */
2704
+ static struct fixup_entry *
2705
+ sort_dir_list(struct fixup_entry *p)
2706
+ {
2707
+ struct fixup_entry *a, *b, *t;
2708
+
2709
+ if (p == NULL)
2710
+ return (NULL);
2711
+ /* A one-item list is already sorted. */
2712
+ if (p->next == NULL)
2713
+ return (p);
2714
+
2715
+ /* Step 1: split the list. */
2716
+ t = p;
2717
+ a = p->next->next;
2718
+ while (a != NULL) {
2719
+ /* Step a twice, t once. */
2720
+ a = a->next;
2721
+ if (a != NULL)
2722
+ a = a->next;
2723
+ t = t->next;
2724
+ }
2725
+ /* Now, t is at the mid-point, so break the list here. */
2726
+ b = t->next;
2727
+ t->next = NULL;
2728
+ a = p;
2729
+
2730
+ /* Step 2: Recursively sort the two sub-lists. */
2731
+ a = sort_dir_list(a);
2732
+ b = sort_dir_list(b);
2733
+
2734
+ /* Step 3: Merge the returned lists. */
2735
+ /* Pick the first element for the merged list. */
2736
+ if (strcmp(a->name, b->name) > 0) {
2737
+ t = p = a;
2738
+ a = a->next;
2739
+ } else {
2740
+ t = p = b;
2741
+ b = b->next;
2742
+ }
2743
+
2744
+ /* Always put the later element on the list first. */
2745
+ while (a != NULL && b != NULL) {
2746
+ if (strcmp(a->name, b->name) > 0) {
2747
+ t->next = a;
2748
+ a = a->next;
2749
+ } else {
2750
+ t->next = b;
2751
+ b = b->next;
2752
+ }
2753
+ t = t->next;
2754
+ }
2755
+
2756
+ /* Only one list is non-empty, so just splice it on. */
2757
+ if (a != NULL)
2758
+ t->next = a;
2759
+ if (b != NULL)
2760
+ t->next = b;
2761
+
2762
+ return (p);
2763
+ }
2764
+
2765
+ /*
2766
+ * Returns a new, initialized fixup entry.
2767
+ *
2768
+ * TODO: Reduce the memory requirements for this list by using a tree
2769
+ * structure rather than a simple list of names.
2770
+ */
2771
+ static struct fixup_entry *
2772
+ new_fixup(struct archive_write_disk *a, const char *pathname)
2773
+ {
2774
+ struct fixup_entry *fe;
2775
+
2776
+ fe = calloc(1, sizeof(struct fixup_entry));
2777
+ if (fe == NULL) {
2778
+ archive_set_error(&a->archive, ENOMEM,
2779
+ "Can't allocate memory for a fixup");
2780
+ return (NULL);
2781
+ }
2782
+ fe->next = a->fixup_list;
2783
+ a->fixup_list = fe;
2784
+ fe->fixup = 0;
2785
+ fe->filetype = 0;
2786
+ fe->name = strdup(pathname);
2787
+ return (fe);
2788
+ }
2789
+
2790
+ /*
2791
+ * Returns a fixup structure for the current entry.
2792
+ */
2793
+ static struct fixup_entry *
2794
+ current_fixup(struct archive_write_disk *a, const char *pathname)
2795
+ {
2796
+ if (a->current_fixup == NULL)
2797
+ a->current_fixup = new_fixup(a, pathname);
2798
+ return (a->current_fixup);
2799
+ }
2800
+
2801
+ /* Error helper for new *_fsobj functions */
2802
+ static void
2803
+ fsobj_error(int *a_eno, struct archive_string *a_estr,
2804
+ int err, const char *errstr, const char *path)
2805
+ {
2806
+ if (a_eno)
2807
+ *a_eno = err;
2808
+ if (a_estr)
2809
+ archive_string_sprintf(a_estr, "%s%s", errstr, path);
2810
+ }
2811
+
2812
+ /*
2813
+ * TODO: Someday, integrate this with the deep dir support; they both
2814
+ * scan the path and both can be optimized by comparing against other
2815
+ * recent paths.
2816
+ */
2817
+ /*
2818
+ * Checks the given path to see if any elements along it are symlinks. Returns
2819
+ * ARCHIVE_OK if there are none, otherwise puts an error in errmsg.
2820
+ */
2821
+ static int
2822
+ check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
2823
+ int flags, int checking_linkname)
2824
+ {
2825
+ #if !defined(HAVE_LSTAT) && \
2826
+ !(defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT))
2827
+ /* Platform doesn't have lstat, so we can't look for symlinks. */
2828
+ (void)path; /* UNUSED */
2829
+ (void)a_eno; /* UNUSED */
2830
+ (void)a_estr; /* UNUSED */
2831
+ (void)flags; /* UNUSED */
2832
+ (void)checking_linkname; /* UNUSED */
2833
+ return (ARCHIVE_OK);
2834
+ #else
2835
+ int res = ARCHIVE_OK;
2836
+ char *tail;
2837
+ char *head;
2838
+ int last;
2839
+ char c = '\0';
2840
+ int r;
2841
+ struct stat st;
2842
+ int chdir_fd;
2843
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
2844
+ int fd;
2845
+ #endif
2846
+
2847
+ /* Nothing to do here if name is empty */
2848
+ if(path[0] == '\0')
2849
+ return (ARCHIVE_OK);
2850
+
2851
+ /*
2852
+ * Guard against symlink tricks. Reject any archive entry whose
2853
+ * destination would be altered by a symlink.
2854
+ *
2855
+ * Walk the filename in chunks separated by '/'. For each segment:
2856
+ * - if it doesn't exist, continue
2857
+ * - if it's symlink, abort or remove it
2858
+ * - if it's a directory and it's not the last chunk, cd into it
2859
+ * As we go:
2860
+ * head points to the current (relative) path
2861
+ * tail points to the temporary \0 terminating the segment we're
2862
+ * currently examining
2863
+ * c holds what used to be in *tail
2864
+ * last is 1 if this is the last tail
2865
+ */
2866
+ chdir_fd = la_opendirat(AT_FDCWD, ".");
2867
+ __archive_ensure_cloexec_flag(chdir_fd);
2868
+ if (chdir_fd < 0) {
2869
+ fsobj_error(a_eno, a_estr, errno,
2870
+ "Could not open ", path);
2871
+ return (ARCHIVE_FATAL);
2872
+ }
2873
+ head = path;
2874
+ tail = path;
2875
+ last = 0;
2876
+ /* TODO: reintroduce a safe cache here? */
2877
+ /* Skip the root directory if the path is absolute. */
2878
+ if(tail == path && tail[0] == '/')
2879
+ ++tail;
2880
+ /* Keep going until we've checked the entire name.
2881
+ * head, tail, path all alias the same string, which is
2882
+ * temporarily zeroed at tail, so be careful restoring the
2883
+ * stashed (c=tail[0]) for error messages.
2884
+ * Exiting the loop with break is okay; continue is not.
2885
+ */
2886
+ while (!last) {
2887
+ /*
2888
+ * Skip the separator we just consumed, plus any adjacent ones
2889
+ */
2890
+ while (*tail == '/')
2891
+ ++tail;
2892
+ /* Skip the next path element. */
2893
+ while (*tail != '\0' && *tail != '/')
2894
+ ++tail;
2895
+ /* is this the last path component? */
2896
+ last = (tail[0] == '\0') || (tail[0] == '/' && tail[1] == '\0');
2897
+ /* temporarily truncate the string here */
2898
+ c = tail[0];
2899
+ tail[0] = '\0';
2900
+ /* Check that we haven't hit a symlink. */
2901
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
2902
+ r = fstatat(chdir_fd, head, &st, AT_SYMLINK_NOFOLLOW);
2903
+ #elif defined(HAVE_LSTAT)
2904
+ r = lstat(head, &st);
2905
+ #else
2906
+ r = la_stat(head, &st);
2907
+ #endif
2908
+ if (r != 0) {
2909
+ tail[0] = c;
2910
+ /* We've hit a dir that doesn't exist; stop now. */
2911
+ if (errno == ENOENT) {
2912
+ break;
2913
+ } else {
2914
+ /*
2915
+ * Treat any other error as fatal - best to be
2916
+ * paranoid here.
2917
+ * Note: This effectively disables deep
2918
+ * directory support when security checks are
2919
+ * enabled. Otherwise, very long pathnames that
2920
+ * trigger an error here could evade the
2921
+ * sandbox.
2922
+ * TODO: We could do better, but it would
2923
+ * probably require merging the symlink checks
2924
+ * with the deep-directory editing.
2925
+ */
2926
+ fsobj_error(a_eno, a_estr, errno,
2927
+ "Could not stat ", path);
2928
+ res = ARCHIVE_FAILED;
2929
+ break;
2930
+ }
2931
+ } else if (S_ISDIR(st.st_mode)) {
2932
+ if (!last) {
2933
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
2934
+ fd = la_opendirat(chdir_fd, head);
2935
+ if (fd < 0)
2936
+ r = -1;
2937
+ else {
2938
+ r = 0;
2939
+ close(chdir_fd);
2940
+ chdir_fd = fd;
2941
+ }
2942
+ #else
2943
+ r = chdir(head);
2944
+ #endif
2945
+ if (r != 0) {
2946
+ tail[0] = c;
2947
+ fsobj_error(a_eno, a_estr, errno,
2948
+ "Could not chdir ", path);
2949
+ res = (ARCHIVE_FATAL);
2950
+ break;
2951
+ }
2952
+ /* Our view is now from inside this dir: */
2953
+ head = tail + 1;
2954
+ }
2955
+ } else if (S_ISLNK(st.st_mode)) {
2956
+ if (last && checking_linkname) {
2957
+ #ifdef HAVE_LINKAT
2958
+ /*
2959
+ * Hardlinks to symlinks are safe to write
2960
+ * if linkat() is supported as it does not
2961
+ * follow symlinks.
2962
+ */
2963
+ res = ARCHIVE_OK;
2964
+ #else
2965
+ /*
2966
+ * We return ARCHIVE_FAILED here as we are
2967
+ * not able to safely write hardlinks
2968
+ * to symlinks.
2969
+ */
2970
+ tail[0] = c;
2971
+ fsobj_error(a_eno, a_estr, errno,
2972
+ "Cannot write hardlink to symlink ",
2973
+ path);
2974
+ res = ARCHIVE_FAILED;
2975
+ #endif
2976
+ break;
2977
+ } else
2978
+ if (last) {
2979
+ /*
2980
+ * Last element is symlink; remove it
2981
+ * so we can overwrite it with the
2982
+ * item being extracted.
2983
+ */
2984
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
2985
+ r = unlinkat(chdir_fd, head, 0);
2986
+ #else
2987
+ r = unlink(head);
2988
+ #endif
2989
+ if (r != 0) {
2990
+ tail[0] = c;
2991
+ fsobj_error(a_eno, a_estr, errno,
2992
+ "Could not remove symlink ",
2993
+ path);
2994
+ res = ARCHIVE_FAILED;
2995
+ break;
2996
+ }
2997
+ /*
2998
+ * Even if we did remove it, a warning
2999
+ * is in order. The warning is silly,
3000
+ * though, if we're just replacing one
3001
+ * symlink with another symlink.
3002
+ */
3003
+ tail[0] = c;
3004
+ /*
3005
+ * FIXME: not sure how important this is to
3006
+ * restore
3007
+ */
3008
+ /*
3009
+ if (!S_ISLNK(path)) {
3010
+ fsobj_error(a_eno, a_estr, -1,
3011
+ "Removing symlink ", path);
3012
+ }
3013
+ */
3014
+ /* Symlink gone. No more problem! */
3015
+ res = ARCHIVE_OK;
3016
+ break;
3017
+ } else if (flags & ARCHIVE_EXTRACT_UNLINK) {
3018
+ /* User asked us to remove problems. */
3019
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
3020
+ r = unlinkat(chdir_fd, head, 0);
3021
+ #else
3022
+ r = unlink(head);
3023
+ #endif
3024
+ if (r != 0) {
3025
+ tail[0] = c;
3026
+ fsobj_error(a_eno, a_estr, EIO,
3027
+ "Cannot remove intervening "
3028
+ "symlink ", path);
3029
+ res = ARCHIVE_FAILED;
3030
+ break;
3031
+ }
3032
+ tail[0] = c;
3033
+ } else if ((flags &
3034
+ ARCHIVE_EXTRACT_SECURE_SYMLINKS) == 0) {
3035
+ /*
3036
+ * We are not the last element and we want to
3037
+ * follow symlinks if they are a directory.
3038
+ *
3039
+ * This is needed to extract hardlinks over
3040
+ * symlinks.
3041
+ */
3042
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
3043
+ r = fstatat(chdir_fd, head, &st, 0);
3044
+ #else
3045
+ r = la_stat(head, &st);
3046
+ #endif
3047
+ if (r != 0) {
3048
+ tail[0] = c;
3049
+ if (errno == ENOENT) {
3050
+ break;
3051
+ } else {
3052
+ fsobj_error(a_eno, a_estr,
3053
+ errno,
3054
+ "Could not stat ", path);
3055
+ res = (ARCHIVE_FAILED);
3056
+ break;
3057
+ }
3058
+ } else if (S_ISDIR(st.st_mode)) {
3059
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
3060
+ fd = la_opendirat(chdir_fd, head);
3061
+ if (fd < 0)
3062
+ r = -1;
3063
+ else {
3064
+ r = 0;
3065
+ close(chdir_fd);
3066
+ chdir_fd = fd;
3067
+ }
3068
+ #else
3069
+ r = chdir(head);
3070
+ #endif
3071
+ if (r != 0) {
3072
+ tail[0] = c;
3073
+ fsobj_error(a_eno, a_estr,
3074
+ errno,
3075
+ "Could not chdir ", path);
3076
+ res = (ARCHIVE_FATAL);
3077
+ break;
3078
+ }
3079
+ /*
3080
+ * Our view is now from inside
3081
+ * this dir:
3082
+ */
3083
+ head = tail + 1;
3084
+ } else {
3085
+ tail[0] = c;
3086
+ fsobj_error(a_eno, a_estr, ELOOP,
3087
+ "Cannot extract through "
3088
+ "symlink ", path);
3089
+ res = ARCHIVE_FAILED;
3090
+ break;
3091
+ }
3092
+ } else {
3093
+ tail[0] = c;
3094
+ fsobj_error(a_eno, a_estr, ELOOP,
3095
+ "Cannot extract through symlink ", path);
3096
+ res = ARCHIVE_FAILED;
3097
+ break;
3098
+ }
3099
+ }
3100
+ /* be sure to always maintain this */
3101
+ tail[0] = c;
3102
+ if (tail[0] != '\0')
3103
+ tail++; /* Advance to the next segment. */
3104
+ }
3105
+ /* Catches loop exits via break */
3106
+ tail[0] = c;
3107
+ #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT) && defined(HAVE_UNLINKAT)
3108
+ /* If we operate with openat(), fstatat() and unlinkat() there was
3109
+ * no chdir(), so just close the fd */
3110
+ if (chdir_fd >= 0)
3111
+ close(chdir_fd);
3112
+ #elif HAVE_FCHDIR
3113
+ /* If we changed directory above, restore it here. */
3114
+ if (chdir_fd >= 0) {
3115
+ r = fchdir(chdir_fd);
3116
+ if (r != 0) {
3117
+ fsobj_error(a_eno, a_estr, errno,
3118
+ "chdir() failure", "");
3119
+ }
3120
+ close(chdir_fd);
3121
+ chdir_fd = -1;
3122
+ if (r != 0) {
3123
+ res = (ARCHIVE_FATAL);
3124
+ }
3125
+ }
3126
+ #endif
3127
+ /* TODO: reintroduce a safe cache here? */
3128
+ return res;
3129
+ #endif
3130
+ }
3131
+
3132
+ /*
3133
+ * Check a->name for symlinks, returning ARCHIVE_OK if its clean, otherwise
3134
+ * calls archive_set_error and returns ARCHIVE_{FATAL,FAILED}
3135
+ */
3136
+ static int
3137
+ check_symlinks(struct archive_write_disk *a)
3138
+ {
3139
+ struct archive_string error_string;
3140
+ int error_number;
3141
+ int rc;
3142
+ archive_string_init(&error_string);
3143
+ rc = check_symlinks_fsobj(a->name, &error_number, &error_string,
3144
+ a->flags, 0);
3145
+ if (rc != ARCHIVE_OK) {
3146
+ archive_set_error(&a->archive, error_number, "%s",
3147
+ error_string.s);
3148
+ }
3149
+ archive_string_free(&error_string);
3150
+ a->pst = NULL; /* to be safe */
3151
+ return rc;
3152
+ }
3153
+
3154
+
3155
+ #if defined(__CYGWIN__)
3156
+ /*
3157
+ * 1. Convert a path separator from '\' to '/' .
3158
+ * We shouldn't check multibyte character directly because some
3159
+ * character-set have been using the '\' character for a part of
3160
+ * its multibyte character code.
3161
+ * 2. Replace unusable characters in Windows with underscore('_').
3162
+ * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
3163
+ */
3164
+ static void
3165
+ cleanup_pathname_win(char *path)
3166
+ {
3167
+ wchar_t wc;
3168
+ char *p;
3169
+ size_t alen, l;
3170
+ int mb, complete, utf8;
3171
+
3172
+ alen = 0;
3173
+ mb = 0;
3174
+ complete = 1;
3175
+ utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0;
3176
+ for (p = path; *p != '\0'; p++) {
3177
+ ++alen;
3178
+ if (*p == '\\') {
3179
+ /* If previous byte is smaller than 128,
3180
+ * this is not second byte of multibyte characters,
3181
+ * so we can replace '\' with '/'. */
3182
+ if (utf8 || !mb)
3183
+ *p = '/';
3184
+ else
3185
+ complete = 0;/* uncompleted. */
3186
+ } else if (*(unsigned char *)p > 127)
3187
+ mb = 1;
3188
+ else
3189
+ mb = 0;
3190
+ /* Rewrite the path name if its next character is unusable. */
3191
+ if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
3192
+ *p == '<' || *p == '>' || *p == '|')
3193
+ *p = '_';
3194
+ }
3195
+ if (complete)
3196
+ return;
3197
+
3198
+ /*
3199
+ * Convert path separator in wide-character.
3200
+ */
3201
+ p = path;
3202
+ while (*p != '\0' && alen) {
3203
+ l = mbtowc(&wc, p, alen);
3204
+ if (l == (size_t)-1) {
3205
+ while (*p != '\0') {
3206
+ if (*p == '\\')
3207
+ *p = '/';
3208
+ ++p;
3209
+ }
3210
+ break;
3211
+ }
3212
+ if (l == 1 && wc == L'\\')
3213
+ *p = '/';
3214
+ p += l;
3215
+ alen -= l;
3216
+ }
3217
+ }
3218
+ #endif
3219
+
3220
+ /*
3221
+ * Canonicalize the pathname. In particular, this strips duplicate
3222
+ * '/' characters, '.' elements, and trailing '/'. It also raises an
3223
+ * error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is
3224
+ * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
3225
+ * is set) if the path is absolute.
3226
+ */
3227
+ static int
3228
+ cleanup_pathname_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
3229
+ int flags)
3230
+ {
3231
+ char *dest, *src;
3232
+ char separator = '\0';
3233
+
3234
+ dest = src = path;
3235
+ if (*src == '\0') {
3236
+ fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
3237
+ "Invalid empty ", "pathname");
3238
+ return (ARCHIVE_FAILED);
3239
+ }
3240
+
3241
+ #if defined(__CYGWIN__)
3242
+ cleanup_pathname_win(path);
3243
+ #endif
3244
+ /* Skip leading '/'. */
3245
+ if (*src == '/') {
3246
+ if (flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) {
3247
+ fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
3248
+ "Path is ", "absolute");
3249
+ return (ARCHIVE_FAILED);
3250
+ }
3251
+
3252
+ separator = *src++;
3253
+ }
3254
+
3255
+ /* Scan the pathname one element at a time. */
3256
+ for (;;) {
3257
+ /* src points to first char after '/' */
3258
+ if (src[0] == '\0') {
3259
+ break;
3260
+ } else if (src[0] == '/') {
3261
+ /* Found '//', ignore second one. */
3262
+ src++;
3263
+ continue;
3264
+ } else if (src[0] == '.') {
3265
+ if (src[1] == '\0') {
3266
+ /* Ignore trailing '.' */
3267
+ break;
3268
+ } else if (src[1] == '/') {
3269
+ /* Skip './'. */
3270
+ src += 2;
3271
+ continue;
3272
+ } else if (src[1] == '.') {
3273
+ if (src[2] == '/' || src[2] == '\0') {
3274
+ /* Conditionally warn about '..' */
3275
+ if (flags
3276
+ & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
3277
+ fsobj_error(a_eno, a_estr,
3278
+ ARCHIVE_ERRNO_MISC,
3279
+ "Path contains ", "'..'");
3280
+ return (ARCHIVE_FAILED);
3281
+ }
3282
+ }
3283
+ /*
3284
+ * Note: Under no circumstances do we
3285
+ * remove '..' elements. In
3286
+ * particular, restoring
3287
+ * '/foo/../bar/' should create the
3288
+ * 'foo' dir as a side-effect.
3289
+ */
3290
+ }
3291
+ }
3292
+
3293
+ /* Copy current element, including leading '/'. */
3294
+ if (separator)
3295
+ *dest++ = '/';
3296
+ while (*src != '\0' && *src != '/') {
3297
+ *dest++ = *src++;
3298
+ }
3299
+
3300
+ if (*src == '\0')
3301
+ break;
3302
+
3303
+ /* Skip '/' separator. */
3304
+ separator = *src++;
3305
+ }
3306
+ /*
3307
+ * We've just copied zero or more path elements, not including the
3308
+ * final '/'.
3309
+ */
3310
+ if (dest == path) {
3311
+ /*
3312
+ * Nothing got copied. The path must have been something
3313
+ * like '.' or '/' or './' or '/././././/./'.
3314
+ */
3315
+ if (separator)
3316
+ *dest++ = '/';
3317
+ else
3318
+ *dest++ = '.';
3319
+ }
3320
+ /* Terminate the result. */
3321
+ *dest = '\0';
3322
+ return (ARCHIVE_OK);
3323
+ }
3324
+
3325
+ static int
3326
+ cleanup_pathname(struct archive_write_disk *a)
3327
+ {
3328
+ struct archive_string error_string;
3329
+ int error_number;
3330
+ int rc;
3331
+ archive_string_init(&error_string);
3332
+ rc = cleanup_pathname_fsobj(a->name, &error_number, &error_string,
3333
+ a->flags);
3334
+ if (rc != ARCHIVE_OK) {
3335
+ archive_set_error(&a->archive, error_number, "%s",
3336
+ error_string.s);
3337
+ }
3338
+ archive_string_free(&error_string);
3339
+ return rc;
3340
+ }
3341
+
3342
+ /*
3343
+ * Create the parent directory of the specified path, assuming path
3344
+ * is already in mutable storage.
3345
+ */
3346
+ static int
3347
+ create_parent_dir(struct archive_write_disk *a, char *path)
3348
+ {
3349
+ char *slash;
3350
+ int r;
3351
+
3352
+ /* Remove tail element to obtain parent name. */
3353
+ slash = strrchr(path, '/');
3354
+ if (slash == NULL)
3355
+ return (ARCHIVE_OK);
3356
+ *slash = '\0';
3357
+ r = create_dir(a, path);
3358
+ *slash = '/';
3359
+ return (r);
3360
+ }
3361
+
3362
+ /*
3363
+ * Create the specified dir, recursing to create parents as necessary.
3364
+ *
3365
+ * Returns ARCHIVE_OK if the path exists when we're done here.
3366
+ * Otherwise, returns ARCHIVE_FAILED.
3367
+ * Assumes path is in mutable storage; path is unchanged on exit.
3368
+ */
3369
+ static int
3370
+ create_dir(struct archive_write_disk *a, char *path)
3371
+ {
3372
+ struct stat st;
3373
+ struct fixup_entry *le;
3374
+ char *slash, *base;
3375
+ mode_t mode_final, mode;
3376
+ int r;
3377
+
3378
+ /* Check for special names and just skip them. */
3379
+ slash = strrchr(path, '/');
3380
+ if (slash == NULL)
3381
+ base = path;
3382
+ else
3383
+ base = slash + 1;
3384
+
3385
+ if (base[0] == '\0' ||
3386
+ (base[0] == '.' && base[1] == '\0') ||
3387
+ (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
3388
+ /* Don't bother trying to create null path, '.', or '..'. */
3389
+ if (slash != NULL) {
3390
+ *slash = '\0';
3391
+ r = create_dir(a, path);
3392
+ *slash = '/';
3393
+ return (r);
3394
+ }
3395
+ return (ARCHIVE_OK);
3396
+ }
3397
+
3398
+ /*
3399
+ * Yes, this should be stat() and not lstat(). Using lstat()
3400
+ * here loses the ability to extract through symlinks. Also note
3401
+ * that this should not use the a->st cache.
3402
+ */
3403
+ if (la_stat(path, &st) == 0) {
3404
+ if (S_ISDIR(st.st_mode))
3405
+ return (ARCHIVE_OK);
3406
+ if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
3407
+ archive_set_error(&a->archive, EEXIST,
3408
+ "Can't create directory '%s'", path);
3409
+ return (ARCHIVE_FAILED);
3410
+ }
3411
+ if (unlink(path) != 0) {
3412
+ archive_set_error(&a->archive, errno,
3413
+ "Can't create directory '%s': "
3414
+ "Conflicting file cannot be removed",
3415
+ path);
3416
+ return (ARCHIVE_FAILED);
3417
+ }
3418
+ } else if (errno != ENOENT && errno != ENOTDIR) {
3419
+ /* Stat failed? */
3420
+ archive_set_error(&a->archive, errno,
3421
+ "Can't test directory '%s'", path);
3422
+ return (ARCHIVE_FAILED);
3423
+ } else if (slash != NULL) {
3424
+ *slash = '\0';
3425
+ r = create_dir(a, path);
3426
+ *slash = '/';
3427
+ if (r != ARCHIVE_OK)
3428
+ return (r);
3429
+ }
3430
+
3431
+ /*
3432
+ * Mode we want for the final restored directory. Per POSIX,
3433
+ * implicitly-created dirs must be created obeying the umask.
3434
+ * There's no mention whether this is different for privileged
3435
+ * restores (which the rest of this code handles by pretending
3436
+ * umask=0). I've chosen here to always obey the user's umask for
3437
+ * implicit dirs, even if _EXTRACT_PERM was specified.
3438
+ */
3439
+ mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
3440
+ /* Mode we want on disk during the restore process. */
3441
+ mode = mode_final;
3442
+ mode |= MINIMUM_DIR_MODE;
3443
+ mode &= MAXIMUM_DIR_MODE;
3444
+ if (mkdir(path, mode) == 0) {
3445
+ if (mode != mode_final) {
3446
+ le = new_fixup(a, path);
3447
+ if (le == NULL)
3448
+ return (ARCHIVE_FATAL);
3449
+ le->fixup |= TODO_MODE_BASE;
3450
+ le->mode = mode_final;
3451
+ }
3452
+ return (ARCHIVE_OK);
3453
+ }
3454
+
3455
+ /*
3456
+ * Without the following check, a/b/../b/c/d fails at the
3457
+ * second visit to 'b', so 'd' can't be created. Note that we
3458
+ * don't add it to the fixup list here, as it's already been
3459
+ * added.
3460
+ */
3461
+ if (errno == EEXIST) {
3462
+ if (la_stat(path, &st) == 0) {
3463
+ if (S_ISDIR(st.st_mode))
3464
+ return (ARCHIVE_OK);
3465
+ /* path exists but is not a directory */
3466
+ errno = ENOTDIR;
3467
+ } else {
3468
+ /* restore original errno */
3469
+ errno = EEXIST;
3470
+ }
3471
+ }
3472
+
3473
+ archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
3474
+ path);
3475
+ return (ARCHIVE_FAILED);
3476
+ }
3477
+
3478
+ /*
3479
+ * Note: Although we can skip setting the user id if the desired user
3480
+ * id matches the current user, we cannot skip setting the group, as
3481
+ * many systems set the gid based on the containing directory. So
3482
+ * we have to perform a chown syscall if we want to set the SGID
3483
+ * bit. (The alternative is to stat() and then possibly chown(); it's
3484
+ * more efficient to skip the stat() and just always chown().) Note
3485
+ * that a successful chown() here clears the TODO_SGID_CHECK bit, which
3486
+ * allows set_mode to skip the stat() check for the GID.
3487
+ */
3488
+ static int
3489
+ set_ownership(struct archive_write_disk *a)
3490
+ {
3491
+ #if !defined(__CYGWIN__) && !defined(__linux__)
3492
+ /*
3493
+ * On Linux, a process may have the CAP_CHOWN capability.
3494
+ * On Windows there is no 'root' user with uid 0.
3495
+ * Elsewhere we can skip calling chown if we are not root and the desired
3496
+ * user id does not match the current user.
3497
+ */
3498
+ if (a->user_uid != 0 && a->user_uid != a->uid) {
3499
+ archive_set_error(&a->archive, errno,
3500
+ "Can't set UID=%jd", (intmax_t)a->uid);
3501
+ return (ARCHIVE_WARN);
3502
+ }
3503
+ #endif
3504
+
3505
+ #ifdef HAVE_FCHOWN
3506
+ /* If we have an fd, we can avoid a race. */
3507
+ if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
3508
+ /* We've set owner and know uid/gid are correct. */
3509
+ a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3510
+ return (ARCHIVE_OK);
3511
+ }
3512
+ #endif
3513
+
3514
+ /* We prefer lchown() but will use chown() if that's all we have. */
3515
+ /* Of course, if we have neither, this will always fail. */
3516
+ #ifdef HAVE_LCHOWN
3517
+ if (lchown(a->name, a->uid, a->gid) == 0) {
3518
+ /* We've set owner and know uid/gid are correct. */
3519
+ a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3520
+ return (ARCHIVE_OK);
3521
+ }
3522
+ #elif HAVE_CHOWN
3523
+ if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
3524
+ /* We've set owner and know uid/gid are correct. */
3525
+ a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3526
+ return (ARCHIVE_OK);
3527
+ }
3528
+ #endif
3529
+
3530
+ archive_set_error(&a->archive, errno,
3531
+ "Can't set user=%jd/group=%jd for %s",
3532
+ (intmax_t)a->uid, (intmax_t)a->gid, a->name);
3533
+ return (ARCHIVE_WARN);
3534
+ }
3535
+
3536
+ /*
3537
+ * Note: Returns 0 on success, non-zero on failure.
3538
+ */
3539
+ static int
3540
+ set_time(int fd, int mode, const char *name,
3541
+ time_t atime, long atime_nsec,
3542
+ time_t mtime, long mtime_nsec)
3543
+ {
3544
+ /* Select the best implementation for this platform. */
3545
+ #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
3546
+ /*
3547
+ * utimensat() and futimens() are defined in
3548
+ * POSIX.1-2008. They support ns resolution and setting times
3549
+ * on fds and symlinks.
3550
+ */
3551
+ struct timespec ts[2];
3552
+ (void)mode; /* UNUSED */
3553
+ ts[0].tv_sec = atime;
3554
+ ts[0].tv_nsec = atime_nsec;
3555
+ ts[1].tv_sec = mtime;
3556
+ ts[1].tv_nsec = mtime_nsec;
3557
+ if (fd >= 0)
3558
+ return futimens(fd, ts);
3559
+ return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
3560
+
3561
+ #elif HAVE_UTIMES
3562
+ /*
3563
+ * The utimes()-family functions support µs-resolution and
3564
+ * setting times fds and symlinks. utimes() is documented as
3565
+ * LEGACY by POSIX, futimes() and lutimes() are not described
3566
+ * in POSIX.
3567
+ */
3568
+ struct timeval times[2];
3569
+
3570
+ times[0].tv_sec = atime;
3571
+ times[0].tv_usec = atime_nsec / 1000;
3572
+ times[1].tv_sec = mtime;
3573
+ times[1].tv_usec = mtime_nsec / 1000;
3574
+
3575
+ #ifdef HAVE_FUTIMES
3576
+ if (fd >= 0)
3577
+ return (futimes(fd, times));
3578
+ #else
3579
+ (void)fd; /* UNUSED */
3580
+ #endif
3581
+ #ifdef HAVE_LUTIMES
3582
+ (void)mode; /* UNUSED */
3583
+ return (lutimes(name, times));
3584
+ #else
3585
+ if (S_ISLNK(mode))
3586
+ return (0);
3587
+ return (utimes(name, times));
3588
+ #endif
3589
+
3590
+ #elif defined(HAVE_UTIME)
3591
+ /*
3592
+ * utime() is POSIX-standard but only supports 1s resolution and
3593
+ * does not support fds or symlinks.
3594
+ */
3595
+ struct utimbuf times;
3596
+ (void)fd; /* UNUSED */
3597
+ (void)name; /* UNUSED */
3598
+ (void)atime_nsec; /* UNUSED */
3599
+ (void)mtime_nsec; /* UNUSED */
3600
+ times.actime = atime;
3601
+ times.modtime = mtime;
3602
+ if (S_ISLNK(mode))
3603
+ return (ARCHIVE_OK);
3604
+ return (utime(name, &times));
3605
+
3606
+ #else
3607
+ /*
3608
+ * We don't know how to set the time on this platform.
3609
+ */
3610
+ (void)fd; /* UNUSED */
3611
+ (void)mode; /* UNUSED */
3612
+ (void)name; /* UNUSED */
3613
+ (void)atime; /* UNUSED */
3614
+ (void)atime_nsec; /* UNUSED */
3615
+ (void)mtime; /* UNUSED */
3616
+ (void)mtime_nsec; /* UNUSED */
3617
+ return (ARCHIVE_WARN);
3618
+ #endif
3619
+ }
3620
+
3621
+ #ifdef F_SETTIMES
3622
+ static int
3623
+ set_time_tru64(int fd, int mode, const char *name,
3624
+ time_t atime, long atime_nsec,
3625
+ time_t mtime, long mtime_nsec,
3626
+ time_t ctime, long ctime_nsec)
3627
+ {
3628
+ struct attr_timbuf tstamp;
3629
+ tstamp.atime.tv_sec = atime;
3630
+ tstamp.mtime.tv_sec = mtime;
3631
+ tstamp.ctime.tv_sec = ctime;
3632
+ #if defined (__hpux) && ( defined (__ia64) || defined (__hppa) )
3633
+ tstamp.atime.tv_nsec = atime_nsec;
3634
+ tstamp.mtime.tv_nsec = mtime_nsec;
3635
+ tstamp.ctime.tv_nsec = ctime_nsec;
3636
+ #else
3637
+ tstamp.atime.tv_usec = atime_nsec / 1000;
3638
+ tstamp.mtime.tv_usec = mtime_nsec / 1000;
3639
+ tstamp.ctime.tv_usec = ctime_nsec / 1000;
3640
+ #endif
3641
+ return (fcntl(fd,F_SETTIMES,&tstamp));
3642
+ }
3643
+ #endif /* F_SETTIMES */
3644
+
3645
+ static int
3646
+ set_times(struct archive_write_disk *a,
3647
+ int fd, int mode, const char *name,
3648
+ time_t atime, long atime_nanos,
3649
+ time_t birthtime, long birthtime_nanos,
3650
+ time_t mtime, long mtime_nanos,
3651
+ time_t cctime, long ctime_nanos)
3652
+ {
3653
+ /* Note: set_time doesn't use libarchive return conventions!
3654
+ * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */
3655
+ int r1 = 0, r2 = 0;
3656
+
3657
+ #ifdef F_SETTIMES
3658
+ /*
3659
+ * on Tru64 try own fcntl first which can restore even the
3660
+ * ctime, fall back to default code path below if it fails
3661
+ * or if we are not running as root
3662
+ */
3663
+ if (a->user_uid == 0 &&
3664
+ set_time_tru64(fd, mode, name,
3665
+ atime, atime_nanos, mtime,
3666
+ mtime_nanos, cctime, ctime_nanos) == 0) {
3667
+ return (ARCHIVE_OK);
3668
+ }
3669
+ #else /* Tru64 */
3670
+ (void)cctime; /* UNUSED */
3671
+ (void)ctime_nanos; /* UNUSED */
3672
+ #endif /* Tru64 */
3673
+
3674
+ #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
3675
+ /*
3676
+ * If you have struct stat.st_birthtime, we assume BSD
3677
+ * birthtime semantics, in which {f,l,}utimes() updates
3678
+ * birthtime to earliest mtime. So we set the time twice,
3679
+ * first using the birthtime, then using the mtime. If
3680
+ * birthtime == mtime, this isn't necessary, so we skip it.
3681
+ * If birthtime > mtime, then this won't work, so we skip it.
3682
+ */
3683
+ if (birthtime < mtime
3684
+ || (birthtime == mtime && birthtime_nanos < mtime_nanos))
3685
+ r1 = set_time(fd, mode, name,
3686
+ atime, atime_nanos,
3687
+ birthtime, birthtime_nanos);
3688
+ #else
3689
+ (void)birthtime; /* UNUSED */
3690
+ (void)birthtime_nanos; /* UNUSED */
3691
+ #endif
3692
+ r2 = set_time(fd, mode, name,
3693
+ atime, atime_nanos,
3694
+ mtime, mtime_nanos);
3695
+ if (r1 != 0 || r2 != 0) {
3696
+ archive_set_error(&a->archive, errno,
3697
+ "Can't restore time");
3698
+ return (ARCHIVE_WARN);
3699
+ }
3700
+ return (ARCHIVE_OK);
3701
+ }
3702
+
3703
+ static int
3704
+ set_times_from_entry(struct archive_write_disk *a)
3705
+ {
3706
+ time_t atime, birthtime, mtime, cctime;
3707
+ long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
3708
+
3709
+ /* Suitable defaults. */
3710
+ atime = birthtime = mtime = cctime = a->start_time;
3711
+ atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
3712
+
3713
+ /* If no time was provided, we're done. */
3714
+ if (!archive_entry_atime_is_set(a->entry)
3715
+ #if HAVE_STRUCT_STAT_ST_BIRTHTIME
3716
+ && !archive_entry_birthtime_is_set(a->entry)
3717
+ #endif
3718
+ && !archive_entry_mtime_is_set(a->entry))
3719
+ return (ARCHIVE_OK);
3720
+
3721
+ if (archive_entry_atime_is_set(a->entry)) {
3722
+ atime = archive_entry_atime(a->entry);
3723
+ atime_nsec = archive_entry_atime_nsec(a->entry);
3724
+ }
3725
+ if (archive_entry_birthtime_is_set(a->entry)) {
3726
+ birthtime = archive_entry_birthtime(a->entry);
3727
+ birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
3728
+ }
3729
+ if (archive_entry_mtime_is_set(a->entry)) {
3730
+ mtime = archive_entry_mtime(a->entry);
3731
+ mtime_nsec = archive_entry_mtime_nsec(a->entry);
3732
+ }
3733
+ if (archive_entry_ctime_is_set(a->entry)) {
3734
+ cctime = archive_entry_ctime(a->entry);
3735
+ ctime_nsec = archive_entry_ctime_nsec(a->entry);
3736
+ }
3737
+
3738
+ return set_times(a, a->fd, a->mode, a->name,
3739
+ atime, atime_nsec,
3740
+ birthtime, birthtime_nsec,
3741
+ mtime, mtime_nsec,
3742
+ cctime, ctime_nsec);
3743
+ }
3744
+
3745
+ static int
3746
+ set_mode(struct archive_write_disk *a, int mode)
3747
+ {
3748
+ int r = ARCHIVE_OK;
3749
+ int r2;
3750
+ mode &= 07777; /* Strip off file type bits. */
3751
+
3752
+ if (a->todo & TODO_SGID_CHECK) {
3753
+ /*
3754
+ * If we don't know the GID is right, we must stat()
3755
+ * to verify it. We can't just check the GID of this
3756
+ * process, since systems sometimes set GID from
3757
+ * the enclosing dir or based on ACLs.
3758
+ */
3759
+ if ((r = lazy_stat(a)) != ARCHIVE_OK)
3760
+ return (r);
3761
+ if (a->pst->st_gid != a->gid) {
3762
+ mode &= ~ S_ISGID;
3763
+ if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3764
+ /*
3765
+ * This is only an error if you
3766
+ * requested owner restore. If you
3767
+ * didn't, we'll try to restore
3768
+ * sgid/suid, but won't consider it a
3769
+ * problem if we can't.
3770
+ */
3771
+ archive_set_error(&a->archive, -1,
3772
+ "Can't restore SGID bit");
3773
+ r = ARCHIVE_WARN;
3774
+ }
3775
+ }
3776
+ /* While we're here, double-check the UID. */
3777
+ if (a->pst->st_uid != a->uid
3778
+ && (a->todo & TODO_SUID)) {
3779
+ mode &= ~ S_ISUID;
3780
+ if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3781
+ archive_set_error(&a->archive, -1,
3782
+ "Can't restore SUID bit");
3783
+ r = ARCHIVE_WARN;
3784
+ }
3785
+ }
3786
+ a->todo &= ~TODO_SGID_CHECK;
3787
+ a->todo &= ~TODO_SUID_CHECK;
3788
+ } else if (a->todo & TODO_SUID_CHECK) {
3789
+ /*
3790
+ * If we don't know the UID is right, we can just check
3791
+ * the user, since all systems set the file UID from
3792
+ * the process UID.
3793
+ */
3794
+ if (a->user_uid != a->uid) {
3795
+ mode &= ~ S_ISUID;
3796
+ if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3797
+ archive_set_error(&a->archive, -1,
3798
+ "Can't make file SUID");
3799
+ r = ARCHIVE_WARN;
3800
+ }
3801
+ }
3802
+ a->todo &= ~TODO_SUID_CHECK;
3803
+ }
3804
+
3805
+ if (S_ISLNK(a->mode)) {
3806
+ #ifdef HAVE_LCHMOD
3807
+ /*
3808
+ * If this is a symlink, use lchmod(). If the
3809
+ * platform doesn't support lchmod(), just skip it. A
3810
+ * platform that doesn't provide a way to set
3811
+ * permissions on symlinks probably ignores
3812
+ * permissions on symlinks, so a failure here has no
3813
+ * impact.
3814
+ */
3815
+ if (lchmod(a->name, (mode_t)mode) != 0) {
3816
+ switch (errno) {
3817
+ case ENOTSUP:
3818
+ case ENOSYS:
3819
+ #if ENOTSUP != EOPNOTSUPP
3820
+ case EOPNOTSUPP:
3821
+ #endif
3822
+ /*
3823
+ * if lchmod is defined but the platform
3824
+ * doesn't support it, silently ignore
3825
+ * error
3826
+ */
3827
+ break;
3828
+ default:
3829
+ archive_set_error(&a->archive, errno,
3830
+ "Can't set permissions to 0%o",
3831
+ (unsigned int)mode);
3832
+ r = ARCHIVE_WARN;
3833
+ }
3834
+ }
3835
+ #endif
3836
+ } else if (!S_ISDIR(a->mode)) {
3837
+ /*
3838
+ * If it's not a symlink and not a dir, then use
3839
+ * fchmod() or chmod(), depending on whether we have
3840
+ * an fd. Dirs get their perms set during the
3841
+ * post-extract fixup, which is handled elsewhere.
3842
+ */
3843
+ #ifdef HAVE_FCHMOD
3844
+ if (a->fd >= 0)
3845
+ r2 = fchmod(a->fd, (mode_t)mode);
3846
+ else
3847
+ #endif
3848
+ /* If this platform lacks fchmod(), then
3849
+ * we'll just use chmod(). */
3850
+ r2 = chmod(a->name, (mode_t)mode);
3851
+
3852
+ if (r2 != 0) {
3853
+ archive_set_error(&a->archive, errno,
3854
+ "Can't set permissions to 0%o", (unsigned int)mode);
3855
+ r = ARCHIVE_WARN;
3856
+ }
3857
+ }
3858
+ return (r);
3859
+ }
3860
+
3861
+ static int
3862
+ set_fflags(struct archive_write_disk *a)
3863
+ {
3864
+ struct fixup_entry *le;
3865
+ unsigned long set, clear;
3866
+ int r;
3867
+ mode_t mode = archive_entry_mode(a->entry);
3868
+ /*
3869
+ * Make 'critical_flags' hold all file flags that can't be
3870
+ * immediately restored. For example, on BSD systems,
3871
+ * SF_IMMUTABLE prevents hardlinks from being created, so
3872
+ * should not be set until after any hardlinks are created. To
3873
+ * preserve some semblance of portability, this uses #ifdef
3874
+ * extensively. Ugly, but it works.
3875
+ *
3876
+ * Yes, Virginia, this does create a security race. It's mitigated
3877
+ * somewhat by the practice of creating dirs 0700 until the extract
3878
+ * is done, but it would be nice if we could do more than that.
3879
+ * People restoring critical file systems should be wary of
3880
+ * other programs that might try to muck with files as they're
3881
+ * being restored.
3882
+ */
3883
+ const int critical_flags = 0
3884
+ #ifdef SF_IMMUTABLE
3885
+ | SF_IMMUTABLE
3886
+ #endif
3887
+ #ifdef UF_IMMUTABLE
3888
+ | UF_IMMUTABLE
3889
+ #endif
3890
+ #ifdef SF_APPEND
3891
+ | SF_APPEND
3892
+ #endif
3893
+ #ifdef UF_APPEND
3894
+ | UF_APPEND
3895
+ #endif
3896
+ #if defined(FS_APPEND_FL)
3897
+ | FS_APPEND_FL
3898
+ #elif defined(EXT2_APPEND_FL)
3899
+ | EXT2_APPEND_FL
3900
+ #endif
3901
+ #if defined(FS_IMMUTABLE_FL)
3902
+ | FS_IMMUTABLE_FL
3903
+ #elif defined(EXT2_IMMUTABLE_FL)
3904
+ | EXT2_IMMUTABLE_FL
3905
+ #endif
3906
+ #ifdef FS_JOURNAL_DATA_FL
3907
+ | FS_JOURNAL_DATA_FL
3908
+ #endif
3909
+ ;
3910
+
3911
+ if (a->todo & TODO_FFLAGS) {
3912
+ archive_entry_fflags(a->entry, &set, &clear);
3913
+
3914
+ /*
3915
+ * The first test encourages the compiler to eliminate
3916
+ * all of this if it's not necessary.
3917
+ */
3918
+ if ((critical_flags != 0) && (set & critical_flags)) {
3919
+ le = current_fixup(a, a->name);
3920
+ if (le == NULL)
3921
+ return (ARCHIVE_FATAL);
3922
+ le->filetype = archive_entry_filetype(a->entry);
3923
+ le->fixup |= TODO_FFLAGS;
3924
+ le->fflags_set = set;
3925
+ /* Store the mode if it's not already there. */
3926
+ if ((le->fixup & TODO_MODE) == 0)
3927
+ le->mode = mode;
3928
+ } else {
3929
+ r = set_fflags_platform(a, a->fd,
3930
+ a->name, mode, set, clear);
3931
+ if (r != ARCHIVE_OK)
3932
+ return (r);
3933
+ }
3934
+ }
3935
+ return (ARCHIVE_OK);
3936
+ }
3937
+
3938
+ static int
3939
+ clear_nochange_fflags(struct archive_write_disk *a)
3940
+ {
3941
+ mode_t mode = archive_entry_mode(a->entry);
3942
+ const int nochange_flags = 0
3943
+ #ifdef SF_IMMUTABLE
3944
+ | SF_IMMUTABLE
3945
+ #endif
3946
+ #ifdef UF_IMMUTABLE
3947
+ | UF_IMMUTABLE
3948
+ #endif
3949
+ #ifdef SF_APPEND
3950
+ | SF_APPEND
3951
+ #endif
3952
+ #ifdef UF_APPEND
3953
+ | UF_APPEND
3954
+ #endif
3955
+ #if defined(FS_APPEND_FL)
3956
+ | FS_APPEND_FL
3957
+ #elif defined(EXT2_APPEND_FL)
3958
+ | EXT2_APPEND_FL
3959
+ #endif
3960
+ #if defined(FS_IMMUTABLE_FL)
3961
+ | FS_IMMUTABLE_FL
3962
+ #elif defined(EXT2_IMMUTABLE_FL)
3963
+ | EXT2_IMMUTABLE_FL
3964
+ #endif
3965
+ ;
3966
+
3967
+ return (set_fflags_platform(a, a->fd, a->name, mode, 0,
3968
+ nochange_flags));
3969
+ }
3970
+
3971
+
3972
+ #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
3973
+ /*
3974
+ * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
3975
+ */
3976
+ static int
3977
+ set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
3978
+ mode_t mode, unsigned long set, unsigned long clear)
3979
+ {
3980
+ int r;
3981
+ const int sf_mask = 0
3982
+ #ifdef SF_APPEND
3983
+ | SF_APPEND
3984
+ #endif
3985
+ #ifdef SF_ARCHIVED
3986
+ | SF_ARCHIVED
3987
+ #endif
3988
+ #ifdef SF_IMMUTABLE
3989
+ | SF_IMMUTABLE
3990
+ #endif
3991
+ #ifdef SF_NOUNLINK
3992
+ | SF_NOUNLINK
3993
+ #endif
3994
+ ;
3995
+ (void)mode; /* UNUSED */
3996
+
3997
+ if (set == 0 && clear == 0)
3998
+ return (ARCHIVE_OK);
3999
+
4000
+ /*
4001
+ * XXX Is the stat here really necessary? Or can I just use
4002
+ * the 'set' flags directly? In particular, I'm not sure
4003
+ * about the correct approach if we're overwriting an existing
4004
+ * file that already has flags on it. XXX
4005
+ */
4006
+ if ((r = lazy_stat(a)) != ARCHIVE_OK)
4007
+ return (r);
4008
+
4009
+ a->st.st_flags &= ~clear;
4010
+ a->st.st_flags |= set;
4011
+
4012
+ /* Only super-user may change SF_* flags */
4013
+
4014
+ if (a->user_uid != 0)
4015
+ a->st.st_flags &= ~sf_mask;
4016
+
4017
+ #ifdef HAVE_FCHFLAGS
4018
+ /* If platform has fchflags() and we were given an fd, use it. */
4019
+ if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
4020
+ return (ARCHIVE_OK);
4021
+ #endif
4022
+ /*
4023
+ * If we can't use the fd to set the flags, we'll use the
4024
+ * pathname to set flags. We prefer lchflags() but will use
4025
+ * chflags() if we must.
4026
+ */
4027
+ #ifdef HAVE_LCHFLAGS
4028
+ if (lchflags(name, a->st.st_flags) == 0)
4029
+ return (ARCHIVE_OK);
4030
+ #elif defined(HAVE_CHFLAGS)
4031
+ if (S_ISLNK(a->st.st_mode)) {
4032
+ archive_set_error(&a->archive, errno,
4033
+ "Can't set file flags on symlink");
4034
+ return (ARCHIVE_WARN);
4035
+ }
4036
+ if (chflags(name, a->st.st_flags) == 0)
4037
+ return (ARCHIVE_OK);
4038
+ #endif
4039
+ archive_set_error(&a->archive, errno,
4040
+ "Failed to set file flags");
4041
+ return (ARCHIVE_WARN);
4042
+ }
4043
+
4044
+ #elif (defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) && \
4045
+ defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
4046
+ (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && \
4047
+ defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
4048
+ /*
4049
+ * Linux uses ioctl() to read and write file flags.
4050
+ */
4051
+ static int
4052
+ set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
4053
+ mode_t mode, unsigned long set, unsigned long clear)
4054
+ {
4055
+ int ret;
4056
+ int myfd = fd;
4057
+ int newflags, oldflags;
4058
+ /*
4059
+ * Linux has no define for the flags that are only settable by
4060
+ * the root user. This code may seem a little complex, but
4061
+ * there seem to be some Linux systems that lack these
4062
+ * defines. (?) The code below degrades reasonably gracefully
4063
+ * if sf_mask is incomplete.
4064
+ */
4065
+ const int sf_mask = 0
4066
+ #if defined(FS_IMMUTABLE_FL)
4067
+ | FS_IMMUTABLE_FL
4068
+ #elif defined(EXT2_IMMUTABLE_FL)
4069
+ | EXT2_IMMUTABLE_FL
4070
+ #endif
4071
+ #if defined(FS_APPEND_FL)
4072
+ | FS_APPEND_FL
4073
+ #elif defined(EXT2_APPEND_FL)
4074
+ | EXT2_APPEND_FL
4075
+ #endif
4076
+ #if defined(FS_JOURNAL_DATA_FL)
4077
+ | FS_JOURNAL_DATA_FL
4078
+ #endif
4079
+ ;
4080
+
4081
+ if (set == 0 && clear == 0)
4082
+ return (ARCHIVE_OK);
4083
+ /* Only regular files and dirs can have flags. */
4084
+ if (!S_ISREG(mode) && !S_ISDIR(mode))
4085
+ return (ARCHIVE_OK);
4086
+
4087
+ /* If we weren't given an fd, open it ourselves. */
4088
+ if (myfd < 0) {
4089
+ myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY |
4090
+ O_CLOEXEC | O_NOFOLLOW);
4091
+ __archive_ensure_cloexec_flag(myfd);
4092
+ }
4093
+ if (myfd < 0)
4094
+ return (ARCHIVE_OK);
4095
+
4096
+ /*
4097
+ * XXX As above, this would be way simpler if we didn't have
4098
+ * to read the current flags from disk. XXX
4099
+ */
4100
+ ret = ARCHIVE_OK;
4101
+
4102
+ /* Read the current file flags. */
4103
+ if (ioctl(myfd,
4104
+ #ifdef FS_IOC_GETFLAGS
4105
+ FS_IOC_GETFLAGS,
4106
+ #else
4107
+ EXT2_IOC_GETFLAGS,
4108
+ #endif
4109
+ &oldflags) < 0)
4110
+ goto fail;
4111
+
4112
+ /* Try setting the flags as given. */
4113
+ newflags = (oldflags & ~clear) | set;
4114
+ if (ioctl(myfd,
4115
+ #ifdef FS_IOC_SETFLAGS
4116
+ FS_IOC_SETFLAGS,
4117
+ #else
4118
+ EXT2_IOC_SETFLAGS,
4119
+ #endif
4120
+ &newflags) >= 0)
4121
+ goto cleanup;
4122
+ if (errno != EPERM)
4123
+ goto fail;
4124
+
4125
+ /* If we couldn't set all the flags, try again with a subset. */
4126
+ newflags &= ~sf_mask;
4127
+ oldflags &= sf_mask;
4128
+ newflags |= oldflags;
4129
+ if (ioctl(myfd,
4130
+ #ifdef FS_IOC_SETFLAGS
4131
+ FS_IOC_SETFLAGS,
4132
+ #else
4133
+ EXT2_IOC_SETFLAGS,
4134
+ #endif
4135
+ &newflags) >= 0)
4136
+ goto cleanup;
4137
+
4138
+ /* We couldn't set the flags, so report the failure. */
4139
+ fail:
4140
+ archive_set_error(&a->archive, errno,
4141
+ "Failed to set file flags");
4142
+ ret = ARCHIVE_WARN;
4143
+ cleanup:
4144
+ if (fd < 0)
4145
+ close(myfd);
4146
+ return (ret);
4147
+ }
4148
+
4149
+ #else
4150
+
4151
+ /*
4152
+ * Of course, some systems have neither BSD chflags() nor Linux' flags
4153
+ * support through ioctl().
4154
+ */
4155
+ static int
4156
+ set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
4157
+ mode_t mode, unsigned long set, unsigned long clear)
4158
+ {
4159
+ (void)a; /* UNUSED */
4160
+ (void)fd; /* UNUSED */
4161
+ (void)name; /* UNUSED */
4162
+ (void)mode; /* UNUSED */
4163
+ (void)set; /* UNUSED */
4164
+ (void)clear; /* UNUSED */
4165
+ return (ARCHIVE_OK);
4166
+ }
4167
+
4168
+ #endif /* __linux */
4169
+
4170
+ #ifndef HAVE_COPYFILE_H
4171
+ /* Default is to simply drop Mac extended metadata. */
4172
+ static int
4173
+ set_mac_metadata(struct archive_write_disk *a, const char *pathname,
4174
+ const void *metadata, size_t metadata_size)
4175
+ {
4176
+ (void)a; /* UNUSED */
4177
+ (void)pathname; /* UNUSED */
4178
+ (void)metadata; /* UNUSED */
4179
+ (void)metadata_size; /* UNUSED */
4180
+ return (ARCHIVE_OK);
4181
+ }
4182
+
4183
+ static int
4184
+ fixup_appledouble(struct archive_write_disk *a, const char *pathname)
4185
+ {
4186
+ (void)a; /* UNUSED */
4187
+ (void)pathname; /* UNUSED */
4188
+ return (ARCHIVE_OK);
4189
+ }
4190
+ #else
4191
+
4192
+ /*
4193
+ * On Mac OS, we use copyfile() to unpack the metadata and
4194
+ * apply it to the target file.
4195
+ */
4196
+
4197
+ #if defined(HAVE_SYS_XATTR_H)
4198
+ static int
4199
+ copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd)
4200
+ {
4201
+ ssize_t xattr_size;
4202
+ char *xattr_names = NULL, *xattr_val = NULL;
4203
+ int ret = ARCHIVE_OK, xattr_i;
4204
+
4205
+ xattr_size = flistxattr(tmpfd, NULL, 0, 0);
4206
+ if (xattr_size == -1) {
4207
+ archive_set_error(&a->archive, errno,
4208
+ "Failed to read metadata(xattr)");
4209
+ ret = ARCHIVE_WARN;
4210
+ goto exit_xattr;
4211
+ }
4212
+ xattr_names = malloc(xattr_size);
4213
+ if (xattr_names == NULL) {
4214
+ archive_set_error(&a->archive, ENOMEM,
4215
+ "Can't allocate memory for metadata(xattr)");
4216
+ ret = ARCHIVE_FATAL;
4217
+ goto exit_xattr;
4218
+ }
4219
+ xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0);
4220
+ if (xattr_size == -1) {
4221
+ archive_set_error(&a->archive, errno,
4222
+ "Failed to read metadata(xattr)");
4223
+ ret = ARCHIVE_WARN;
4224
+ goto exit_xattr;
4225
+ }
4226
+ for (xattr_i = 0; xattr_i < xattr_size;
4227
+ xattr_i += strlen(xattr_names + xattr_i) + 1) {
4228
+ char *p;
4229
+ ssize_t s;
4230
+ int f;
4231
+
4232
+ s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0);
4233
+ if (s == -1) {
4234
+ archive_set_error(&a->archive, errno,
4235
+ "Failed to get metadata(xattr)");
4236
+ ret = ARCHIVE_WARN;
4237
+ goto exit_xattr;
4238
+ }
4239
+ p = realloc(xattr_val, s);
4240
+ if (p == NULL) {
4241
+ archive_set_error(&a->archive, ENOMEM,
4242
+ "Failed to get metadata(xattr)");
4243
+ ret = ARCHIVE_WARN;
4244
+ goto exit_xattr;
4245
+ }
4246
+ xattr_val = p;
4247
+ s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0);
4248
+ if (s == -1) {
4249
+ archive_set_error(&a->archive, errno,
4250
+ "Failed to get metadata(xattr)");
4251
+ ret = ARCHIVE_WARN;
4252
+ goto exit_xattr;
4253
+ }
4254
+ f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0);
4255
+ if (f == -1) {
4256
+ archive_set_error(&a->archive, errno,
4257
+ "Failed to get metadata(xattr)");
4258
+ ret = ARCHIVE_WARN;
4259
+ goto exit_xattr;
4260
+ }
4261
+ }
4262
+ exit_xattr:
4263
+ free(xattr_names);
4264
+ free(xattr_val);
4265
+ return (ret);
4266
+ }
4267
+ #endif
4268
+
4269
+ static int
4270
+ copy_acls(struct archive_write_disk *a, int tmpfd, int dffd)
4271
+ {
4272
+ #ifndef HAVE_SYS_ACL_H
4273
+ return 0;
4274
+ #else
4275
+ acl_t acl, dfacl = NULL;
4276
+ int acl_r, ret = ARCHIVE_OK;
4277
+
4278
+ acl = acl_get_fd(tmpfd);
4279
+ if (acl == NULL) {
4280
+ if (errno == ENOENT)
4281
+ /* There are not any ACLs. */
4282
+ return (ret);
4283
+ archive_set_error(&a->archive, errno,
4284
+ "Failed to get metadata(acl)");
4285
+ ret = ARCHIVE_WARN;
4286
+ goto exit_acl;
4287
+ }
4288
+ dfacl = acl_dup(acl);
4289
+ acl_r = acl_set_fd(dffd, dfacl);
4290
+ if (acl_r == -1) {
4291
+ archive_set_error(&a->archive, errno,
4292
+ "Failed to get metadata(acl)");
4293
+ ret = ARCHIVE_WARN;
4294
+ goto exit_acl;
4295
+ }
4296
+ exit_acl:
4297
+ if (acl)
4298
+ acl_free(acl);
4299
+ if (dfacl)
4300
+ acl_free(dfacl);
4301
+ return (ret);
4302
+ #endif
4303
+ }
4304
+
4305
+ static int
4306
+ create_tempdatafork(struct archive_write_disk *a, const char *pathname)
4307
+ {
4308
+ struct archive_string tmpdatafork;
4309
+ int tmpfd;
4310
+
4311
+ archive_string_init(&tmpdatafork);
4312
+ archive_strcpy(&tmpdatafork, pathname);
4313
+ archive_string_dirname(&tmpdatafork);
4314
+ archive_strcat(&tmpdatafork, "/tar.XXXXXXXX");
4315
+ tmpfd = __archive_mkstemp(tmpdatafork.s);
4316
+ if (tmpfd < 0) {
4317
+ archive_set_error(&a->archive, errno,
4318
+ "Failed to mkstemp");
4319
+ archive_string_free(&tmpdatafork);
4320
+ return (-1);
4321
+ }
4322
+ if (copyfile(pathname, tmpdatafork.s, 0,
4323
+ COPYFILE_UNPACK | COPYFILE_NOFOLLOW
4324
+ | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
4325
+ archive_set_error(&a->archive, errno,
4326
+ "Failed to restore metadata");
4327
+ close(tmpfd);
4328
+ tmpfd = -1;
4329
+ }
4330
+ unlink(tmpdatafork.s);
4331
+ archive_string_free(&tmpdatafork);
4332
+ return (tmpfd);
4333
+ }
4334
+
4335
+ static int
4336
+ copy_metadata(struct archive_write_disk *a, const char *metadata,
4337
+ const char *datafork, int datafork_compressed)
4338
+ {
4339
+ int ret = ARCHIVE_OK;
4340
+
4341
+ if (datafork_compressed) {
4342
+ int dffd, tmpfd;
4343
+
4344
+ tmpfd = create_tempdatafork(a, metadata);
4345
+ if (tmpfd == -1)
4346
+ return (ARCHIVE_WARN);
4347
+
4348
+ /*
4349
+ * Do not open the data fork compressed by HFS+ compression
4350
+ * with at least a writing mode(O_RDWR or O_WRONLY). it
4351
+ * makes the data fork uncompressed.
4352
+ */
4353
+ dffd = open(datafork, O_SYMLINK);
4354
+ if (dffd == -1) {
4355
+ archive_set_error(&a->archive, errno,
4356
+ "Failed to open the data fork for metadata");
4357
+ close(tmpfd);
4358
+ return (ARCHIVE_WARN);
4359
+ }
4360
+
4361
+ #if defined(HAVE_SYS_XATTR_H)
4362
+ ret = copy_xattrs(a, tmpfd, dffd);
4363
+ if (ret == ARCHIVE_OK)
4364
+ #endif
4365
+ ret = copy_acls(a, tmpfd, dffd);
4366
+ close(tmpfd);
4367
+ close(dffd);
4368
+ } else {
4369
+ if (copyfile(metadata, datafork, 0,
4370
+ COPYFILE_UNPACK | COPYFILE_NOFOLLOW
4371
+ | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
4372
+ archive_set_error(&a->archive, errno,
4373
+ "Failed to restore metadata");
4374
+ ret = ARCHIVE_WARN;
4375
+ }
4376
+ }
4377
+ return (ret);
4378
+ }
4379
+
4380
+ static int
4381
+ set_mac_metadata(struct archive_write_disk *a, const char *pathname,
4382
+ const void *metadata, size_t metadata_size)
4383
+ {
4384
+ struct archive_string tmp;
4385
+ ssize_t written;
4386
+ int fd;
4387
+ int ret = ARCHIVE_OK;
4388
+
4389
+ /* This would be simpler if copyfile() could just accept the
4390
+ * metadata as a block of memory; then we could sidestep this
4391
+ * silly dance of writing the data to disk just so that
4392
+ * copyfile() can read it back in again. */
4393
+ archive_string_init(&tmp);
4394
+ archive_strcpy(&tmp, pathname);
4395
+ archive_string_dirname(&tmp);
4396
+ archive_strcat(&tmp, "/tar.XXXXXXXX");
4397
+ fd = __archive_mkstemp(tmp.s);
4398
+
4399
+ if (fd < 0) {
4400
+ archive_set_error(&a->archive, errno,
4401
+ "Failed to restore metadata");
4402
+ archive_string_free(&tmp);
4403
+ return (ARCHIVE_WARN);
4404
+ }
4405
+ written = write(fd, metadata, metadata_size);
4406
+ close(fd);
4407
+ if ((size_t)written != metadata_size) {
4408
+ archive_set_error(&a->archive, errno,
4409
+ "Failed to restore metadata");
4410
+ ret = ARCHIVE_WARN;
4411
+ } else {
4412
+ int compressed;
4413
+
4414
+ #if defined(UF_COMPRESSED)
4415
+ if ((a->todo & TODO_HFS_COMPRESSION) != 0 &&
4416
+ (ret = lazy_stat(a)) == ARCHIVE_OK)
4417
+ compressed = a->st.st_flags & UF_COMPRESSED;
4418
+ else
4419
+ #endif
4420
+ compressed = 0;
4421
+ ret = copy_metadata(a, tmp.s, pathname, compressed);
4422
+ }
4423
+ unlink(tmp.s);
4424
+ archive_string_free(&tmp);
4425
+ return (ret);
4426
+ }
4427
+
4428
+ static int
4429
+ fixup_appledouble(struct archive_write_disk *a, const char *pathname)
4430
+ {
4431
+ char buff[8];
4432
+ struct stat st;
4433
+ const char *p;
4434
+ struct archive_string datafork;
4435
+ int fd = -1, ret = ARCHIVE_OK;
4436
+
4437
+ archive_string_init(&datafork);
4438
+ /* Check if the current file name is a type of the resource
4439
+ * fork file. */
4440
+ p = strrchr(pathname, '/');
4441
+ if (p == NULL)
4442
+ p = pathname;
4443
+ else
4444
+ p++;
4445
+ if (p[0] != '.' || p[1] != '_')
4446
+ goto skip_appledouble;
4447
+
4448
+ /*
4449
+ * Check if the data fork file exists.
4450
+ *
4451
+ * TODO: Check if this write disk object has handled it.
4452
+ */
4453
+ archive_strncpy(&datafork, pathname, p - pathname);
4454
+ archive_strcat(&datafork, p + 2);
4455
+ #ifdef HAVE_LSTAT
4456
+ if (lstat(datafork.s, &st) == -1)
4457
+ goto skip_appledouble;
4458
+ #else
4459
+ if (la_stat(datafork.s, &st) == -1)
4460
+ goto skip_appledouble;
4461
+ #endif
4462
+ if (!S_ISREG(st.st_mode) &&
4463
+ !S_ISDIR(st.st_mode) &&
4464
+ !S_ISLNK(st.st_mode))
4465
+ goto skip_appledouble;
4466
+
4467
+ /*
4468
+ * Check if the file is in the AppleDouble form.
4469
+ */
4470
+ fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC);
4471
+ __archive_ensure_cloexec_flag(fd);
4472
+ if (fd == -1) {
4473
+ archive_set_error(&a->archive, errno,
4474
+ "Failed to open a restoring file");
4475
+ ret = ARCHIVE_WARN;
4476
+ goto skip_appledouble;
4477
+ }
4478
+ if (read(fd, buff, 8) == -1) {
4479
+ archive_set_error(&a->archive, errno,
4480
+ "Failed to read a restoring file");
4481
+ close(fd);
4482
+ ret = ARCHIVE_WARN;
4483
+ goto skip_appledouble;
4484
+ }
4485
+ close(fd);
4486
+ /* Check AppleDouble Magic Code. */
4487
+ if (archive_be32dec(buff) != 0x00051607)
4488
+ goto skip_appledouble;
4489
+ /* Check AppleDouble Version. */
4490
+ if (archive_be32dec(buff+4) != 0x00020000)
4491
+ goto skip_appledouble;
4492
+
4493
+ ret = copy_metadata(a, pathname, datafork.s,
4494
+ #if defined(UF_COMPRESSED)
4495
+ st.st_flags & UF_COMPRESSED);
4496
+ #else
4497
+ 0);
4498
+ #endif
4499
+ if (ret == ARCHIVE_OK) {
4500
+ unlink(pathname);
4501
+ ret = ARCHIVE_EOF;
4502
+ }
4503
+ skip_appledouble:
4504
+ archive_string_free(&datafork);
4505
+ return (ret);
4506
+ }
4507
+ #endif
4508
+
4509
+ #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
4510
+ /*
4511
+ * Restore extended attributes - Linux, Darwin and AIX implementations:
4512
+ * AIX' ea interface is syntaxwise identical to the Linux xattr interface.
4513
+ */
4514
+ static int
4515
+ set_xattrs(struct archive_write_disk *a)
4516
+ {
4517
+ struct archive_entry *entry = a->entry;
4518
+ struct archive_string errlist;
4519
+ int ret = ARCHIVE_OK;
4520
+ int i = archive_entry_xattr_reset(entry);
4521
+ short fail = 0;
4522
+
4523
+ archive_string_init(&errlist);
4524
+
4525
+ while (i--) {
4526
+ const char *name;
4527
+ const void *value;
4528
+ size_t size;
4529
+ int e;
4530
+
4531
+ archive_entry_xattr_next(entry, &name, &value, &size);
4532
+
4533
+ if (name == NULL)
4534
+ continue;
4535
+ #if ARCHIVE_XATTR_LINUX
4536
+ /* Linux: quietly skip POSIX.1e ACL extended attributes */
4537
+ if (strncmp(name, "system.", 7) == 0 &&
4538
+ (strcmp(name + 7, "posix_acl_access") == 0 ||
4539
+ strcmp(name + 7, "posix_acl_default") == 0))
4540
+ continue;
4541
+ if (strncmp(name, "trusted.SGI_", 12) == 0 &&
4542
+ (strcmp(name + 12, "ACL_DEFAULT") == 0 ||
4543
+ strcmp(name + 12, "ACL_FILE") == 0))
4544
+ continue;
4545
+
4546
+ /* Linux: xfsroot namespace is obsolete and unsupported */
4547
+ if (strncmp(name, "xfsroot.", 8) == 0) {
4548
+ fail = 1;
4549
+ archive_strcat(&errlist, name);
4550
+ archive_strappend_char(&errlist, ' ');
4551
+ continue;
4552
+ }
4553
+ #endif
4554
+
4555
+ if (a->fd >= 0) {
4556
+ #if ARCHIVE_XATTR_LINUX
4557
+ e = fsetxattr(a->fd, name, value, size, 0);
4558
+ #elif ARCHIVE_XATTR_DARWIN
4559
+ e = fsetxattr(a->fd, name, value, size, 0, 0);
4560
+ #elif ARCHIVE_XATTR_AIX
4561
+ e = fsetea(a->fd, name, value, size, 0);
4562
+ #endif
4563
+ } else {
4564
+ #if ARCHIVE_XATTR_LINUX
4565
+ e = lsetxattr(archive_entry_pathname(entry),
4566
+ name, value, size, 0);
4567
+ #elif ARCHIVE_XATTR_DARWIN
4568
+ e = setxattr(archive_entry_pathname(entry),
4569
+ name, value, size, 0, XATTR_NOFOLLOW);
4570
+ #elif ARCHIVE_XATTR_AIX
4571
+ e = lsetea(archive_entry_pathname(entry),
4572
+ name, value, size, 0);
4573
+ #endif
4574
+ }
4575
+ if (e == -1) {
4576
+ ret = ARCHIVE_WARN;
4577
+ archive_strcat(&errlist, name);
4578
+ archive_strappend_char(&errlist, ' ');
4579
+ if (errno != ENOTSUP && errno != ENOSYS)
4580
+ fail = 1;
4581
+ }
4582
+ }
4583
+
4584
+ if (ret == ARCHIVE_WARN) {
4585
+ if (fail && errlist.length > 0) {
4586
+ errlist.length--;
4587
+ errlist.s[errlist.length] = '\0';
4588
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4589
+ "Cannot restore extended attributes: %s",
4590
+ errlist.s);
4591
+ } else
4592
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4593
+ "Cannot restore extended "
4594
+ "attributes on this file system");
4595
+ }
4596
+
4597
+ archive_string_free(&errlist);
4598
+ return (ret);
4599
+ }
4600
+ #elif ARCHIVE_XATTR_FREEBSD
4601
+ /*
4602
+ * Restore extended attributes - FreeBSD implementation
4603
+ */
4604
+ static int
4605
+ set_xattrs(struct archive_write_disk *a)
4606
+ {
4607
+ struct archive_entry *entry = a->entry;
4608
+ struct archive_string errlist;
4609
+ int ret = ARCHIVE_OK;
4610
+ int i = archive_entry_xattr_reset(entry);
4611
+ short fail = 0;
4612
+
4613
+ archive_string_init(&errlist);
4614
+
4615
+ while (i--) {
4616
+ const char *name;
4617
+ const void *value;
4618
+ size_t size;
4619
+ archive_entry_xattr_next(entry, &name, &value, &size);
4620
+ if (name != NULL) {
4621
+ int e;
4622
+ int namespace;
4623
+
4624
+ namespace = EXTATTR_NAMESPACE_USER;
4625
+
4626
+ if (strncmp(name, "user.", 5) == 0) {
4627
+ /* "user." attributes go to user namespace */
4628
+ name += 5;
4629
+ namespace = EXTATTR_NAMESPACE_USER;
4630
+ } else if (strncmp(name, "system.", 7) == 0) {
4631
+ name += 7;
4632
+ namespace = EXTATTR_NAMESPACE_SYSTEM;
4633
+ if (!strcmp(name, "nfs4.acl") ||
4634
+ !strcmp(name, "posix1e.acl_access") ||
4635
+ !strcmp(name, "posix1e.acl_default"))
4636
+ continue;
4637
+ } else {
4638
+ /* Other namespaces are unsupported */
4639
+ archive_strcat(&errlist, name);
4640
+ archive_strappend_char(&errlist, ' ');
4641
+ fail = 1;
4642
+ ret = ARCHIVE_WARN;
4643
+ continue;
4644
+ }
4645
+
4646
+ if (a->fd >= 0) {
4647
+ /*
4648
+ * On FreeBSD, extattr_set_fd does not
4649
+ * return the same as
4650
+ * extattr_set_file. It returns zero
4651
+ * on success, non-zero on failure.
4652
+ *
4653
+ * We can detect the failure by
4654
+ * manually setting errno prior to the
4655
+ * call and checking after.
4656
+ *
4657
+ * If errno remains zero, fake the
4658
+ * return value by setting e to size.
4659
+ *
4660
+ * This is a hack for now until I
4661
+ * (Shawn Webb) get FreeBSD to fix the
4662
+ * issue, if that's even possible.
4663
+ */
4664
+ errno = 0;
4665
+ e = extattr_set_fd(a->fd, namespace, name,
4666
+ value, size);
4667
+ if (e == 0 && errno == 0) {
4668
+ e = size;
4669
+ }
4670
+ } else {
4671
+ e = extattr_set_link(
4672
+ archive_entry_pathname(entry), namespace,
4673
+ name, value, size);
4674
+ }
4675
+ if (e != (int)size) {
4676
+ archive_strcat(&errlist, name);
4677
+ archive_strappend_char(&errlist, ' ');
4678
+ ret = ARCHIVE_WARN;
4679
+ if (errno != ENOTSUP && errno != ENOSYS)
4680
+ fail = 1;
4681
+ }
4682
+ }
4683
+ }
4684
+
4685
+ if (ret == ARCHIVE_WARN) {
4686
+ if (fail && errlist.length > 0) {
4687
+ errlist.length--;
4688
+ errlist.s[errlist.length] = '\0';
4689
+
4690
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4691
+ "Cannot restore extended attributes: %s",
4692
+ errlist.s);
4693
+ } else
4694
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4695
+ "Cannot restore extended "
4696
+ "attributes on this file system");
4697
+ }
4698
+
4699
+ archive_string_free(&errlist);
4700
+ return (ret);
4701
+ }
4702
+ #else
4703
+ /*
4704
+ * Restore extended attributes - stub implementation for unsupported systems
4705
+ */
4706
+ static int
4707
+ set_xattrs(struct archive_write_disk *a)
4708
+ {
4709
+ /* If there aren't any extended attributes, then it's okay not
4710
+ * to extract them, otherwise, issue a single warning. */
4711
+ if (archive_entry_xattr_count(a->entry) != 0 && !a->warning_done) {
4712
+ a->warning_done = 1;
4713
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4714
+ "Cannot restore extended attributes on this system");
4715
+ return (ARCHIVE_WARN);
4716
+ }
4717
+ /* Warning was already emitted; suppress further warnings. */
4718
+ return (ARCHIVE_OK);
4719
+ }
4720
+ #endif
4721
+
4722
+ /*
4723
+ * Test if file on disk is older than entry.
4724
+ */
4725
+ static int
4726
+ older(struct stat *st, struct archive_entry *entry)
4727
+ {
4728
+ /* First, test the seconds and return if we have a definite answer. */
4729
+ /* Definitely older. */
4730
+ if (to_int64_time(st->st_mtime) < to_int64_time(archive_entry_mtime(entry)))
4731
+ return (1);
4732
+ /* Definitely younger. */
4733
+ if (to_int64_time(st->st_mtime) > to_int64_time(archive_entry_mtime(entry)))
4734
+ return (0);
4735
+ /* If this platform supports fractional seconds, try those. */
4736
+ #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
4737
+ /* Definitely older. */
4738
+ if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
4739
+ return (1);
4740
+ #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
4741
+ /* Definitely older. */
4742
+ if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
4743
+ return (1);
4744
+ #elif HAVE_STRUCT_STAT_ST_MTIME_N
4745
+ /* older. */
4746
+ if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
4747
+ return (1);
4748
+ #elif HAVE_STRUCT_STAT_ST_UMTIME
4749
+ /* older. */
4750
+ if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
4751
+ return (1);
4752
+ #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
4753
+ /* older. */
4754
+ if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
4755
+ return (1);
4756
+ #else
4757
+ /* This system doesn't have high-res timestamps. */
4758
+ #endif
4759
+ /* Same age or newer, so not older. */
4760
+ return (0);
4761
+ }
4762
+
4763
+ #ifndef ARCHIVE_ACL_SUPPORT
4764
+ int
4765
+ archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
4766
+ struct archive_acl *abstract_acl, __LA_MODE_T mode)
4767
+ {
4768
+ (void)a; /* UNUSED */
4769
+ (void)fd; /* UNUSED */
4770
+ (void)name; /* UNUSED */
4771
+ (void)abstract_acl; /* UNUSED */
4772
+ (void)mode; /* UNUSED */
4773
+ return (ARCHIVE_OK);
4774
+ }
4775
+ #endif
4776
+
4777
+ /*
4778
+ * Close the file descriptor if one is open.
4779
+ */
4780
+ static void close_file_descriptor(struct archive_write_disk* a)
4781
+ {
4782
+ if (a->fd >= 0) {
4783
+ close(a->fd);
4784
+ a->fd = -1;
4785
+ }
4786
+ }
4787
+
4788
+
4789
+ #endif /* !_WIN32 || __CYGWIN__ */