amp 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (295) hide show
  1. data/.gitignore +1 -0
  2. data/.hgignore +26 -0
  3. data/AUTHORS +2 -0
  4. data/History.txt +6 -0
  5. data/LICENSE +37 -0
  6. data/MANIFESTO +7 -0
  7. data/Manifest.txt +294 -0
  8. data/README.md +129 -0
  9. data/Rakefile +102 -0
  10. data/SCHEDULE.markdown +12 -0
  11. data/STYLE +27 -0
  12. data/TODO.markdown +149 -0
  13. data/ampfile.rb +47 -0
  14. data/bin/amp +30 -0
  15. data/bin/amp1.9 +30 -0
  16. data/ext/amp/bz2/README.txt +39 -0
  17. data/ext/amp/bz2/bz2.c +1582 -0
  18. data/ext/amp/bz2/extconf.rb +77 -0
  19. data/ext/amp/bz2/mkmf.log +29 -0
  20. data/ext/amp/mercurial_patch/extconf.rb +5 -0
  21. data/ext/amp/mercurial_patch/mpatch.c +405 -0
  22. data/ext/amp/priority_queue/extconf.rb +5 -0
  23. data/ext/amp/priority_queue/priority_queue.c +947 -0
  24. data/ext/amp/support/extconf.rb +5 -0
  25. data/ext/amp/support/support.c +250 -0
  26. data/lib/amp.rb +200 -0
  27. data/lib/amp/commands/command.rb +507 -0
  28. data/lib/amp/commands/command_support.rb +137 -0
  29. data/lib/amp/commands/commands/config.rb +143 -0
  30. data/lib/amp/commands/commands/help.rb +29 -0
  31. data/lib/amp/commands/commands/init.rb +10 -0
  32. data/lib/amp/commands/commands/templates.rb +137 -0
  33. data/lib/amp/commands/commands/version.rb +7 -0
  34. data/lib/amp/commands/commands/workflow.rb +28 -0
  35. data/lib/amp/commands/commands/workflows/git/add.rb +65 -0
  36. data/lib/amp/commands/commands/workflows/git/copy.rb +27 -0
  37. data/lib/amp/commands/commands/workflows/git/mv.rb +23 -0
  38. data/lib/amp/commands/commands/workflows/git/rm.rb +60 -0
  39. data/lib/amp/commands/commands/workflows/hg/add.rb +53 -0
  40. data/lib/amp/commands/commands/workflows/hg/addremove.rb +86 -0
  41. data/lib/amp/commands/commands/workflows/hg/annotate.rb +46 -0
  42. data/lib/amp/commands/commands/workflows/hg/archive.rb +126 -0
  43. data/lib/amp/commands/commands/workflows/hg/branch.rb +28 -0
  44. data/lib/amp/commands/commands/workflows/hg/branches.rb +30 -0
  45. data/lib/amp/commands/commands/workflows/hg/bundle.rb +115 -0
  46. data/lib/amp/commands/commands/workflows/hg/clone.rb +95 -0
  47. data/lib/amp/commands/commands/workflows/hg/commit.rb +42 -0
  48. data/lib/amp/commands/commands/workflows/hg/copy.rb +31 -0
  49. data/lib/amp/commands/commands/workflows/hg/debug/dirstate.rb +32 -0
  50. data/lib/amp/commands/commands/workflows/hg/debug/index.rb +36 -0
  51. data/lib/amp/commands/commands/workflows/hg/default.rb +9 -0
  52. data/lib/amp/commands/commands/workflows/hg/diff.rb +30 -0
  53. data/lib/amp/commands/commands/workflows/hg/forget.rb +11 -0
  54. data/lib/amp/commands/commands/workflows/hg/heads.rb +25 -0
  55. data/lib/amp/commands/commands/workflows/hg/identify.rb +23 -0
  56. data/lib/amp/commands/commands/workflows/hg/import.rb +135 -0
  57. data/lib/amp/commands/commands/workflows/hg/incoming.rb +85 -0
  58. data/lib/amp/commands/commands/workflows/hg/info.rb +18 -0
  59. data/lib/amp/commands/commands/workflows/hg/log.rb +21 -0
  60. data/lib/amp/commands/commands/workflows/hg/manifest.rb +13 -0
  61. data/lib/amp/commands/commands/workflows/hg/merge.rb +53 -0
  62. data/lib/amp/commands/commands/workflows/hg/move.rb +28 -0
  63. data/lib/amp/commands/commands/workflows/hg/outgoing.rb +61 -0
  64. data/lib/amp/commands/commands/workflows/hg/pull.rb +74 -0
  65. data/lib/amp/commands/commands/workflows/hg/push.rb +20 -0
  66. data/lib/amp/commands/commands/workflows/hg/remove.rb +45 -0
  67. data/lib/amp/commands/commands/workflows/hg/resolve.rb +83 -0
  68. data/lib/amp/commands/commands/workflows/hg/revert.rb +53 -0
  69. data/lib/amp/commands/commands/workflows/hg/root.rb +13 -0
  70. data/lib/amp/commands/commands/workflows/hg/serve.rb +38 -0
  71. data/lib/amp/commands/commands/workflows/hg/status.rb +116 -0
  72. data/lib/amp/commands/commands/workflows/hg/tag.rb +69 -0
  73. data/lib/amp/commands/commands/workflows/hg/tags.rb +27 -0
  74. data/lib/amp/commands/commands/workflows/hg/tip.rb +13 -0
  75. data/lib/amp/commands/commands/workflows/hg/update.rb +27 -0
  76. data/lib/amp/commands/commands/workflows/hg/verify.rb +9 -0
  77. data/lib/amp/commands/commands/workflows/hg/view.rb +36 -0
  78. data/lib/amp/commands/dispatch.rb +181 -0
  79. data/lib/amp/commands/hooks.rb +81 -0
  80. data/lib/amp/dependencies/amp_support.rb +1 -0
  81. data/lib/amp/dependencies/amp_support/ruby_amp_support.rb +103 -0
  82. data/lib/amp/dependencies/minitar.rb +979 -0
  83. data/lib/amp/dependencies/priority_queue.rb +18 -0
  84. data/lib/amp/dependencies/priority_queue/c_priority_queue.rb +1 -0
  85. data/lib/amp/dependencies/priority_queue/poor_priority_queue.rb +46 -0
  86. data/lib/amp/dependencies/priority_queue/ruby_priority_queue.rb +525 -0
  87. data/lib/amp/dependencies/python_config.rb +211 -0
  88. data/lib/amp/dependencies/trollop.rb +713 -0
  89. data/lib/amp/dependencies/zip/ioextras.rb +155 -0
  90. data/lib/amp/dependencies/zip/stdrubyext.rb +111 -0
  91. data/lib/amp/dependencies/zip/tempfile_bugfixed.rb +186 -0
  92. data/lib/amp/dependencies/zip/zip.rb +1850 -0
  93. data/lib/amp/dependencies/zip/zipfilesystem.rb +609 -0
  94. data/lib/amp/dependencies/zip/ziprequire.rb +90 -0
  95. data/lib/amp/encoding/base85.rb +97 -0
  96. data/lib/amp/encoding/binary_diff.rb +82 -0
  97. data/lib/amp/encoding/difflib.rb +166 -0
  98. data/lib/amp/encoding/mercurial_diff.rb +378 -0
  99. data/lib/amp/encoding/mercurial_patch.rb +1 -0
  100. data/lib/amp/encoding/patch.rb +292 -0
  101. data/lib/amp/encoding/pure_ruby/ruby_mercurial_patch.rb +123 -0
  102. data/lib/amp/extensions/ditz.rb +41 -0
  103. data/lib/amp/extensions/lighthouse.rb +167 -0
  104. data/lib/amp/graphs/ancestor.rb +147 -0
  105. data/lib/amp/graphs/copies.rb +261 -0
  106. data/lib/amp/merges/merge_state.rb +164 -0
  107. data/lib/amp/merges/merge_ui.rb +322 -0
  108. data/lib/amp/merges/simple_merge.rb +450 -0
  109. data/lib/amp/profiling_hacks.rb +36 -0
  110. data/lib/amp/repository/branch_manager.rb +234 -0
  111. data/lib/amp/repository/dir_state.rb +950 -0
  112. data/lib/amp/repository/journal.rb +203 -0
  113. data/lib/amp/repository/lock.rb +207 -0
  114. data/lib/amp/repository/repositories/bundle_repository.rb +214 -0
  115. data/lib/amp/repository/repositories/http_repository.rb +377 -0
  116. data/lib/amp/repository/repositories/local_repository.rb +2661 -0
  117. data/lib/amp/repository/repository.rb +94 -0
  118. data/lib/amp/repository/store.rb +485 -0
  119. data/lib/amp/repository/tag_manager.rb +319 -0
  120. data/lib/amp/repository/updatable.rb +532 -0
  121. data/lib/amp/repository/verification.rb +431 -0
  122. data/lib/amp/repository/versioned_file.rb +475 -0
  123. data/lib/amp/revlogs/bundle_revlogs.rb +246 -0
  124. data/lib/amp/revlogs/changegroup.rb +217 -0
  125. data/lib/amp/revlogs/changelog.rb +338 -0
  126. data/lib/amp/revlogs/changeset.rb +521 -0
  127. data/lib/amp/revlogs/file_log.rb +165 -0
  128. data/lib/amp/revlogs/index.rb +493 -0
  129. data/lib/amp/revlogs/manifest.rb +195 -0
  130. data/lib/amp/revlogs/node.rb +18 -0
  131. data/lib/amp/revlogs/revlog.rb +1032 -0
  132. data/lib/amp/revlogs/revlog_support.rb +126 -0
  133. data/lib/amp/server/amp_user.rb +44 -0
  134. data/lib/amp/server/extension/amp_extension.rb +396 -0
  135. data/lib/amp/server/extension/authorization.rb +201 -0
  136. data/lib/amp/server/fancy_http_server.rb +252 -0
  137. data/lib/amp/server/fancy_views/_browser.haml +28 -0
  138. data/lib/amp/server/fancy_views/_diff_file.haml +13 -0
  139. data/lib/amp/server/fancy_views/_navbar.haml +17 -0
  140. data/lib/amp/server/fancy_views/changeset.haml +31 -0
  141. data/lib/amp/server/fancy_views/commits.haml +32 -0
  142. data/lib/amp/server/fancy_views/file.haml +35 -0
  143. data/lib/amp/server/fancy_views/file_diff.haml +23 -0
  144. data/lib/amp/server/fancy_views/harshcss/all_hallows_eve.css +72 -0
  145. data/lib/amp/server/fancy_views/harshcss/amy.css +147 -0
  146. data/lib/amp/server/fancy_views/harshcss/twilight.css +138 -0
  147. data/lib/amp/server/fancy_views/stylesheet.sass +175 -0
  148. data/lib/amp/server/http_server.rb +140 -0
  149. data/lib/amp/server/repo_user_management.rb +287 -0
  150. data/lib/amp/support/amp_config.rb +164 -0
  151. data/lib/amp/support/amp_ui.rb +287 -0
  152. data/lib/amp/support/docs.rb +54 -0
  153. data/lib/amp/support/generator.rb +78 -0
  154. data/lib/amp/support/ignore.rb +144 -0
  155. data/lib/amp/support/loaders.rb +93 -0
  156. data/lib/amp/support/logger.rb +103 -0
  157. data/lib/amp/support/match.rb +151 -0
  158. data/lib/amp/support/multi_io.rb +87 -0
  159. data/lib/amp/support/openers.rb +121 -0
  160. data/lib/amp/support/ruby_19_compatibility.rb +66 -0
  161. data/lib/amp/support/support.rb +1095 -0
  162. data/lib/amp/templates/blank.commit.erb +23 -0
  163. data/lib/amp/templates/blank.log.erb +18 -0
  164. data/lib/amp/templates/default.commit.erb +23 -0
  165. data/lib/amp/templates/default.log.erb +26 -0
  166. data/lib/amp/templates/template.rb +165 -0
  167. data/site/Rakefile +24 -0
  168. data/site/src/about/ampfile.haml +57 -0
  169. data/site/src/about/commands.haml +106 -0
  170. data/site/src/about/index.haml +33 -0
  171. data/site/src/about/performance.haml +31 -0
  172. data/site/src/about/workflows.haml +34 -0
  173. data/site/src/contribute/index.haml +65 -0
  174. data/site/src/contribute/style.haml +297 -0
  175. data/site/src/css/active4d.css +114 -0
  176. data/site/src/css/all_hallows_eve.css +72 -0
  177. data/site/src/css/all_themes.css +3299 -0
  178. data/site/src/css/amp.css +260 -0
  179. data/site/src/css/amy.css +147 -0
  180. data/site/src/css/blackboard.css +88 -0
  181. data/site/src/css/brilliance_black.css +605 -0
  182. data/site/src/css/brilliance_dull.css +599 -0
  183. data/site/src/css/cobalt.css +149 -0
  184. data/site/src/css/cur_amp.css +185 -0
  185. data/site/src/css/dawn.css +121 -0
  186. data/site/src/css/eiffel.css +121 -0
  187. data/site/src/css/espresso_libre.css +109 -0
  188. data/site/src/css/idle.css +62 -0
  189. data/site/src/css/iplastic.css +80 -0
  190. data/site/src/css/lazy.css +73 -0
  191. data/site/src/css/mac_classic.css +123 -0
  192. data/site/src/css/magicwb_amiga.css +104 -0
  193. data/site/src/css/pastels_on_dark.css +188 -0
  194. data/site/src/css/reset.css +55 -0
  195. data/site/src/css/slush_poppies.css +85 -0
  196. data/site/src/css/spacecadet.css +51 -0
  197. data/site/src/css/sunburst.css +180 -0
  198. data/site/src/css/twilight.css +137 -0
  199. data/site/src/css/zenburnesque.css +91 -0
  200. data/site/src/get/index.haml +32 -0
  201. data/site/src/helpers.rb +121 -0
  202. data/site/src/images/amp_logo.png +0 -0
  203. data/site/src/images/carbonica.png +0 -0
  204. data/site/src/images/revolution.png +0 -0
  205. data/site/src/images/tab-bg.png +0 -0
  206. data/site/src/images/tab-sliding-left.png +0 -0
  207. data/site/src/images/tab-sliding-right.png +0 -0
  208. data/site/src/include/_footer.haml +22 -0
  209. data/site/src/include/_header.haml +17 -0
  210. data/site/src/index.haml +104 -0
  211. data/site/src/learn/index.haml +46 -0
  212. data/site/src/scripts/jquery-1.3.2.min.js +19 -0
  213. data/site/src/scripts/jquery.cookie.js +96 -0
  214. data/tasks/stats.rake +155 -0
  215. data/tasks/yard.rake +171 -0
  216. data/test/dirstate_tests/dirstate +0 -0
  217. data/test/dirstate_tests/hgrc +5 -0
  218. data/test/dirstate_tests/test_dir_state.rb +192 -0
  219. data/test/functional_tests/resources/.hgignore +2 -0
  220. data/test/functional_tests/resources/STYLE.txt +25 -0
  221. data/test/functional_tests/resources/command.rb +372 -0
  222. data/test/functional_tests/resources/commands/annotate.rb +57 -0
  223. data/test/functional_tests/resources/commands/experimental/lolcats.rb +17 -0
  224. data/test/functional_tests/resources/commands/heads.rb +22 -0
  225. data/test/functional_tests/resources/commands/manifest.rb +12 -0
  226. data/test/functional_tests/resources/commands/status.rb +90 -0
  227. data/test/functional_tests/resources/version2/.hgignore +5 -0
  228. data/test/functional_tests/resources/version2/STYLE.txt +25 -0
  229. data/test/functional_tests/resources/version2/command.rb +372 -0
  230. data/test/functional_tests/resources/version2/commands/annotate.rb +45 -0
  231. data/test/functional_tests/resources/version2/commands/experimental/lolcats.rb +17 -0
  232. data/test/functional_tests/resources/version2/commands/heads.rb +22 -0
  233. data/test/functional_tests/resources/version2/commands/manifest.rb +12 -0
  234. data/test/functional_tests/resources/version2/commands/status.rb +90 -0
  235. data/test/functional_tests/resources/version3/.hgignore +5 -0
  236. data/test/functional_tests/resources/version3/STYLE.txt +31 -0
  237. data/test/functional_tests/resources/version3/command.rb +376 -0
  238. data/test/functional_tests/resources/version3/commands/annotate.rb +45 -0
  239. data/test/functional_tests/resources/version3/commands/experimental/lolcats.rb +17 -0
  240. data/test/functional_tests/resources/version3/commands/heads.rb +22 -0
  241. data/test/functional_tests/resources/version3/commands/manifest.rb +12 -0
  242. data/test/functional_tests/resources/version3/commands/status.rb +90 -0
  243. data/test/functional_tests/resources/version4/.hgignore +5 -0
  244. data/test/functional_tests/resources/version4/STYLE.txt +31 -0
  245. data/test/functional_tests/resources/version4/command.rb +376 -0
  246. data/test/functional_tests/resources/version4/commands/experimental/lolcats.rb +17 -0
  247. data/test/functional_tests/resources/version4/commands/heads.rb +22 -0
  248. data/test/functional_tests/resources/version4/commands/manifest.rb +12 -0
  249. data/test/functional_tests/resources/version4/commands/stats.rb +25 -0
  250. data/test/functional_tests/resources/version4/commands/status.rb +90 -0
  251. data/test/functional_tests/resources/version5_1/.hgignore +5 -0
  252. data/test/functional_tests/resources/version5_1/STYLE.txt +2 -0
  253. data/test/functional_tests/resources/version5_1/command.rb +374 -0
  254. data/test/functional_tests/resources/version5_1/commands/experimental/lolcats.rb +17 -0
  255. data/test/functional_tests/resources/version5_1/commands/heads.rb +22 -0
  256. data/test/functional_tests/resources/version5_1/commands/manifest.rb +12 -0
  257. data/test/functional_tests/resources/version5_1/commands/stats.rb +25 -0
  258. data/test/functional_tests/resources/version5_1/commands/status.rb +90 -0
  259. data/test/functional_tests/resources/version5_2/.hgignore +5 -0
  260. data/test/functional_tests/resources/version5_2/STYLE.txt +14 -0
  261. data/test/functional_tests/resources/version5_2/command.rb +376 -0
  262. data/test/functional_tests/resources/version5_2/commands/experimental/lolcats.rb +17 -0
  263. data/test/functional_tests/resources/version5_2/commands/manifest.rb +12 -0
  264. data/test/functional_tests/resources/version5_2/commands/newz.rb +12 -0
  265. data/test/functional_tests/resources/version5_2/commands/stats.rb +25 -0
  266. data/test/functional_tests/resources/version5_2/commands/status.rb +90 -0
  267. data/test/functional_tests/test_functional.rb +604 -0
  268. data/test/localrepo_tests/test_local_repo.rb +121 -0
  269. data/test/localrepo_tests/testrepo.tar.gz +0 -0
  270. data/test/manifest_tests/00manifest.i +0 -0
  271. data/test/manifest_tests/test_manifest.rb +72 -0
  272. data/test/merge_tests/base.txt +10 -0
  273. data/test/merge_tests/expected.local.txt +16 -0
  274. data/test/merge_tests/local.txt +11 -0
  275. data/test/merge_tests/remote.txt +11 -0
  276. data/test/merge_tests/test_merge.rb +26 -0
  277. data/test/revlog_tests/00changelog.i +0 -0
  278. data/test/revlog_tests/revision_added_changelog.i +0 -0
  279. data/test/revlog_tests/test_adding_index.i +0 -0
  280. data/test/revlog_tests/test_revlog.rb +333 -0
  281. data/test/revlog_tests/testindex.i +0 -0
  282. data/test/store_tests/store.tar.gz +0 -0
  283. data/test/store_tests/test_fncache_store.rb +122 -0
  284. data/test/test_amp.rb +9 -0
  285. data/test/test_base85.rb +14 -0
  286. data/test/test_bdiff.rb +42 -0
  287. data/test/test_commands.rb +122 -0
  288. data/test/test_difflib.rb +50 -0
  289. data/test/test_helper.rb +15 -0
  290. data/test/test_journal.rb +29 -0
  291. data/test/test_match.rb +134 -0
  292. data/test/test_mdiff.rb +74 -0
  293. data/test/test_mpatch.rb +14 -0
  294. data/test/test_support.rb +24 -0
  295. metadata +385 -0
