3 /// API for reading and writing sequentially from compressed
7 Copyright (C) 2007-2010, Chris Frey <cdfrey@foursquare.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
32 TarFile::TarFile(const char *filename
,
34 tartype_t
*compress_ops
,
37 m_throw(always_throw
),
40 // figure out how to handle the file flags/modes
42 mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
45 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
51 // open... throw on error, as we are in the constructor
52 if( tar_open(&m_tar
, const_cast<char*>(filename
),
53 compress_ops
, flags
, mode
, TAR_VERBOSE
| TAR_GNU
) == -1 ) {
54 throw TarError(std::string("Unable to open tar file: ") + strerror(errno
));
62 } catch( TarError
&te
) {}
65 bool TarFile::False(const char *msg
)
74 bool TarFile::False(const std::string
&msg
, int err
)
76 std::string str
= msg
;
86 if( tar_append_eof(m_tar
) != 0 )
87 return False("Unable to write eof", errno
);
90 if( tar_close(m_tar
) != 0 ) {
91 return False("Unable to close file", errno
);
98 /// Appends a new file to the current tarfile, using tarpath as
99 /// its internal filename, and data as the complete file contents.
100 /// Uses current date and time as file mtime.
101 bool TarFile::AppendFile(const char *tarpath
, const std::string
&data
)
103 // write standard file header
104 th_set_type(m_tar
, REGTYPE
);
105 th_set_mode(m_tar
, 0644);
106 th_set_path(m_tar
, const_cast<char*>(tarpath
));
107 th_set_user(m_tar
, 0);
108 th_set_group(m_tar
, 0);
109 th_set_size(m_tar
, data
.size());
110 th_set_mtime(m_tar
, time(NULL
));
111 if( th_write(m_tar
) != 0 ) {
112 return False("Unable to write tar header", errno
);
115 // write the data in blocks until finished
116 char block
[T_BLOCKSIZE
];
117 for( size_t pos
= 0; pos
< data
.size(); pos
+= T_BLOCKSIZE
) {
118 memset(block
, 0, T_BLOCKSIZE
);
120 size_t size
= T_BLOCKSIZE
;
121 if( data
.size() - pos
< T_BLOCKSIZE
)
122 size
= data
.size() - pos
;
124 memcpy(block
, data
.data() + pos
, size
);
126 if( tar_block_write(m_tar
, block
) != T_BLOCKSIZE
) {
127 return False("Unable to write block", errno
);
134 /// Reads next available file into data, filling tarpath with
135 /// internal filename from tarball.
136 bool TarFile::ReadNextFile(std::string
&tarpath
, std::string
&data
)
142 // read next tar file header
143 if( th_read(m_tar
) != 0 ) {
144 // this is not necessarily an error, as it could just
145 // be the end of file, so a simple false is good here,
146 // don't throw an exception
151 // write standard file header
152 if( !TH_ISREG(m_tar
) ) {
153 return False("Only regular files are supported inside a tarball.");
156 char *pathname
= th_get_pathname(m_tar
);
159 // FIXME (leak) - someday, when all distros use a patched version of
160 // libtar, we may be able to avoid this memory leak, but
161 // th_get_pathname() does not consistently return a user-freeable
162 // string on all distros.
164 // See the following links for more information:
165 // https://bugs.launchpad.net/ubuntu/+source/libtar/+bug/41804
166 // https://lists.feep.net:8080/pipermail/libtar/2006-April/000222.html
169 size_t size
= th_get_size(m_tar
);
171 // read the data in blocks until finished
172 char block
[T_BLOCKSIZE
];
173 for( size_t pos
= 0; pos
< size
; pos
+= T_BLOCKSIZE
) {
174 memset(block
, 0, T_BLOCKSIZE
);
176 size_t readsize
= T_BLOCKSIZE
;
177 if( size
- pos
< T_BLOCKSIZE
)
178 readsize
= size
- pos
;
180 if( tar_block_read(m_tar
, block
) != T_BLOCKSIZE
) {
181 return False("Unable to read block", errno
);
184 data
.append(block
, readsize
);
190 // FIXME - yes, this is blatant copying of code, but this is
191 // specific to Barry, to use a Barry::Data object instead of std::string
192 // in order to reduce copies.
193 bool TarFile::ReadNextFile(std::string
&tarpath
, Barry::Data
&data
)
199 // read next tar file header
200 if( th_read(m_tar
) != 0 ) {
201 // this is not necessarily an error, as it could just
202 // be the end of file, so a simple false is good here,
203 // don't throw an exception
208 // write standard file header
209 if( !TH_ISREG(m_tar
) ) {
210 return False("Only regular files are supported inside a tarball.");
213 char *pathname
= th_get_pathname(m_tar
);
216 // FIXME (leak) - someday, when all distros use a patched version of
217 // libtar, we may be able to avoid this memory leak, but
218 // th_get_pathname() does not consistently return a user-freeable
219 // string on all distros.
221 // See the following links for more information:
222 // https://bugs.launchpad.net/ubuntu/+source/libtar/+bug/41804
223 // https://lists.feep.net:8080/pipermail/libtar/2006-April/000222.html
226 size_t size
= th_get_size(m_tar
);
228 // read the data in blocks until finished
229 char block
[T_BLOCKSIZE
];
230 for( size_t pos
= 0; pos
< size
; pos
+= T_BLOCKSIZE
) {
231 memset(block
, 0, T_BLOCKSIZE
);
233 size_t readsize
= T_BLOCKSIZE
;
234 if( size
- pos
< T_BLOCKSIZE
)
235 readsize
= size
- pos
;
237 if( tar_block_read(m_tar
, block
) != T_BLOCKSIZE
) {
238 return False("Unable to read block", errno
);
241 data
.Append(block
, readsize
);
247 /// Read next available filename, skipping the data if it is
249 bool TarFile::ReadNextFilenameOnly(std::string
&tarpath
)
254 // read next tar file header
255 if( th_read(m_tar
) != 0 ) {
256 // this is not necessarily an error, as it could just
257 // be the end of file, so a simple false is good here,
258 // don't throw an exception
263 // write standard file header
264 if( !TH_ISREG(m_tar
) ) {
265 return False("Only regular files are supported inside a tarball.");
268 char *pathname
= th_get_pathname(m_tar
);
270 // See above FIXME (leak) comment
273 if( tar_skip_regfile(m_tar
) != 0 ) {
274 return False("Unable to skip tar file", errno
);
294 cout
<< "Writing test file..." << endl
;
295 reuse::TarFile
output("tartest.tar.gz", true, true, true);
297 for( int i
= 0; i
< 60; i
++ ) {
298 data
.append("0123456789", 10);
301 output
.AppendFile("path1/test1.txt", data
);
302 output
.AppendFile("path2/test2.txt", data
);
306 cout
<< "Reading test file..." << endl
;
307 reuse::TarFile
input("tartest.tar.gz", false, true, true);
308 std::string path
, incoming
;
310 while( input
.ReadNextFile(path
, incoming
) ) {
311 cout
<< "Read: " << path
313 << (( data
== incoming
) ? "equal" : "different")
319 unlink("tartest.tar.gz");
321 } catch( reuse::TarFile::TarError
&te
) {
322 cerr
<< te
.what() << endl
;