@papyrus-sdk/engine-native 0.2.22-beta.9.epub.2 → 0.2.22

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 (276) hide show
  1. package/PapyrusNativeEngine.podspec +45 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/cpp/CMakeLists.txt +137 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  5. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  6. package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
  7. package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +491 -0
  10. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +38 -0
  12. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +305 -35
  13. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  15. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +195 -5
  16. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +20 -1
  17. package/android/src/main/java/com/papyrus/engine/PapyrusSelectionMenuIcon.java +48 -0
  18. package/android/src/main/java/com/papyrus/engine/PapyrusTextAnnotationModel.java +22 -0
  19. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +661 -0
  20. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +49 -0
  21. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  22. package/dist/index.d.mts +614 -4
  23. package/dist/index.d.ts +614 -4
  24. package/dist/index.js +484 -8
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +477 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/ios/PapyrusComicDocumentView.h +21 -0
  29. package/ios/PapyrusComicDocumentView.m +554 -0
  30. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  31. package/ios/PapyrusEngineStore.h +3 -0
  32. package/ios/PapyrusEngineStore.m +32 -6
  33. package/ios/PapyrusNativeEngine.m +684 -5
  34. package/ios/PapyrusPageRotationRegistry.h +29 -0
  35. package/ios/PapyrusPageRotationRegistry.m +179 -0
  36. package/ios/PapyrusPageView.m +38 -11
  37. package/ios/PapyrusPdfDocumentView.h +55 -0
  38. package/ios/PapyrusPdfDocumentView.m +2370 -0
  39. package/ios/PapyrusPdfDocumentViewManager.m +61 -0
  40. package/ios/PapyrusPdfPageTheme.h +15 -0
  41. package/ios/PapyrusPdfPageTheme.m +218 -0
  42. package/ios/PapyrusSelectionMenu.h +27 -0
  43. package/ios/PapyrusTextAnnotationRange.h +2 -0
  44. package/ios/PapyrusTextAnnotationRange.m +29 -0
  45. package/ios/PapyrusTextDocumentView.h +32 -0
  46. package/ios/PapyrusTextDocumentView.m +518 -0
  47. package/ios/PapyrusTextDocumentViewManager.m +34 -0
  48. package/ios/PapyrusTextStore.h +13 -0
  49. package/ios/PapyrusTextStore.m +73 -0
  50. package/package.json +10 -8
  51. package/react-native.config.js +1 -1
  52. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  53. package/third_party_licenses/libarchive-COPYING +65 -0
  54. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  55. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  56. package/vendor/libarchive/README.papyrus.md +15 -0
  57. package/vendor/libarchive/android.h +192 -0
  58. package/vendor/libarchive/config/PapyrusArchiveConfig.h +71 -0
  59. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  60. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  61. package/vendor/libarchive/libarchive/archive.h +1266 -0
  62. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  63. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  64. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  65. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  66. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  67. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  68. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  69. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  70. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  71. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  72. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  73. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  74. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  75. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  76. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  77. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  78. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  79. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  80. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  81. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  82. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  83. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  84. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  85. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  86. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  87. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  88. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  89. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  90. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  91. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  92. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  93. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  94. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  95. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  96. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  97. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  98. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  99. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  100. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  101. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  102. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  103. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  104. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  105. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  106. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  107. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  108. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  109. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  110. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  111. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  112. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  113. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  114. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  115. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  116. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  117. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  118. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  119. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  120. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  121. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  122. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  123. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  124. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  125. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  126. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  127. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  128. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  129. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  130. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  131. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  132. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  133. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  134. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  135. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  136. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  137. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  138. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  139. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  140. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  141. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  142. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  143. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  144. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  145. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  146. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  147. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  148. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  149. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  150. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  151. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  152. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  153. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  154. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  155. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  156. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  180. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  181. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  182. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  183. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  184. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  185. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  186. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  187. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  188. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  189. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  190. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  191. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  192. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  193. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  194. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  195. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  196. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  197. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  198. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  199. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  200. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  201. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  202. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  209. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  210. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  211. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  212. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  213. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  214. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  215. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  216. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  217. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  218. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  219. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  220. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  221. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  222. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  223. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  224. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  225. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  226. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  227. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  228. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  229. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  230. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  231. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  232. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  233. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  234. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  235. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  236. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  253. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  254. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  255. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  256. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  257. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  258. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  259. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  260. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  261. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  262. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  263. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  264. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  265. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  266. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  267. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  268. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  269. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  270. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  271. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  272. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  273. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  274. package/vendor/libarchive/libarchive/tar.5 +947 -0
  275. package/vendor/libarchive/libarchive/xxhash.c +531 -0
  276. package/ios/PapyrusNativeEngine.podspec +0 -15