@@ -0,0 +1 @@
1
+ amp_c_extension '../../../ext/amp/support/Support', 'amp_support/ruby_amp_support'
@@ -0,0 +1,103 @@
1
+ ##
2
+ # Ruby versions of slow functions we've implemented in C
3
+ class Integer
4
+
5
+ ##
6
+ # Used for byte-swapping a 64-bit double long.
7
+ # Unfortuantely, this will invoke bignum logic, which is ridiculously slow.
8
+ # That's why we have a C extension.
9
+ #
10
+ # If the system is little endian, we work some magic. If the system is big
11
+ # endian, we just return self.
12
+ #
13
+ # @return [Integer] the number swapped as if it were a 64-bit integer
14
+ def byte_swap_64
15
+ if Amp::Support::SYSTEM[:endian] == :little
16
+ ((self >> 56)) | ((self & 0x00FF000000000000) >> 40) |
17
+ ((self & 0x0000FF0000000000) >> 24) | ((self & 0x000000FF00000000) >> 8 ) |
18
+ ((self & 0x00000000FF000000) << 8 ) | ((self & 0x0000000000FF0000) << 24) |
19
+ ((self & 0x000000000000FF00) << 40) | ((self & 0x00000000000000FF) << 56)
20
+ else
21
+ self
22
+ end
23
+ end
24
+
25
+ ##
26
+ # Returns the number as if it were a signed 16-bit integer. Since unpack() always returns
27
+ # unsigned integers, we have to sign them here.
28
+ #
29
+ # @return [Integer] the number overflowed if it would overflow as a 16-bit integer
30
+ def to_signed_16
31
+ return self if self < 32785
32
+ return self - 65536
33
+ end
34
+
35
+ ##
36
+ # Returns the number as if it were a signed 32-bit integer. Since unpack() always returns
37
+ # unsigned integers, we have to sign them here.
38
+ #
39
+ # @return [Integer] the number overflowed if it would overflow as a 32-bit integer
40
+ def to_signed_32
41
+ return self if self < 2147483648
42
+ return self - 4294967296
43
+ end
44
+
45
+ ##
46
+ # Converts to a useful symbol for dir_state. States are represented as a character
47
+ # in the DirState file, such as "n" for "normal". We will read them in as a number,
48
+ # then convert those numbers to a nice symbol!
49
+ #
50
+ # @example "n".ord.to_dirstate_symbol #=> :normal
51
+ # @return [Symbol] a symbol representing the dirstate state this number stands for
52
+ def to_dirstate_symbol
53
+ case self
54
+ when 110 # "n".ord
55
+ :normal
56
+ when 63 # "?".ord
57
+ :untracked
58
+ when 97 # "a".ord
59
+ :added
60
+ when 109 # "m".ord
61
+ :merged
62
+ when 114 # "r".ord
63
+ :removed
64
+ else
65
+ raise "No known hg value for #{self}"
66
+ end
67
+ end
68
+ end
69
+
70
+ class String
71
+
72
+ if RUBY_VERSION < "1.9"
73
+ ##
74
+ # Converts a string of hex into the binary values it represents. This is used for
75
+ # when we store a node ID in a human-readable format, and need to convert it back.
76
+ #
77
+ # @example "DEADBEEF".unhexlify #=> "\336\255\276\357"
78
+ # @return [String] the string decoded from hex form
79
+ def unhexlify
80
+ str = "\000" * (size/2)
81
+ c = 0
82
+ (0..size-2).step(2) do |i|
83
+ hex = self[i,2].to_i(16)
84
+ str[c] = hex
85
+ c += 1
86
+ end
87
+ str
88
+ end
89
+ else
90
+ def unhexlify
91
+ str = "\000" * (size/2)
92
+ c = 0
93
+ (0..size-2).step(2) do |i|
94
+ hex = self[i,2].to_i(16)
95
+ str[c] = hex.chr
96
+ c += 1
97
+ end
98
+ str
99
+ end
100
+ end
101
+ end
102
+
103
+
@@ -0,0 +1,979 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Archive::Tar::Minitar 0.5.2
4
+ # Copyright 2004 Mauricio Julio Ferna'ndez Pradier and Austin Ziegler
5
+ #
6
+ # This program is based on and incorporates parts of RPA::Package from
7
+ # rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and has been
8
+ # adapted to be more generic by Austin.
9
+ #
10
+ # It is licensed under the GNU General Public Licence or Ruby's licence.
11
+ #
12
+ # $Id: minitar.rb 213 2008-02-26 22:32:11Z austin $
13
+ #++
14
+
15
+ module Archive; end
16
+ module Archive::Tar; end
17
+
18
+ # = Archive::Tar::PosixHeader
19
+ # Implements the POSIX tar header as a Ruby class. The structure of
20
+ # the POSIX tar header is:
21
+ #
22
+ # struct tarfile_entry_posix
23
+ # { // pack/unpack
24
+ # char name[100]; // ASCII (+ Z unless filled) a100/Z100
25
+ # char mode[8]; // 0 padded, octal, null a8 /A8
26
+ # char uid[8]; // ditto a8 /A8
27
+ # char gid[8]; // ditto a8 /A8
28
+ # char size[12]; // 0 padded, octal, null a12 /A12
29
+ # char mtime[12]; // 0 padded, octal, null a12 /A12
30
+ # char checksum[8]; // 0 padded, octal, null, space a8 /A8
31
+ # char typeflag[1]; // see below a /a
32
+ # char linkname[100]; // ASCII + (Z unless filled) a100/Z100
33
+ # char magic[6]; // "ustar\0" a6 /A6
34
+ # char version[2]; // "00" a2 /A2
35
+ # char uname[32]; // ASCIIZ a32 /Z32
36
+ # char gname[32]; // ASCIIZ a32 /Z32
37
+ # char devmajor[8]; // 0 padded, octal, null a8 /A8
38
+ # char devminor[8]; // 0 padded, octal, null a8 /A8
39
+ # char prefix[155]; // ASCII (+ Z unless filled) a155/Z155
40
+ # };
41
+ #
42
+ # The +typeflag+ may be one of the following known values:
43
+ #
44
+ # <tt>"0"</tt>:: Regular file. NULL should be treated as a synonym, for
45
+ # compatibility purposes.
46
+ # <tt>"1"</tt>:: Hard link.
47
+ # <tt>"2"</tt>:: Symbolic link.
48
+ # <tt>"3"</tt>:: Character device node.
49
+ # <tt>"4"</tt>:: Block device node.
50
+ # <tt>"5"</tt>:: Directory.
51
+ # <tt>"6"</tt>:: FIFO node.
52
+ # <tt>"7"</tt>:: Reserved.
53
+ #
54
+ # POSIX indicates that "A POSIX-compliant implementation must treat any
55
+ # unrecognized typeflag value as a regular file."
56
+ class Archive::Tar::PosixHeader
57
+ FIELDS = %w(name mode uid gid size mtime checksum typeflag linkname) +
58
+ %w(magic version uname gname devmajor devminor prefix)
59
+
60
+ FIELDS.each { |field| attr_reader field.intern }
61
+
62
+ HEADER_PACK_FORMAT = "a100a8a8a8a12a12a7aaa100a6a2a32a32a8a8a155"
63
+ HEADER_UNPACK_FORMAT = "Z100A8A8A8A12A12A8aZ100A6A2Z32Z32A8A8Z155"
64
+
65
+ # Creates a new PosixHeader from a data stream.
66
+ def self.new_from_stream(stream)
67
+ data = stream.read(512)
68
+ fields = data.unpack(HEADER_UNPACK_FORMAT)
69
+ name = fields.shift
70
+ mode = fields.shift.oct
71
+ uid = fields.shift.oct
72
+ gid = fields.shift.oct
73
+ size = fields.shift.oct
74
+ mtime = fields.shift.oct
75
+ checksum = fields.shift.oct
76
+ typeflag = fields.shift
77
+ linkname = fields.shift
78
+ magic = fields.shift
79
+ version = fields.shift.oct
80
+ uname = fields.shift
81
+ gname = fields.shift
82
+ devmajor = fields.shift.oct
83
+ devminor = fields.shift.oct
84
+ prefix = fields.shift
85
+
86
+ empty = (data == "\0" * 512)
87
+
88
+ new(:name => name, :mode => mode, :uid => uid, :gid => gid,
89
+ :size => size, :mtime => mtime, :checksum => checksum,
90
+ :typeflag => typeflag, :magic => magic, :version => version,
91
+ :uname => uname, :gname => gname, :devmajor => devmajor,
92
+ :devminor => devminor, :prefix => prefix, :empty => empty)
93
+ end
94
+
95
+ # Creates a new PosixHeader. A PosixHeader cannot be created unless the
96
+ # #name, #size, #prefix, and #mode are provided.
97
+ def initialize(vals)
98
+ unless vals[:name] && vals[:size] && vals[:prefix] && vals[:mode]
99
+ raise ArgumentError
100
+ end
101
+
102
+ vals[:mtime] ||= 0
103
+ vals[:checksum] ||= ""
104
+ vals[:typeflag] ||= "0"
105
+ vals[:magic] ||= "ustar"
106
+ vals[:version] ||= "00"
107
+
108
+ FIELDS.each do |field|
109
+ instance_variable_set("@#{field}", vals[field.intern])
110
+ end
111
+ @empty = vals[:empty]
112
+ end
113
+
114
+ def empty?
115
+ @empty
116
+ end
117
+
118
+ def to_s
119
+ update_checksum
120
+ header(@checksum)
121
+ end
122
+
123
+ # Update the checksum field.
124
+ def update_checksum
125
+ hh = header(" " * 8)
126
+ @checksum = oct(calculate_checksum(hh), 6)
127
+ end
128
+
129
+ private
130
+ def oct(num, len)
131
+ if num.nil?
132
+ "\0" * (len + 1)
133
+ else
134
+ "%0#{len}o" % num
135
+ end
136
+ end
137
+
138
+ def calculate_checksum(hdr)
139
+ hdr.unpack("C*").inject { |aa, bb| aa + bb }
140
+ end
141
+
142
+ def header(chksum)
143
+ arr = [name, oct(mode, 7), oct(uid, 7), oct(gid, 7), oct(size, 11),
144
+ oct(mtime, 11), chksum, " ", typeflag, linkname, magic, version,
145
+ uname, gname, oct(devmajor, 7), oct(devminor, 7), prefix]
146
+ str = arr.pack(HEADER_PACK_FORMAT)
147
+ str + "\0" * ((512 - str.size) % 512)
148
+ end
149
+ end
150
+
151
+ require 'fileutils'
152
+ require 'find'
153
+
154
+ # = Archive::Tar::Minitar 0.5.2
155
+ # Archive::Tar::Minitar is a pure-Ruby library and command-line
156
+ # utility that provides the ability to deal with POSIX tar(1) archive
157
+ # files. The implementation is based heavily on Mauricio Ferna'ndez's
158
+ # implementation in rpa-base, but has been reorganised to promote
159
+ # reuse in other projects.
160
+ #
161
+ # This tar class performs a subset of all tar (POSIX tape archive)
162
+ # operations. We can only deal with typeflags 0, 1, 2, and 5 (see
163
+ # Archive::Tar::PosixHeader). All other typeflags will be treated as
164
+ # normal files.
165
+ #
166
+ # NOTE::: support for typeflags 1 and 2 is not yet implemented in this
167
+ # version.
168
+ #
169
+ # This release is version 0.5.2. The library can only handle files and
170
+ # directories at this point. A future version will be expanded to
171
+ # handle symbolic links and hard links in a portable manner. The
172
+ # command line utility, minitar, can only create archives, extract
173
+ # from archives, and list archive contents.
174
+ #
175
+ # == Synopsis
176
+ # Using this library is easy. The simplest case is:
177
+ #
178
+ # require 'zlib'
179
+ # require 'archive/tar/minitar'
180
+ # include Archive::Tar
181
+ #
182
+ # # Packs everything that matches Find.find('tests')
183
+ # File.open('test.tar', 'wb') { |tar| Minitar.pack('tests', tar) }
184
+ # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
185
+ # Minitar.unpack('test.tar', 'x')
186
+ #
187
+ # A gzipped tar can be written with:
188
+ #
189
+ # tgz = Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
190
+ # # Warning: tgz will be closed!
191
+ # Minitar.pack('tests', tgz)
192
+ #
193
+ # tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
194
+ # # Warning: tgz will be closed!
195
+ # Minitar.unpack(tgz, 'x')
196
+ #
197
+ # As the case above shows, one need not write to a file. However, it
198
+ # will sometimes require that one dive a little deeper into the API,
199
+ # as in the case of StringIO objects. Note that I'm not providing a
200
+ # block with Minitar::Output, as Minitar::Output#close automatically
201
+ # closes both the Output object and the wrapped data stream object.
202
+ #
203
+ # begin
204
+ # sgz = Zlib::GzipWriter.new(StringIO.new(""))
205
+ # tar = Output.new(sgz)
206
+ # Find.find('tests') do |entry|
207
+ # Minitar.pack_file(entry, tar)
208
+ # end
209
+ # ensure
210
+ # # Closes both tar and sgz.
211
+ # tar.close
212
+ # end
213
+ #
214
+ # == Copyright
215
+ # Copyright 2004 Mauricio Julio Ferna'ndez Pradier and Austin Ziegler
216
+ #
217
+ # This program is based on and incorporates parts of RPA::Package from
218
+ # rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and
219
+ # has been adapted to be more generic by Austin.
220
+ #
221
+ # 'minitar' contains an adaptation of Ruby/ProgressBar by Satoru
222
+ # Takabayashi <satoru@namazu.org>, copyright 2001 - 2004.
223
+ #
224
+ # This program is free software. It may be redistributed and/or
225
+ # modified under the terms of the GPL version 2 (or later) or Ruby's
226
+ # licence.
227
+ module Archive::Tar::Minitar
228
+ VERSION = "0.5.2"
229
+
230
+ # The exception raised when a wrapped data stream class is expected to
231
+ # respond to #rewind or #pos but does not.
232
+ class NonSeekableStream < StandardError; end
233
+ # The exception raised when a block is required for proper operation of
234
+ # the method.
235
+ class BlockRequired < ArgumentError; end
236
+ # The exception raised when operations are performed on a stream that has
237
+ # previously been closed.
238
+ class ClosedStream < StandardError; end
239
+ # The exception raised when a filename exceeds 256 bytes in length,
240
+ # the maximum supported by the standard Tar format.
241
+ class FileNameTooLong < StandardError; end
242
+ # The exception raised when a data stream ends before the amount of data
243
+ # expected in the archive's PosixHeader.
244
+ class UnexpectedEOF < StandardError; end
245
+
246
+ # The class that writes a tar format archive to a data stream.
247
+ class Writer
248
+ # A stream wrapper that can only be written to. Any attempt to read
249
+ # from this restricted stream will result in a NameError being thrown.
250
+ class RestrictedStream
251
+ def initialize(anIO)
252
+ @io = anIO
253
+ end
254
+
255
+ def write(data)
256
+ @io.write(data)
257
+ end
258
+ end
259
+
260
+ # A RestrictedStream that also has a size limit.
261
+ class BoundedStream < Archive::Tar::Minitar::Writer::RestrictedStream
262
+ # The exception raised when the user attempts to write more data to
263
+ # a BoundedStream than has been allocated.
264
+ class FileOverflow < RuntimeError; end
265
+
266
+ # The maximum number of bytes that may be written to this data
267
+ # stream.
268
+ attr_reader :limit
269
+ # The current total number of bytes written to this data stream.
270
+ attr_reader :written
271
+
272
+ def initialize(io, limit)
273
+ @io = io
274
+ @limit = limit
275
+ @written = 0
276
+ end
277
+
278
+ def write(data)
279
+ raise FileOverflow if (data.size + @written) > @limit
280
+ @io.write(data)
281
+ @written += data.size
282
+ data.size
283
+ end
284
+ end
285
+
286
+ # With no associated block, +Writer::open+ is a synonym for
287
+ # +Writer::new+. If the optional code block is given, it will be
288
+ # passed the new _writer_ as an argument and the Writer object will
289
+ # automatically be closed when the block terminates. In this instance,
290
+ # +Writer::open+ returns the value of the block.
291
+ def self.open(anIO)
292
+ writer = Writer.new(anIO)
293
+
294
+ return writer unless block_given?
295
+
296
+ begin
297
+ res = yield writer
298
+ ensure
299
+ writer.close
300
+ end
301
+
302
+ res
303
+ end
304
+
305
+ # Creates and returns a new Writer object.
306
+ def initialize(anIO)
307
+ @io = anIO
308
+ @closed = false
309
+ end
310
+
311
+ # Adds a file to the archive as +name+. +opts+ must contain the
312
+ # following values:
313
+ #
314
+ # <tt>:mode</tt>:: The Unix file permissions mode value.
315
+ # <tt>:size</tt>:: The size, in bytes.
316
+ #
317
+ # +opts+ may contain the following values:
318
+ #
319
+ # <tt>:uid</tt>: The Unix file owner user ID number.
320
+ # <tt>:gid</tt>: The Unix file owner group ID number.
321
+ # <tt>:mtime</tt>:: The *integer* modification time value.
322
+ #
323
+ # It will not be possible to add more than <tt>opts[:size]</tt> bytes
324
+ # to the file.
325
+ def add_file_simple(name, opts = {}) # :yields BoundedStream:
326
+ raise Archive::Tar::Minitar::BlockRequired unless block_given?
327
+ raise Archive::Tar::ClosedStream if @closed
328
+
329
+ name, prefix = split_name(name)
330
+
331
+ header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
332
+ :size => opts[:size], :gid => opts[:gid], :uid => opts[:uid],
333
+ :prefix => prefix }
334
+ header = Archive::Tar::PosixHeader.new(header).to_s
335
+ @io.write(header)
336
+
337
+ os = BoundedStream.new(@io, opts[:size])
338
+ yield os
339
+ # FIXME: what if an exception is raised in the block?
340
+
341
+ min_padding = opts[:size] - os.written
342
+ @io.write("\0" * min_padding)
343
+ remainder = (512 - (opts[:size] % 512)) % 512
344
+ @io.write("\0" * remainder)
345
+ end
346
+
347
+ # Adds a file to the archive as +name+. +opts+ must contain the
348
+ # following value:
349
+ #
350
+ # <tt>:mode</tt>:: The Unix file permissions mode value.
351
+ #
352
+ # +opts+ may contain the following values:
353
+ #
354
+ # <tt>:uid</tt>: The Unix file owner user ID number.
355
+ # <tt>:gid</tt>: The Unix file owner group ID number.
356
+ # <tt>:mtime</tt>:: The *integer* modification time value.
357
+ #
358
+ # The file's size will be determined from the amount of data written
359
+ # to the stream.
360
+ #
361
+ # For #add_file to be used, the Archive::Tar::Minitar::Writer must be
362
+ # wrapping a stream object that is seekable (e.g., it responds to
363
+ # #pos=). Otherwise, #add_file_simple must be used.
364
+ #
365
+ # +opts+ may be modified during the writing to the stream.
366
+ def add_file(name, opts = {}) # :yields RestrictedStream, +opts+:
367
+ raise Archive::Tar::Minitar::BlockRequired unless block_given?
368
+ raise Archive::Tar::Minitar::ClosedStream if @closed
369
+ raise Archive::Tar::Minitar::NonSeekableStream unless @io.respond_to?(:pos=)
370
+
371
+ name, prefix = split_name(name)
372
+ init_pos = @io.pos
373
+ @io.write("\0" * 512) # placeholder for the header
374
+
375
+ yield RestrictedStream.new(@io), opts
376
+ # FIXME: what if an exception is raised in the block?
377
+
378
+ size = @io.pos - (init_pos + 512)
379
+ remainder = (512 - (size % 512)) % 512
380
+ @io.write("\0" * remainder)
381
+
382
+ final_pos = @io.pos
383
+ @io.pos = init_pos
384
+
385
+ header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
386
+ :size => size, :gid => opts[:gid], :uid => opts[:uid],
387
+ :prefix => prefix }
388
+ header = Archive::Tar::PosixHeader.new(header).to_s
389
+ @io.write(header)
390
+ @io.pos = final_pos
391
+ end
392
+
393
+ # Creates a directory in the tar.
394
+ def mkdir(name, opts = {})
395
+ raise ClosedStream if @closed
396
+ name, prefix = split_name(name)
397
+ header = { :name => name, :mode => opts[:mode], :typeflag => "5",
398
+ :size => 0, :gid => opts[:gid], :uid => opts[:uid],
399
+ :mtime => opts[:mtime], :prefix => prefix }
400
+ header = Archive::Tar::PosixHeader.new(header).to_s
401
+ @io.write(header)
402
+ nil
403
+ end
404
+
405
+ # Passes the #flush method to the wrapped stream, used for buffered
406
+ # streams.
407
+ def flush
408
+ raise ClosedStream if @closed
409
+ @io.flush if @io.respond_to?(:flush)
410
+ end
411
+
412
+ # Closes the Writer.
413
+ def close
414
+ return if @closed
415
+ @io.write("\0" * 1024)
416
+ @closed = true
417
+ end
418
+
419
+ private
420
+ def split_name(name)
421
+ raise FileNameTooLong if name.size > 256
422
+ if name.size <= 100
423
+ prefix = ""
424
+ else
425
+ parts = name.split(/\//)
426
+ newname = parts.pop
427
+
428
+ nxt = ""
429
+
430
+ loop do
431
+ nxt = parts.pop
432
+ break if newname.size + 1 + nxt.size > 100
433
+ newname = "#{nxt}/#{newname}"
434
+ end
435
+
436
+ prefix = (parts + [nxt]).join("/")
437
+
438
+ name = newname
439
+
440
+ raise FileNameTooLong if name.size > 100 || prefix.size > 155
441
+ end
442
+ return name, prefix
443
+ end
444
+ end
445
+
446
+ # The class that reads a tar format archive from a data stream. The data
447
+ # stream may be sequential or random access, but certain features only work
448
+ # with random access data streams.
449
+ class Reader
450
+ # This marks the EntryStream closed for reading without closing the
451
+ # actual data stream.
452
+ module InvalidEntryStream
453
+ def read(len = nil); raise ClosedStream; end
454
+ def getc; raise ClosedStream; end
455
+ def rewind; raise ClosedStream; end
456
+ end
457
+
458
+ # EntryStreams are pseudo-streams on top of the main data stream.
459
+ class EntryStream
460
+ Archive::Tar::PosixHeader::FIELDS.each do |field|
461
+ attr_reader field.intern
462
+ end
463
+
464
+ def initialize(header, anIO)
465
+ @io = anIO
466
+ @name = header.name
467
+ @mode = header.mode
468
+ @uid = header.uid
469
+ @gid = header.gid
470
+ @size = header.size
471
+ @mtime = header.mtime
472
+ @checksum = header.checksum
473
+ @typeflag = header.typeflag
474
+ @linkname = header.linkname
475
+ @magic = header.magic
476
+ @version = header.version
477
+ @uname = header.uname
478
+ @gname = header.gname
479
+ @devmajor = header.devmajor
480
+ @devminor = header.devminor
481
+ @prefix = header.prefix
482
+ @read = 0
483
+ @orig_pos = @io.pos
484
+ end
485
+
486
+ # Reads +len+ bytes (or all remaining data) from the entry. Returns
487
+ # +nil+ if there is no more data to read.
488
+ def read(len = nil)
489
+ return nil if @read >= @size
490
+ len ||= @size - @read
491
+ max_read = [len, @size - @read].min
492
+ ret = @io.read(max_read)
493
+ @read += ret.size
494
+ ret
495
+ end
496
+
497
+ # Reads one byte from the entry. Returns +nil+ if there is no more data
498
+ # to read.
499
+ def getc
500
+ return nil if @read >= @size
501
+ ret = @io.getc
502
+ @read += 1 if ret
503
+ ret
504
+ end
505
+
506
+ # Returns +true+ if the entry represents a directory.
507
+ def directory?
508
+ @typeflag == "5"
509
+ end
510
+ alias_method :directory, :directory?
511
+
512
+ # Returns +true+ if the entry represents a plain file.
513
+ def file?
514
+ @typeflag == "0"
515
+ end
516
+ alias_method :file, :file?
517
+
518
+ # Returns +true+ if the current read pointer is at the end of the
519
+ # EntryStream data.
520
+ def eof?
521
+ @read >= @size
522
+ end
523
+
524
+ # Returns the current read pointer in the EntryStream.
525
+ def pos
526
+ @read
527
+ end
528
+
529
+ # Sets the current read pointer to the beginning of the EntryStream.
530
+ def rewind
531
+ raise NonSeekableStream unless @io.respond_to?(:pos=)
532
+ @io.pos = @orig_pos
533
+ @read = 0
534
+ end
535
+
536
+ def bytes_read
537
+ @read
538
+ end
539
+
540
+ # Returns the full and proper name of the entry.
541
+ def full_name
542
+ if @prefix != ""
543
+ File.join(@prefix, @name)
544
+ else
545
+ @name
546
+ end
547
+ end
548
+
549
+ # Closes the entry.
550
+ def close
551
+ invalidate
552
+ end
553
+
554
+ private
555
+ def invalidate
556
+ extend InvalidEntryStream
557
+ end
558
+ end
559
+
560
+ # With no associated block, +Reader::open+ is a synonym for
561
+ # +Reader::new+. If the optional code block is given, it will be passed
562
+ # the new _writer_ as an argument and the Reader object will
563
+ # automatically be closed when the block terminates. In this instance,
564
+ # +Reader::open+ returns the value of the block.
565
+ def self.open(anIO)
566
+ reader = Reader.new(anIO)
567
+
568
+ return reader unless block_given?
569
+
570
+ begin
571
+ res = yield reader
572
+ ensure
573
+ reader.close
574
+ end
575
+
576
+ res
577
+ end
578
+
579
+ # Creates and returns a new Reader object.
580
+ def initialize(anIO)
581
+ @io = anIO
582
+ @init_pos = anIO.pos
583
+ end
584
+
585
+ # Iterates through each entry in the data stream.
586
+ def each(&block)
587
+ each_entry(&block)
588
+ end
589
+
590
+ # Resets the read pointer to the beginning of data stream. Do not call
591
+ # this during a #each or #each_entry iteration. This only works with
592
+ # random access data streams that respond to #rewind and #pos.
593
+ def rewind
594
+ if @init_pos == 0
595
+ raise NonSeekableStream unless @io.respond_to?(:rewind)
596
+ @io.rewind
597
+ else
598
+ raise NonSeekableStream unless @io.respond_to?(:pos=)
599
+ @io.pos = @init_pos
600
+ end
601
+ end
602
+
603
+ # Iterates through each entry in the data stream.
604
+ def each_entry
605
+ loop do
606
+ return if @io.eof?
607
+
608
+ header = Archive::Tar::PosixHeader.new_from_stream(@io)
609
+ return if header.empty?
610
+
611
+ entry = EntryStream.new(header, @io)
612
+ size = entry.size
613
+
614
+ yield entry
615
+
616
+ skip = (512 - (size % 512)) % 512
617
+
618
+ if @io.respond_to?(:seek)
619
+ # avoid reading...
620
+ @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
621
+ else
622
+ pending = size - entry.bytes_read
623
+ while pending > 0
624
+ bread = @io.read([pending, 4096].min).size
625
+ raise UnexpectedEOF if @io.eof?
626
+ pending -= bread
627
+ end
628
+ end
629
+ @io.read(skip) # discard trailing zeros
630
+ # make sure nobody can use #read, #getc or #rewind anymore
631
+ entry.close
632
+ end
633
+ end
634
+
635
+ def close
636
+ end
637
+ end
638
+
639
+ # Wraps a Archive::Tar::Minitar::Reader with convenience methods and
640
+ # wrapped stream management; Input only works with random access data
641
+ # streams. See Input::new for details.
642
+ class Input
643
+ include Enumerable
644
+
645
+ # With no associated block, +Input::open+ is a synonym for
646
+ # +Input::new+. If the optional code block is given, it will be passed
647
+ # the new _writer_ as an argument and the Input object will
648
+ # automatically be closed when the block terminates. In this instance,
649
+ # +Input::open+ returns the value of the block.
650
+ def self.open(input)
651
+ stream = Input.new(input)
652
+ return stream unless block_given?
653
+
654
+ begin
655
+ res = yield stream
656
+ ensure
657
+ stream.close
658
+ end
659
+
660
+ res
661
+ end
662
+
663
+ # Creates a new Input object. If +input+ is a stream object that responds
664
+ # to #read), then it will simply be wrapped. Otherwise, one will be
665
+ # created and opened using Kernel#open. When Input#close is called, the
666
+ # stream object wrapped will be closed.
667
+ def initialize(input)
668
+ if input.respond_to?(:read)
669
+ @io = input
670
+ else
671
+ @io = open(input, "rb")
672
+ end
673
+ @tarreader = Archive::Tar::Minitar::Reader.new(@io)
674
+ end
675
+
676
+ # Iterates through each entry and rewinds to the beginning of the stream
677
+ # when finished.
678
+ def each(&block)
679
+ @tarreader.each { |entry| yield entry }
680
+ ensure
681
+ @tarreader.rewind
682
+ end
683
+
684
+ # Extracts the current +entry+ to +destdir+. If a block is provided, it
685
+ # yields an +action+ Symbol, the full name of the file being extracted
686
+ # (+name+), and a Hash of statistical information (+stats+).
687
+ #
688
+ # The +action+ will be one of:
689
+ # <tt>:dir</tt>:: The +entry+ is a directory.
690
+ # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
691
+ # file is just beginning.
692
+ # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
693
+ # of the +entry+.
694
+ # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
695
+ #
696
+ # The +stats+ hash contains the following keys:
697
+ # <tt>:current</tt>:: The current total number of bytes read in the
698
+ # +entry+.
699
+ # <tt>:currinc</tt>:: The current number of bytes read in this read
700
+ # cycle.
701
+ # <tt>:entry</tt>:: The entry being extracted; this is a
702
+ # Reader::EntryStream, with all methods thereof.
703
+ def extract_entry(destdir, entry) # :yields action, name, stats:
704
+ stats = {
705
+ :current => 0,
706
+ :currinc => 0,
707
+ :entry => entry
708
+ }
709
+
710
+ if entry.directory?
711
+ dest = File.join(destdir, entry.full_name)
712
+
713
+ yield :dir, entry.full_name, stats if block_given?
714
+
715
+ if Archive::Tar::Minitar.dir?(dest)
716
+ begin
717
+ FileUtils.chmod(entry.mode, dest)
718
+ rescue Exception
719
+ nil
720
+ end
721
+ else
722
+ FileUtils.mkdir_p(dest, :mode => entry.mode)
723
+ FileUtils.chmod(entry.mode, dest)
724
+ end
725
+
726
+ fsync_dir(dest)
727
+ fsync_dir(File.join(dest, ".."))
728
+ return
729
+ else # it's a file
730
+ destdir = File.join(destdir, File.dirname(entry.full_name))
731
+ FileUtils.mkdir_p(destdir, :mode => 0755)
732
+
733
+ destfile = File.join(destdir, File.basename(entry.full_name))
734
+ FileUtils.chmod(0600, destfile) rescue nil # Errno::ENOENT
735
+
736
+ yield :file_start, entry.full_name, stats if block_given?
737
+
738
+ File.open(destfile, "wb", entry.mode) do |os|
739
+ loop do
740
+ data = entry.read(4096)
741
+ break unless data
742
+
743
+ stats[:currinc] = os.write(data)
744
+ stats[:current] += stats[:currinc]
745
+
746
+ yield :file_progress, entry.full_name, stats if block_given?
747
+ end
748
+ os.fsync
749
+ end
750
+
751
+ FileUtils.chmod(entry.mode, destfile)
752
+ fsync_dir(File.dirname(destfile))
753
+ fsync_dir(File.join(File.dirname(destfile), ".."))
754
+
755
+ yield :file_done, entry.full_name, stats if block_given?
756
+ end
757
+ end
758
+
759
+ # Returns the Reader object for direct access.
760
+ def tar
761
+ @tarreader
762
+ end
763
+
764
+ # Closes the Reader object and the wrapped data stream.
765
+ def close
766
+ @io.close
767
+ @tarreader.close
768
+ end
769
+
770
+ private
771
+ def fsync_dir(dirname)
772
+ # make sure this hits the disc
773
+ dir = open(dirname, 'rb')
774
+ dir.fsync
775
+ rescue # ignore IOError if it's an unpatched (old) Ruby
776
+ nil
777
+ ensure
778
+ dir.close if dir rescue nil
779
+ end
780
+ end
781
+
782
+ # Wraps a Archive::Tar::Minitar::Writer with convenience methods and
783
+ # wrapped stream management; Output only works with random access data
784
+ # streams. See Output::new for details.
785
+ class Output
786
+ # With no associated block, +Output::open+ is a synonym for
787
+ # +Output::new+. If the optional code block is given, it will be passed
788
+ # the new _writer_ as an argument and the Output object will
789
+ # automatically be closed when the block terminates. In this instance,
790
+ # +Output::open+ returns the value of the block.
791
+ def self.open(output)
792
+ stream = Output.new(output)
793
+ return stream unless block_given?
794
+
795
+ begin
796
+ res = yield stream
797
+ ensure
798
+ stream.close
799
+ end
800
+
801
+ res
802
+ end
803
+
804
+ # Creates a new Output object. If +output+ is a stream object that
805
+ # responds to #read), then it will simply be wrapped. Otherwise, one will
806
+ # be created and opened using Kernel#open. When Output#close is called,
807
+ # the stream object wrapped will be closed.
808
+ def initialize(output)
809
+ if output.respond_to?(:write)
810
+ @io = output
811
+ else
812
+ @io = ::File.open(output, "wb")
813
+ end
814
+ @tarwriter = Archive::Tar::Minitar::Writer.new(@io)
815
+ end
816
+
817
+ # Returns the Writer object for direct access.
818
+ def tar
819
+ @tarwriter
820
+ end
821
+
822
+ # Closes the Writer object and the wrapped data stream.
823
+ def close
824
+ @tarwriter.close
825
+ @io.close
826
+ end
827
+ end
828
+
829
+ class << self
830
+ # Tests if +path+ refers to a directory. Fixes an apparently
831
+ # corrupted <tt>stat()</tt> call on Windows.
832
+ def dir?(path)
833
+ File.directory?((path[-1] == ?/) ? path : "#{path}/")
834
+ end
835
+
836
+ # A convenience method for wrapping Archive::Tar::Minitar::Input.open
837
+ # (mode +r+) and Archive::Tar::Minitar::Output.open (mode +w+). No other
838
+ # modes are currently supported.
839
+ def open(dest, mode = "r", &block)
840
+ case mode
841
+ when "r"
842
+ Input.open(dest, &block)
843
+ when "w"
844
+ Output.open(dest, &block)
845
+ else
846
+ raise "Unknown open mode for Archive::Tar::Minitar.open."
847
+ end
848
+ end
849
+
850
+ # A convenience method to packs the file provided. +entry+ may either be
851
+ # a filename (in which case various values for the file (see below) will
852
+ # be obtained from <tt>File#stat(entry)</tt> or a Hash with the fields:
853
+ #
854
+ # <tt>:name</tt>:: The filename to be packed into the tarchive.
855
+ # *REQUIRED*.
856
+ # <tt>:mode</tt>:: The mode to be applied.
857
+ # <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
858
+ # <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
859
+ # <tt>:mtime</tt>:: The modification Time of the file.
860
+ #
861
+ # During packing, if a block is provided, #pack_file yields an +action+
862
+ # Symol, the full name of the file being packed, and a Hash of
863
+ # statistical information, just as with
864
+ # Archive::Tar::Minitar::Input#extract_entry.
865
+ #
866
+ # The +action+ will be one of:
867
+ # <tt>:dir</tt>:: The +entry+ is a directory.
868
+ # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
869
+ # file is just beginning.
870
+ # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
871
+ # of the +entry+.
872
+ # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
873
+ #
874
+ # The +stats+ hash contains the following keys:
875
+ # <tt>:current</tt>:: The current total number of bytes read in the
876
+ # +entry+.
877
+ # <tt>:currinc</tt>:: The current number of bytes read in this read
878
+ # cycle.
879
+ # <tt>:name</tt>:: The filename to be packed into the tarchive.
880
+ # *REQUIRED*.
881
+ # <tt>:mode</tt>:: The mode to be applied.
882
+ # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
883
+ # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
884
+ # <tt>:mtime</tt>:: The modification Time of the file.
885
+ def pack_file(entry, outputter) #:yields action, name, stats:
886
+ outputter = outputter.tar if outputter.kind_of?(Archive::Tar::Minitar::Output)
887
+
888
+ stats = {}
889
+
890
+ if entry.kind_of?(Hash)
891
+ name = entry[:name]
892
+
893
+ entry.each { |kk, vv| stats[kk] = vv unless vv.nil? }
894
+ else
895
+ name = entry
896
+ end
897
+
898
+ name = name.sub(%r{\./}, '')
899
+ stat = File.stat(name)
900
+ stats[:mode] ||= stat.mode
901
+ stats[:mtime] ||= stat.mtime
902
+ stats[:size] = stat.size
903
+
904
+ if RUBY_PLATFORM =~ /win32/
905
+ stats[:uid] = nil
906
+ stats[:gid] = nil
907
+ else
908
+ stats[:uid] ||= stat.uid
909
+ stats[:gid] ||= stat.gid
910
+ end
911
+
912
+ case
913
+ when File.file?(name)
914
+ outputter.add_file_simple(name, stats) do |os|
915
+ stats[:current] = 0
916
+ yield :file_start, name, stats if block_given?
917
+ File.open(name, "rb") do |ff|
918
+ until ff.eof?
919
+ stats[:currinc] = os.write(ff.read(4096))
920
+ stats[:current] += stats[:currinc]
921
+ yield :file_progress, name, stats if block_given?
922
+ end
923
+ end
924
+ yield :file_done, name, stats if block_given?
925
+ end
926
+ when dir?(name)
927
+ yield :dir, name, stats if block_given?
928
+ outputter.mkdir(name, stats)
929
+ else
930
+ raise "Don't yet know how to pack this type of file."
931
+ end
932
+ end
933
+
934
+ # A convenience method to pack files specified by +src+ into +dest+. If
935
+ # +src+ is an Array, then each file detailed therein will be packed into
936
+ # the resulting Archive::Tar::Minitar::Output stream; if +recurse_dirs+
937
+ # is true, then directories will be recursed.
938
+ #
939
+ # If +src+ is an Array, it will be treated as the argument to Find.find;
940
+ # all files matching will be packed.
941
+ def pack(src, dest, recurse_dirs = true, &block)
942
+ Output.open(dest) do |outp|
943
+ if src.kind_of?(Array)
944
+ src.each do |entry|
945
+ pack_file(entry, outp, &block)
946
+ if dir?(entry) and recurse_dirs
947
+ Dir["#{entry}/**/**"].each do |ee|
948
+ pack_file(ee, outp, &block)
949
+ end
950
+ end
951
+ end
952
+ else
953
+ Find.find(src) do |entry|
954
+ pack_file(entry, outp, &block)
955
+ end
956
+ end
957
+ end
958
+ end
959
+
960
+ # A convenience method to unpack files from +src+ into the directory
961
+ # specified by +dest+. Only those files named explicitly in +files+
962
+ # will be extracted.
963
+ def unpack(src, dest, files = [], &block)
964
+ Input.open(src) do |inp|
965
+ if File.exist?(dest) and (not dir?(dest))
966
+ raise "Can't unpack to a non-directory."
967
+ elsif not File.exist?(dest)
968
+ FileUtils.mkdir_p(dest)
969
+ end
970
+
971
+ inp.each do |entry|
972
+ if files.empty? or files.include?(entry.full_name)
973
+ inp.extract_entry(dest, entry, &block)
974
+ end
975
+ end
976
+ end
977
+ end
978
+ end
979
+ end