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.
31 TarFile::TarFile(const char *filename
,
33 tartype_t
*compress_ops
,
36 m_throw(always_throw
),
39 // figure out how to handle the file flags/modes
41 mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
44 flags
= O_WRONLY
| O_CREAT
| O_EXCL
;
50 // open... throw on error, as we are in the constructor
51 if( tar_open(&m_tar
, const_cast<char*>(filename
),
52 compress_ops
, flags
, mode
, TAR_VERBOSE
| TAR_GNU
) == -1 ) {
53 throw TarError(std::string("Unable to open tar file: ") + strerror(errno
));
61 } catch( TarError
&te
) {}
64 bool TarFile::False(const char *msg
)
73 bool TarFile::False(const std::string
&msg
, int err
)
75 std::string str
= msg
;
85 if( tar_append_eof(m_tar
) != 0 )
86 return False("Unable to write eof", errno
);
89 if( tar_close(m_tar
) != 0 ) {
90 return False("Unable to close file", errno
);
97 /// Appends a new file to the current tarfile, using tarpath as
98 /// its internal filename, and data as the complete file contents.
99 /// Uses current date and time as file mtime.
100 bool TarFile::AppendFile(const char *tarpath
, const std::string
&data
)
102 // write standard file header
103 th_set_type(m_tar
, REGTYPE
);
104 th_set_mode(m_tar
, 0644);
105 th_set_path(m_tar
, const_cast<char*>(tarpath
));
106 th_set_user(m_tar
, 0);
107 th_set_group(m_tar
, 0);
108 th_set_size(m_tar
, data
.size());
109 th_set_mtime(m_tar
, time(NULL
));
110 if( th_write(m_tar
) != 0 ) {
111 return False("Unable to write tar header", errno
);
114 // write the data in blocks until finished
115 char block
[T_BLOCKSIZE
];
116 for( size_t pos
= 0; pos
< data
.size(); pos
+= T_BLOCKSIZE
) {
117 memset(block
, 0, T_BLOCKSIZE
);
119 size_t size
= T_BLOCKSIZE
;
120 if( data
.size() - pos
< T_BLOCKSIZE
)
121 size
= data
.size() - pos
;
123 memcpy(block
, data
.data() + pos
, size
);
125 if( tar_block_write(m_tar
, block
) != T_BLOCKSIZE
) {
126 return False("Unable to write block", errno
);
133 /// Reads next available file into data, filling tarpath with
134 /// internal filename from tarball.
135 bool TarFile::ReadNextFile(std::string
&tarpath
, std::string
&data
)
141 // read next tar file header
142 if( th_read(m_tar
) != 0 ) {
143 // this is not necessarily an error, as it could just
144 // be the end of file, so a simple false is good here,
145 // don't throw an exception
150 // write standard file header
151 if( !TH_ISREG(m_tar
) ) {
152 return False("Only regular files are supported inside a tarball.");
155 char *pathname
= th_get_pathname(m_tar
);
158 // FIXME (leak) - someday, when all distros use a patched version of
159 // libtar, we may be able to avoid this memory leak, but
160 // th_get_pathname() does not consistently return a user-freeable
161 // string on all distros.
163 // See the following links for more information:
164 // https://bugs.launchpad.net/ubuntu/+source/libtar/+bug/41804
165 // https://lists.feep.net:8080/pipermail/libtar/2006-April/000222.html
168 size_t size
= th_get_size(m_tar
);
170 // read the data in blocks until finished
171 char block
[T_BLOCKSIZE
];
172 for( size_t pos
= 0; pos
< size
; pos
+= T_BLOCKSIZE
) {
173 memset(block
, 0, T_BLOCKSIZE
);
175 size_t readsize
= T_BLOCKSIZE
;
176 if( size
- pos
< T_BLOCKSIZE
)
177 readsize
= size
- pos
;
179 if( tar_block_read(m_tar
, block
) != T_BLOCKSIZE
) {
180 return False("Unable to read block", errno
);
183 data
.append(block
, readsize
);
189 /// Read next available filename, skipping the data if it is
191 bool TarFile::ReadNextFilenameOnly(std::string
&tarpath
)
196 // read next tar file header
197 if( th_read(m_tar
) != 0 ) {
198 // this is not necessarily an error, as it could just
199 // be the end of file, so a simple false is good here,
200 // don't throw an exception
205 // write standard file header
206 if( !TH_ISREG(m_tar
) ) {
207 return False("Only regular files are supported inside a tarball.");
210 char *pathname
= th_get_pathname(m_tar
);
212 // See above FIXME (leak) comment
215 if( tar_skip_regfile(m_tar
) != 0 ) {
216 return False("Unable to skip tar file", errno
);
236 cout
<< "Writing test file..." << endl
;
237 reuse::TarFile
output("tartest.tar.gz", true, true, true);
239 for( int i
= 0; i
< 60; i
++ ) {
240 data
.append("0123456789", 10);
243 output
.AppendFile("path1/test1.txt", data
);
244 output
.AppendFile("path2/test2.txt", data
);
248 cout
<< "Reading test file..." << endl
;
249 reuse::TarFile
input("tartest.tar.gz", false, true, true);
250 std::string path
, incoming
;
252 while( input
.ReadNextFile(path
, incoming
) ) {
253 cout
<< "Read: " << path
255 << (( data
== incoming
) ? "equal" : "different")
261 unlink("tartest.tar.gz");
263 } catch( reuse::TarFile::TarError
&te
) {
264 cerr
<< te
.what() << endl
;