@@ -0,0 +1,2603 @@
1
+ /*-
2
+ * Copyright (c) 2003-2009 Tim Kientzle
3
+ * Copyright (c) 2010-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
+ #include "archive_platform.h"
28
+
29
+ #if defined(_WIN32) && !defined(__CYGWIN__)
30
+
31
+ #ifdef HAVE_ERRNO_H
32
+ #include <errno.h>
33
+ #endif
34
+ #ifdef HAVE_STDLIB_H
35
+ #include <stdlib.h>
36
+ #endif
37
+ #include <winioctl.h>
38
+
39
+ #include "archive.h"
40
+ #include "archive_entry.h"
41
+ #include "archive_integer.h"
42
+ #include "archive_private.h"
43
+ #include "archive_read_disk_private.h"
44
+ #include "archive_string.h"
45
+ #include "archive_time_private.h"
46
+
47
+ #ifndef O_BINARY
48
+ #define O_BINARY 0
49
+ #endif
50
+ #ifndef IO_REPARSE_TAG_SYMLINK
51
+ /* Old SDKs do not provide IO_REPARSE_TAG_SYMLINK */
52
+ #define IO_REPARSE_TAG_SYMLINK 0xA000000CL
53
+ #endif
54
+ /* To deal with absolute symlink issues */
55
+ #define START_ABSOLUTE_SYMLINK_REPARSE L"\\??\\"
56
+
57
+ #define MAX_FILESYSTEM_ID 1000000
58
+
59
+ /*-
60
+ * This is a new directory-walking system that addresses a number
61
+ * of problems I've had with fts(3). In particular, it has no
62
+ * pathname-length limits (other than the size of 'int'), handles
63
+ * deep logical traversals, uses considerably less memory, and has
64
+ * an opaque interface (easier to modify in the future).
65
+ *
66
+ * Internally, it keeps a single list of "tree_entry" items that
67
+ * represent filesystem objects that require further attention.
68
+ * Non-directories are not kept in memory: they are pulled from
69
+ * readdir(), returned to the client, then freed as soon as possible.
70
+ * Any directory entry to be traversed gets pushed onto the stack.
71
+ *
72
+ * There is surprisingly little information that needs to be kept for
73
+ * each item on the stack. Just the name, depth (represented here as the
74
+ * string length of the parent directory's pathname), and some markers
75
+ * indicating how to get back to the parent (via chdir("..") for a
76
+ * regular dir or via fchdir(2) for a symlink).
77
+ */
78
+
79
+ struct restore_time {
80
+ const wchar_t *full_path;
81
+ FILETIME lastWriteTime;
82
+ FILETIME lastAccessTime;
83
+ mode_t filetype;
84
+ };
85
+
86
+ struct tree_entry {
87
+ int depth;
88
+ struct tree_entry *next;
89
+ struct tree_entry *parent;
90
+ size_t full_path_dir_length;
91
+ struct archive_wstring name;
92
+ struct archive_wstring full_path;
93
+ size_t dirname_length;
94
+ int64_t dev;
95
+ int64_t ino;
96
+ int flags;
97
+ int filesystem_id;
98
+ /* How to restore time of a directory. */
99
+ struct restore_time restore_time;
100
+ };
101
+
102
+ struct filesystem {
103
+ int64_t dev;
104
+ int synthetic;
105
+ int remote;
106
+ DWORD bytesPerSector;
107
+ };
108
+
109
+ /* Definitions for tree_entry.flags bitmap. */
110
+ #define isDir 1 /* This entry is a regular directory. */
111
+ #define isDirLink 2 /* This entry is a symbolic link to a directory. */
112
+ #define needsFirstVisit 4 /* This is an initial entry. */
113
+ #define needsDescent 8 /* This entry needs to be previsited. */
114
+ #define needsOpen 16 /* This is a directory that needs to be opened. */
115
+ #define needsAscent 32 /* This entry needs to be postvisited. */
116
+
117
+ /*
118
+ * On Windows, "first visit" is handled as a pattern to be handed to
119
+ * _findfirst(). This is consistent with Windows conventions that
120
+ * file patterns are handled within the application. On Posix,
121
+ * "first visit" is just returned to the client.
122
+ */
123
+
124
+ #define MAX_OVERLAPPED 8
125
+ #define READ_BUFFER_SIZE (1024 * 64) /* Default to 64KB per https://technet.microsoft.com/en-us/library/cc938632.aspx */
126
+ #define DIRECT_IO 0/* Disabled */
127
+ #define ASYNC_IO 1/* Enabled */
128
+
129
+ /*
130
+ * Local data for this package.
131
+ */
132
+ struct tree {
133
+ struct tree_entry *stack;
134
+ struct tree_entry *current;
135
+ HANDLE d;
136
+ WIN32_FIND_DATAW _findData;
137
+ WIN32_FIND_DATAW *findData;
138
+ int flags;
139
+ int visit_type;
140
+ /* Error code from last failed operation. */
141
+ int tree_errno;
142
+
143
+ /* A full path with "\\?\" prefix. */
144
+ struct archive_wstring full_path;
145
+ size_t full_path_dir_length;
146
+ /* Dynamically-sized buffer for holding path */
147
+ struct archive_wstring path;
148
+
149
+ /* Last path element */
150
+ const wchar_t *basename;
151
+ /* Leading dir length */
152
+ size_t dirname_length;
153
+
154
+ int depth;
155
+ /* Whether this tree began as a wildcard search */
156
+ int rootwild;
157
+
158
+ BY_HANDLE_FILE_INFORMATION lst;
159
+ BY_HANDLE_FILE_INFORMATION st;
160
+ int descend;
161
+ /* How to restore time of a file. */
162
+ struct restore_time restore_time;
163
+
164
+ struct entry_sparse {
165
+ int64_t length;
166
+ int64_t offset;
167
+ } *sparse_list, *current_sparse;
168
+ int sparse_count;
169
+ int sparse_list_size;
170
+
171
+ char initial_symlink_mode;
172
+ char symlink_mode;
173
+ struct filesystem *current_filesystem;
174
+ struct filesystem *filesystem_table;
175
+ int initial_filesystem_id;
176
+ int current_filesystem_id;
177
+ int max_filesystem_id;
178
+ int allocated_filesystem;
179
+
180
+ HANDLE entry_fh;
181
+ int entry_eof;
182
+ int64_t entry_remaining_bytes;
183
+ int64_t entry_total;
184
+
185
+ int ol_idx_doing;
186
+ int ol_idx_done;
187
+ int ol_num_doing;
188
+ int ol_num_done;
189
+ int64_t ol_remaining_bytes;
190
+ int64_t ol_total;
191
+ struct la_overlapped {
192
+ OVERLAPPED ol;
193
+ struct archive * _a;
194
+ unsigned char *buff;
195
+ size_t buff_size;
196
+ int64_t offset;
197
+ size_t bytes_expected;
198
+ size_t bytes_transferred;
199
+ } ol[MAX_OVERLAPPED];
200
+ int direct_io;
201
+ int async_io;
202
+ };
203
+
204
+ #define bhfi_dev(bhfi) ((bhfi)->dwVolumeSerialNumber)
205
+ /* Treat FileIndex as i-node. We should remove a sequence number
206
+ * which is high-16-bits of nFileIndexHigh. */
207
+ #define bhfi_ino(bhfi) \
208
+ ((((int64_t)((bhfi)->nFileIndexHigh & 0x0000FFFFUL)) << 32) \
209
+ + (bhfi)->nFileIndexLow)
210
+
211
+ /* Definitions for tree.flags bitmap. */
212
+ #define hasStat 16 /* The st entry is valid. */
213
+ #define hasLstat 32 /* The lst entry is valid. */
214
+ #define needsRestoreTimes 128
215
+
216
+ static int
217
+ tree_dir_next_windows(struct tree *t, const wchar_t *pattern);
218
+
219
+ /* Initiate/terminate a tree traversal. */
220
+ static struct tree *tree_open(const wchar_t *, int, int);
221
+ static struct tree *tree_reopen(struct tree *, const wchar_t *, int);
222
+ static void tree_close(struct tree *);
223
+ static void tree_free(struct tree *);
224
+ static void tree_push(struct tree *, const wchar_t *, const wchar_t *,
225
+ int, int64_t, int64_t, struct restore_time *);
226
+
227
+ /*
228
+ * tree_next() returns Zero if there is no next entry, non-zero if
229
+ * there is. Note that directories are visited three times.
230
+ * Directories are always visited first as part of enumerating their
231
+ * parent; that is a "regular" visit. If tree_descend() is invoked at
232
+ * that time, the directory is added to a work list and will
233
+ * subsequently be visited two more times: once just after descending
234
+ * into the directory ("postdescent") and again just after ascending
235
+ * back to the parent ("postascent").
236
+ *
237
+ * TREE_ERROR_DIR is returned if the descent failed (because the
238
+ * directory couldn't be opened, for instance). This is returned
239
+ * instead of TREE_POSTDESCENT/TREE_POSTASCENT. TREE_ERROR_DIR is not a
240
+ * fatal error, but it does imply that the relevant subtree won't be
241
+ * visited. TREE_ERROR_FATAL is returned for an error that left the
242
+ * traversal completely hosed. Right now, this is only returned for
243
+ * chdir() failures during ascent.
244
+ */
245
+ #define TREE_REGULAR 1
246
+ #define TREE_POSTDESCENT 2
247
+ #define TREE_POSTASCENT 3
248
+ #define TREE_ERROR_DIR -1
249
+ #define TREE_ERROR_FATAL -2
250
+
251
+ static int tree_next(struct tree *);
252
+
253
+ /*
254
+ * Return information about the current entry.
255
+ */
256
+
257
+ /*
258
+ * The current full pathname, length of the full pathname, and a name
259
+ * that can be used to access the file. Because tree does use chdir
260
+ * extensively, the access path is almost never the same as the full
261
+ * current path.
262
+ *
263
+ */
264
+ static const wchar_t *tree_current_path(struct tree *);
265
+ static const wchar_t *tree_current_access_path(struct tree *);
266
+
267
+ /*
268
+ * Request the lstat() or stat() data for the current path. Since the
269
+ * tree package needs to do some of this anyway, and caches the
270
+ * results, you should take advantage of it here if you need it rather
271
+ * than make a redundant stat() or lstat() call of your own.
272
+ */
273
+ static const BY_HANDLE_FILE_INFORMATION *tree_current_stat(struct tree *);
274
+ static const BY_HANDLE_FILE_INFORMATION *tree_current_lstat(struct tree *);
275
+
276
+ /* The following functions use tricks to avoid a certain number of
277
+ * stat()/lstat() calls. */
278
+ /* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
279
+ static int tree_current_is_physical_dir(struct tree *);
280
+ /* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
281
+ static int tree_current_is_physical_link(struct tree *);
282
+ /* Instead of archive_entry_copy_stat for BY_HANDLE_FILE_INFORMATION */
283
+ static void tree_archive_entry_copy_bhfi(struct archive_entry *,
284
+ struct tree *, const BY_HANDLE_FILE_INFORMATION *);
285
+ /* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
286
+ static int tree_current_is_dir(struct tree *);
287
+ static int update_current_filesystem(struct archive_read_disk *a,
288
+ int64_t dev);
289
+ static int setup_current_filesystem(struct archive_read_disk *);
290
+ static int tree_target_is_same_as_parent(struct tree *,
291
+ const BY_HANDLE_FILE_INFORMATION *);
292
+
293
+ static int _archive_read_disk_open_w(struct archive *, const wchar_t *);
294
+ static int _archive_read_free(struct archive *);
295
+ static int _archive_read_close(struct archive *);
296
+ static int _archive_read_data_block(struct archive *,
297
+ const void **, size_t *, int64_t *);
298
+ static int _archive_read_next_header(struct archive *,
299
+ struct archive_entry **);
300
+ static int _archive_read_next_header2(struct archive *,
301
+ struct archive_entry *);
302
+ static const char *trivial_lookup_gname(void *, int64_t gid);
303
+ static const char *trivial_lookup_uname(void *, int64_t uid);
304
+ static int setup_sparse(struct archive_read_disk *, struct archive_entry *);
305
+ static int close_and_restore_time(HANDLE, struct tree *,
306
+ struct restore_time *);
307
+ static int setup_sparse_from_disk(struct archive_read_disk *,
308
+ struct archive_entry *, HANDLE);
309
+ static int la_linkname_from_handle(HANDLE, wchar_t **, int *);
310
+ static int la_linkname_from_pathw(const wchar_t *, wchar_t **, int *);
311
+ static void entry_symlink_from_pathw(struct archive_entry *,
312
+ const wchar_t *path);
313
+
314
+ typedef struct _REPARSE_DATA_BUFFER {
315
+ ULONG ReparseTag;
316
+ USHORT ReparseDataLength;
317
+ USHORT Reserved;
318
+ union {
319
+ struct {
320
+ USHORT SubstituteNameOffset;
321
+ USHORT SubstituteNameLength;
322
+ USHORT PrintNameOffset;
323
+ USHORT PrintNameLength;
324
+ ULONG Flags;
325
+ WCHAR PathBuffer[1];
326
+ } SymbolicLinkReparseBuffer;
327
+ struct {
328
+ USHORT SubstituteNameOffset;
329
+ USHORT SubstituteNameLength;
330
+ USHORT PrintNameOffset;
331
+ USHORT PrintNameLength;
332
+ WCHAR PathBuffer[1];
333
+ } MountPointReparseBuffer;
334
+ struct {
335
+ UCHAR DataBuffer[1];
336
+ } GenericReparseBuffer;
337
+ } DUMMYUNIONNAME;
338
+ } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
339
+
340
+ /*
341
+ * Reads the target of a symbolic link
342
+ *
343
+ * Returns 0 on success and -1 on failure
344
+ * outbuf is allocated in the function
345
+ */
346
+ static int
347
+ la_linkname_from_handle(HANDLE h, wchar_t **linkname, int *linktype)
348
+ {
349
+ DWORD inbytes;
350
+ REPARSE_DATA_BUFFER *buf;
351
+ BY_HANDLE_FILE_INFORMATION st;
352
+ size_t len;
353
+ BOOL ret;
354
+ BYTE *indata;
355
+ wchar_t *tbuf;
356
+
357
+ ret = GetFileInformationByHandle(h, &st);
358
+ if (ret == 0 ||
359
+ (st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
360
+ return (-1);
361
+ }
362
+
363
+ indata = malloc(MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
364
+ if (indata == NULL)
365
+ return (-1);
366
+ ret = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, indata,
367
+ 1024, &inbytes, NULL);
368
+ if (ret == 0) {
369
+ la_dosmaperr(GetLastError());
370
+ free(indata);
371
+ return (-1);
372
+ }
373
+
374
+ buf = (REPARSE_DATA_BUFFER *) indata;
375
+ if (buf->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
376
+ free(indata);
377
+ /* File is not a symbolic link */
378
+ errno = EINVAL;
379
+ return (-1);
380
+ }
381
+
382
+ len = buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
383
+ if (len <= 0) {
384
+ free(indata);
385
+ return (-1);
386
+ }
387
+
388
+ tbuf = malloc(len + sizeof(wchar_t));
389
+ if (tbuf == NULL) {
390
+ free(indata);
391
+ return (-1);
392
+ }
393
+
394
+ memcpy(tbuf, &((BYTE *)buf->SymbolicLinkReparseBuffer.PathBuffer)
395
+ [buf->SymbolicLinkReparseBuffer.SubstituteNameOffset], len);
396
+ free(indata);
397
+
398
+ tbuf[len / sizeof(wchar_t)] = L'\0';
399
+ if (wcsncmp(tbuf, START_ABSOLUTE_SYMLINK_REPARSE, 4) == 0) {
400
+ /* Absolute symlink, so we'll change the NT path into a verbatim one */
401
+ tbuf[1] = L'\\';
402
+ } else {
403
+ /* Relative symlink, so we can translate backslashes to slashes */
404
+ wchar_t *temp = tbuf;
405
+ do {
406
+ if (*temp == L'\\')
407
+ *temp = L'/';
408
+ temp++;
409
+ } while(*temp != L'\0');
410
+ }
411
+
412
+ *linkname = tbuf;
413
+
414
+ if ((st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
415
+ *linktype = AE_SYMLINK_TYPE_FILE;
416
+ else
417
+ *linktype = AE_SYMLINK_TYPE_DIRECTORY;
418
+
419
+ return (0);
420
+ }
421
+
422
+ /*
423
+ * Returns AE_SYMLINK_TYPE_FILE, AE_SYMLINK_TYPE_DIRECTORY or -1 on error
424
+ */
425
+ static int
426
+ la_linkname_from_pathw(const wchar_t *path, wchar_t **outbuf, int *linktype)
427
+ {
428
+ HANDLE h;
429
+ const DWORD flag = FILE_FLAG_BACKUP_SEMANTICS |
430
+ FILE_FLAG_OPEN_REPARSE_POINT;
431
+ int ret;
432
+
433
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
434
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
435
+ ZeroMemory(&createExParams, sizeof(createExParams));
436
+ createExParams.dwSize = sizeof(createExParams);
437
+ createExParams.dwFileFlags = flag;
438
+ h = CreateFile2(path, 0,
439
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
440
+ OPEN_EXISTING, &createExParams);
441
+ #else
442
+ h = CreateFileW(path, 0,
443
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
444
+ OPEN_EXISTING, flag, NULL);
445
+ #endif
446
+ if (h == INVALID_HANDLE_VALUE) {
447
+ la_dosmaperr(GetLastError());
448
+ return (-1);
449
+ }
450
+
451
+ ret = la_linkname_from_handle(h, outbuf, linktype);
452
+ CloseHandle(h);
453
+
454
+ return (ret);
455
+ }
456
+
457
+ static void
458
+ entry_symlink_from_pathw(struct archive_entry *entry, const wchar_t *path)
459
+ {
460
+ wchar_t *linkname = NULL;
461
+ int ret, linktype;
462
+
463
+ ret = la_linkname_from_pathw(path, &linkname, &linktype);
464
+ if (ret != 0)
465
+ return;
466
+ if (linktype >= 0) {
467
+ archive_entry_copy_symlink_w(entry, linkname);
468
+ archive_entry_set_symlink_type(entry, linktype);
469
+ }
470
+ free(linkname);
471
+
472
+ return;
473
+ }
474
+
475
+ static const struct archive_vtable
476
+ archive_read_disk_vtable = {
477
+ .archive_free = _archive_read_free,
478
+ .archive_close = _archive_read_close,
479
+ .archive_read_data_block = _archive_read_data_block,
480
+ .archive_read_next_header = _archive_read_next_header,
481
+ .archive_read_next_header2 = _archive_read_next_header2,
482
+ };
483
+
484
+ const char *
485
+ archive_read_disk_gname(struct archive *_a, la_int64_t gid)
486
+ {
487
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
488
+ if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
489
+ ARCHIVE_STATE_ANY, "archive_read_disk_gname"))
490
+ return (NULL);
491
+ if (a->lookup_gname == NULL)
492
+ return (NULL);
493
+ return ((*a->lookup_gname)(a->lookup_gname_data, gid));
494
+ }
495
+
496
+ const char *
497
+ archive_read_disk_uname(struct archive *_a, la_int64_t uid)
498
+ {
499
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
500
+ if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
501
+ ARCHIVE_STATE_ANY, "archive_read_disk_uname"))
502
+ return (NULL);
503
+ if (a->lookup_uname == NULL)
504
+ return (NULL);
505
+ return ((*a->lookup_uname)(a->lookup_uname_data, uid));
506
+ }
507
+
508
+ int
509
+ archive_read_disk_set_gname_lookup(struct archive *_a,
510
+ void *private_data,
511
+ const char * (*lookup_gname)(void *private, la_int64_t gid),
512
+ void (*cleanup_gname)(void *private))
513
+ {
514
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
515
+ archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
516
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
517
+
518
+ if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
519
+ (a->cleanup_gname)(a->lookup_gname_data);
520
+
521
+ a->lookup_gname = lookup_gname;
522
+ a->cleanup_gname = cleanup_gname;
523
+ a->lookup_gname_data = private_data;
524
+ return (ARCHIVE_OK);
525
+ }
526
+
527
+ int
528
+ archive_read_disk_set_uname_lookup(struct archive *_a,
529
+ void *private_data,
530
+ const char * (*lookup_uname)(void *private, int64_t uid),
531
+ void (*cleanup_uname)(void *private))
532
+ {
533
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
534
+ archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
535
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
536
+
537
+ if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
538
+ (a->cleanup_uname)(a->lookup_uname_data);
539
+
540
+ a->lookup_uname = lookup_uname;
541
+ a->cleanup_uname = cleanup_uname;
542
+ a->lookup_uname_data = private_data;
543
+ return (ARCHIVE_OK);
544
+ }
545
+
546
+ /*
547
+ * Create a new archive_read_disk object and initialize it with global state.
548
+ */
549
+ struct archive *
550
+ archive_read_disk_new(void)
551
+ {
552
+ struct archive_read_disk *a;
553
+
554
+ a = calloc(1, sizeof(*a));
555
+ if (a == NULL)
556
+ return (NULL);
557
+ a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
558
+ a->archive.state = ARCHIVE_STATE_NEW;
559
+ a->archive.vtable = &archive_read_disk_vtable;
560
+ a->entry = archive_entry_new2(&a->archive);
561
+ if (a->entry == NULL) {
562
+ free(a);
563
+ return (NULL);
564
+ }
565
+ a->lookup_uname = trivial_lookup_uname;
566
+ a->lookup_gname = trivial_lookup_gname;
567
+ a->flags = ARCHIVE_READDISK_MAC_COPYFILE;
568
+ return (&a->archive);
569
+ }
570
+
571
+ static int
572
+ _archive_read_free(struct archive *_a)
573
+ {
574
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
575
+ int r;
576
+
577
+ if (_a == NULL)
578
+ return (ARCHIVE_OK);
579
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
580
+ ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
581
+
582
+ if (a->archive.state != ARCHIVE_STATE_CLOSED)
583
+ r = _archive_read_close(&a->archive);
584
+ else
585
+ r = ARCHIVE_OK;
586
+
587
+ tree_free(a->tree);
588
+ if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
589
+ (a->cleanup_gname)(a->lookup_gname_data);
590
+ if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
591
+ (a->cleanup_uname)(a->lookup_uname_data);
592
+ archive_string_free(&a->archive.error_string);
593
+ archive_entry_free(a->entry);
594
+ a->archive.magic = 0;
595
+ free(a);
596
+ return (r);
597
+ }
598
+
599
+ static int
600
+ _archive_read_close(struct archive *_a)
601
+ {
602
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
603
+
604
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
605
+ ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
606
+
607
+ if (a->archive.state != ARCHIVE_STATE_FATAL)
608
+ a->archive.state = ARCHIVE_STATE_CLOSED;
609
+
610
+ tree_close(a->tree);
611
+
612
+ return (ARCHIVE_OK);
613
+ }
614
+
615
+ static void
616
+ setup_symlink_mode(struct archive_read_disk *a, char symlink_mode,
617
+ char follow_symlinks)
618
+ {
619
+ a->symlink_mode = symlink_mode;
620
+ a->follow_symlinks = follow_symlinks;
621
+ if (a->tree != NULL) {
622
+ a->tree->initial_symlink_mode = a->symlink_mode;
623
+ a->tree->symlink_mode = a->symlink_mode;
624
+ }
625
+ }
626
+
627
+ int
628
+ archive_read_disk_set_symlink_logical(struct archive *_a)
629
+ {
630
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
631
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
632
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_logical");
633
+ setup_symlink_mode(a, 'L', 1);
634
+ return (ARCHIVE_OK);
635
+ }
636
+
637
+ int
638
+ archive_read_disk_set_symlink_physical(struct archive *_a)
639
+ {
640
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
641
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
642
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_physical");
643
+ setup_symlink_mode(a, 'P', 0);
644
+ return (ARCHIVE_OK);
645
+ }
646
+
647
+ int
648
+ archive_read_disk_set_symlink_hybrid(struct archive *_a)
649
+ {
650
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
651
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
652
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_symlink_hybrid");
653
+ setup_symlink_mode(a, 'H', 1);/* Follow symlinks initially. */
654
+ return (ARCHIVE_OK);
655
+ }
656
+
657
+ int
658
+ archive_read_disk_set_atime_restored(struct archive *_a)
659
+ {
660
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
661
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
662
+ ARCHIVE_STATE_ANY, "archive_read_disk_restore_atime");
663
+ a->flags |= ARCHIVE_READDISK_RESTORE_ATIME;
664
+ if (a->tree != NULL)
665
+ a->tree->flags |= needsRestoreTimes;
666
+ return (ARCHIVE_OK);
667
+ }
668
+
669
+ int
670
+ archive_read_disk_set_behavior(struct archive *_a, int flags)
671
+ {
672
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
673
+ int r = ARCHIVE_OK;
674
+
675
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
676
+ ARCHIVE_STATE_ANY, "archive_read_disk_honor_nodump");
677
+
678
+ a->flags = flags;
679
+
680
+ if (flags & ARCHIVE_READDISK_RESTORE_ATIME)
681
+ r = archive_read_disk_set_atime_restored(_a);
682
+ else {
683
+ if (a->tree != NULL)
684
+ a->tree->flags &= ~needsRestoreTimes;
685
+ }
686
+ return (r);
687
+ }
688
+
689
+ /*
690
+ * Trivial implementations of gname/uname lookup functions.
691
+ * These are normally overridden by the client, but these stub
692
+ * versions ensure that we always have something that works.
693
+ */
694
+ static const char *
695
+ trivial_lookup_gname(void *private_data, int64_t gid)
696
+ {
697
+ (void)private_data; /* UNUSED */
698
+ (void)gid; /* UNUSED */
699
+ return (NULL);
700
+ }
701
+
702
+ static const char *
703
+ trivial_lookup_uname(void *private_data, int64_t uid)
704
+ {
705
+ (void)private_data; /* UNUSED */
706
+ (void)uid; /* UNUSED */
707
+ return (NULL);
708
+ }
709
+
710
+ static int64_t
711
+ align_num_per_sector(struct tree *t, int64_t size)
712
+ {
713
+ int64_t surplus;
714
+
715
+ size += t->current_filesystem->bytesPerSector -1;
716
+ surplus = size % t->current_filesystem->bytesPerSector;
717
+ size -= surplus;
718
+ return (size);
719
+ }
720
+
721
+ static int
722
+ start_next_async_read(struct archive_read_disk *a, struct tree *t)
723
+ {
724
+ struct la_overlapped *olp;
725
+ DWORD buffbytes, rbytes;
726
+
727
+ if (t->ol_remaining_bytes == 0)
728
+ return (ARCHIVE_EOF);
729
+
730
+ olp = &(t->ol[t->ol_idx_doing]);
731
+ t->ol_idx_doing = (t->ol_idx_doing + 1) % MAX_OVERLAPPED;
732
+
733
+ /* Allocate read buffer. */
734
+ if (olp->buff == NULL) {
735
+ void *p;
736
+ size_t s = (size_t)align_num_per_sector(t, READ_BUFFER_SIZE);
737
+ p = VirtualAlloc(NULL, s, MEM_COMMIT, PAGE_READWRITE);
738
+ if (p == NULL) {
739
+ archive_set_error(&a->archive, ENOMEM,
740
+ "Couldn't allocate memory");
741
+ a->archive.state = ARCHIVE_STATE_FATAL;
742
+ return (ARCHIVE_FATAL);
743
+ }
744
+ olp->buff = p;
745
+ olp->buff_size = s;
746
+ olp->_a = &a->archive;
747
+ olp->ol.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
748
+ if (olp->ol.hEvent == NULL) {
749
+ la_dosmaperr(GetLastError());
750
+ archive_set_error(&a->archive, errno,
751
+ "CreateEvent failed");
752
+ a->archive.state = ARCHIVE_STATE_FATAL;
753
+ return (ARCHIVE_FATAL);
754
+ }
755
+ } else
756
+ ResetEvent(olp->ol.hEvent);
757
+
758
+ buffbytes = (DWORD)olp->buff_size;
759
+ if (buffbytes > t->current_sparse->length)
760
+ buffbytes = (DWORD)t->current_sparse->length;
761
+
762
+ /* Skip hole. */
763
+ if (t->current_sparse->offset > t->ol_total) {
764
+ t->ol_remaining_bytes -=
765
+ t->current_sparse->offset - t->ol_total;
766
+ }
767
+
768
+ olp->offset = t->current_sparse->offset;
769
+ olp->ol.Offset = (DWORD)(olp->offset & 0xffffffff);
770
+ olp->ol.OffsetHigh = (DWORD)(olp->offset >> 32);
771
+
772
+ if (t->ol_remaining_bytes > buffbytes) {
773
+ olp->bytes_expected = buffbytes;
774
+ t->ol_remaining_bytes -= buffbytes;
775
+ } else {
776
+ olp->bytes_expected = (size_t)t->ol_remaining_bytes;
777
+ t->ol_remaining_bytes = 0;
778
+ }
779
+ olp->bytes_transferred = 0;
780
+ t->current_sparse->offset += buffbytes;
781
+ t->current_sparse->length -= buffbytes;
782
+ t->ol_total = t->current_sparse->offset;
783
+ if (t->current_sparse->length == 0 && t->ol_remaining_bytes > 0)
784
+ t->current_sparse++;
785
+
786
+ if (!ReadFile(t->entry_fh, olp->buff, buffbytes, &rbytes, &(olp->ol))) {
787
+ DWORD lasterr;
788
+
789
+ lasterr = GetLastError();
790
+ if (lasterr == ERROR_HANDLE_EOF) {
791
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
792
+ "Reading file truncated");
793
+ a->archive.state = ARCHIVE_STATE_FATAL;
794
+ return (ARCHIVE_FATAL);
795
+ } else if (lasterr != ERROR_IO_PENDING) {
796
+ if (lasterr == ERROR_NO_DATA)
797
+ errno = EAGAIN;
798
+ else if (lasterr == ERROR_ACCESS_DENIED)
799
+ errno = EBADF;
800
+ else
801
+ la_dosmaperr(lasterr);
802
+ archive_set_error(&a->archive, errno, "Read error");
803
+ a->archive.state = ARCHIVE_STATE_FATAL;
804
+ return (ARCHIVE_FATAL);
805
+ }
806
+ } else
807
+ olp->bytes_transferred = rbytes;
808
+ t->ol_num_doing++;
809
+
810
+ return (t->ol_remaining_bytes == 0)? ARCHIVE_EOF: ARCHIVE_OK;
811
+ }
812
+
813
+ static void
814
+ cancel_async(struct tree *t)
815
+ {
816
+ if (t->ol_num_doing != t->ol_num_done) {
817
+ CancelIo(t->entry_fh);
818
+ t->ol_num_doing = t->ol_num_done = 0;
819
+ }
820
+ }
821
+
822
+ static int
823
+ _archive_read_data_block(struct archive *_a, const void **buff,
824
+ size_t *size, int64_t *offset)
825
+ {
826
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
827
+ struct tree *t = a->tree;
828
+ struct la_overlapped *olp;
829
+ DWORD bytes_transferred;
830
+ int r = ARCHIVE_FATAL;
831
+
832
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
833
+ "archive_read_data_block");
834
+
835
+ if (t->entry_eof || t->entry_remaining_bytes <= 0) {
836
+ r = ARCHIVE_EOF;
837
+ goto abort_read_data;
838
+ }
839
+
840
+ /*
841
+ * Make a request to read the file in asynchronous.
842
+ */
843
+ if (t->ol_num_doing == 0) {
844
+ do {
845
+ r = start_next_async_read(a, t);
846
+ if (r == ARCHIVE_FATAL)
847
+ goto abort_read_data;
848
+ if (!t->async_io)
849
+ break;
850
+ } while (r == ARCHIVE_OK && t->ol_num_doing < MAX_OVERLAPPED);
851
+ } else {
852
+ if ((r = start_next_async_read(a, t)) == ARCHIVE_FATAL)
853
+ goto abort_read_data;
854
+ }
855
+
856
+ olp = &(t->ol[t->ol_idx_done]);
857
+ t->ol_idx_done = (t->ol_idx_done + 1) % MAX_OVERLAPPED;
858
+ if (olp->bytes_transferred)
859
+ bytes_transferred = (DWORD)olp->bytes_transferred;
860
+ else if (!GetOverlappedResult(t->entry_fh, &(olp->ol),
861
+ &bytes_transferred, TRUE)) {
862
+ la_dosmaperr(GetLastError());
863
+ archive_set_error(&a->archive, errno,
864
+ "GetOverlappedResult failed");
865
+ a->archive.state = ARCHIVE_STATE_FATAL;
866
+ r = ARCHIVE_FATAL;
867
+ goto abort_read_data;
868
+ }
869
+ t->ol_num_done++;
870
+
871
+ if (bytes_transferred == 0 ||
872
+ olp->bytes_expected != bytes_transferred) {
873
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
874
+ "Reading file truncated");
875
+ a->archive.state = ARCHIVE_STATE_FATAL;
876
+ r = ARCHIVE_FATAL;
877
+ goto abort_read_data;
878
+ }
879
+
880
+ *buff = olp->buff;
881
+ *size = bytes_transferred;
882
+ *offset = olp->offset;
883
+ if (olp->offset > t->entry_total)
884
+ t->entry_remaining_bytes -= olp->offset - t->entry_total;
885
+ t->entry_total = olp->offset + *size;
886
+ t->entry_remaining_bytes -= *size;
887
+ if (t->entry_remaining_bytes == 0) {
888
+ /* Close the current file descriptor */
889
+ close_and_restore_time(t->entry_fh, t, &t->restore_time);
890
+ t->entry_fh = INVALID_HANDLE_VALUE;
891
+ t->entry_eof = 1;
892
+ }
893
+ return (ARCHIVE_OK);
894
+
895
+ abort_read_data:
896
+ *buff = NULL;
897
+ *size = 0;
898
+ *offset = t->entry_total;
899
+ if (t->entry_fh != INVALID_HANDLE_VALUE) {
900
+ cancel_async(t);
901
+ /* Close the current file descriptor */
902
+ close_and_restore_time(t->entry_fh, t, &t->restore_time);
903
+ t->entry_fh = INVALID_HANDLE_VALUE;
904
+ }
905
+ return (r);
906
+ }
907
+
908
+ static int
909
+ next_entry(struct archive_read_disk *a, struct tree *t,
910
+ struct archive_entry *entry)
911
+ {
912
+ const BY_HANDLE_FILE_INFORMATION *st;
913
+ const BY_HANDLE_FILE_INFORMATION *lst;
914
+ const char*name;
915
+ int descend, r;
916
+
917
+ st = NULL;
918
+ lst = NULL;
919
+ t->descend = 0;
920
+ do {
921
+ switch (tree_next(t)) {
922
+ case TREE_ERROR_FATAL:
923
+ archive_set_error(&a->archive, t->tree_errno,
924
+ "%ls: Unable to continue traversing directory tree",
925
+ tree_current_path(t));
926
+ a->archive.state = ARCHIVE_STATE_FATAL;
927
+ return (ARCHIVE_FATAL);
928
+ case TREE_ERROR_DIR:
929
+ archive_set_error(&a->archive, t->tree_errno,
930
+ "%ls: Couldn't visit directory",
931
+ tree_current_path(t));
932
+ return (ARCHIVE_FAILED);
933
+ case 0:
934
+ return (ARCHIVE_EOF);
935
+ case TREE_POSTDESCENT:
936
+ if (t->depth == 1 && t->rootwild && t->symlink_mode == 'H') {
937
+ /*
938
+ * We have descended from a directory at depth 0 specified by a
939
+ * wildcard search in 'H'ybrid link mode. All items under this
940
+ * directory should be 'P'hysical.
941
+ */
942
+ t->symlink_mode = 'P';
943
+ a->follow_symlinks = 0;
944
+ }
945
+ break;
946
+ case TREE_POSTASCENT:
947
+ if (t->depth == 0 && t->rootwild && t->initial_symlink_mode == 'H') {
948
+ /*
949
+ * We have returned to the root of a wildcard search, in 'H'ybrid
950
+ * mode, so restore the symlink mode we overwrote in
951
+ * TREE_POSTDESCENT.
952
+ */
953
+ t->symlink_mode = t->initial_symlink_mode;
954
+ /* Mimics setup done in setup_symlink_mode */
955
+ a->follow_symlinks = 1;
956
+ }
957
+ break;
958
+ case TREE_REGULAR:
959
+ lst = tree_current_lstat(t);
960
+ if (lst == NULL) {
961
+ archive_set_error(&a->archive, t->tree_errno,
962
+ "%ls: Cannot stat",
963
+ tree_current_path(t));
964
+ return (ARCHIVE_FAILED);
965
+ }
966
+ break;
967
+ }
968
+ } while (lst == NULL);
969
+
970
+ archive_entry_copy_pathname_w(entry, tree_current_path(t));
971
+
972
+ /*
973
+ * Perform path matching.
974
+ */
975
+ if (a->matching) {
976
+ r = archive_match_path_excluded(a->matching, entry);
977
+ if (r < 0) {
978
+ archive_set_error(&(a->archive), archive_errno(a->matching),
979
+ "%s", archive_error_string(a->matching));
980
+ return (r);
981
+ }
982
+ if (r) {
983
+ if (a->excluded_cb_func)
984
+ a->excluded_cb_func(&(a->archive),
985
+ a->excluded_cb_data, entry);
986
+ return (ARCHIVE_RETRY);
987
+ }
988
+ }
989
+
990
+ /*
991
+ * Distinguish 'L'/'P'/'H' symlink following.
992
+ */
993
+ switch(t->symlink_mode) {
994
+ case 'H':
995
+ /* 'H': After the first item, rest like 'P'. */
996
+ if (!t->rootwild) {
997
+ /*
998
+ * 'H': When we started with a wildcard, entries at the starting
999
+ * depth should be kept in `H` mode; switching into 'P' mode
1000
+ * is handled in TREE_POSTDESCENT when we enter depth = 1.
1001
+ */
1002
+ t->symlink_mode = 'P';
1003
+ }
1004
+ /* 'H': First item (from command line) like 'L'. */
1005
+ /* FALLTHROUGH */
1006
+ case 'L':
1007
+ /* 'L': Do descend through a symlink to dir. */
1008
+ descend = tree_current_is_dir(t);
1009
+ /* 'L': Follow symlinks to files. */
1010
+ a->symlink_mode = 'L';
1011
+ a->follow_symlinks = 1;
1012
+ /* 'L': Archive symlinks as targets, if we can. */
1013
+ st = tree_current_stat(t);
1014
+ if (st != NULL && !tree_target_is_same_as_parent(t, st))
1015
+ break;
1016
+ /* If stat fails, we have a broken symlink;
1017
+ * in that case, don't follow the link. */
1018
+ /* FALLTHROUGH */
1019
+ default:
1020
+ /* 'P': Don't descend through a symlink to dir. */
1021
+ descend = tree_current_is_physical_dir(t);
1022
+ /* 'P': Don't follow symlinks to files. */
1023
+ a->symlink_mode = 'P';
1024
+ a->follow_symlinks = 0;
1025
+ /* 'P': Archive symlinks as symlinks. */
1026
+ st = lst;
1027
+ break;
1028
+ }
1029
+
1030
+ if (update_current_filesystem(a, bhfi_dev(st)) != ARCHIVE_OK) {
1031
+ a->archive.state = ARCHIVE_STATE_FATAL;
1032
+ return (ARCHIVE_FATAL);
1033
+ }
1034
+ if (t->initial_filesystem_id == -1)
1035
+ t->initial_filesystem_id = t->current_filesystem_id;
1036
+ if (a->flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS) {
1037
+ if (t->initial_filesystem_id != t->current_filesystem_id)
1038
+ return (ARCHIVE_RETRY);
1039
+ }
1040
+ t->descend = descend;
1041
+
1042
+ tree_archive_entry_copy_bhfi(entry, t, st);
1043
+
1044
+ /* Save the times to be restored. This must be in before
1045
+ * calling archive_read_disk_descend() or any chance of it,
1046
+ * especially, invoking a callback. */
1047
+ t->restore_time.lastWriteTime = st->ftLastWriteTime;
1048
+ t->restore_time.lastAccessTime = st->ftLastAccessTime;
1049
+ t->restore_time.filetype = archive_entry_filetype(entry);
1050
+
1051
+ /*
1052
+ * Perform time matching.
1053
+ */
1054
+ if (a->matching) {
1055
+ r = archive_match_time_excluded(a->matching, entry);
1056
+ if (r < 0) {
1057
+ archive_set_error(&(a->archive), archive_errno(a->matching),
1058
+ "%s", archive_error_string(a->matching));
1059
+ return (r);
1060
+ }
1061
+ if (r) {
1062
+ if (a->excluded_cb_func)
1063
+ a->excluded_cb_func(&(a->archive),
1064
+ a->excluded_cb_data, entry);
1065
+ return (ARCHIVE_RETRY);
1066
+ }
1067
+ }
1068
+
1069
+ /* Lookup uname/gname */
1070
+ name = archive_read_disk_uname(&(a->archive), archive_entry_uid(entry));
1071
+ if (name != NULL)
1072
+ archive_entry_copy_uname(entry, name);
1073
+ name = archive_read_disk_gname(&(a->archive), archive_entry_gid(entry));
1074
+ if (name != NULL)
1075
+ archive_entry_copy_gname(entry, name);
1076
+
1077
+ /*
1078
+ * Perform owner matching.
1079
+ */
1080
+ if (a->matching) {
1081
+ r = archive_match_owner_excluded(a->matching, entry);
1082
+ if (r < 0) {
1083
+ archive_set_error(&(a->archive), archive_errno(a->matching),
1084
+ "%s", archive_error_string(a->matching));
1085
+ return (r);
1086
+ }
1087
+ if (r) {
1088
+ if (a->excluded_cb_func)
1089
+ a->excluded_cb_func(&(a->archive),
1090
+ a->excluded_cb_data, entry);
1091
+ return (ARCHIVE_RETRY);
1092
+ }
1093
+ }
1094
+
1095
+ /*
1096
+ * File attributes
1097
+ */
1098
+ if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
1099
+ const int supported_attrs =
1100
+ FILE_ATTRIBUTE_READONLY |
1101
+ FILE_ATTRIBUTE_HIDDEN |
1102
+ FILE_ATTRIBUTE_SYSTEM;
1103
+ DWORD file_attrs = st->dwFileAttributes & supported_attrs;
1104
+ if (file_attrs != 0)
1105
+ archive_entry_set_fflags(entry, file_attrs, 0);
1106
+ }
1107
+
1108
+ /*
1109
+ * Invoke a meta data filter callback.
1110
+ */
1111
+ if (a->metadata_filter_func) {
1112
+ if (!a->metadata_filter_func(&(a->archive),
1113
+ a->metadata_filter_data, entry))
1114
+ return (ARCHIVE_RETRY);
1115
+ }
1116
+
1117
+ archive_entry_copy_sourcepath_w(entry, tree_current_access_path(t));
1118
+
1119
+ r = ARCHIVE_OK;
1120
+ if (archive_entry_filetype(entry) == AE_IFREG &&
1121
+ archive_entry_size(entry) > 0) {
1122
+ DWORD flags = FILE_FLAG_BACKUP_SEMANTICS;
1123
+ #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
1124
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
1125
+ #endif
1126
+ if (t->async_io)
1127
+ flags |= FILE_FLAG_OVERLAPPED;
1128
+ if (t->direct_io)
1129
+ flags |= FILE_FLAG_NO_BUFFERING;
1130
+ else
1131
+ flags |= FILE_FLAG_SEQUENTIAL_SCAN;
1132
+ #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
1133
+ ZeroMemory(&createExParams, sizeof(createExParams));
1134
+ createExParams.dwSize = sizeof(createExParams);
1135
+ createExParams.dwFileFlags = flags;
1136
+ t->entry_fh = CreateFile2(tree_current_access_path(t),
1137
+ GENERIC_READ,
1138
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1139
+ OPEN_EXISTING, &createExParams);
1140
+ #else
1141
+ t->entry_fh = CreateFileW(tree_current_access_path(t),
1142
+ GENERIC_READ,
1143
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1144
+ NULL, OPEN_EXISTING, flags, NULL);
1145
+ #endif
1146
+ if (t->entry_fh == INVALID_HANDLE_VALUE) {
1147
+ la_dosmaperr(GetLastError());
1148
+ archive_set_error(&a->archive, errno,
1149
+ "Couldn't open %ls", tree_current_path(a->tree));
1150
+ return (ARCHIVE_FAILED);
1151
+ }
1152
+
1153
+ /* Find sparse data from the disk. */
1154
+ if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
1155
+ if (archive_entry_hardlink(entry) == NULL &&
1156
+ (st->dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) != 0)
1157
+ r = setup_sparse_from_disk(a, entry, t->entry_fh);
1158
+ }
1159
+ }
1160
+ return (r);
1161
+ }
1162
+
1163
+ static int
1164
+ _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
1165
+ {
1166
+ int ret;
1167
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1168
+ *entryp = NULL;
1169
+ ret = _archive_read_next_header2(_a, a->entry);
1170
+ *entryp = a->entry;
1171
+ return ret;
1172
+ }
1173
+
1174
+ static int
1175
+ _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
1176
+ {
1177
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1178
+ struct tree *t;
1179
+ int r;
1180
+
1181
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1182
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1183
+ "archive_read_next_header2");
1184
+
1185
+ t = a->tree;
1186
+ if (t->entry_fh != INVALID_HANDLE_VALUE) {
1187
+ cancel_async(t);
1188
+ close_and_restore_time(t->entry_fh, t, &t->restore_time);
1189
+ t->entry_fh = INVALID_HANDLE_VALUE;
1190
+ }
1191
+
1192
+ archive_entry_clear(entry);
1193
+
1194
+ while ((r = next_entry(a, t, entry)) == ARCHIVE_RETRY)
1195
+ archive_entry_clear(entry);
1196
+
1197
+ /*
1198
+ * EOF and FATAL are persistent at this layer. By
1199
+ * modifying the state, we guarantee that future calls to
1200
+ * read a header or read data will fail.
1201
+ */
1202
+ switch (r) {
1203
+ case ARCHIVE_EOF:
1204
+ a->archive.state = ARCHIVE_STATE_EOF;
1205
+ break;
1206
+ case ARCHIVE_OK:
1207
+ case ARCHIVE_WARN:
1208
+ t->entry_total = 0;
1209
+ if (archive_entry_filetype(entry) == AE_IFREG) {
1210
+ t->entry_remaining_bytes = archive_entry_size(entry);
1211
+ t->entry_eof = (t->entry_remaining_bytes == 0)? 1: 0;
1212
+ if (!t->entry_eof &&
1213
+ setup_sparse(a, entry) != ARCHIVE_OK)
1214
+ return (ARCHIVE_FATAL);
1215
+ } else {
1216
+ t->entry_remaining_bytes = 0;
1217
+ t->entry_eof = 1;
1218
+ }
1219
+ t->ol_idx_doing = t->ol_idx_done = 0;
1220
+ t->ol_num_doing = t->ol_num_done = 0;
1221
+ t->ol_remaining_bytes = t->entry_remaining_bytes;
1222
+ t->ol_total = 0;
1223
+ a->archive.state = ARCHIVE_STATE_DATA;
1224
+ break;
1225
+ case ARCHIVE_RETRY:
1226
+ break;
1227
+ case ARCHIVE_FATAL:
1228
+ a->archive.state = ARCHIVE_STATE_FATAL;
1229
+ break;
1230
+ }
1231
+
1232
+ __archive_reset_read_data(&a->archive);
1233
+ return (r);
1234
+ }
1235
+
1236
+ static int
1237
+ setup_sparse(struct archive_read_disk *a, struct archive_entry *entry)
1238
+ {
1239
+ struct tree *t = a->tree;
1240
+ int64_t aligned, length, offset;
1241
+ int i;
1242
+
1243
+ t->sparse_count = archive_entry_sparse_reset(entry);
1244
+ if (t->sparse_count+1 > t->sparse_list_size) {
1245
+ free(t->sparse_list);
1246
+ t->sparse_list_size = t->sparse_count + 1;
1247
+ t->sparse_list = malloc(sizeof(t->sparse_list[0]) *
1248
+ t->sparse_list_size);
1249
+ if (t->sparse_list == NULL) {
1250
+ t->sparse_list_size = 0;
1251
+ archive_set_error(&a->archive, ENOMEM,
1252
+ "Can't allocate data");
1253
+ a->archive.state = ARCHIVE_STATE_FATAL;
1254
+ return (ARCHIVE_FATAL);
1255
+ }
1256
+ }
1257
+ /*
1258
+ * Get sparse list and make sure those offsets and lengths are
1259
+ * aligned by a sector size.
1260
+ */
1261
+ for (i = 0; i < t->sparse_count; i++) {
1262
+ archive_entry_sparse_next(entry, &offset, &length);
1263
+ aligned = align_num_per_sector(t, offset);
1264
+ if (aligned != offset) {
1265
+ aligned -= t->current_filesystem->bytesPerSector;
1266
+ length += offset - aligned;
1267
+ }
1268
+ t->sparse_list[i].offset = aligned;
1269
+ aligned = align_num_per_sector(t, length);
1270
+ t->sparse_list[i].length = aligned;
1271
+ }
1272
+
1273
+ aligned = align_num_per_sector(t, archive_entry_size(entry));
1274
+ if (i == 0) {
1275
+ t->sparse_list[i].offset = 0;
1276
+ t->sparse_list[i].length = aligned;
1277
+ } else {
1278
+ int j, last = i;
1279
+
1280
+ t->sparse_list[i].offset = aligned;
1281
+ t->sparse_list[i].length = 0;
1282
+ for (i = 0; i < last; i++) {
1283
+ if ((t->sparse_list[i].offset +
1284
+ t->sparse_list[i].length) <=
1285
+ t->sparse_list[i+1].offset)
1286
+ continue;
1287
+ /*
1288
+ * Now sparse_list[i+1] is overlapped by sparse_list[i].
1289
+ * Merge those two.
1290
+ */
1291
+ length = t->sparse_list[i+1].offset -
1292
+ t->sparse_list[i].offset;
1293
+ t->sparse_list[i+1].offset = t->sparse_list[i].offset;
1294
+ t->sparse_list[i+1].length += length;
1295
+ /* Remove sparse_list[i]. */
1296
+ for (j = i; j < last; j++) {
1297
+ t->sparse_list[j].offset =
1298
+ t->sparse_list[j+1].offset;
1299
+ t->sparse_list[j].length =
1300
+ t->sparse_list[j+1].length;
1301
+ }
1302
+ last--;
1303
+ }
1304
+ }
1305
+ t->current_sparse = t->sparse_list;
1306
+
1307
+ return (ARCHIVE_OK);
1308
+ }
1309
+
1310
+ int
1311
+ archive_read_disk_set_matching(struct archive *_a, struct archive *_ma,
1312
+ void (*_excluded_func)(struct archive *, void *, struct archive_entry *),
1313
+ void *_client_data)
1314
+ {
1315
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1316
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1317
+ ARCHIVE_STATE_ANY, "archive_read_disk_set_matching");
1318
+ a->matching = _ma;
1319
+ a->excluded_cb_func = _excluded_func;
1320
+ a->excluded_cb_data = _client_data;
1321
+ return (ARCHIVE_OK);
1322
+ }
1323
+
1324
+ int
1325
+ archive_read_disk_set_metadata_filter_callback(struct archive *_a,
1326
+ int (*_metadata_filter_func)(struct archive *, void *,
1327
+ struct archive_entry *), void *_client_data)
1328
+ {
1329
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1330
+
1331
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_ANY,
1332
+ "archive_read_disk_set_metadata_filter_callback");
1333
+
1334
+ a->metadata_filter_func = _metadata_filter_func;
1335
+ a->metadata_filter_data = _client_data;
1336
+ return (ARCHIVE_OK);
1337
+ }
1338
+
1339
+ int
1340
+ archive_read_disk_can_descend(struct archive *_a)
1341
+ {
1342
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1343
+ struct tree *t = a->tree;
1344
+
1345
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1346
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1347
+ "archive_read_disk_can_descend");
1348
+
1349
+ return (t->visit_type == TREE_REGULAR && t->descend);
1350
+ }
1351
+
1352
+ /*
1353
+ * Called by the client to mark the directory just returned from
1354
+ * tree_next() as needing to be visited.
1355
+ */
1356
+ int
1357
+ archive_read_disk_descend(struct archive *_a)
1358
+ {
1359
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1360
+ struct tree *t = a->tree;
1361
+
1362
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1363
+ ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1364
+ "archive_read_disk_descend");
1365
+
1366
+ if (!archive_read_disk_can_descend(_a))
1367
+ return (ARCHIVE_OK);
1368
+
1369
+ if (tree_current_is_physical_dir(t)) {
1370
+ tree_push(t, t->basename, t->full_path.s,
1371
+ t->current_filesystem_id,
1372
+ bhfi_dev(&(t->lst)), bhfi_ino(&(t->lst)),
1373
+ &t->restore_time);
1374
+ t->stack->flags |= isDir;
1375
+ } else if (tree_current_is_dir(t)) {
1376
+ tree_push(t, t->basename, t->full_path.s,
1377
+ t->current_filesystem_id,
1378
+ bhfi_dev(&(t->st)), bhfi_ino(&(t->st)),
1379
+ &t->restore_time);
1380
+ t->stack->flags |= isDirLink;
1381
+ }
1382
+ t->descend = 0;
1383
+ return (ARCHIVE_OK);
1384
+ }
1385
+
1386
+ int
1387
+ archive_read_disk_open(struct archive *_a, const char *pathname)
1388
+ {
1389
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1390
+ struct archive_wstring wpath;
1391
+ int ret;
1392
+
1393
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1394
+ ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1395
+ "archive_read_disk_open");
1396
+ archive_clear_error(&a->archive);
1397
+
1398
+ /* Make a wchar_t string from a char string. */
1399
+ archive_string_init(&wpath);
1400
+ if (archive_wstring_append_from_mbs(&wpath, pathname,
1401
+ strlen(pathname)) != 0) {
1402
+ if (errno == ENOMEM)
1403
+ archive_set_error(&a->archive, ENOMEM,
1404
+ "Can't allocate memory");
1405
+ else
1406
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1407
+ "Can't convert a path to a wchar_t string");
1408
+ a->archive.state = ARCHIVE_STATE_FATAL;
1409
+ ret = ARCHIVE_FATAL;
1410
+ } else
1411
+ ret = _archive_read_disk_open_w(_a, wpath.s);
1412
+
1413
+ archive_wstring_free(&wpath);
1414
+ return (ret);
1415
+ }
1416
+
1417
+ int
1418
+ archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1419
+ {
1420
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1421
+
1422
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC,
1423
+ ARCHIVE_STATE_NEW | ARCHIVE_STATE_CLOSED,
1424
+ "archive_read_disk_open_w");
1425
+ archive_clear_error(&a->archive);
1426
+
1427
+ return (_archive_read_disk_open_w(_a, pathname));
1428
+ }
1429
+
1430
+ static int
1431
+ _archive_read_disk_open_w(struct archive *_a, const wchar_t *pathname)
1432
+ {
1433
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1434
+
1435
+ if (a->tree != NULL)
1436
+ a->tree = tree_reopen(a->tree, pathname,
1437
+ a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1438
+ else
1439
+ a->tree = tree_open(pathname, a->symlink_mode,
1440
+ a->flags & ARCHIVE_READDISK_RESTORE_ATIME);
1441
+ if (a->tree == NULL) {
1442
+ archive_set_error(&a->archive, ENOMEM,
1443
+ "Can't allocate directory traversal data");
1444
+ a->archive.state = ARCHIVE_STATE_FATAL;
1445
+ return (ARCHIVE_FATAL);
1446
+ }
1447
+ a->archive.state = ARCHIVE_STATE_HEADER;
1448
+
1449
+ return (ARCHIVE_OK);
1450
+ }
1451
+
1452
+ /*
1453
+ * Return a current filesystem ID which is index of the filesystem entry
1454
+ * you've visited through archive_read_disk.
1455
+ */
1456
+ int
1457
+ archive_read_disk_current_filesystem(struct archive *_a)
1458
+ {
1459
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1460
+
1461
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1462
+ "archive_read_disk_current_filesystem");
1463
+
1464
+ return (a->tree->current_filesystem_id);
1465
+ }
1466
+
1467
+ static int
1468
+ update_current_filesystem(struct archive_read_disk *a, int64_t dev)
1469
+ {
1470
+ struct tree *t = a->tree;
1471
+ int i, fid;
1472
+
1473
+ if (t->current_filesystem != NULL &&
1474
+ t->current_filesystem->dev == dev)
1475
+ return (ARCHIVE_OK);
1476
+
1477
+ for (i = 0; i < t->max_filesystem_id; i++) {
1478
+ if (t->filesystem_table[i].dev == dev) {
1479
+ /* There is the filesystem ID we've already generated. */
1480
+ t->current_filesystem_id = i;
1481
+ t->current_filesystem = &(t->filesystem_table[i]);
1482
+ return (ARCHIVE_OK);
1483
+ }
1484
+ }
1485
+
1486
+ /*
1487
+ * There is a new filesystem, we generate a new ID for.
1488
+ */
1489
+ fid = t->max_filesystem_id;
1490
+ if (fid >= MAX_FILESYSTEM_ID) {
1491
+ archive_set_error(&a->archive, ENOMEM, "Too many filesystems");
1492
+ return (ARCHIVE_FATAL);
1493
+ }
1494
+ if (fid + 1 > t->allocated_filesystem) {
1495
+ int s;
1496
+ void *p;
1497
+
1498
+ s = (fid + 1) * 2;
1499
+ p = realloc(t->filesystem_table,
1500
+ s * sizeof(*t->filesystem_table));
1501
+ if (p == NULL) {
1502
+ archive_set_error(&a->archive, ENOMEM,
1503
+ "Can't allocate tar data");
1504
+ return (ARCHIVE_FATAL);
1505
+ }
1506
+ t->filesystem_table = (struct filesystem *)p;
1507
+ t->allocated_filesystem = s;
1508
+ }
1509
+ t->max_filesystem_id = fid + 1;
1510
+ t->current_filesystem_id = fid;
1511
+ t->current_filesystem = &(t->filesystem_table[fid]);
1512
+ t->current_filesystem->dev = dev;
1513
+
1514
+ return (setup_current_filesystem(a));
1515
+ }
1516
+
1517
+ /*
1518
+ * Returns 1 if current filesystem is generated filesystem, 0 if it is not
1519
+ * or -1 if it is unknown.
1520
+ */
1521
+ int
1522
+ archive_read_disk_current_filesystem_is_synthetic(struct archive *_a)
1523
+ {
1524
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1525
+
1526
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1527
+ "archive_read_disk_current_filesystem");
1528
+
1529
+ return (a->tree->current_filesystem->synthetic);
1530
+ }
1531
+
1532
+ /*
1533
+ * Returns 1 if current filesystem is remote filesystem, 0 if it is not
1534
+ * or -1 if it is unknown.
1535
+ */
1536
+ int
1537
+ archive_read_disk_current_filesystem_is_remote(struct archive *_a)
1538
+ {
1539
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
1540
+
1541
+ archive_check_magic(_a, ARCHIVE_READ_DISK_MAGIC, ARCHIVE_STATE_DATA,
1542
+ "archive_read_disk_current_filesystem");
1543
+
1544
+ return (a->tree->current_filesystem->remote);
1545
+ }
1546
+
1547
+ /*
1548
+ * If symlink is broken, statfs or statvfs will fail.
1549
+ * Use its directory path instead.
1550
+ */
1551
+ static wchar_t *
1552
+ safe_path_for_statfs(struct tree *t)
1553
+ {
1554
+ const wchar_t *path;
1555
+ wchar_t *cp, *p = NULL;
1556
+
1557
+ path = tree_current_access_path(t);
1558
+ if (tree_current_stat(t) == NULL) {
1559
+ p = _wcsdup(path);
1560
+ cp = wcsrchr(p, '/');
1561
+ if (cp != NULL && wcslen(cp) >= 2) {
1562
+ cp[1] = '.';
1563
+ cp[2] = '\0';
1564
+ path = p;
1565
+ }
1566
+ } else
1567
+ p = _wcsdup(path);
1568
+ return (p);
1569
+ }
1570
+
1571
+ /*
1572
+ * Get conditions of synthetic and remote on Windows
1573
+ */
1574
+ static int
1575
+ setup_current_filesystem(struct archive_read_disk *a)
1576
+ {
1577
+ struct tree *t = a->tree;
1578
+ wchar_t vol[256];
1579
+ wchar_t *path;
1580
+
1581
+ t->current_filesystem->synthetic = -1;/* Not supported */
1582
+ path = safe_path_for_statfs(t);
1583
+ if (!GetVolumePathNameW(path, vol, sizeof(vol)/sizeof(vol[0]))) {
1584
+ free(path);
1585
+ t->current_filesystem->remote = -1;
1586
+ t->current_filesystem->bytesPerSector = 0;
1587
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1588
+ "GetVolumePathName failed: %d", (int)GetLastError());
1589
+ return (ARCHIVE_FAILED);
1590
+ }
1591
+ free(path);
1592
+ switch (GetDriveTypeW(vol)) {
1593
+ case DRIVE_UNKNOWN:
1594
+ case DRIVE_NO_ROOT_DIR:
1595
+ t->current_filesystem->remote = -1;
1596
+ break;
1597
+ case DRIVE_REMOTE:
1598
+ t->current_filesystem->remote = 1;
1599
+ break;
1600
+ default:
1601
+ t->current_filesystem->remote = 0;
1602
+ break;
1603
+ }
1604
+
1605
+ if (!GetDiskFreeSpaceW(vol, NULL,
1606
+ &(t->current_filesystem->bytesPerSector), NULL, NULL)) {
1607
+ t->current_filesystem->bytesPerSector = 0;
1608
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1609
+ "GetDiskFreeSpace failed: %d", (int)GetLastError());
1610
+ return (ARCHIVE_FAILED);
1611
+ }
1612
+
1613
+ return (ARCHIVE_OK);
1614
+ }
1615
+
1616
+ static int
1617
+ close_and_restore_time(HANDLE h, struct tree *t, struct restore_time *rt)
1618
+ {
1619
+ HANDLE handle;
1620
+ int r = 0;
1621
+ #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
1622
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
1623
+ #endif
1624
+
1625
+ if (h == INVALID_HANDLE_VALUE && AE_IFLNK == rt->filetype)
1626
+ return (0);
1627
+
1628
+ /* Close a file descriptor.
1629
+ * It will not be used for SetFileTime() because it has been opened
1630
+ * by a read only mode.
1631
+ */
1632
+ if (h != INVALID_HANDLE_VALUE)
1633
+ CloseHandle(h);
1634
+ if ((t->flags & needsRestoreTimes) == 0)
1635
+ return (r);
1636
+
1637
+ #if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
1638
+ ZeroMemory(&createExParams, sizeof(createExParams));
1639
+ createExParams.dwSize = sizeof(createExParams);
1640
+ createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
1641
+ handle = CreateFile2(rt->full_path, FILE_WRITE_ATTRIBUTES,
1642
+ 0, OPEN_EXISTING, &createExParams);
1643
+ #else
1644
+ handle = CreateFileW(rt->full_path, FILE_WRITE_ATTRIBUTES,
1645
+ 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1646
+ #endif
1647
+ if (handle == INVALID_HANDLE_VALUE) {
1648
+ errno = EINVAL;
1649
+ return (-1);
1650
+ }
1651
+
1652
+ if (SetFileTime(handle, NULL, &rt->lastAccessTime,
1653
+ &rt->lastWriteTime) == 0) {
1654
+ errno = EINVAL;
1655
+ r = -1;
1656
+ } else
1657
+ r = 0;
1658
+ CloseHandle(handle);
1659
+ return (r);
1660
+ }
1661
+
1662
+ /*
1663
+ * Add a directory path to the current stack.
1664
+ */
1665
+ static void
1666
+ tree_push(struct tree *t, const wchar_t *path, const wchar_t *full_path,
1667
+ int filesystem_id, int64_t dev, int64_t ino, struct restore_time *rt)
1668
+ {
1669
+ struct tree_entry *te;
1670
+
1671
+ te = calloc(1, sizeof(*te));
1672
+ if (te == NULL)
1673
+ return;
1674
+ te->next = t->stack;
1675
+ te->parent = t->current;
1676
+ if (te->parent)
1677
+ te->depth = te->parent->depth + 1;
1678
+ t->stack = te;
1679
+ archive_string_init(&te->name);
1680
+ archive_wstrcpy(&te->name, path);
1681
+ archive_string_init(&te->full_path);
1682
+ archive_wstrcpy(&te->full_path, full_path);
1683
+ te->flags = needsDescent | needsOpen | needsAscent;
1684
+ te->filesystem_id = filesystem_id;
1685
+ te->dev = dev;
1686
+ te->ino = ino;
1687
+ te->dirname_length = t->dirname_length;
1688
+ te->full_path_dir_length = t->full_path_dir_length;
1689
+ te->restore_time.full_path = te->full_path.s;
1690
+ if (rt != NULL) {
1691
+ te->restore_time.lastWriteTime = rt->lastWriteTime;
1692
+ te->restore_time.lastAccessTime = rt->lastAccessTime;
1693
+ te->restore_time.filetype = rt->filetype;
1694
+ }
1695
+ }
1696
+
1697
+ /*
1698
+ * Append a name to the current dir path.
1699
+ */
1700
+ static int
1701
+ tree_append(struct tree *t, const wchar_t *name, size_t name_length)
1702
+ {
1703
+ size_t size_needed;
1704
+
1705
+ t->path.s[t->dirname_length] = L'\0';
1706
+ t->path.length = t->dirname_length;
1707
+ /* Strip trailing '/' from name, unless entire name is "/". */
1708
+ while (name_length > 1 && name[name_length - 1] == L'/')
1709
+ name_length--;
1710
+
1711
+ /* Resize pathname buffer as needed. */
1712
+ size_needed = name_length + t->dirname_length + 2;
1713
+ if (archive_wstring_ensure(&t->path, size_needed) == NULL)
1714
+ return (TREE_ERROR_FATAL);
1715
+ /* Add a separating '/' if it's needed. */
1716
+ if (t->dirname_length > 0 &&
1717
+ t->path.s[archive_strlen(&t->path)-1] != L'/')
1718
+ archive_wstrappend_wchar(&t->path, L'/');
1719
+ t->basename = t->path.s + archive_strlen(&t->path);
1720
+ archive_wstrncat(&t->path, name, name_length);
1721
+ t->restore_time.full_path = t->basename;
1722
+ if (t->full_path_dir_length > 0) {
1723
+ t->full_path.s[t->full_path_dir_length] = L'\0';
1724
+ t->full_path.length = t->full_path_dir_length;
1725
+ size_needed = name_length + t->full_path_dir_length + 2;
1726
+ if (archive_wstring_ensure(&t->full_path, size_needed) == NULL)
1727
+ return (TREE_ERROR_FATAL);
1728
+ /* Add a separating '\' if it's needed. */
1729
+ if (t->full_path.s[archive_strlen(&t->full_path)-1] != L'\\')
1730
+ archive_wstrappend_wchar(&t->full_path, L'\\');
1731
+ archive_wstrncat(&t->full_path, name, name_length);
1732
+ t->restore_time.full_path = t->full_path.s;
1733
+ }
1734
+ return (0);
1735
+ }
1736
+
1737
+ /*
1738
+ * Open a directory tree for traversal.
1739
+ */
1740
+ static struct tree *
1741
+ tree_open(const wchar_t *path, int symlink_mode, int restore_time)
1742
+ {
1743
+ struct tree *t;
1744
+
1745
+ t = calloc(1, sizeof(*t));
1746
+ if (t == NULL)
1747
+ return (NULL);
1748
+ archive_string_init(&(t->full_path));
1749
+ archive_string_init(&t->path);
1750
+ if (archive_wstring_ensure(&t->path, 15) == NULL) {
1751
+ free(t);
1752
+ return (NULL);
1753
+ }
1754
+ t->initial_symlink_mode = symlink_mode;
1755
+ return (tree_reopen(t, path, restore_time));
1756
+ }
1757
+
1758
+ static struct tree *
1759
+ tree_reopen(struct tree *t, const wchar_t *path, int restore_time)
1760
+ {
1761
+ struct archive_wstring ws;
1762
+ wchar_t *pathname, *p, *base;
1763
+
1764
+ t->flags = (restore_time != 0)?needsRestoreTimes:0;
1765
+ t->visit_type = 0;
1766
+ t->tree_errno = 0;
1767
+ t->full_path_dir_length = 0;
1768
+ t->dirname_length = 0;
1769
+ t->depth = 0;
1770
+ t->rootwild = 0;
1771
+ t->descend = 0;
1772
+ t->current = NULL;
1773
+ t->d = INVALID_HANDLE_VALUE;
1774
+ t->symlink_mode = t->initial_symlink_mode;
1775
+ archive_string_empty(&(t->full_path));
1776
+ archive_string_empty(&t->path);
1777
+ t->entry_fh = INVALID_HANDLE_VALUE;
1778
+ t->entry_eof = 0;
1779
+ t->entry_remaining_bytes = 0;
1780
+ t->initial_filesystem_id = -1;
1781
+
1782
+ /* Get wchar_t strings from char strings. */
1783
+ archive_string_init(&ws);
1784
+ archive_wstrcpy(&ws, path);
1785
+ pathname = ws.s;
1786
+ /* Get a full-path-name. */
1787
+ p = __la_win_permissive_name_w(pathname);
1788
+ if (p == NULL)
1789
+ goto failed;
1790
+ archive_wstrcpy(&(t->full_path), p);
1791
+ free(p);
1792
+
1793
+ /* Convert path separators from '\' to '/' */
1794
+ for (p = pathname; *p != L'\0'; ++p) {
1795
+ if (*p == L'\\')
1796
+ *p = L'/';
1797
+ }
1798
+ base = pathname;
1799
+
1800
+ /* First item is set up a lot like a symlink traversal. */
1801
+ /* printf("Looking for wildcard in %s\n", path); */
1802
+ if ((base[0] == L'/' && base[1] == L'/' &&
1803
+ base[2] == L'?' && base[3] == L'/' &&
1804
+ (wcschr(base+4, L'*') || wcschr(base+4, L'?'))) ||
1805
+ (!(base[0] == L'/' && base[1] == L'/' &&
1806
+ base[2] == L'?' && base[3] == L'/') &&
1807
+ (wcschr(base, L'*') || wcschr(base, L'?')))) {
1808
+ // It has a wildcard in it...
1809
+ // Separate the last element.
1810
+ p = wcsrchr(base, L'/');
1811
+ if (p != NULL) {
1812
+ *p = L'\0';
1813
+ if (tree_append(t, base, p - base))
1814
+ goto failed;
1815
+ t->dirname_length = archive_strlen(&t->path);
1816
+ base = p + 1;
1817
+ }
1818
+ p = wcsrchr(t->full_path.s, L'\\');
1819
+ if (p != NULL) {
1820
+ *p = L'\0';
1821
+ t->full_path.length = wcslen(t->full_path.s);
1822
+ t->full_path_dir_length = archive_strlen(&t->full_path);
1823
+ }
1824
+ t->rootwild = 1;
1825
+ }
1826
+ tree_push(t, base, t->full_path.s, 0, 0, 0, NULL);
1827
+ archive_wstring_free(&ws);
1828
+ t->stack->flags = needsFirstVisit;
1829
+ /*
1830
+ * Debug flag for Direct IO(No buffering) or Async IO.
1831
+ * Those dependent on environment variable switches
1832
+ * will be removed until next release.
1833
+ */
1834
+ {
1835
+ const char *e;
1836
+ if ((e = getenv("LIBARCHIVE_DIRECT_IO")) != NULL) {
1837
+ if (e[0] == '0')
1838
+ t->direct_io = 0;
1839
+ else
1840
+ t->direct_io = 1;
1841
+ fprintf(stderr, "LIBARCHIVE_DIRECT_IO=%s\n",
1842
+ (t->direct_io)?"Enabled":"Disabled");
1843
+ } else
1844
+ t->direct_io = DIRECT_IO;
1845
+ if ((e = getenv("LIBARCHIVE_ASYNC_IO")) != NULL) {
1846
+ if (e[0] == '0')
1847
+ t->async_io = 0;
1848
+ else
1849
+ t->async_io = 1;
1850
+ fprintf(stderr, "LIBARCHIVE_ASYNC_IO=%s\n",
1851
+ (t->async_io)?"Enabled":"Disabled");
1852
+ } else
1853
+ t->async_io = ASYNC_IO;
1854
+ }
1855
+ return (t);
1856
+ failed:
1857
+ archive_wstring_free(&ws);
1858
+ tree_free(t);
1859
+ return (NULL);
1860
+ }
1861
+
1862
+ static int
1863
+ tree_descent(struct tree *t)
1864
+ {
1865
+ t->dirname_length = archive_strlen(&t->path);
1866
+ t->full_path_dir_length = archive_strlen(&t->full_path);
1867
+ t->depth++;
1868
+ return (0);
1869
+ }
1870
+
1871
+ /*
1872
+ * We've finished a directory; ascend back to the parent.
1873
+ */
1874
+ static int
1875
+ tree_ascend(struct tree *t)
1876
+ {
1877
+ struct tree_entry *te;
1878
+
1879
+ te = t->stack;
1880
+ t->depth--;
1881
+ close_and_restore_time(INVALID_HANDLE_VALUE, t, &te->restore_time);
1882
+ return (0);
1883
+ }
1884
+
1885
+ /*
1886
+ * Pop the working stack.
1887
+ */
1888
+ static void
1889
+ tree_pop(struct tree *t)
1890
+ {
1891
+ struct tree_entry *te;
1892
+
1893
+ t->full_path.s[t->full_path_dir_length] = L'\0';
1894
+ t->full_path.length = t->full_path_dir_length;
1895
+ t->path.s[t->dirname_length] = L'\0';
1896
+ t->path.length = t->dirname_length;
1897
+ if (t->stack == t->current && t->current != NULL)
1898
+ t->current = t->current->parent;
1899
+ te = t->stack;
1900
+ t->stack = te->next;
1901
+ t->dirname_length = te->dirname_length;
1902
+ t->basename = t->path.s + t->dirname_length;
1903
+ t->full_path_dir_length = te->full_path_dir_length;
1904
+ while (t->basename[0] == L'/')
1905
+ t->basename++;
1906
+ archive_wstring_free(&te->name);
1907
+ archive_wstring_free(&te->full_path);
1908
+ free(te);
1909
+ }
1910
+
1911
+ /*
1912
+ * Get the next item in the tree traversal.
1913
+ */
1914
+ static int
1915
+ tree_next(struct tree *t)
1916
+ {
1917
+ int r;
1918
+
1919
+ while (t->stack != NULL) {
1920
+ /* If there's an open dir, get the next entry from there. */
1921
+ if (t->d != INVALID_HANDLE_VALUE) {
1922
+ r = tree_dir_next_windows(t, NULL);
1923
+ if (r == 0)
1924
+ continue;
1925
+ return (r);
1926
+ }
1927
+
1928
+ if (t->stack->flags & needsFirstVisit) {
1929
+ wchar_t *d = t->stack->name.s;
1930
+ t->stack->flags &= ~needsFirstVisit;
1931
+ if (!(d[0] == L'/' && d[1] == L'/' &&
1932
+ d[2] == L'?' && d[3] == L'/') &&
1933
+ (wcschr(d, L'*') || wcschr(d, L'?'))) {
1934
+ r = tree_dir_next_windows(t, d);
1935
+ if (r == 0)
1936
+ continue;
1937
+ return (r);
1938
+ } else {
1939
+ HANDLE h = FindFirstFileW(t->stack->full_path.s, &t->_findData);
1940
+ if (h == INVALID_HANDLE_VALUE) {
1941
+ la_dosmaperr(GetLastError());
1942
+ t->tree_errno = errno;
1943
+ t->visit_type = TREE_ERROR_DIR;
1944
+ return (t->visit_type);
1945
+ }
1946
+ t->findData = &t->_findData;
1947
+ FindClose(h);
1948
+ }
1949
+ /* Top stack item needs a regular visit. */
1950
+ t->current = t->stack;
1951
+ r = tree_append(t, t->stack->name.s,
1952
+ archive_strlen(&(t->stack->name)));
1953
+ if (r != 0)
1954
+ return (r);
1955
+ //t->dirname_length = t->path_length;
1956
+ //tree_pop(t);
1957
+ t->stack->flags &= ~needsFirstVisit;
1958
+ return (t->visit_type = TREE_REGULAR);
1959
+ } else if (t->stack->flags & needsDescent) {
1960
+ /* Top stack item is dir to descend into. */
1961
+ t->current = t->stack;
1962
+ r = tree_append(t, t->stack->name.s,
1963
+ archive_strlen(&(t->stack->name)));
1964
+ if (r != 0)
1965
+ return (r);
1966
+ t->stack->flags &= ~needsDescent;
1967
+ r = tree_descent(t);
1968
+ if (r != 0) {
1969
+ tree_pop(t);
1970
+ t->visit_type = r;
1971
+ } else
1972
+ t->visit_type = TREE_POSTDESCENT;
1973
+ return (t->visit_type);
1974
+ } else if (t->stack->flags & needsOpen) {
1975
+ t->stack->flags &= ~needsOpen;
1976
+ r = tree_dir_next_windows(t, L"*");
1977
+ if (r == 0)
1978
+ continue;
1979
+ return (r);
1980
+ } else if (t->stack->flags & needsAscent) {
1981
+ /* Top stack item is dir and we're done with it. */
1982
+ r = tree_ascend(t);
1983
+ tree_pop(t);
1984
+ t->visit_type = r != 0 ? r : TREE_POSTASCENT;
1985
+ return (t->visit_type);
1986
+ } else {
1987
+ /* Top item on stack is dead. */
1988
+ tree_pop(t);
1989
+ t->flags &= ~hasLstat;
1990
+ t->flags &= ~hasStat;
1991
+ }
1992
+ }
1993
+ return (t->visit_type = 0);
1994
+ }
1995
+
1996
+ static int
1997
+ tree_dir_next_windows(struct tree *t, const wchar_t *pattern)
1998
+ {
1999
+ const wchar_t *name;
2000
+ size_t namelen;
2001
+ int r;
2002
+
2003
+ for (;;) {
2004
+ if (pattern != NULL) {
2005
+ struct archive_wstring pt;
2006
+
2007
+ archive_string_init(&pt);
2008
+ if (archive_wstring_ensure(&pt,
2009
+ archive_strlen(&(t->full_path))
2010
+ + 2 + wcslen(pattern)) == NULL)
2011
+ return (TREE_ERROR_FATAL);
2012
+ archive_wstring_copy(&pt, &(t->full_path));
2013
+ archive_wstrappend_wchar(&pt, L'\\');
2014
+ archive_wstrcat(&pt, pattern);
2015
+ t->d = FindFirstFileW(pt.s, &t->_findData);
2016
+ archive_wstring_free(&pt);
2017
+ if (t->d == INVALID_HANDLE_VALUE) {
2018
+ la_dosmaperr(GetLastError());
2019
+ t->tree_errno = errno;
2020
+ r = tree_ascend(t); /* Undo "chdir" */
2021
+ tree_pop(t);
2022
+ t->visit_type = r != 0 ? r : TREE_ERROR_DIR;
2023
+ return (t->visit_type);
2024
+ }
2025
+ /* Top stack item needs a regular visit. */
2026
+ t->current = t->stack;
2027
+ t->findData = &t->_findData;
2028
+ pattern = NULL;
2029
+ } else if (!FindNextFileW(t->d, &t->_findData)) {
2030
+ FindClose(t->d);
2031
+ t->d = INVALID_HANDLE_VALUE;
2032
+ t->findData = NULL;
2033
+ return (0);
2034
+ }
2035
+ name = t->findData->cFileName;
2036
+ namelen = wcslen(name);
2037
+ t->flags &= ~hasLstat;
2038
+ t->flags &= ~hasStat;
2039
+ if (name[0] == L'.' && name[1] == L'\0')
2040
+ continue;
2041
+ if (name[0] == L'.' && name[1] == L'.' && name[2] == L'\0')
2042
+ continue;
2043
+ r = tree_append(t, name, namelen);
2044
+ if (r != 0)
2045
+ return (r);
2046
+ return (t->visit_type = TREE_REGULAR);
2047
+ }
2048
+ }
2049
+
2050
+ static void
2051
+ entry_copy_bhfi(struct archive_entry *entry, const wchar_t *path,
2052
+ const WIN32_FIND_DATAW *findData,
2053
+ const BY_HANDLE_FILE_INFORMATION *bhfi)
2054
+ {
2055
+ int64_t secs;
2056
+ uint32_t nsecs;
2057
+ mode_t mode;
2058
+
2059
+ ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastAccessTime), &secs, &nsecs);
2060
+ archive_entry_set_atime(entry, secs, nsecs);
2061
+ ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftLastWriteTime), &secs, &nsecs);
2062
+ archive_entry_set_mtime(entry, secs, nsecs);
2063
+ ntfs_to_unix(FILETIME_to_ntfs(&bhfi->ftCreationTime), &secs, &nsecs);
2064
+ archive_entry_set_birthtime(entry, secs, nsecs);
2065
+ archive_entry_set_ctime(entry, secs, nsecs);
2066
+ archive_entry_set_dev(entry, bhfi_dev(bhfi));
2067
+ archive_entry_set_ino64(entry, bhfi_ino(bhfi));
2068
+ if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2069
+ archive_entry_set_nlink(entry, bhfi->nNumberOfLinks + 1);
2070
+ else
2071
+ archive_entry_set_nlink(entry, bhfi->nNumberOfLinks);
2072
+ archive_entry_set_size(entry,
2073
+ (((int64_t)bhfi->nFileSizeHigh) << 32)
2074
+ + bhfi->nFileSizeLow);
2075
+ archive_entry_set_uid(entry, 0);
2076
+ archive_entry_set_gid(entry, 0);
2077
+ archive_entry_set_rdev(entry, 0);
2078
+
2079
+ mode = S_IRUSR | S_IRGRP | S_IROTH;
2080
+ if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
2081
+ mode |= S_IWUSR | S_IWGRP | S_IWOTH;
2082
+ if ((bhfi->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
2083
+ findData != NULL &&
2084
+ findData->dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
2085
+ mode |= S_IFLNK;
2086
+ entry_symlink_from_pathw(entry, path);
2087
+ } else if (bhfi->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2088
+ mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
2089
+ else {
2090
+ const wchar_t *p;
2091
+
2092
+ mode |= S_IFREG;
2093
+ p = wcsrchr(path, L'.');
2094
+ if (p != NULL && wcslen(p) == 4) {
2095
+ switch (p[1]) {
2096
+ case L'B': case L'b':
2097
+ if ((p[2] == L'A' || p[2] == L'a' ) &&
2098
+ (p[3] == L'T' || p[3] == L't' ))
2099
+ mode |= S_IXUSR | S_IXGRP | S_IXOTH;
2100
+ break;
2101
+ case L'C': case L'c':
2102
+ if (((p[2] == L'M' || p[2] == L'm' ) &&
2103
+ (p[3] == L'D' || p[3] == L'd' )))
2104
+ mode |= S_IXUSR | S_IXGRP | S_IXOTH;
2105
+ break;
2106
+ case L'E': case L'e':
2107
+ if ((p[2] == L'X' || p[2] == L'x' ) &&
2108
+ (p[3] == L'E' || p[3] == L'e' ))
2109
+ mode |= S_IXUSR | S_IXGRP | S_IXOTH;
2110
+ break;
2111
+ default:
2112
+ break;
2113
+ }
2114
+ }
2115
+ }
2116
+ archive_entry_set_mode(entry, mode);
2117
+ }
2118
+
2119
+ static void
2120
+ tree_archive_entry_copy_bhfi(struct archive_entry *entry, struct tree *t,
2121
+ const BY_HANDLE_FILE_INFORMATION *bhfi)
2122
+ {
2123
+ entry_copy_bhfi(entry, tree_current_path(t), t->findData, bhfi);
2124
+ }
2125
+
2126
+ static int
2127
+ tree_current_file_information(struct tree *t, BY_HANDLE_FILE_INFORMATION *st,
2128
+ int sim_lstat)
2129
+ {
2130
+ HANDLE h;
2131
+ int r;
2132
+ DWORD flag = FILE_FLAG_BACKUP_SEMANTICS;
2133
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
2134
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
2135
+ #endif
2136
+
2137
+ if (sim_lstat && tree_current_is_physical_link(t))
2138
+ flag |= FILE_FLAG_OPEN_REPARSE_POINT;
2139
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
2140
+ ZeroMemory(&createExParams, sizeof(createExParams));
2141
+ createExParams.dwSize = sizeof(createExParams);
2142
+ createExParams.dwFileFlags = flag;
2143
+ h = CreateFile2(tree_current_access_path(t), 0,
2144
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2145
+ OPEN_EXISTING, &createExParams);
2146
+ #else
2147
+ h = CreateFileW(tree_current_access_path(t), 0,
2148
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2149
+ OPEN_EXISTING, flag, NULL);
2150
+ #endif
2151
+ if (h == INVALID_HANDLE_VALUE) {
2152
+ la_dosmaperr(GetLastError());
2153
+ t->tree_errno = errno;
2154
+ return (0);
2155
+ }
2156
+ r = GetFileInformationByHandle(h, st);
2157
+ CloseHandle(h);
2158
+ return (r);
2159
+ }
2160
+
2161
+ /*
2162
+ * Get the stat() data for the entry just returned from tree_next().
2163
+ */
2164
+ static const BY_HANDLE_FILE_INFORMATION *
2165
+ tree_current_stat(struct tree *t)
2166
+ {
2167
+ if (!(t->flags & hasStat)) {
2168
+ if (!tree_current_file_information(t, &t->st, 0))
2169
+ return NULL;
2170
+ t->flags |= hasStat;
2171
+ }
2172
+ return (&t->st);
2173
+ }
2174
+
2175
+ /*
2176
+ * Get the lstat() data for the entry just returned from tree_next().
2177
+ */
2178
+ static const BY_HANDLE_FILE_INFORMATION *
2179
+ tree_current_lstat(struct tree *t)
2180
+ {
2181
+ if (!(t->flags & hasLstat)) {
2182
+ if (!tree_current_file_information(t, &t->lst, 1))
2183
+ return NULL;
2184
+ t->flags |= hasLstat;
2185
+ }
2186
+ return (&t->lst);
2187
+ }
2188
+
2189
+ /*
2190
+ * Test whether current entry is a dir or link to a dir.
2191
+ */
2192
+ static int
2193
+ tree_current_is_dir(struct tree *t)
2194
+ {
2195
+ if (t->findData)
2196
+ return (t->findData->dwFileAttributes
2197
+ & FILE_ATTRIBUTE_DIRECTORY);
2198
+ return (0);
2199
+ }
2200
+
2201
+ /*
2202
+ * Test whether current entry is a physical directory. Usually, we
2203
+ * already have at least one of stat() or lstat() in memory, so we
2204
+ * use tricks to try to avoid an extra trip to the disk.
2205
+ */
2206
+ static int
2207
+ tree_current_is_physical_dir(struct tree *t)
2208
+ {
2209
+ if (tree_current_is_physical_link(t))
2210
+ return (0);
2211
+ return (tree_current_is_dir(t));
2212
+ }
2213
+
2214
+ /*
2215
+ * Test whether current entry is a symbolic link.
2216
+ */
2217
+ static int
2218
+ tree_current_is_physical_link(struct tree *t)
2219
+ {
2220
+ if (t->findData)
2221
+ return ((t->findData->dwFileAttributes
2222
+ & FILE_ATTRIBUTE_REPARSE_POINT) &&
2223
+ (t->findData->dwReserved0
2224
+ == IO_REPARSE_TAG_SYMLINK));
2225
+ return (0);
2226
+ }
2227
+
2228
+ /*
2229
+ * Test whether the same file has been in the tree as its parent.
2230
+ */
2231
+ static int
2232
+ tree_target_is_same_as_parent(struct tree *t,
2233
+ const BY_HANDLE_FILE_INFORMATION *st)
2234
+ {
2235
+ struct tree_entry *te;
2236
+ int64_t dev = bhfi_dev(st);
2237
+ int64_t ino = bhfi_ino(st);
2238
+
2239
+ for (te = t->current->parent; te != NULL; te = te->parent) {
2240
+ if (te->dev == dev && te->ino == ino)
2241
+ return (1);
2242
+ }
2243
+ return (0);
2244
+ }
2245
+
2246
+ /*
2247
+ * Return the access path for the entry just returned from tree_next().
2248
+ */
2249
+ static const wchar_t *
2250
+ tree_current_access_path(struct tree *t)
2251
+ {
2252
+ return (t->full_path.s);
2253
+ }
2254
+
2255
+ /*
2256
+ * Return the full path for the entry just returned from tree_next().
2257
+ */
2258
+ static const wchar_t *
2259
+ tree_current_path(struct tree *t)
2260
+ {
2261
+ return (t->path.s);
2262
+ }
2263
+
2264
+ /*
2265
+ * Terminate the traversal.
2266
+ */
2267
+ static void
2268
+ tree_close(struct tree *t)
2269
+ {
2270
+
2271
+ if (t == NULL)
2272
+ return;
2273
+ if (t->entry_fh != INVALID_HANDLE_VALUE) {
2274
+ cancel_async(t);
2275
+ close_and_restore_time(t->entry_fh, t, &t->restore_time);
2276
+ t->entry_fh = INVALID_HANDLE_VALUE;
2277
+ }
2278
+ /* Close the handle of FindFirstFileW */
2279
+ if (t->d != INVALID_HANDLE_VALUE) {
2280
+ FindClose(t->d);
2281
+ t->d = INVALID_HANDLE_VALUE;
2282
+ t->findData = NULL;
2283
+ }
2284
+ /* Release anything remaining in the stack. */
2285
+ while (t->stack != NULL)
2286
+ tree_pop(t);
2287
+ }
2288
+
2289
+ /*
2290
+ * Release any resources.
2291
+ */
2292
+ static void
2293
+ tree_free(struct tree *t)
2294
+ {
2295
+ int i;
2296
+
2297
+ if (t == NULL)
2298
+ return;
2299
+ archive_wstring_free(&t->path);
2300
+ archive_wstring_free(&t->full_path);
2301
+ free(t->sparse_list);
2302
+ free(t->filesystem_table);
2303
+ for (i = 0; i < MAX_OVERLAPPED; i++) {
2304
+ if (t->ol[i].buff)
2305
+ VirtualFree(t->ol[i].buff, 0, MEM_RELEASE);
2306
+ CloseHandle(t->ol[i].ol.hEvent);
2307
+ }
2308
+ free(t);
2309
+ }
2310
+
2311
+
2312
+ /*
2313
+ * Populate the archive_entry with metadata from the disk.
2314
+ */
2315
+ int
2316
+ archive_read_disk_entry_from_file(struct archive *_a,
2317
+ struct archive_entry *entry, int fd, const struct stat *st)
2318
+ {
2319
+ struct archive_read_disk *a = (struct archive_read_disk *)_a;
2320
+ const wchar_t *path;
2321
+ const wchar_t *wname;
2322
+ const char *name;
2323
+ HANDLE h;
2324
+ BY_HANDLE_FILE_INFORMATION bhfi;
2325
+ DWORD fileAttributes = 0;
2326
+ int r;
2327
+
2328
+ archive_clear_error(_a);
2329
+ wname = archive_entry_sourcepath_w(entry);
2330
+ if (wname == NULL)
2331
+ wname = archive_entry_pathname_w(entry);
2332
+ if (wname == NULL) {
2333
+ archive_set_error(&a->archive, EINVAL,
2334
+ "Can't get a wide character version of the path");
2335
+ return (ARCHIVE_FAILED);
2336
+ }
2337
+ path = __la_win_permissive_name_w(wname);
2338
+
2339
+ if (st == NULL) {
2340
+ /*
2341
+ * Get metadata through GetFileInformationByHandle().
2342
+ */
2343
+ if (fd >= 0) {
2344
+ h = (HANDLE)_get_osfhandle(fd);
2345
+ r = GetFileInformationByHandle(h, &bhfi);
2346
+ if (r == 0) {
2347
+ la_dosmaperr(GetLastError());
2348
+ archive_set_error(&a->archive, errno,
2349
+ "Can't GetFileInformationByHandle");
2350
+ return (ARCHIVE_FAILED);
2351
+ }
2352
+ entry_copy_bhfi(entry, path, NULL, &bhfi);
2353
+ } else {
2354
+ WIN32_FIND_DATAW findData;
2355
+ DWORD flag, desiredAccess;
2356
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
2357
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
2358
+ #endif
2359
+
2360
+ h = FindFirstFileW(path, &findData);
2361
+ if (h == INVALID_HANDLE_VALUE) {
2362
+ la_dosmaperr(GetLastError());
2363
+ archive_set_error(&a->archive, errno,
2364
+ "Can't FindFirstFileW");
2365
+ return (ARCHIVE_FAILED);
2366
+ }
2367
+ FindClose(h);
2368
+
2369
+ flag = FILE_FLAG_BACKUP_SEMANTICS;
2370
+ if (!a->follow_symlinks &&
2371
+ (findData.dwFileAttributes
2372
+ & FILE_ATTRIBUTE_REPARSE_POINT) &&
2373
+ (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
2374
+ flag |= FILE_FLAG_OPEN_REPARSE_POINT;
2375
+ desiredAccess = 0;
2376
+ } else if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2377
+ desiredAccess = 0;
2378
+ } else
2379
+ desiredAccess = GENERIC_READ;
2380
+
2381
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
2382
+ ZeroMemory(&createExParams, sizeof(createExParams));
2383
+ createExParams.dwSize = sizeof(createExParams);
2384
+ createExParams.dwFileFlags = flag;
2385
+ h = CreateFile2(path, desiredAccess,
2386
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2387
+ OPEN_EXISTING, &createExParams);
2388
+ #else
2389
+ h = CreateFileW(path, desiredAccess,
2390
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2391
+ OPEN_EXISTING, flag, NULL);
2392
+ #endif
2393
+ if (h == INVALID_HANDLE_VALUE) {
2394
+ la_dosmaperr(GetLastError());
2395
+ archive_set_error(&a->archive, errno,
2396
+ "Can't CreateFileW");
2397
+ return (ARCHIVE_FAILED);
2398
+ }
2399
+ r = GetFileInformationByHandle(h, &bhfi);
2400
+ if (r == 0) {
2401
+ la_dosmaperr(GetLastError());
2402
+ archive_set_error(&a->archive, errno,
2403
+ "Can't GetFileInformationByHandle");
2404
+ CloseHandle(h);
2405
+ return (ARCHIVE_FAILED);
2406
+ }
2407
+ entry_copy_bhfi(entry, path, &findData, &bhfi);
2408
+ }
2409
+ fileAttributes = bhfi.dwFileAttributes;
2410
+ } else {
2411
+ archive_entry_copy_stat(entry, st);
2412
+ if (st->st_mode & S_IFLNK)
2413
+ entry_symlink_from_pathw(entry, path);
2414
+ h = INVALID_HANDLE_VALUE;
2415
+ }
2416
+
2417
+ /* Lookup uname/gname */
2418
+ name = archive_read_disk_uname(_a, archive_entry_uid(entry));
2419
+ if (name != NULL)
2420
+ archive_entry_copy_uname(entry, name);
2421
+ name = archive_read_disk_gname(_a, archive_entry_gid(entry));
2422
+ if (name != NULL)
2423
+ archive_entry_copy_gname(entry, name);
2424
+
2425
+ /*
2426
+ * File attributes
2427
+ */
2428
+ if ((a->flags & ARCHIVE_READDISK_NO_FFLAGS) == 0) {
2429
+ const int supported_attrs =
2430
+ FILE_ATTRIBUTE_READONLY |
2431
+ FILE_ATTRIBUTE_HIDDEN |
2432
+ FILE_ATTRIBUTE_SYSTEM;
2433
+ DWORD file_attrs = fileAttributes & supported_attrs;
2434
+ if (file_attrs != 0)
2435
+ archive_entry_set_fflags(entry, file_attrs, 0);
2436
+ }
2437
+
2438
+ /*
2439
+ * Can this file be sparse file ?
2440
+ */
2441
+ if (archive_entry_filetype(entry) != AE_IFREG
2442
+ || archive_entry_size(entry) <= 0
2443
+ || archive_entry_hardlink(entry) != NULL) {
2444
+ if (h != INVALID_HANDLE_VALUE && fd < 0)
2445
+ CloseHandle(h);
2446
+ return (ARCHIVE_OK);
2447
+ }
2448
+
2449
+ if (h == INVALID_HANDLE_VALUE) {
2450
+ if (fd >= 0) {
2451
+ h = (HANDLE)_get_osfhandle(fd);
2452
+ } else {
2453
+ # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
2454
+ CREATEFILE2_EXTENDED_PARAMETERS createExParams;
2455
+ ZeroMemory(&createExParams, sizeof(createExParams));
2456
+ createExParams.dwSize = sizeof(createExParams);
2457
+ createExParams.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
2458
+ h = CreateFile2(path, GENERIC_READ,
2459
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2460
+ OPEN_EXISTING, &createExParams);
2461
+ #else
2462
+ h = CreateFileW(path, GENERIC_READ,
2463
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
2464
+ OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2465
+ #endif
2466
+ if (h == INVALID_HANDLE_VALUE) {
2467
+ la_dosmaperr(GetLastError());
2468
+ archive_set_error(&a->archive, errno,
2469
+ "Can't CreateFileW");
2470
+ return (ARCHIVE_FAILED);
2471
+ }
2472
+ }
2473
+ r = GetFileInformationByHandle(h, &bhfi);
2474
+ if (r == 0) {
2475
+ la_dosmaperr(GetLastError());
2476
+ archive_set_error(&a->archive, errno,
2477
+ "Can't GetFileInformationByHandle");
2478
+ if (h != INVALID_HANDLE_VALUE && fd < 0)
2479
+ CloseHandle(h);
2480
+ return (ARCHIVE_FAILED);
2481
+ }
2482
+ fileAttributes = bhfi.dwFileAttributes;
2483
+ }
2484
+
2485
+ /* Sparse file must be set a mark, FILE_ATTRIBUTE_SPARSE_FILE */
2486
+ if ((fileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2487
+ if (fd < 0)
2488
+ CloseHandle(h);
2489
+ return (ARCHIVE_OK);
2490
+ }
2491
+
2492
+ r = ARCHIVE_OK;
2493
+ if ((a->flags & ARCHIVE_READDISK_NO_SPARSE) == 0) {
2494
+ r = setup_sparse_from_disk(a, entry, h);
2495
+ if (fd < 0)
2496
+ CloseHandle(h);
2497
+ }
2498
+
2499
+ return (r);
2500
+ }
2501
+
2502
+ /*
2503
+ * Windows sparse interface.
2504
+ */
2505
+ #if defined(__MINGW32__) && !defined(FSCTL_QUERY_ALLOCATED_RANGES)
2506
+ #define FSCTL_QUERY_ALLOCATED_RANGES 0x940CF
2507
+ typedef struct {
2508
+ LARGE_INTEGER FileOffset;
2509
+ LARGE_INTEGER Length;
2510
+ } FILE_ALLOCATED_RANGE_BUFFER;
2511
+ #endif
2512
+
2513
+ static int
2514
+ setup_sparse_from_disk(struct archive_read_disk *a,
2515
+ struct archive_entry *entry, HANDLE handle)
2516
+ {
2517
+ FILE_ALLOCATED_RANGE_BUFFER range, *outranges = NULL;
2518
+ size_t outranges_size;
2519
+ int64_t entry_size = archive_entry_size(entry);
2520
+ int exit_sts = ARCHIVE_OK;
2521
+
2522
+ range.FileOffset.QuadPart = 0;
2523
+ range.Length.QuadPart = entry_size;
2524
+ outranges_size = 2048;
2525
+ outranges = malloc(outranges_size);
2526
+ if (outranges == NULL) {
2527
+ archive_set_error(&a->archive, ENOMEM,
2528
+ "Couldn't allocate memory");
2529
+ exit_sts = ARCHIVE_FATAL;
2530
+ goto exit_setup_sparse;
2531
+ }
2532
+
2533
+ for (;;) {
2534
+ DWORD retbytes;
2535
+ BOOL ret;
2536
+
2537
+ for (;;) {
2538
+ ret = DeviceIoControl(handle,
2539
+ FSCTL_QUERY_ALLOCATED_RANGES,
2540
+ &range, sizeof(range), outranges,
2541
+ (DWORD)outranges_size, &retbytes, NULL);
2542
+ if (ret == 0 && GetLastError() == ERROR_MORE_DATA) {
2543
+ free(outranges);
2544
+ if (archive_ckd_mul_size(&outranges_size, outranges_size, 2)) {
2545
+ archive_set_error(&a->archive, ENOMEM,
2546
+ "Couldn't allocate memory");
2547
+ exit_sts = ARCHIVE_FATAL;
2548
+ goto exit_setup_sparse;
2549
+ }
2550
+ outranges = (FILE_ALLOCATED_RANGE_BUFFER *)
2551
+ malloc(outranges_size);
2552
+ if (outranges == NULL) {
2553
+ archive_set_error(&a->archive, ENOMEM,
2554
+ "Couldn't allocate memory");
2555
+ exit_sts = ARCHIVE_FATAL;
2556
+ goto exit_setup_sparse;
2557
+ }
2558
+ continue;
2559
+ } else
2560
+ break;
2561
+ }
2562
+ if (ret != 0) {
2563
+ if (retbytes > 0) {
2564
+ DWORD i, n;
2565
+
2566
+ n = retbytes / sizeof(outranges[0]);
2567
+ if (n == 1 &&
2568
+ outranges[0].FileOffset.QuadPart == 0 &&
2569
+ outranges[0].Length.QuadPart == entry_size)
2570
+ break;/* This is not sparse. */
2571
+ for (i = 0; i < n; i++)
2572
+ archive_entry_sparse_add_entry(entry,
2573
+ outranges[i].FileOffset.QuadPart,
2574
+ outranges[i].Length.QuadPart);
2575
+ range.FileOffset.QuadPart =
2576
+ outranges[n-1].FileOffset.QuadPart
2577
+ + outranges[n-1].Length.QuadPart;
2578
+ range.Length.QuadPart =
2579
+ entry_size - range.FileOffset.QuadPart;
2580
+ if (range.Length.QuadPart > 0)
2581
+ continue;
2582
+ } else {
2583
+ /* The entire file is a hole. Add one data block of size 0 at the end. */
2584
+ archive_entry_sparse_add_entry(entry,
2585
+ entry_size,
2586
+ 0);
2587
+ }
2588
+ break;
2589
+ } else {
2590
+ la_dosmaperr(GetLastError());
2591
+ archive_set_error(&a->archive, errno,
2592
+ "DeviceIoControl Failed: %lu", GetLastError());
2593
+ exit_sts = ARCHIVE_FAILED;
2594
+ goto exit_setup_sparse;
2595
+ }
2596
+ }
2597
+ exit_setup_sparse:
2598
+ free(outranges);
2599
+
2600
+ return (exit_sts);
2601
+ }
2602
+
2603
+ #endif