tarfile.h

Go to the documentation of this file.
00001 ///
00002 /// \file       tarfile.h
00003 ///             API for reading and writing sequentially from compressed
00004 ///             tar files.
00005 
00006 /*
00007     Copyright (C) 2007-2012, Chris Frey <cdfrey@foursquare.net>
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __REUSE_TARFILE_H__
00023 #define __REUSE_TARFILE_H__
00024 
00025 #include "dll.h"
00026 #include <string>
00027 #include <stdexcept>
00028 #include <libtar.h>
00029 
00030 namespace Barry {
00031         class Data;
00032 }
00033 
00034 namespace reuse {
00035 
00036 //
00037 // Compression options... more op sets can be added based on
00038 // threading needs, or threading library support.
00039 //
00040 
00041 /// Compression op set for zlib, non-threadsafe.
00042 extern tartype_t gztar_ops_nonthread;
00043 
00044 class BXLOCAL TarFile
00045 {
00046         TAR *m_tar;
00047         bool m_throw;
00048         bool m_writemode;
00049         std::string m_last_error;
00050 
00051 private:
00052         bool False(const char *msg);
00053         bool False(const std::string &str) { return False(str.c_str()); }
00054         bool False(const std::string &msg, int err);
00055 
00056 public:
00057         class TarError : public std::runtime_error
00058         {
00059         public:
00060                 TarError(const std::string &msg) : std::runtime_error(msg) {}
00061         };
00062 
00063 public:
00064         explicit TarFile(const char *filename, bool write = false,
00065                 tartype_t *compress_ops = 0, bool always_throw = false);
00066         ~TarFile();
00067 
00068         const std::string& get_last_error() const { return m_last_error; }
00069 
00070         bool Close();
00071 
00072         /// Appends a new file to the current tarfile, using tarpath as
00073         /// its internal filename, and data as the complete file contents.
00074         /// Uses current date and time as file mtime.
00075         bool AppendFile(const char *tarpath, const std::string &data);
00076 
00077         /// Reads next available file into data, filling tarpath with
00078         /// internal filename from tarball.
00079         /// Returns false on end of archive.
00080         bool ReadNextFile(std::string &tarpath, std::string &data);
00081         bool ReadNextFile(std::string &tarpath, Barry::Data &data);
00082 
00083         /// Read next available filename, skipping the data if it is
00084         /// a regular file
00085         bool ReadNextFilenameOnly(std::string &tarpath);
00086 };
00087 
00088 }
00089 
00090 #endif
00091