← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3269: wrapper around libarchive & some tests

 

------------------------------------------------------------
revno: 3269
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sun 2013-04-21 19:39:36 +0200
message:
  wrapper around libarchive & some tests
added:
  dcpp/Archive.cpp
  dcpp/Archive.h
  test/data/
  test/data/gtest_h.tar.gz
  test/data/out/
  test/testarchive.cpp
modified:
  .bzrignore
  SConstruct
  ThirdPartyLicenses.txt
  dcpp/SConscript
  test/SConscript
  win32/SConscript


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file '.bzrignore'
--- .bzrignore	2012-11-15 18:32:12 +0000
+++ .bzrignore	2013-04-21 17:39:36 +0000
@@ -24,3 +24,4 @@
 ./msvc/debug-*
 ./msvc/release-*
 ./mingw/include/*.h
+./test/data/out/*

=== modified file 'SConstruct'
--- SConstruct	2013-03-30 14:28:28 +0000
+++ SConstruct	2013-04-21 17:39:36 +0000
@@ -269,6 +269,7 @@
 		conf.env['CPPDEFINES'].remove('HAS_PCH')
 env = conf.Finish()
 
+dev.archive = dev.build('archive/')
 dev.boost = dev.build('boost/')
 dev.dwarf = dev.build('dwarf/')
 dev.zlib = dev.build('zlib/')

=== modified file 'ThirdPartyLicenses.txt'
--- ThirdPartyLicenses.txt	2011-08-09 11:15:32 +0000
+++ ThirdPartyLicenses.txt	2013-04-21 17:39:36 +0000
@@ -1,9 +1,36 @@
 Licenses contained in this file:
+- libarchive license
 - GeoIP license
 - MiniUPnPc license
 - libnatpmp license
 - OpenSSL license
 
+--- libarchive license ---
+
+Copyright (c) 2003-2009 <author(s)>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer
+   in this position and unchanged.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 --- GeoIP license ---
 
 There are two licenses, one for the C library software, and one for

=== added file 'dcpp/Archive.cpp'
--- dcpp/Archive.cpp	1970-01-01 00:00:00 +0000
+++ dcpp/Archive.cpp	2013-04-21 17:39:36 +0000
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2001-2013 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "stdinc.h"
+#include "Archive.h"
+
+#include "debug.h"
+#include "File.h"
+#include "Util.h"
+
+#ifndef LIBARCHIVE_STATIC
+#define LIBARCHIVE_STATIC
+#endif
+#include <archive.h>
+#include <archive_entry.h>
+
+namespace dcpp {
+
+Archive::Archive(const string& path) : a(nullptr), f(nullptr) {
+	a = archive_read_new();
+	if(!a) {
+		throw Exception(Util::translateError(ERROR_OUTOFMEMORY));
+	}
+
+	f = dcpp_fopen(path.c_str(), "rb");
+	if(!f) {
+		throw FileException(Util::translateError(ERROR_FILE_NOT_FOUND));
+	}
+
+	check(archive_read_support_format_tar(a));
+	check(archive_read_support_filter_bzip2(a));
+	check(archive_read_support_filter_gzip(a));
+
+	check(archive_read_open_FILE(a, f));
+}
+
+Archive::~Archive() {
+	if(a) {
+		archive_read_free(a);
+	}
+
+	if(f) {
+		fclose(f);
+	}
+}
+
+void Archive::extract(const string& path) {
+	dcassert(!path.empty() && (*(path.end() - 1) == '/' || *(path.end() - 1) == '\\'));
+
+	::archive_entry* entry;
+	while(true) {
+		if(check(archive_read_next_header(a, &entry)) == ARCHIVE_EOF) {
+			break;
+		}
+
+		auto path_out = path + archive_entry_pathname(entry);
+		File::ensureDirectory(path_out);
+		File f_out(path_out, File::WRITE, File::CREATE | File::TRUNCATE);
+
+		const void* buf;
+		size_t size;
+		__LA_INT64_T offset;
+		while(true) {
+			if(check(archive_read_data_block(a, &buf, &size, &offset)) == ARCHIVE_EOF) {
+				break;
+			}
+			f_out.write(buf, size);
+		}
+	}
+}
+
+int Archive::check(int ret) {
+	if(ret != ARCHIVE_OK && ret != ARCHIVE_EOF) {
+		throw Exception(archive_error_string(a));
+	}
+	return ret;
+}
+
+} // namespace dcpp

=== added file 'dcpp/Archive.h'
--- dcpp/Archive.h	1970-01-01 00:00:00 +0000
+++ dcpp/Archive.h	2013-04-21 17:39:36 +0000
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2001-2013 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef DCPLUSPLUS_DCPP_ARCHIVE_H
+#define DCPLUSPLUS_DCPP_ARCHIVE_H
+
+#include <string>
+
+struct archive;
+
+namespace dcpp {
+
+using std::string;
+
+/** Wrappers around libarchive. */
+class Archive {
+public:
+	Archive(const string& path);
+	~Archive();
+
+	void extract(const string& path);
+
+private:
+	inline int check(int ret);
+
+	::archive* a;
+	::FILE* f;
+};
+
+} // namespace dcpp
+
+#endif

