facets 2.6.0 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (321) hide show
  1. data/HISTORY.rdoc +331 -35
  2. data/MANIFEST +685 -826
  3. data/{doc/guide/notes.rd → NOTES} +0 -0
  4. data/README.rdoc +73 -25
  5. data/Rakefile +245 -2
  6. data/TODO +5 -0
  7. data/demo/hook.rd +47 -0
  8. data/demo/scenario_require.rd +9 -0
  9. data/doc/README.more +24 -6
  10. data/doc/manual/about.rb +47 -0
  11. data/doc/manual/annotations.rdoc +60 -0
  12. data/doc/manual/associations.rdoc +55 -0
  13. data/doc/manual/blockups.rdoc +101 -0
  14. data/doc/manual/capsule.rdoc +34 -0
  15. data/doc/manual/command.rdoc +177 -0
  16. data/doc/manual/core.rdoc +37 -0
  17. data/doc/manual/faq.rdoc +32 -0
  18. data/doc/manual/typecast.html +112 -0
  19. data/lib/core/facets.rb +359 -11
  20. data/lib/core/facets/array/combination.rb +3 -3
  21. data/lib/core/facets/array/index.rb +4 -1
  22. data/lib/core/facets/array/permutation.rb +2 -2
  23. data/lib/core/facets/array/product.rb +1 -1
  24. data/lib/core/facets/binding/eval.rb +1 -1
  25. data/lib/core/facets/denumerable.rb +76 -0
  26. data/lib/core/facets/duplicable.rb +34 -0
  27. data/lib/core/facets/enumerable/count.rb +10 -4
  28. data/lib/core/facets/enumerable/defer.rb +77 -0
  29. data/lib/core/facets/enumerable/each_by.rb +1 -1
  30. data/lib/core/facets/enumerable/every.rb +35 -0
  31. data/lib/{more/facets/elementwise.rb → core/facets/enumerable/ewise.rb} +0 -0
  32. data/lib/core/facets/enumerable/filter.rb +25 -0
  33. data/lib/core/facets/enumerable/group_by.rb +1 -1
  34. data/lib/core/facets/enumerable/none.rb +3 -2
  35. data/lib/core/facets/enumerable/one.rb +3 -2
  36. data/lib/core/facets/enumerable/per.rb +61 -0
  37. data/lib/core/facets/exception/raised.rb +14 -0
  38. data/lib/core/facets/integer/odd.rb +5 -1
  39. data/lib/core/facets/kernel/__dir__.rb +13 -3
  40. data/lib/core/facets/kernel/__here__.rb +14 -0
  41. data/lib/core/facets/kernel/__method__.rb +9 -3
  42. data/lib/core/facets/kernel/ask.rb +1 -0
  43. data/lib/core/facets/kernel/equate.rb +13 -0
  44. data/lib/core/facets/kernel/extension.rb +9 -0
  45. data/lib/core/facets/kernel/identical.rb +4 -0
  46. data/lib/core/facets/kernel/instance_exec.rb +2 -1
  47. data/lib/core/facets/kernel/method.rb +49 -0
  48. data/lib/core/facets/kernel/object_send.rb +2 -2
  49. data/lib/core/facets/kernel/{state.rb → object_state.rb} +23 -12
  50. data/lib/core/facets/kernel/require_all.rb +6 -1
  51. data/lib/core/facets/kernel/require_local.rb +8 -1
  52. data/lib/core/facets/kernel/require_relative.rb +52 -0
  53. data/lib/core/facets/kernel/source_location.rb +13 -0
  54. data/lib/core/facets/kernel/tap.rb +13 -6
  55. data/lib/core/facets/module/attr_setter.rb +57 -0
  56. data/lib/core/facets/module/instance_method.rb +24 -0
  57. data/lib/core/facets/module/module_load.rb +60 -44
  58. data/lib/core/facets/module/module_require.rb +1 -0
  59. data/lib/core/facets/nilclass/to_f.rb +1 -1
  60. data/lib/core/facets/objectspace/op_fetch.rb +3 -0
  61. data/lib/core/facets/proc/curry.rb +4 -3
  62. data/lib/core/facets/string/bytes.rb +10 -4
  63. data/lib/core/facets/string/camelcase.rb +6 -5
  64. data/lib/core/facets/string/chars.rb +5 -1
  65. data/lib/core/facets/string/each_char.rb +1 -1
  66. data/lib/core/facets/string/each_word.rb +1 -1
  67. data/lib/core/facets/string/lines.rb +11 -4
  68. data/lib/core/facets/string/start_with.rb +9 -2
  69. data/lib/core/facets/string/unfold.rb +27 -0
  70. data/lib/core/facets/symbol/succ.rb +3 -3
  71. data/lib/core/facets/symbol/thrown.rb +20 -0
  72. data/lib/core/facets/symbol/to_proc.rb +3 -2
  73. data/lib/core/facets/time/to_time.rb +1 -1
  74. data/lib/core/facets/to_hash.rb +41 -100
  75. data/lib/core/facets/unboundmethod/name.rb +20 -23
  76. data/lib/more/facets/ansicode.rb +1 -10
  77. data/lib/more/facets/autoarray.rb +3 -31
  78. data/lib/more/facets/basicobject.rb +73 -0
  79. data/lib/more/facets/blankslate.rb +2 -66
  80. data/lib/{lore → more}/facets/cgi.rb +0 -0
  81. data/lib/more/facets/class_extend.rb +1 -0
  82. data/lib/{lore → more}/facets/continuation.rb +0 -0
  83. data/lib/{lore → more}/facets/date.rb +3 -3
  84. data/lib/more/facets/enumargs.rb +192 -0
  85. data/lib/more/facets/enumerablepass.rb +2 -216
  86. data/lib/more/facets/enumerator.rb +62 -0
  87. data/lib/more/facets/{equatable.rb → equitable.rb} +11 -11
  88. data/lib/more/facets/expirable.rb +13 -41
  89. data/lib/{lore → more}/facets/fileutils.rb +0 -0
  90. data/lib/{lore → more}/facets/fileutils/head.rb +0 -0
  91. data/lib/{lore → more}/facets/fileutils/safe_ln.rb +0 -0
  92. data/lib/{lore → more}/facets/fileutils/slice.rb +0 -0
  93. data/lib/{lore → more}/facets/fileutils/tail.rb +0 -0
  94. data/lib/{lore → more}/facets/fileutils/wc.rb +0 -0
  95. data/lib/{lore → more}/facets/fileutils/whereis.rb +0 -0
  96. data/lib/{lore → more}/facets/fileutils/which.rb +0 -0
  97. data/lib/{lore → more}/facets/getoptlong.rb +0 -0
  98. data/lib/more/facets/hook.rb +2 -29
  99. data/lib/more/facets/inheritor.rb +2 -2
  100. data/lib/more/facets/instance_eval.rb +50 -0
  101. data/lib/more/facets/instance_function.rb +78 -0
  102. data/lib/more/facets/main.rb +20 -15
  103. data/lib/more/facets/memoize.rb +1 -113
  104. data/lib/more/facets/module/attr.rb +83 -0
  105. data/lib/more/facets/module/attr_tester.rb +44 -0
  106. data/lib/more/facets/module/attr_toggler.rb +59 -0
  107. data/lib/more/facets/module/attr_validator.rb +34 -0
  108. data/lib/more/facets/{class_extension.rb → module/class_extend.rb} +21 -13
  109. data/lib/more/facets/once.rb +59 -0
  110. data/lib/more/facets/openmodule.rb +1 -0
  111. data/lib/more/facets/orderedhash.rb +1 -33
  112. data/lib/{lore → more}/facets/ostruct.rb +0 -0
  113. data/lib/more/facets/ostructable.rb +1 -4
  114. data/lib/more/facets/partial.rb +18 -16
  115. data/lib/{lore → more}/facets/pathname.rb +0 -0
  116. data/lib/more/facets/preinitialize.rb +157 -0
  117. data/lib/{lore → more}/facets/rbconfig.rb +0 -0
  118. data/lib/more/facets/recorder.rb +1 -2
  119. data/lib/{lore → more}/facets/set.rb +0 -0
  120. data/lib/{lore → more}/facets/shellwords.rb +0 -0
  121. data/lib/{lore → more}/facets/uri.rb +0 -0
  122. data/lib/{lore → more}/facets/yaml.rb +0 -0
  123. data/lib/{lore → more}/facets/zlib.rb +0 -0
  124. data/meta/loadpath +0 -1
  125. data/meta/sitemap +4 -0
  126. data/meta/version +1 -1
  127. data/test/core/enumerable/test_count.rb +1 -1
  128. data/test/{more/test_filter.rb → core/enumerable/test_defer.rb} +24 -22
  129. data/test/{more/test_elementor.rb → core/enumerable/test_every.rb} +2 -15
  130. data/test/core/enumerable/test_ewise.rb +23 -0
  131. data/test/core/enumerable/test_per.rb +18 -0
  132. data/test/core/enumerable/test_take.rb +13 -0
  133. data/test/core/kernel/test_deepcopy.rb +1 -1
  134. data/test/{more/test_1stclassmethod.rb → core/kernel/test_method.rb} +2 -7
  135. data/test/core/kernel/test_tap.rb +1 -1
  136. data/test/core/proc/test_curry.rb +11 -0
  137. data/test/core/string/test_bytes.rb +1 -1
  138. data/test/core/string/test_camelcase.rb +23 -6
  139. data/test/core/string/test_lines.rb +1 -1
  140. data/test/core/string/test_unfold.rb +14 -0
  141. data/test/{more → core}/test_blank.rb +0 -0
  142. data/test/{more → core}/test_boolean.rb +0 -0
  143. data/test/{more → core}/test_functor.rb +0 -0
  144. data/test/{lore → more}/test_basicobject.rb +0 -0
  145. data/test/more/{test_class_extension.rb → test_class_extend.rb} +6 -6
  146. data/test/{lore → more}/test_continuation.rb +0 -0
  147. data/test/{lore → more}/test_date.rb +0 -0
  148. data/test/more/{test_enumerablepass.rb → test_enumargs.rb} +2 -4
  149. data/test/more/{test_equatable.rb → test_equitable.rb} +2 -2
  150. data/test/more/{test_instantise.rb → test_instance_function.rb} +3 -2
  151. data/test/more/test_memoize.rb +1 -1
  152. data/test/{lore → more}/test_ostruct.rb +0 -0
  153. metadata +865 -1016
  154. data/RELEASE +0 -38
  155. data/doc/README.lore +0 -51
  156. data/doc/log/basic_stats/index.html +0 -39
  157. data/doc/log/changelog.html +0 -648
  158. data/doc/log/changelog.txt +0 -217
  159. data/doc/log/stats/index.html +0 -39
  160. data/doc/log/testlog.txt +0 -278
  161. data/doc/notes/CHANGES +0 -2529
  162. data/doc/rdoc/lore/classes/Array.html +0 -176
  163. data/doc/rdoc/lore/classes/CGI.html +0 -191
  164. data/doc/rdoc/lore/classes/Config.html +0 -135
  165. data/doc/rdoc/lore/classes/Continuation.html +0 -113
  166. data/doc/rdoc/lore/classes/Date.html +0 -631
  167. data/doc/rdoc/lore/classes/DateTime.html +0 -583
  168. data/doc/rdoc/lore/classes/Enumerable.html +0 -89
  169. data/doc/rdoc/lore/classes/Enumerable/Enumerator.html +0 -147
  170. data/doc/rdoc/lore/classes/File.html +0 -128
  171. data/doc/rdoc/lore/classes/FileUtils.html +0 -434
  172. data/doc/rdoc/lore/classes/GetoptLong.html +0 -118
  173. data/doc/rdoc/lore/classes/GetoptLong/DSL.html +0 -208
  174. data/doc/rdoc/lore/classes/Kernel.html +0 -135
  175. data/doc/rdoc/lore/classes/Logger.html +0 -229
  176. data/doc/rdoc/lore/classes/Logger/Ansicolor.html +0 -277
  177. data/doc/rdoc/lore/classes/Logger/LogDevice.html +0 -121
  178. data/doc/rdoc/lore/classes/NilClass.html +0 -119
  179. data/doc/rdoc/lore/classes/OpenStruct.html +0 -432
  180. data/doc/rdoc/lore/classes/Pathname.html +0 -353
  181. data/doc/rdoc/lore/classes/Set.html +0 -117
  182. data/doc/rdoc/lore/classes/Shellwords.html +0 -111
  183. data/doc/rdoc/lore/classes/String.html +0 -140
  184. data/doc/rdoc/lore/classes/Time.html +0 -154
  185. data/doc/rdoc/lore/classes/URI.html +0 -454
  186. data/doc/rdoc/lore/classes/URI/Hash.html +0 -105
  187. data/doc/rdoc/lore/classes/URI/Kernel.html +0 -122
  188. data/doc/rdoc/lore/classes/Zlib.html +0 -188
  189. data/doc/rdoc/lore/created.rid +0 -1
  190. data/doc/rdoc/lore/files/README.html +0 -286
  191. data/doc/rdoc/lore/files/doc/README_lore.html +0 -155
  192. data/doc/rdoc/lore/files/lib/lore/facets/basicobject_rb.html +0 -118
  193. data/doc/rdoc/lore/files/lib/lore/facets/cgi_rb.html +0 -111
  194. data/doc/rdoc/lore/files/lib/lore/facets/continuation_rb.html +0 -147
  195. data/doc/rdoc/lore/files/lib/lore/facets/date_rb.html +0 -97
  196. data/doc/rdoc/lore/files/lib/lore/facets/enumerator_rb.html +0 -111
  197. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/head_rb.html +0 -96
  198. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/safe_ln_rb.html +0 -96
  199. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/slice_rb.html +0 -96
  200. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/tail_rb.html +0 -96
  201. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/wc_rb.html +0 -96
  202. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/whereis_rb.html +0 -96
  203. data/doc/rdoc/lore/files/lib/lore/facets/fileutils/which_rb.html +0 -96
  204. data/doc/rdoc/lore/files/lib/lore/facets/fileutils_rb.html +0 -131
  205. data/doc/rdoc/lore/files/lib/lore/facets/getoptlong_rb.html +0 -135
  206. data/doc/rdoc/lore/files/lib/lore/facets/logger_rb.html +0 -142
  207. data/doc/rdoc/lore/files/lib/lore/facets/ostruct_rb.html +0 -135
  208. data/doc/rdoc/lore/files/lib/lore/facets/pathname_rb.html +0 -145
  209. data/doc/rdoc/lore/files/lib/lore/facets/rbconfig_rb.html +0 -124
  210. data/doc/rdoc/lore/files/lib/lore/facets/set_rb.html +0 -96
  211. data/doc/rdoc/lore/files/lib/lore/facets/shellwords_rb.html +0 -124
  212. data/doc/rdoc/lore/files/lib/lore/facets/uri_rb.html +0 -125
  213. data/doc/rdoc/lore/files/lib/lore/facets/yaml_rb.html +0 -146
  214. data/doc/rdoc/lore/files/lib/lore/facets/zlib_rb.html +0 -97
  215. data/doc/rdoc/lore/fr_class_index.html +0 -73
  216. data/doc/rdoc/lore/fr_file_index.html +0 -71
  217. data/doc/rdoc/lore/fr_method_index.html +0 -177
  218. data/doc/rdoc/lore/index.html +0 -26
  219. data/doc/rdoc/lore/rdoc-style.css +0 -177
  220. data/doc/release-notes/RELEASE-2.0.5 +0 -8
  221. data/doc/release-notes/RELEASE-2.1.0 +0 -9
  222. data/doc/release-notes/RELEASE-2.1.1 +0 -5
  223. data/doc/release-notes/RELEASE-2.1.2 +0 -6
  224. data/doc/release-notes/RELEASE-2.1.3 +0 -5
  225. data/doc/release-notes/RELEASE-2.2.0 +0 -14
  226. data/doc/release-notes/RELEASE-2.2.1 +0 -4
  227. data/doc/release-notes/RELEASE-2.3.0 +0 -6
  228. data/doc/release-notes/RELEASE-2.4.0 +0 -70
  229. data/doc/release-notes/RELEASE-2.4.1 +0 -8
  230. data/doc/release-notes/RELEASE-2.4.2 +0 -4
  231. data/doc/release-notes/RELEASE-2.4.3 +0 -78
  232. data/doc/release-notes/RELEASE-2.4.4 +0 -38
  233. data/doc/release-notes/RELEASE-2.4.5 +0 -37
  234. data/doc/release-notes/RELEASE-2.5.0 +0 -83
  235. data/lib/core/facets/kernel/instance.rb +0 -19
  236. data/lib/lore/facets/basicobject.rb +0 -14
  237. data/lib/lore/facets/enumerator.rb +0 -67
  238. data/lib/lore/facets/logger.rb +0 -291
  239. data/lib/more/facets/1stclassmethod.rb +0 -140
  240. data/lib/more/facets/advisable.rb +0 -162
  241. data/lib/more/facets/association.rb +0 -210
  242. data/lib/more/facets/attr.rb +0 -209
  243. data/lib/more/facets/basex.rb +0 -37
  244. data/lib/more/facets/bbcode.rb +0 -397
  245. data/lib/more/facets/bicrypt.rb +0 -265
  246. data/lib/more/facets/binreadable.rb +0 -221
  247. data/lib/more/facets/censor.rb +0 -97
  248. data/lib/more/facets/classmethods.rb +0 -199
  249. data/lib/more/facets/consoleutils.rb +0 -99
  250. data/lib/more/facets/crypt.rb +0 -166
  251. data/lib/more/facets/dependency.rb +0 -151
  252. data/lib/more/facets/downloader.rb +0 -281
  253. data/lib/more/facets/duplicable.rb +0 -43
  254. data/lib/more/facets/elementor.rb +0 -133
  255. data/lib/more/facets/filter.rb +0 -121
  256. data/lib/more/facets/heap.rb +0 -22
  257. data/lib/more/facets/infinity.rb +0 -193
  258. data/lib/more/facets/ini.rb +0 -264
  259. data/lib/more/facets/instantise.rb +0 -1
  260. data/lib/more/facets/instantize.rb +0 -95
  261. data/lib/more/facets/interval.rb +0 -282
  262. data/lib/more/facets/iteration.rb +0 -65
  263. data/lib/more/facets/linkedlist.rb +0 -222
  264. data/lib/more/facets/lrucache.rb +0 -157
  265. data/lib/more/facets/matcher.rb +0 -140
  266. data/lib/more/facets/memoizer.rb +0 -74
  267. data/lib/more/facets/minitar.rb +0 -1063
  268. data/lib/more/facets/nackclass.rb +0 -41
  269. data/lib/more/facets/net/smtp_tls.rb +0 -131
  270. data/lib/more/facets/nilstatus.rb +0 -48
  271. data/lib/more/facets/overload.rb +0 -94
  272. data/lib/more/facets/paramix.rb +0 -202
  273. data/lib/more/facets/pool.rb +0 -91
  274. data/lib/more/facets/pqueue.rb +0 -449
  275. data/lib/more/facets/pry.rb +0 -32
  276. data/lib/more/facets/reflection.rb +0 -145
  277. data/lib/more/facets/semaphore.rb +0 -92
  278. data/lib/more/facets/settings.rb +0 -248
  279. data/lib/more/facets/snapshot.rb +0 -209
  280. data/lib/more/facets/sparse_array.rb +0 -809
  281. data/lib/more/facets/string/mask.rb +0 -278
  282. data/lib/more/facets/string/obfuscate.rb +0 -65
  283. data/lib/more/facets/string/stylize.rb +0 -169
  284. data/lib/more/facets/string/words.rb +0 -167
  285. data/lib/more/facets/syncarray.rb +0 -114
  286. data/lib/more/facets/synchash.rb +0 -157
  287. data/lib/more/facets/typecast.rb +0 -261
  288. data/lib/more/facets/uninheritable.rb +0 -50
  289. data/lib/more/facets/xmlhash.rb +0 -112
  290. data/lib/more/facets/xoxo.rb +0 -259
  291. data/lib/more/facets/ziputils.rb +0 -490
  292. data/task/conflicts +0 -63
  293. data/task/coverage.rake +0 -37
  294. data/task/methods +0 -49
  295. data/task/rdoc.rake +0 -17
  296. data/task/setup.rake +0 -38
  297. data/task/test.rake +0 -145
  298. data/test/more/test_advisable.rb +0 -71
  299. data/test/more/test_association.rb +0 -38
  300. data/test/more/test_bbcode.rb +0 -21
  301. data/test/more/test_binreadable.rb +0 -50
  302. data/test/more/test_buildable.rb +0 -73
  303. data/test/more/test_classmethods.rb +0 -56
  304. data/test/more/test_crypt.rb +0 -32
  305. data/test/more/test_dependency.rb +0 -69
  306. data/test/more/test_elementwise.rb +0 -25
  307. data/test/more/test_infinity.rb +0 -40
  308. data/test/more/test_interval.rb +0 -151
  309. data/test/more/test_linkedlist.rb +0 -41
  310. data/test/more/test_lrucache.rb +0 -14
  311. data/test/more/test_overload.rb +0 -160
  312. data/test/more/test_paramix.rb +0 -170
  313. data/test/more/test_prototype.rb +0 -35
  314. data/test/more/test_snapshot.rb +0 -21
  315. data/test/more/test_sparsearray.rb +0 -279
  316. data/test/more/test_syncarray.rb +0 -15
  317. data/test/more/test_synchash.rb +0 -16
  318. data/test/more/test_typecast.rb +0 -54
  319. data/test/more/test_uninheritable.rb +0 -31
  320. data/test/more/test_xoxo.rb +0 -274
  321. data/test/test_facets.rb +0 -9
