FileHandle.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. # Copyright © 2008-2010 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2012-2014 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Compression::FileHandle;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. use POSIX qw(:signal_h :sys_wait_h);
  21. use Carp;
  22. use Dpkg::Compression;
  23. use Dpkg::Compression::Process;
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use parent qw(IO::File Tie::Handle);
  27. # Useful reference to understand some kludges required to
  28. # have the object behave like a filehandle
  29. # http://blog.woobling.org/2009/10/are-filehandles-objects.html
  30. =encoding utf8
  31. =head1 NAME
  32. Dpkg::Compression::FileHandle - object dealing transparently with file compression
  33. =head1 SYNOPSIS
  34. use Dpkg::Compression::FileHandle;
  35. my ($fh, @lines);
  36. $fh = Dpkg::Compression::FileHandle->new(filename => 'sample.gz');
  37. print $fh "Something\n";
  38. close $fh;
  39. $fh = Dpkg::Compression::FileHandle->new();
  40. open($fh, '>', 'sample.bz2');
  41. print $fh "Something\n";
  42. close $fh;
  43. $fh = Dpkg::Compression::FileHandle->new();
  44. $fh->open('sample.xz', 'w');
  45. $fh->print("Something\n");
  46. $fh->close();
  47. $fh = Dpkg::Compression::FileHandle->new(filename => 'sample.gz');
  48. @lines = <$fh>;
  49. close $fh;
  50. $fh = Dpkg::Compression::FileHandle->new();
  51. open($fh, '<', 'sample.bz2');
  52. @lines = <$fh>;
  53. close $fh;
  54. $fh = Dpkg::Compression::FileHandle->new();
  55. $fh->open('sample.xz', 'r');
  56. @lines = $fh->getlines();
  57. $fh->close();
  58. =head1 DESCRIPTION
  59. Dpkg::Compression::FileHandle is an object that can be used
  60. like any filehandle and that deals transparently with compressed
  61. files. By default, the compression scheme is guessed from the filename
  62. but you can override this behaviour with the method C<set_compression>.
  63. If you don't open the file explicitly, it will be auto-opened on the
  64. first read or write operation based on the filename set at creation time
  65. (or later with the C<set_filename> method).
  66. Once a file has been opened, the filehandle must be closed before being
  67. able to open another file.
  68. =head1 STANDARD FUNCTIONS
  69. The standard functions acting on filehandles should accept a
  70. Dpkg::Compression::FileHandle object transparently including
  71. C<open> (only when using the variant with 3 parameters), C<close>,
  72. C<binmode>, C<eof>, C<fileno>, C<getc>, C<print>, C<printf>, C<read>,
  73. C<sysread>, C<say>, C<write>, C<syswrite>, C<seek>, C<sysseek>, C<tell>.
  74. Note however that C<seek> and C<sysseek> will only work on uncompressed
  75. files as compressed files are really pipes to the compressor programs
  76. and you can't seek on a pipe.
  77. =head1 FileHandle METHODS
  78. The object inherits from IO::File so all methods that work on this
  79. object should work for Dpkg::Compression::FileHandle too. There
  80. may be exceptions though.
  81. =head1 PUBLIC METHODS
  82. =over 4
  83. =item $fh = Dpkg::Compression::FileHandle->new(%opts)
  84. Creates a new filehandle supporting on-the-fly compression/decompression.
  85. Supported options are "filename", "compression", "compression_level" (see
  86. respective set_* functions) and "add_comp_ext". If "add_comp_ext"
  87. evaluates to true, then the extension corresponding to the selected
  88. compression scheme is automatically added to the recorded filename. It's
  89. obviously incompatible with automatic detection of the compression method.
  90. =cut
  91. # Object methods
  92. sub new {
  93. my ($this, %args) = @_;
  94. my $class = ref($this) || $this;
  95. my $self = IO::File->new();
  96. # Tying is required to overload the open functions and to auto-open
  97. # the file on first read/write operation
  98. tie *$self, $class, $self;
  99. bless $self, $class;
  100. # Initializations
  101. *$self->{compression} = 'auto';
  102. *$self->{compressor} = Dpkg::Compression::Process->new();
  103. *$self->{add_comp_ext} = $args{add_compression_extension} ||
  104. $args{add_comp_ext} || 0;
  105. *$self->{allow_sigpipe} = 0;
  106. if (exists $args{filename}) {
  107. $self->set_filename($args{filename});
  108. }
  109. if (exists $args{compression}) {
  110. $self->set_compression($args{compression});
  111. }
  112. if (exists $args{compression_level}) {
  113. $self->set_compression_level($args{compression_level});
  114. }
  115. return $self;
  116. }
  117. =item $fh->ensure_open($mode, %opts)
  118. Ensure the file is opened in the requested mode ("r" for read and "w" for
  119. write). The options are passed down to the compressor's spawn() call, if one
  120. is used. Opens the file with the recorded filename if needed. If the file
  121. is already open but not in the requested mode, then it errors out.
  122. =cut
  123. sub ensure_open {
  124. my ($self, $mode, %opts) = @_;
  125. if (exists *$self->{mode}) {
  126. return if *$self->{mode} eq $mode;
  127. croak "ensure_open requested incompatible mode: $mode";
  128. } else {
  129. # Sanitize options.
  130. delete $opts{from_pipe};
  131. delete $opts{from_file};
  132. delete $opts{to_pipe};
  133. delete $opts{to_file};
  134. if ($mode eq 'w') {
  135. $self->_open_for_write(%opts);
  136. } elsif ($mode eq 'r') {
  137. $self->_open_for_read(%opts);
  138. } else {
  139. croak "invalid mode in ensure_open: $mode";
  140. }
  141. }
  142. }
  143. ##
  144. ## METHODS FOR TIED HANDLE
  145. ##
  146. sub TIEHANDLE {
  147. my ($class, $self) = @_;
  148. return $self;
  149. }
  150. sub WRITE {
  151. my ($self, $scalar, $length, $offset) = @_;
  152. $self->ensure_open('w');
  153. return *$self->{file}->write($scalar, $length, $offset);
  154. }
  155. sub READ {
  156. my ($self, $scalar, $length, $offset) = @_;
  157. $self->ensure_open('r');
  158. return *$self->{file}->read($scalar, $length, $offset);
  159. }
  160. sub READLINE {
  161. my ($self) = shift;
  162. $self->ensure_open('r');
  163. return *$self->{file}->getlines() if wantarray;
  164. return *$self->{file}->getline();
  165. }
  166. sub OPEN {
  167. my ($self) = shift;
  168. if (scalar(@_) == 2) {
  169. my ($mode, $filename) = @_;
  170. $self->set_filename($filename);
  171. if ($mode eq '>') {
  172. $self->_open_for_write();
  173. } elsif ($mode eq '<') {
  174. $self->_open_for_read();
  175. } else {
  176. croak 'Dpkg::Compression::FileHandle does not support ' .
  177. "open() mode $mode";
  178. }
  179. } else {
  180. croak 'Dpkg::Compression::FileHandle only supports open() ' .
  181. 'with 3 parameters';
  182. }
  183. return 1; # Always works (otherwise errors out)
  184. }
  185. sub CLOSE {
  186. my ($self) = shift;
  187. my $ret = 1;
  188. if (defined *$self->{file}) {
  189. $ret = *$self->{file}->close(@_) if *$self->{file}->opened();
  190. } else {
  191. $ret = 0;
  192. }
  193. $self->_cleanup();
  194. return $ret;
  195. }
  196. sub FILENO {
  197. my ($self) = shift;
  198. return *$self->{file}->fileno(@_) if defined *$self->{file};
  199. return;
  200. }
  201. sub EOF {
  202. # Since perl 5.12, an integer parameter is passed describing how the
  203. # function got called, just ignore it.
  204. my ($self, $param) = (shift, shift);
  205. return *$self->{file}->eof(@_) if defined *$self->{file};
  206. return 1;
  207. }
  208. sub SEEK {
  209. my ($self) = shift;
  210. return *$self->{file}->seek(@_) if defined *$self->{file};
  211. return 0;
  212. }
  213. sub TELL {
  214. my ($self) = shift;
  215. return *$self->{file}->tell(@_) if defined *$self->{file};
  216. return -1;
  217. }
  218. sub BINMODE {
  219. my ($self) = shift;
  220. return *$self->{file}->binmode(@_) if defined *$self->{file};
  221. return;
  222. }
  223. ##
  224. ## NORMAL METHODS
  225. ##
  226. =item $fh->set_compression($comp)
  227. Defines the compression method used. $comp should one of the methods supported by
  228. B<Dpkg::Compression> or "none" or "auto". "none" indicates that the file is
  229. uncompressed and "auto" indicates that the method must be guessed based
  230. on the filename extension used.
  231. =cut
  232. sub set_compression {
  233. my ($self, $method) = @_;
  234. if ($method ne 'none' and $method ne 'auto') {
  235. *$self->{compressor}->set_compression($method);
  236. }
  237. *$self->{compression} = $method;
  238. }
  239. =item $fh->set_compression_level($level)
  240. Indicate the desired compression level. It should be a value accepted
  241. by the function C<compression_is_valid_level> of B<Dpkg::Compression>.
  242. =cut
  243. sub set_compression_level {
  244. my ($self, $level) = @_;
  245. *$self->{compressor}->set_compression_level($level);
  246. }
  247. =item $fh->set_filename($name, [$add_comp_ext])
  248. Use $name as filename when the file must be opened/created. If
  249. $add_comp_ext is passed, it indicates whether the default extension
  250. of the compression method must be automatically added to the filename
  251. (or not).
  252. =cut
  253. sub set_filename {
  254. my ($self, $filename, $add_comp_ext) = @_;
  255. *$self->{filename} = $filename;
  256. # Automatically add compression extension to filename
  257. if (defined($add_comp_ext)) {
  258. *$self->{add_comp_ext} = $add_comp_ext;
  259. }
  260. my $comp_ext_regex = compression_get_file_extension_regex();
  261. if (*$self->{add_comp_ext} and $filename =~ /\.$comp_ext_regex$/) {
  262. warning('filename %s already has an extension of a compressed file ' .
  263. 'and add_comp_ext is active', $filename);
  264. }
  265. }
  266. =item $file = $fh->get_filename()
  267. Returns the filename that would be used when the filehandle must
  268. be opened (both in read and write mode). This function errors out
  269. if "add_comp_ext" is enabled while the compression method is set
  270. to "auto". The returned filename includes the extension of the compression
  271. method if "add_comp_ext" is enabled.
  272. =cut
  273. sub get_filename {
  274. my $self = shift;
  275. my $comp = *$self->{compression};
  276. if (*$self->{add_comp_ext}) {
  277. if ($comp eq 'auto') {
  278. croak 'automatic detection of compression is ' .
  279. 'incompatible with add_comp_ext';
  280. } elsif ($comp eq 'none') {
  281. return *$self->{filename};
  282. } else {
  283. return *$self->{filename} . '.' .
  284. compression_get_property($comp, 'file_ext');
  285. }
  286. } else {
  287. return *$self->{filename};
  288. }
  289. }
  290. =item $ret = $fh->use_compression()
  291. Returns "0" if no compression is used and the compression method used
  292. otherwise. If the compression is set to "auto", the value returned
  293. depends on the extension of the filename obtained with the B<get_filename>
  294. method.
  295. =cut
  296. sub use_compression {
  297. my $self = shift;
  298. my $comp = *$self->{compression};
  299. if ($comp eq 'none') {
  300. return 0;
  301. } elsif ($comp eq 'auto') {
  302. $comp = compression_guess_from_filename($self->get_filename());
  303. *$self->{compressor}->set_compression($comp) if $comp;
  304. }
  305. return $comp;
  306. }
  307. =item $real_fh = $fh->get_filehandle()
  308. Returns the real underlying filehandle. Useful if you want to pass it
  309. along in a derived object.
  310. =cut
  311. sub get_filehandle {
  312. my $self = shift;
  313. return *$self->{file} if exists *$self->{file};
  314. }
  315. ## INTERNAL METHODS
  316. sub _open_for_write {
  317. my ($self, %opts) = @_;
  318. my $filehandle;
  319. croak 'cannot reopen an already opened compressed file'
  320. if exists *$self->{mode};
  321. if ($self->use_compression()) {
  322. *$self->{compressor}->compress(from_pipe => \$filehandle,
  323. to_file => $self->get_filename(), %opts);
  324. } else {
  325. CORE::open($filehandle, '>', $self->get_filename)
  326. or syserr(g_('cannot write %s'), $self->get_filename());
  327. }
  328. *$self->{mode} = 'w';
  329. *$self->{file} = $filehandle;
  330. }
  331. sub _open_for_read {
  332. my ($self, %opts) = @_;
  333. my $filehandle;
  334. croak 'cannot reopen an already opened compressed file'
  335. if exists *$self->{mode};
  336. if ($self->use_compression()) {
  337. *$self->{compressor}->uncompress(to_pipe => \$filehandle,
  338. from_file => $self->get_filename(), %opts);
  339. *$self->{allow_sigpipe} = 1;
  340. } else {
  341. CORE::open($filehandle, '<', $self->get_filename)
  342. or syserr(g_('cannot read %s'), $self->get_filename());
  343. }
  344. *$self->{mode} = 'r';
  345. *$self->{file} = $filehandle;
  346. }
  347. sub _cleanup {
  348. my $self = shift;
  349. my $cmdline = *$self->{compressor}{cmdline} // '';
  350. *$self->{compressor}->wait_end_process(nocheck => *$self->{allow_sigpipe});
  351. if (*$self->{allow_sigpipe}) {
  352. unless (($? == 0) || (WIFSIGNALED($?) && (WTERMSIG($?) == SIGPIPE))) {
  353. subprocerr($cmdline);
  354. }
  355. *$self->{allow_sigpipe} = 0;
  356. }
  357. delete *$self->{mode};
  358. delete *$self->{file};
  359. }
  360. =back
  361. =head1 DERIVED OBJECTS
  362. If you want to create an object that inherits from
  363. Dpkg::Compression::FileHandle you must be aware that
  364. the object is a reference to a GLOB that is returned by Symbol::gensym()
  365. and as such it's not a HASH.
  366. You can store internal data in a hash but you have to use
  367. C<*$self->{...}> to access the associated hash like in the example below:
  368. sub set_option {
  369. my ($self, $value) = @_;
  370. *$self->{option} = $value;
  371. }
  372. =head1 CHANGES
  373. =head2 Version 1.01 (dpkg 1.17.11)
  374. New argument: $fh->ensure_open() accepts an %opts argument.
  375. =head2 Version 1.00 (dpkg 1.15.6)
  376. Mark the module as public.
  377. =cut
  378. 1;