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... skip all directories
144 if( th_read(m_tar
) != 0 ) {
145 // this is not necessarily an error, as it could just
146 // be the end of file, so a simple false is good here,
147 // don't throw an exception
151 } while( TH_ISDIR(m_tar
) );
153 // write standard file header
154 if( !TH_ISREG(m_tar
) ) {
155 return False("Only regular files are supported inside a tarball.");
158 char *pathname
= th_get_pathname(m_tar
);
161 // FIXME (leak) - someday, when all distros use a patched version of
162 // libtar, we may be able to avoid this memory leak, but
163 // th_get_pathname() does not consistently return a user-freeable
164 // string on all distros.
166 // See the following links for more information:
167 // https://bugs.launchpad.net/ubuntu/+source/libtar/+bug/41804
168 // https://lists.feep.net:8080/pipermail/libtar/2006-April/000222.html
171 size_t size
= th_get_size(m_tar
);
173 // read the data in blocks until finished
174 char block
[T_BLOCKSIZE
];
175 for( size_t pos
= 0; pos
< size
; pos
+= T_BLOCKSIZE
) {
176 memset(block
, 0, T_BLOCKSIZE
);
178 size_t readsize
= T_BLOCKSIZE
;
179 if( size
- pos
< T_BLOCKSIZE
)
180 readsize
= size
- pos
;
182 if( tar_block_read(m_tar
, block
) != T_BLOCKSIZE
) {
183 return False("Unable to read block", errno
);
186 data
.append(block
, readsize
);
192 // FIXME - yes, this is blatant copying of code, but this is
193 // specific to Barry, to use a Barry::Data object instead of std::string
194 // in order to reduce copies.
195 bool TarFile::ReadNextFile(std::string
&tarpath
, Barry::Data
&data
)
201 // read next tar file header... skip all directories
203 if( th_read(m_tar
) != 0 ) {
204 // this is not necessarily an error, as it could just
205 // be the end of file, so a simple false is good here,
206 // don't throw an exception
210 } while( TH_ISDIR(m_tar
) );
212 // write standard file header
213 if( !TH_ISREG(m_tar
) ) {
214 return False("Only regular files are supported inside a tarball.");
217 char *pathname
= th_get_pathname(m_tar
);
220 // FIXME (leak) - someday, when all distros use a patched version of
221 // libtar, we may be able to avoid this memory leak, but
222 // th_get_pathname() does not consistently return a user-freeable
223 // string on all distros.
225 // See the following links for more information:
226 // https://bugs.launchpad.net/ubuntu/+source/libtar/+bug/41804
227 // https://lists.feep.net:8080/pipermail/libtar/2006-April/000222.html
230 size_t size
= th_get_size(m_tar
);
232 // read the data in blocks until finished
233 char block
[T_BLOCKSIZE
];
234 for( size_t pos
= 0; pos
< size
; pos
+= T_BLOCKSIZE
) {
235 memset(block
, 0, T_BLOCKSIZE
);
237 size_t readsize
= T_BLOCKSIZE
;
238 if( size
- pos
< T_BLOCKSIZE
)
239 readsize
= size
- pos
;
241 if( tar_block_read(m_tar
, block
) != T_BLOCKSIZE
) {
242 return False("Unable to read block", errno
);
245 data
.Append(block
, readsize
);
251 /// Read next available filename, skipping the data if it is
253 bool TarFile::ReadNextFilenameOnly(std::string
&tarpath
)
258 // read next tar file header... skip all directories
260 if( th_read(m_tar
) != 0 ) {
261 // this is not necessarily an error, as it could just
262 // be the end of file, so a simple false is good here,
263 // don't throw an exception
267 } while( TH_ISDIR(m_tar
) );
269 // write standard file header
270 if( !TH_ISREG(m_tar
) ) {
271 return False("Only regular files are supported inside a tarball.");
274 char *pathname
= th_get_pathname(m_tar
);
276 // See above FIXME (leak) comment
279 if( tar_skip_regfile(m_tar
) != 0 ) {
280 return False("Unable to skip tar file", errno
);
300 cout
<< "Writing test file..." << endl
;
301 reuse::TarFile
output("tartest.tar.gz", true, true, true);
303 for( int i
= 0; i
< 60; i
++ ) {
304 data
.append("0123456789", 10);
307 output
.AppendFile("path1/test1.txt", data
);
308 output
.AppendFile("path2/test2.txt", data
);
312 cout
<< "Reading test file..." << endl
;
313 reuse::TarFile
input("tartest.tar.gz", false, true, true);
314 std::string path
, incoming
;
316 while( input
.ReadNextFile(path
, incoming
) ) {
317 cout
<< "Read: " << path
319 << (( data
== incoming
) ? "equal" : "different")
325 unlink("tartest.tar.gz");
327 } catch( reuse::TarFile::TarError
&te
) {
328 cerr
<< te
.what() << endl
;