@@ -1,74 +0,0 @@
1
- # = Memoizer
2
- #
3
- # == Synopsis
4
- #
5
- # Memoizer wraps objects to provide cached method calls.
6
- #
7
- # class X
8
- # def initialize ; @tick = 0 ; end
9
- # def tick; @tick + 1; end
10
- # def memo; @memo ||= Memoizer.new(self) ; end
11
- # end
12
- #
13
- # x = X.new
14
- # x.tick #=> 1
15
- # x.memo.tick #=> 2
16
- # x.tick #=> 3
17
- # x.memo.tick #=> 2
18
- # x.tick #=> 4
19
- # x.memo.tick #=> 2
20
- #
21
- # You can also use to cache a collections of objects to gain code
22
- # speed ups.
23
- #
24
- # points = points.collect{|point| Memoizer.cache(point)}
25
- #
26
- # After our algorithm has finished using points, we want to get rid of
27
- # these Memoizer objects. That's easy:
28
- #
29
- # points = points.collect{|point| point.self }
30
- #
31
- # Or if you prefer (it is ever so slightly safer):
32
- #
33
- # points = points.collect{|point| Memoizer.uncache(point)}
34
- #
35
- # == References
36
- #
37
- # See http://javathink.blogspot.com/2008/09/what-is-memoizer-and-why-should-you.html
38
- #
39
- # == Authors
40
- #
41
- # * Erik Veenstra
42
- # * Thomas Sawyer
43
- #
44
- # == Copying
45
- #
46
- # Copyright (c) 2006 Erik Veenstra
47
-
48
- class Memoizer
49
-
50
- #private :class, :clone, :display, :type, :method, :to_a, :to_s
51
- private *instance_methods(true).select{ |m| m.to_s !~ /^__/ }
52
-
53
- def initialize(object)
54
- @self = object
55
- @cache = {}
56
- end
57
-
58
- def method_missing(method_name, *args, &block)
59
- # Not thread-safe! Speed is important in caches... ;]
60
- @cache[[method_name, args, block]] ||= @self.__send__(method_name, *args, &block)
61
- end
62
-
63
- def self; @self; end
64
-
65
- def self.cache(object)
66
- new(object)
67
- end
68
-
69
- def self.uncache(cached_object)
70
- cached_object.self
71
- end
72
-
73
- end
74
-
@@ -1,1063 +0,0 @@
1
- # = Archive::Tar::Minitar
2
- #
3
- # = Synopsis
4
- #
5
- # Archive::Tar::Minitar is a pure-Ruby library and command-line
6
- # utility that provides the ability to deal with POSIX tar(1) archive
7
- # files. The implementation is based heavily on Mauricio Ferna'ndez's
8
- # implementation in rpa-base, but has been reorganised to promote
9
- # reuse in other projects.
10
- #
11
- # This tar class performs a subset of all tar (POSIX tape archive)
12
- # operations. We can only deal with typeflags 0, 1, 2, and 5 (see
13
- # Archive::Tar::PosixHeader). All other typeflags will be treated as
14
- # normal files.
15
- #
16
- # NOTE::: support for typeflags 1 and 2 is not yet implemented in this
17
- # version.
18
- #
19
- # This release is version 0.5.2. The library can only handle files and
20
- # directories at this point. A future version will be expanded to
21
- # handle symbolic links and hard links in a portable manner. The
22
- # command line utility, minitar, can only create archives, extract
23
- # from archives, and list archive contents.
24
- #
25
- # == Synopsis
26
- #
27
- # Using this library is easy. The simplest case is:
28
- #
29
- # require 'zlib'
30
- # require 'archive/tar/minitar'
31
- # include Archive::Tar
32
- #
33
- # # Packs everything that matches Find.find('tests')
34
- # File.open('test.tar', 'wb') { |tar| Minitar.pack('tests', tar) }
35
- # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
36
- # Minitar.unpack('test.tar', 'x')
37
- #
38
- # A gzipped tar can be written with:
39
- #
40
- # tgz = Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
41
- # # Warning: tgz will be closed!
42
- # Minitar.pack('tests', tgz)
43
- #
44
- # tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
45
- # # Warning: tgz will be closed!
46
- # Minitar.unpack(tgz, 'x')
47
- #
48
- # As the case above shows, one need not write to a file. However, it
49
- # will sometimes require that one dive a little deeper into the API,
50
- # as in the case of StringIO objects. Note that I'm not providing a
51
- # block with Minitar::Output, as Minitar::Output#close automatically
52
- # closes both the Output object and the wrapped data stream object.
53
- #
54
- # begin
55
- # sgz = Zlib::GzipWriter.new(StringIO.new(""))
56
- # tar = Output.new(sgz)
57
- # Find.find('tests') do |entry|
58
- # Minitar.pack_file(entry, tar)
59
- # end
60
- # ensure
61
- # # Closes both tar and sgz.
62
- # tar.close
63
- # end
64
- #
65
- # == Version
66
- #
67
- # 0.5.2
68
- #
69
- # == Authors
70
- #
71
- # * Mauricio Julio Ferna'ndez Pradier
72
- # * Austin Ziegler
73
- #
74
- # == Copying
75
- #
76
- # Copyright 2004 Mauricio Julio Ferna'ndez Pradier and Austin Ziegler
77
- #
78
- # This program is based on and incorporates parts of RPA::Package from
79
- # rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and
80
- # has been adapted to be more generic by Austin.
81
- #
82
- # 'minitar' contains an adaptation of Ruby/ProgressBar by Satoru
83
- # Takabayashi <satoru@namazu.org>, copyright 2001 - 2004.
84
- #
85
- # This program is free software. It may be redistributed and/or
86
- # modified under the terms of the GPL version 2 (or later) or Ruby's
87
- # licence.
88
-
89
-
90
- #
91
- module Archive; end
92
- module Archive::Tar; end
93
-
94
- # = Archive::Tar::PosixHeader
95
- # Implements the POSIX tar header as a Ruby class. The structure of
96
- # the POSIX tar header is:
97
- #
98
- # struct tarfile_entry_posix
99
- # { // pack/unpack
100
- # char name[100]; // ASCII (+ Z unless filled) a100/Z100
101
- # char mode[8]; // 0 padded, octal, null a8 /A8
102
- # char uid[8]; // ditto a8 /A8
103
- # char gid[8]; // ditto a8 /A8
104
- # char size[12]; // 0 padded, octal, null a12 /A12
105
- # char mtime[12]; // 0 padded, octal, null a12 /A12
106
- # char checksum[8]; // 0 padded, octal, null, space a8 /A8
107
- # char typeflag[1]; // see below a /a
108
- # char linkname[100]; // ASCII + (Z unless filled) a100/Z100
109
- # char magic[6]; // "ustar\0" a6 /A6
110
- # char version[2]; // "00" a2 /A2
111
- # char uname[32]; // ASCIIZ a32 /Z32
112
- # char gname[32]; // ASCIIZ a32 /Z32
113
- # char devmajor[8]; // 0 padded, octal, null a8 /A8
114
- # char devminor[8]; // 0 padded, octal, null a8 /A8
115
- # char prefix[155]; // ASCII (+ Z unless filled) a155/Z155
116
- # };
117
- #
118
- # The +typeflag+ may be one of the following known values:
119
- #
120
- # <tt>"0"</tt>:: Regular file. NULL should be treated as a synonym, for
121
- # compatibility purposes.
122
- # <tt>"1"</tt>:: Hard link.
123
- # <tt>"2"</tt>:: Symbolic link.
124
- # <tt>"3"</tt>:: Character device node.
125
- # <tt>"4"</tt>:: Block device node.
126
- # <tt>"5"</tt>:: Directory.
127
- # <tt>"6"</tt>:: FIFO node.
128
- # <tt>"7"</tt>:: Reserved.
129
- #
130
- # POSIX indicates that "A POSIX-compliant implementation must treat any
131
- # unrecognized typeflag value as a regular file."
132
- class Archive::Tar::PosixHeader #:nodoc:
133
- FIELDS = %w(name mode uid gid size mtime checksum typeflag linkname) +
134
- %w(magic version uname gname devmajor devminor prefix)
135
-
136
- FIELDS.each { |field| attr_reader field.intern }
137
-
138
- HEADER_PACK_FORMAT = "a100a8a8a8a12a12a7aaa100a6a2a32a32a8a8a155"
139
- HEADER_UNPACK_FORMAT = "Z100A8A8A8A12A12A8aZ100A6A2Z32Z32A8A8Z155"
140
-
141
- # Creates a new PosixHeader from a data stream.
142
- def self.new_from_stream(stream)
143
- data = stream.read(512)
144
- fields = data.unpack(HEADER_UNPACK_FORMAT)
145
- name = fields.shift
146
- mode = fields.shift.oct
147
- uid = fields.shift.oct
148
- gid = fields.shift.oct
149
- size = fields.shift.oct
150
- mtime = fields.shift.oct
151
- checksum = fields.shift.oct
152
- typeflag = fields.shift
153
- linkname = fields.shift
154
- magic = fields.shift
155
- version = fields.shift.oct
156
- uname = fields.shift
157
- gname = fields.shift
158
- devmajor = fields.shift.oct
159
- devminor = fields.shift.oct
160
- prefix = fields.shift
161
-
162
- empty = (data == "\0" * 512)
163
-
164
- new(:name => name, :mode => mode, :uid => uid, :gid => gid,
165
- :size => size, :mtime => mtime, :checksum => checksum,
166
- :typeflag => typeflag, :magic => magic, :version => version,
167
- :uname => uname, :gname => gname, :devmajor => devmajor,
168
- :devminor => devminor, :prefix => prefix, :empty => empty)
169
- end
170
-
171
- # Creates a new PosixHeader. A PosixHeader cannot be created unless the
172
- # #name, #size, #prefix, and #mode are provided.
173
- def initialize(vals)
174
- unless vals[:name] && vals[:size] && vals[:prefix] && vals[:mode]
175
- raise ArgumentError
176
- end
177
-
178
- vals[:mtime] ||= 0
179
- vals[:checksum] ||= ""
180
- vals[:typeflag] ||= "0"
181
- vals[:magic] ||= "ustar"
182
- vals[:version] ||= "00"
183
-
184
- FIELDS.each do |field|
185
- instance_variable_set("@#{field}", vals[field.intern])
186
- end
187
- @empty = vals[:empty]
188
- end
189
-
190
- def empty?
191
- @empty
192
- end
193
-
194
- def to_s
195
- update_checksum
196
- header(@checksum)
197
- end
198
-
199
- # Update the checksum field.
200
- def update_checksum
201
- hh = header(" " * 8)
202
- @checksum = oct(calculate_checksum(hh), 6)
203
- end
204
-
205
- private
206
- def oct(num, len)
207
- if num.nil?
208
- "\0" * (len + 1)
209
- else
210
- "%0#{len}o" % num
211
- end
212
- end
213
-
214
- def calculate_checksum(hdr)
215
- hdr.unpack("C*").inject { |aa, bb| aa + bb }
216
- end
217
-
218
- def header(chksum)
219
- arr = [name, oct(mode, 7), oct(uid, 7), oct(gid, 7), oct(size, 11),
220
- oct(mtime, 11), chksum, " ", typeflag, linkname, magic, version,
221
- uname, gname, oct(devmajor, 7), oct(devminor, 7), prefix]
222
- str = arr.pack(HEADER_PACK_FORMAT)
223
- str + "\0" * ((512 - str.size) % 512)
224
- end
225
- end
226
-
227
- require 'fileutils'
228
- require 'find'
229
-
230
- # = Archive::Tar::Minitar 0.5.2
231
- # Archive::Tar::Minitar is a pure-Ruby library and command-line
232
- # utility that provides the ability to deal with POSIX tar(1) archive
233
- # files. The implementation is based heavily on Mauricio Ferna'ndez's
234
- # implementation in rpa-base, but has been reorganised to promote
235
- # reuse in other projects.
236
- #
237
- # This tar class performs a subset of all tar (POSIX tape archive)
238
- # operations. We can only deal with typeflags 0, 1, 2, and 5 (see
239
- # Archive::Tar::PosixHeader). All other typeflags will be treated as
240
- # normal files.
241
- #
242
- # NOTE::: support for typeflags 1 and 2 is not yet implemented in this
243
- # version.
244
- #
245
- # This release is version 0.5.2. The library can only handle files and
246
- # directories at this point. A future version will be expanded to
247
- # handle symbolic links and hard links in a portable manner. The
248
- # command line utility, minitar, can only create archives, extract
249
- # from archives, and list archive contents.
250
- #
251
- # == Synopsis
252
- # Using this library is easy. The simplest case is:
253
- #
254
- # require 'zlib'
255
- # require 'archive/tar/minitar'
256
- # include Archive::Tar
257
- #
258
- # # Packs everything that matches Find.find('tests')
259
- # File.open('test.tar', 'wb') { |tar| Minitar.pack('tests', tar) }
260
- # # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
261
- # Minitar.unpack('test.tar', 'x')
262
- #
263
- # A gzipped tar can be written with:
264
- #
265
- # tgz = Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
266
- # # Warning: tgz will be closed!
267
- # Minitar.pack('tests', tgz)
268
- #
269
- # tgz = Zlib::GzipReader.new(File.open('test.tgz', 'rb'))
270
- # # Warning: tgz will be closed!
271
- # Minitar.unpack(tgz, 'x')
272
- #
273
- # As the case above shows, one need not write to a file. However, it
274
- # will sometimes require that one dive a little deeper into the API,
275
- # as in the case of StringIO objects. Note that I'm not providing a
276
- # block with Minitar::Output, as Minitar::Output#close automatically
277
- # closes both the Output object and the wrapped data stream object.
278
- #
279
- # begin
280
- # sgz = Zlib::GzipWriter.new(StringIO.new(""))
281
- # tar = Output.new(sgz)
282
- # Find.find('tests') do |entry|
283
- # Minitar.pack_file(entry, tar)
284
- # end
285
- # ensure
286
- # # Closes both tar and sgz.
287
- # tar.close
288
- # end
289
- #
290
- # == Copyright
291
- # Copyright 2004 Mauricio Julio Ferna'ndez Pradier and Austin Ziegler
292
- #
293
- # This program is based on and incorporates parts of RPA::Package from
294
- # rpa-base (lib/rpa/package.rb and lib/rpa/util.rb) by Mauricio and
295
- # has been adapted to be more generic by Austin.
296
- #
297
- # 'minitar' contains an adaptation of Ruby/ProgressBar by Satoru
298
- # Takabayashi <satoru@namazu.org>, copyright 2001 - 2004.
299
- #
300
- # This program is free software. It may be redistributed and/or
301
- # modified under the terms of the GPL version 2 (or later) or Ruby's
302
- # licence.
303
- module Archive::Tar::Minitar
304
- VERSION = "0.5.2"
305
-
306
- # The exception raised when a wrapped data stream class is expected to
307
- # respond to #rewind or #pos but does not.
308
- class NonSeekableStream < StandardError #:nodoc:
309
- end
310
- # The exception raised when a block is required for proper operation of
311
- # the method.
312
- class BlockRequired < ArgumentError #:nodoc:
313
- end
314
- # The exception raised when operations are performed on a stream that has
315
- # previously been closed.
316
- class ClosedStream < StandardError #:nodoc:
317
- end
318
- # The exception raised when a filename exceeds 256 bytes in length,
319
- # the maximum supported by the standard Tar format.
320
- class FileNameTooLong < StandardError #:nodoc:
321
- end
322
- # The exception raised when a data stream ends before the amount of data
323
- # expected in the archive's PosixHeader.
324
- class UnexpectedEOF < StandardError #:nodoc:
325
- end
326
-
327
- # The class that writes a tar format archive to a data stream.
328
- class Writer
329
- # A stream wrapper that can only be written to. Any attempt to read
330
- # from this restricted stream will result in a NameError being thrown.
331
- class RestrictedStream #:nodoc:
332
- def initialize(anIO)
333
- @io = anIO
334
- end
335
-
336
- def write(data)
337
- @io.write(data)
338
- end
339
- end
340
-
341
- # A RestrictedStream that also has a size limit.
342
- class BoundedStream < Archive::Tar::Minitar::Writer::RestrictedStream #:nodoc:
343
- # The exception raised when the user attempts to write more data to
344
- # a BoundedStream than has been allocated.
345
- class FileOverflow < RuntimeError #:nodoc:
346
- end
347
-
348
- # The maximum number of bytes that may be written to this data
349
- # stream.
350
- attr_reader :limit
351
- # The current total number of bytes written to this data stream.
352
- attr_reader :written
353
-
354
- def initialize(io, limit)
355
- @io = io
356
- @limit = limit
357
- @written = 0
358
- end
359
-
360
- def write(data)
361
- raise FileOverflow if (data.size + @written) > @limit
362
- @io.write(data)
363
- @written += data.size
364
- data.size
365
- end
366
- end
367
-
368
- # With no associated block, +Writer::open+ is a synonym for
369
- # +Writer::new+. If the optional code block is given, it will be
370
- # passed the new _writer_ as an argument and the Writer object will
371
- # automatically be closed when the block terminates. In this instance,
372
- # +Writer::open+ returns the value of the block.
373
- def self.open(anIO)
374
- writer = Writer.new(anIO)
375
-
376
- return writer unless block_given?
377
-
378
- begin
379
- res = yield writer
380
- ensure
381
- writer.close
382
- end
383
-
384
- res
385
- end
386
-
387
- # Creates and returns a new Writer object.
388
- def initialize(anIO)
389
- @io = anIO
390
- @closed = false
391
- end
392
-
393
- # Adds a file to the archive as +name+. +opts+ must contain the
394
- # following values:
395
- #
396
- # <tt>:mode</tt>:: The Unix file permissions mode value.
397
- # <tt>:size</tt>:: The size, in bytes.
398
- #
399
- # +opts+ may contain the following values:
400
- #
401
- # <tt>:uid</tt>: The Unix file owner user ID number.
402
- # <tt>:gid</tt>: The Unix file owner group ID number.
403
- # <tt>:mtime</tt>:: The *integer* modification time value.
404
- #
405
- # It will not be possible to add more than <tt>opts[:size]</tt> bytes
406
- # to the file.
407
- def add_file_simple(name, opts = {}) # :yields BoundedStream:
408
- raise Archive::Tar::Minitar::BlockRequired unless block_given?
409
- raise Archive::Tar::ClosedStream if @closed
410
-
411
- name, prefix = split_name(name)
412
-
413
- header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
414
- :size => opts[:size], :gid => opts[:gid], :uid => opts[:uid],
415
- :prefix => prefix }
416
- header = Archive::Tar::PosixHeader.new(header).to_s
417
- @io.write(header)
418
-
419
- os = BoundedStream.new(@io, opts[:size])
420
- yield os
421
- # FIXME: what if an exception is raised in the block?
422
-
423
- min_padding = opts[:size] - os.written
424
- @io.write("\0" * min_padding)
425
- remainder = (512 - (opts[:size] % 512)) % 512
426
- @io.write("\0" * remainder)
427
- end
428
-
429
- # Adds a file to the archive as +name+. +opts+ must contain the
430
- # following value:
431
- #
432
- # <tt>:mode</tt>:: The Unix file permissions mode value.
433
- #
434
- # +opts+ may contain the following values:
435
- #
436
- # <tt>:uid</tt>: The Unix file owner user ID number.
437
- # <tt>:gid</tt>: The Unix file owner group ID number.
438
- # <tt>:mtime</tt>:: The *integer* modification time value.
439
- #
440
- # The file's size will be determined from the amount of data written
441
- # to the stream.
442
- #
443
- # For #add_file to be used, the Archive::Tar::Minitar::Writer must be
444
- # wrapping a stream object that is seekable (e.g., it responds to
445
- # #pos=). Otherwise, #add_file_simple must be used.
446
- #
447
- # +opts+ may be modified during the writing to the stream.
448
- def add_file(name, opts = {}) # :yields RestrictedStream, +opts+:
449
- raise Archive::Tar::Minitar::BlockRequired unless block_given?
450
- raise Archive::Tar::Minitar::ClosedStream if @closed
451
- raise Archive::Tar::Minitar::NonSeekableStream unless @io.respond_to?(:pos=)
452
-
453
- name, prefix = split_name(name)
454
- init_pos = @io.pos
455
- @io.write("\0" * 512) # placeholder for the header
456
-
457
- yield RestrictedStream.new(@io), opts
458
- # FIXME: what if an exception is raised in the block?
459
-
460
- size = @io.pos - (init_pos + 512)
461
- remainder = (512 - (size % 512)) % 512
462
- @io.write("\0" * remainder)
463
-
464
- final_pos = @io.pos
465
- @io.pos = init_pos
466
-
467
- header = { :name => name, :mode => opts[:mode], :mtime => opts[:mtime],
468
- :size => size, :gid => opts[:gid], :uid => opts[:uid],
469
- :prefix => prefix }
470
- header = Archive::Tar::PosixHeader.new(header).to_s
471
- @io.write(header)
472
- @io.pos = final_pos
473
- end
474
-
475
- # Creates a directory in the tar.
476
- def mkdir(name, opts = {})
477
- raise ClosedStream if @closed
478
- name, prefix = split_name(name)
479
- header = { :name => name, :mode => opts[:mode], :typeflag => "5",
480
- :size => 0, :gid => opts[:gid], :uid => opts[:uid],
481
- :mtime => opts[:mtime], :prefix => prefix }
482
- header = Archive::Tar::PosixHeader.new(header).to_s
483
- @io.write(header)
484
- nil
485
- end
486
-
487
- # Passes the #flush method to the wrapped stream, used for buffered
488
- # streams.
489
- def flush
490
- raise ClosedStream if @closed
491
- @io.flush if @io.respond_to?(:flush)
492
- end
493
-
494
- # Closes the Writer.
495
- def close
496
- return if @closed
497
- @io.write("\0" * 1024)
498
- @closed = true
499
- end
500
-
501
- private
502
- def split_name(name)
503
- raise FileNameTooLong if name.size > 256
504
- if name.size <= 100
505
- prefix = ""
506
- else
507
- parts = name.split(/\//)
508
- newname = parts.pop
509
-
510
- nxt = ""
511
-
512
- loop do
513
- nxt = parts.pop
514
- break if newname.size + 1 + nxt.size > 100
515
- newname = "#{nxt}/#{newname}"
516
- end
517
-
518
- prefix = (parts + [nxt]).join("/")
519
-
520
- name = newname
521
-
522
- raise FileNameTooLong if name.size > 100 || prefix.size > 155
523
- end
524
- return name, prefix
525
- end
526
- end
527
-
528
- # The class that reads a tar format archive from a data stream. The data
529
- # stream may be sequential or random access, but certain features only work
530
- # with random access data streams.
531
- class Reader
532
- # This marks the EntryStream closed for reading without closing the
533
- # actual data stream.
534
- module InvalidEntryStream #:nodoc:
535
- def read(len = nil); raise ClosedStream; end
536
- def getc; raise ClosedStream; end
537
- def rewind; raise ClosedStream; end
538
- end
539
-
540
- # EntryStreams are pseudo-streams on top of the main data stream.
541
- class EntryStream #:nodoc:
542
- Archive::Tar::PosixHeader::FIELDS.each do |field|
543
- attr_reader field.intern
544
- end
545
-
546
- def initialize(header, anIO)
547
- @io = anIO
548
- @name = header.name
549
- @mode = header.mode
550
- @uid = header.uid
551
- @gid = header.gid
552
- @size = header.size
553
- @mtime = header.mtime
554
- @checksum = header.checksum
555
- @typeflag = header.typeflag
556
- @linkname = header.linkname
557
- @magic = header.magic
558
- @version = header.version
559
- @uname = header.uname
560
- @gname = header.gname
561
- @devmajor = header.devmajor
562
- @devminor = header.devminor
563
- @prefix = header.prefix
564
- @read = 0
565
- @orig_pos = @io.pos
566
- end
567
-
568
- # Reads +len+ bytes (or all remaining data) from the entry. Returns
569
- # +nil+ if there is no more data to read.
570
- def read(len = nil)
571
- return nil if @read >= @size
572
- len ||= @size - @read
573
- max_read = [len, @size - @read].min
574
- ret = @io.read(max_read)
575
- @read += ret.size
576
- ret
577
- end
578
-
579
- # Reads one byte from the entry. Returns +nil+ if there is no more data
580
- # to read.
581
- def getc
582
- return nil if @read >= @size
583
- ret = @io.getc
584
- @read += 1 if ret
585
- ret
586
- end
587
-
588
- # Returns +true+ if the entry represents a directory.
589
- def directory?
590
- @typeflag == "5"
591
- end
592
- alias_method :directory, :directory?
593
-
594
- # Returns +true+ if the entry represents a plain file.
595
- def file?
596
- @typeflag == "0"
597
- end
598
- alias_method :file, :file?
599
-
600
- # Returns +true+ if the current read pointer is at the end of the
601
- # EntryStream data.
602
- def eof?
603
- @read >= @size
604
- end
605
-
606
- # Returns the current read pointer in the EntryStream.
607
- def pos
608
- @read
609
- end
610
-
611
- # Sets the current read pointer to the beginning of the EntryStream.
612
- def rewind
613
- raise NonSeekableStream unless @io.respond_to?(:pos=)
614
- @io.pos = @orig_pos
615
- @read = 0
616
- end
617
-
618
- def bytes_read
619
- @read
620
- end
621
-
622
- # Returns the full and proper name of the entry.
623
- def full_name
624
- if @prefix != ""
625
- File.join(@prefix, @name)
626
- else
627
- @name
628
- end
629
- end
630
-
631
- # Closes the entry.
632
- def close
633
- invalidate
634
- end
635
-
636
- private
637
- def invalidate
638
- extend InvalidEntryStream
639
- end
640
- end
641
-
642
- # With no associated block, +Reader::open+ is a synonym for
643
- # +Reader::new+. If the optional code block is given, it will be passed
644
- # the new _writer_ as an argument and the Reader object will
645
- # automatically be closed when the block terminates. In this instance,
646
- # +Reader::open+ returns the value of the block.
647
- def self.open(anIO)
648
- reader = Reader.new(anIO)
649
-
650
- return reader unless block_given?
651
-
652
- begin
653
- res = yield reader
654
- ensure
655
- reader.close
656
- end
657
-
658
- res
659
- end
660
-
661
- # Creates and returns a new Reader object.
662
- def initialize(anIO)
663
- @io = anIO
664
- @init_pos = anIO.pos
665
- end
666
-
667
- # Iterates through each entry in the data stream.
668
- def each(&block)
669
- each_entry(&block)
670
- end
671
-
672
- # Resets the read pointer to the beginning of data stream. Do not call
673
- # this during a #each or #each_entry iteration. This only works with
674
- # random access data streams that respond to #rewind and #pos.
675
- def rewind
676
- if @init_pos == 0
677
- raise NonSeekableStream unless @io.respond_to?(:rewind)
678
- @io.rewind
679
- else
680
- raise NonSeekableStream unless @io.respond_to?(:pos=)
681
- @io.pos = @init_pos
682
- end
683
- end
684
-
685
- # Iterates through each entry in the data stream.
686
- def each_entry
687
- loop do
688
- return if @io.eof?
689
-
690
- header = Archive::Tar::PosixHeader.new_from_stream(@io)
691
- return if header.empty?
692
-
693
- entry = EntryStream.new(header, @io)
694
- size = entry.size
695
-
696
- yield entry
697
-
698
- skip = (512 - (size % 512)) % 512
699
-
700
- if @io.respond_to?(:seek)
701
- # avoid reading...
702
- @io.seek(size - entry.bytes_read, IO::SEEK_CUR)
703
- else
704
- pending = size - entry.bytes_read
705
- while pending > 0
706
- bread = @io.read([pending, 4096].min).size
707
- raise UnexpectedEOF if @io.eof?
708
- pending -= bread
709
- end
710
- end
711
- @io.read(skip) # discard trailing zeros
712
- # make sure nobody can use #read, #getc or #rewind anymore
713
- entry.close
714
- end
715
- end
716
-
717
- def close
718
- end
719
- end
720
-
721
- # Wraps a Archive::Tar::Minitar::Reader with convenience methods and
722
- # wrapped stream management; Input only works with random access data
723
- # streams. See Input::new for details.
724
- class Input
725
- include Enumerable
726
-
727
- # With no associated block, +Input::open+ is a synonym for
728
- # +Input::new+. If the optional code block is given, it will be passed
729
- # the new _writer_ as an argument and the Input object will
730
- # automatically be closed when the block terminates. In this instance,
731
- # +Input::open+ returns the value of the block.
732
- def self.open(input)
733
- stream = Input.new(input)
734
- return stream unless block_given?
735
-
736
- begin
737
- res = yield stream
738
- ensure
739
- stream.close
740
- end
741
-
742
- res
743
- end
744
-
745
- # Creates a new Input object. If +input+ is a stream object that responds
746
- # to #read), then it will simply be wrapped. Otherwise, one will be
747
- # created and opened using Kernel#open. When Input#close is called, the
748
- # stream object wrapped will be closed.
749
- def initialize(input)
750
- if input.respond_to?(:read)
751
- @io = input
752
- else
753
- @io = open(input, "rb")
754
- end
755
- @tarreader = Archive::Tar::Minitar::Reader.new(@io)
756
- end
757
-
758
- # Iterates through each entry and rewinds to the beginning of the stream
759
- # when finished.
760
- def each(&block)
761
- @tarreader.each { |entry| yield entry }
762
- ensure
763
- @tarreader.rewind
764
- end
765
-
766
- # Extracts the current +entry+ to +destdir+. If a block is provided, it
767
- # yields an +action+ Symbol, the full name of the file being extracted
768
- # (+name+), and a Hash of statistical information (+stats+).
769
- #
770
- # The +action+ will be one of:
771
- # <tt>:dir</tt>:: The +entry+ is a directory.
772
- # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
773
- # file is just beginning.
774
- # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
775
- # of the +entry+.
776
- # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
777
- #
778
- # The +stats+ hash contains the following keys:
779
- # <tt>:current</tt>:: The current total number of bytes read in the
780
- # +entry+.
781
- # <tt>:currinc</tt>:: The current number of bytes read in this read
782
- # cycle.
783
- # <tt>:entry</tt>:: The entry being extracted; this is a
784
- # Reader::EntryStream, with all methods thereof.
785
- def extract_entry(destdir, entry) # :yields action, name, stats:
786
- stats = {
787
- :current => 0,
788
- :currinc => 0,
789
- :entry => entry
790
- }
791
-
792
- if entry.directory?
793
- dest = File.join(destdir, entry.full_name)
794
-
795
- yield :dir, entry.full_name, stats if block_given?
796
-
797
- if Archive::Tar::Minitar.dir?(dest)
798
- begin
799
- FileUtils.chmod(entry.mode, dest)
800
- rescue Exception
801
- nil
802
- end
803
- else
804
- FileUtils.mkdir_p(dest, :mode => entry.mode)
805
- FileUtils.chmod(entry.mode, dest)
806
- end
807
-
808
- fsync_dir(dest)
809
- fsync_dir(File.join(dest, ".."))
810
- return
811
- else # it's a file
812
- destdir = File.join(destdir, File.dirname(entry.full_name))
813
- FileUtils.mkdir_p(destdir, :mode => 0755)
814
-
815
- destfile = File.join(destdir, File.basename(entry.full_name))
816
- FileUtils.chmod(0600, destfile) rescue nil # Errno::ENOENT
817
-
818
- yield :file_start, entry.full_name, stats if block_given?
819
-
820
- File.open(destfile, "wb", entry.mode) do |os|
821
- loop do
822
- data = entry.read(4096)
823
- break unless data
824
-
825
- stats[:currinc] = os.write(data)
826
- stats[:current] += stats[:currinc]
827
-
828
- yield :file_progress, entry.full_name, stats if block_given?
829
- end
830
- os.fsync
831
- end
832
-
833
- FileUtils.chmod(entry.mode, destfile)
834
- fsync_dir(File.dirname(destfile))
835
- fsync_dir(File.join(File.dirname(destfile), ".."))
836
-
837
- yield :file_done, entry.full_name, stats if block_given?
838
- end
839
- end
840
-
841
- # Returns the Reader object for direct access.
842
- def tar
843
- @tarreader
844
- end
845
-
846
- # Closes the Reader object and the wrapped data stream.
847
- def close
848
- @io.close
849
- @tarreader.close
850
- end
851
-
852
- private
853
- def fsync_dir(dirname)
854
- # make sure this hits the disc
855
- dir = open(dirname, 'rb')
856
- dir.fsync
857
- rescue # ignore IOError if it's an unpatched (old) Ruby
858
- nil
859
- ensure
860
- dir.close if dir rescue nil
861
- end
862
- end
863
-
864
- # Wraps a Archive::Tar::Minitar::Writer with convenience methods and
865
- # wrapped stream management; Output only works with random access data
866
- # streams. See Output::new for details.
867
- class Output
868
- # With no associated block, +Output::open+ is a synonym for
869
- # +Output::new+. If the optional code block is given, it will be passed
870
- # the new _writer_ as an argument and the Output object will
871
- # automatically be closed when the block terminates. In this instance,
872
- # +Output::open+ returns the value of the block.
873
- def self.open(output)
874
- stream = Output.new(output)
875
- return stream unless block_given?
876
-
877
- begin
878
- res = yield stream
879
- ensure
880
- stream.close
881
- end
882
-
883
- res
884
- end
885
-
886
- # Creates a new Output object. If +output+ is a stream object that
887
- # responds to #read), then it will simply be wrapped. Otherwise, one will
888
- # be created and opened using Kernel#open. When Output#close is called,
889
- # the stream object wrapped will be closed.
890
- def initialize(output)
891
- if output.respond_to?(:write)
892
- @io = output
893
- else
894
- @io = ::File.open(output, "wb")
895
- end
896
- @tarwriter = Archive::Tar::Minitar::Writer.new(@io)
897
- end
898
-
899
- # Returns the Writer object for direct access.
900
- def tar
901
- @tarwriter
902
- end
903
-
904
- # Closes the Writer object and the wrapped data stream.
905
- def close
906
- @tarwriter.close
907
- @io.close
908
- end
909
- end
910
-
911
- class << self
912
- # Tests if +path+ refers to a directory. Fixes an apparently
913
- # corrupted <tt>stat()</tt> call on Windows.
914
- def dir?(path)
915
- File.directory?((path[-1] == ?/) ? path : "#{path}/")
916
- end
917
-
918
- # A convenience method for wrapping Archive::Tar::Minitar::Input.open
919
- # (mode +r+) and Archive::Tar::Minitar::Output.open (mode +w+). No other
920
- # modes are currently supported.
921
- def open(dest, mode = "r", &block)
922
- case mode
923
- when "r"
924
- Input.open(dest, &block)
925
- when "w"
926
- Output.open(dest, &block)
927
- else
928
- raise "Unknown open mode for Archive::Tar::Minitar.open."
929
- end
930
- end
931
-
932
- # A convenience method to packs the file provided. +entry+ may either be
933
- # a filename (in which case various values for the file (see below) will
934
- # be obtained from <tt>File#stat(entry)</tt> or a Hash with the fields:
935
- #
936
- # <tt>:name</tt>:: The filename to be packed into the tarchive.
937
- # *REQUIRED*.
938
- # <tt>:mode</tt>:: The mode to be applied.
939
- # <tt>:uid</tt>:: The user owner of the file. (Ignored on Windows.)
940
- # <tt>:gid</tt>:: The group owner of the file. (Ignored on Windows.)
941
- # <tt>:mtime</tt>:: The modification Time of the file.
942
- #
943
- # During packing, if a block is provided, #pack_file yields an +action+
944
- # Symol, the full name of the file being packed, and a Hash of
945
- # statistical information, just as with
946
- # Archive::Tar::Minitar::Input#extract_entry.
947
- #
948
- # The +action+ will be one of:
949
- # <tt>:dir</tt>:: The +entry+ is a directory.
950
- # <tt>:file_start</tt>:: The +entry+ is a file; the extract of the
951
- # file is just beginning.
952
- # <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract
953
- # of the +entry+.
954
- # <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
955
- #
956
- # The +stats+ hash contains the following keys:
957
- # <tt>:current</tt>:: The current total number of bytes read in the
958
- # +entry+.
959
- # <tt>:currinc</tt>:: The current number of bytes read in this read
960
- # cycle.
961
- # <tt>:name</tt>:: The filename to be packed into the tarchive.
962
- # *REQUIRED*.
963
- # <tt>:mode</tt>:: The mode to be applied.
964
- # <tt>:uid</tt>:: The user owner of the file. (+nil+ on Windows.)
965
- # <tt>:gid</tt>:: The group owner of the file. (+nil+ on Windows.)
966
- # <tt>:mtime</tt>:: The modification Time of the file.
967
- def pack_file(entry, outputter) #:yields action, name, stats:
968
- outputter = outputter.tar if outputter.kind_of?(Archive::Tar::Minitar::Output)
969
-
970
- stats = {}
971
-
972
- if entry.kind_of?(Hash)
973
- name = entry[:name]
974
-
975
- entry.each { |kk, vv| stats[kk] = vv unless vv.nil? }
976
- else
977
- name = entry
978
- end
979
-
980
- name = name.sub(%r{\./}, '')
981
- stat = File.stat(name)
982
- stats[:mode] ||= stat.mode
983
- stats[:mtime] ||= stat.mtime
984
- stats[:size] = stat.size
985
-
986
- if RUBY_PLATFORM =~ /win32/
987
- stats[:uid] = nil
988
- stats[:gid] = nil
989
- else
990
- stats[:uid] ||= stat.uid
991
- stats[:gid] ||= stat.gid
992
- end
993
-
994
- case
995
- when File.file?(name)
996
- outputter.add_file_simple(name, stats) do |os|
997
- stats[:current] = 0
998
- yield :file_start, name, stats if block_given?
999
- File.open(name, "rb") do |ff|
1000
- until ff.eof?
1001
- stats[:currinc] = os.write(ff.read(4096))
1002
- stats[:current] += stats[:currinc]
1003
- yield :file_progress, name, stats if block_given?
1004
- end
1005
- end
1006
- yield :file_done, name, stats if block_given?
1007
- end
1008
- when dir?(name)
1009
- yield :dir, name, stats if block_given?
1010
- outputter.mkdir(name, stats)
1011
- else
1012
- raise "Don't yet know how to pack this type of file."
1013
- end
1014
- end
1015
-
1016
- # A convenience method to pack files specified by +src+ into +dest+. If
1017
- # +src+ is an Array, then each file detailed therein will be packed into
1018
- # the resulting Archive::Tar::Minitar::Output stream; if +recurse_dirs+
1019
- # is true, then directories will be recursed.
1020
- #
1021
- # If +src+ is an Array, it will be treated as the argument to Find.find;
1022
- # all files matching will be packed.
1023
- def pack(src, dest, recurse_dirs = true, &block)
1024
- Output.open(dest) do |outp|
1025
- if src.kind_of?(Array)
1026
- src.each do |entry|
1027
- pack_file(entry, outp, &block)
1028
- if dir?(entry) and recurse_dirs
1029
- Dir["#{entry}/**/**"].each do |ee|
1030
- pack_file(ee, outp, &block)
1031
- end
1032
- end
1033
- end
1034
- else
1035
- Find.find(src) do |entry|
1036
- pack_file(entry, outp, &block)
1037
- end
1038
- end
1039
- end
1040
- end
1041
-
1042
- # A convenience method to unpack files from +src+ into the directory
1043
- # specified by +dest+. Only those files named explicitly in +files+
1044
- # will be extracted.
1045
- def unpack(src, dest, files = [], &block)
1046
- Input.open(src) do |inp|
1047
- if File.exist?(dest) and (not dir?(dest))
1048
- raise "Can't unpack to a non-directory."
1049
- elsif not File.exist?(dest)
1050
- FileUtils.mkdir_p(dest)
1051
- end
1052
-
1053
- inp.each do |entry|
1054
- if files.empty? or files.include?(entry.full_name)
1055
- inp.extract_entry(dest, entry, &block)
1056
- end
1057
- end
1058
- end
1059
- end
1060
- end
1061
- end
1062
-
1063
-