=== modified file 'dcpp/SConscript'
--- dcpp/SConscript	2012-03-03 15:04:23 +0000
+++ dcpp/SConscript	2013-04-21 17:39:36 +0000
@@ -4,7 +4,7 @@
 
 env, target, sources = dev.prepare_build(source_path, 'dcpp', in_bin = False, precompiled_header = 'stdinc')
 
-env.Append(CPPPATH = ['#/openssl/include', '#/bzip2', '#/zlib', '#/geoip'])
+env.Append(CPPPATH = ['#/openssl/include', '#/archive', '#/bzip2', '#/geoip', '#/zlib'])
 
 env.Append(CPPDEFINES = ['BUILDING_DCPP=1'])
 

=== modified file 'test/SConscript'
--- test/SConscript	2012-03-03 15:04:23 +0000
+++ test/SConscript	2013-04-21 17:39:36 +0000
@@ -28,7 +28,7 @@
 
 env.Append(LIBS = ['comctl32', 'ws2_32', 'ole32', 'gdi32', 'comdlg32', 'iphlpapi', 'winmm', 'shlwapi', 'oleaut32', 'uuid'])
 
-env.Append(CPPPATH = ['#/openssl/include', '#/miniupnpc', '#/dwt/include', '#/', '#/bzip2'])
+env.Append(CPPPATH = ['#/openssl/include', '#/bzip2', '#/dwt/include'])
 
 if '-mwindows' in env['CCFLAGS']:
 	env['CCFLAGS'].remove('-mwindows')
@@ -49,7 +49,7 @@
 if env['msvcproj']:
 	ret = dev.build_lib(env, target, sources, dev.cpp_lib)
 else:
-	ret = env.Program(target, [sources, dev.client, dev.dwarf, dev.zlib, dev.boost, dev.bzip2, dev.geoip, dev.miniupnpc, dev.natpmp, dev.intl])
+	ret = env.Program(target, [sources, dev.client, dev.archive, dev.dwarf, dev.zlib, dev.boost, dev.bzip2, dev.geoip, dev.miniupnpc, dev.natpmp, dev.intl])
 	ret = env.Command(dev.get_target(source_path, 'gtest.passed', in_bin=False), ret[0].abspath, runUnitTest)
 
 env.Help("\nYou can run the test suite by running 'scons test'\n")

=== added directory 'test/data'
=== added file 'test/data/gtest_h.tar.gz'
Binary files test/data/gtest_h.tar.gz	1970-01-01 00:00:00 +0000 and test/data/gtest_h.tar.gz	2013-04-21 17:39:36 +0000 differ
=== added directory 'test/data/out'
=== added file 'test/testarchive.cpp'
--- test/testarchive.cpp	1970-01-01 00:00:00 +0000
+++ test/testarchive.cpp	2013-04-21 17:39:36 +0000
@@ -0,0 +1,26 @@
+#include "testbase.h"
+
+#include <dcpp/Archive.h>
+#include <dcpp/File.h>
+#include <dcpp/MD5Hash.h>
+
+using namespace dcpp;
+
+TEST(testarchive, test_archive)
+{
+	try {
+		Archive("test/data/gtest_h.tar.gz").extract("test/data/out/");
+	}
+	catch(const Exception& e) {
+		FAIL() << e.getError();
+	}
+
+	auto md5 = [](string path) {
+		File f(path, File::READ, File::OPEN);
+		MD5Hash h;
+		h.update(f.read().c_str(), f.getSize());
+		return MD5Value(h.finalize());
+	};
+
+	ASSERT_EQ(md5("test/gtest.h"), md5("test/data/out/gtest.h"));
+}

=== modified file 'win32/SConscript'
--- win32/SConscript	2013-04-18 19:46:26 +0000
+++ win32/SConscript	2013-04-21 17:39:36 +0000
@@ -48,7 +48,7 @@
 if env['msvcproj']:
 	ret = dev.build_lib(env, target, sources, dev.cpp_lib)
 else:
-	ret = env.Program(target, [sources, res, dev.client, dev.dwarf, dev.zlib, dev.boost, dev.bzip2, dev.geoip, dev.miniupnpc, dev.natpmp, dev.dwt, dev.intl])
+	ret = env.Program(target, [sources, res, dev.client, dev.archive, dev.dwarf, dev.zlib, dev.boost, dev.bzip2, dev.geoip, dev.miniupnpc, dev.natpmp, dev.dwt, dev.intl])
 
 if 'gcc' in env['TOOLS']:
 	# strip debug info to a separate PDB file