tools: MimeDump<> can have all static members
[barry.git] / src / tarfile.cc
blob8d3a943df91c79f65a356b8872c4ca74b6b4ebcc
1 ///
2 /// \file tarfile.cc
3 /// API for reading and writing sequentially from compressed
4 /// tar files.
6 /*
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.
22 #include "tarfile.h"
23 #include "data.h"
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
30 namespace reuse {
32 TarFile::TarFile(const char *filename,
33 bool create,
34 tartype_t *compress_ops,
35 bool always_throw)
36 : m_tar(0),
37 m_throw(always_throw),
38 m_writemode(create)
40 // figure out how to handle the file flags/modes
41 int flags = 0;
42 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
44 if( m_writemode ) {
45 flags = O_WRONLY | O_CREAT | O_EXCL;
47 else {
48 flags = O_RDONLY;
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));
58 TarFile::~TarFile()
60 try {
61 Close();
62 } catch( TarError &te ) {}
65 bool TarFile::False(const char *msg)
67 m_last_error = msg;
68 if( m_throw )
69 throw TarError(msg);
70 else
71 return false;
74 bool TarFile::False(const std::string &msg, int err)
76 std::string str = msg;
77 str += ": ";
78 str += strerror(err);
79 return False(str);
82 bool TarFile::Close()
84 if( m_tar ) {
85 if( m_writemode ) {
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);
93 m_tar = 0;
95 return true;
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);
131 return true;
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)
138 // start fresh
139 tarpath.clear();
140 data.clear();
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
147 m_last_error = "";
148 return false;
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);
157 tarpath = pathname;
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
168 // free(pathname);
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);
187 return true;
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)
195 // start fresh
196 tarpath.clear();
197 data.QuickZap();
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
204 m_last_error = "";
205 return false;
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);
214 tarpath = pathname;
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
225 // free(pathname);
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);
244 return true;
247 /// Read next available filename, skipping the data if it is
248 /// a regular file
249 bool TarFile::ReadNextFilenameOnly(std::string &tarpath)
251 // start fresh
252 tarpath.clear();
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
259 m_last_error = "";
260 return false;
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);
269 tarpath = pathname;
270 // See above FIXME (leak) comment
271 // free(pathname);
273 if( tar_skip_regfile(m_tar) != 0 ) {
274 return False("Unable to skip tar file", errno);
277 return true;
281 } // namespace reuse
284 #ifdef __TEST_MODE__
286 #include <iostream>
287 #include <unistd.h>
289 using namespace std;
291 int main()
293 try {
294 cout << "Writing test file..." << endl;
295 reuse::TarFile output("tartest.tar.gz", true, true, true);
296 std::string data;
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);
303 output.Close();
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
312 << " Data: "
313 << (( data == incoming ) ? "equal" : "different")
314 << endl;
317 input.Close();
319 unlink("tartest.tar.gz");
321 } catch( reuse::TarFile::TarError &te ) {
322 cerr << te.what() << endl;
323 return 1;
327 #endif