diff options
-rw-r--r-- | configure.ac | 17 | ||||
-rw-r--r-- | m4/ax_boost_base.m4 | 191 | ||||
-rw-r--r-- | m4/ax_boost_iostreams.m4 | 99 | ||||
-rw-r--r-- | src/generic/util/file_cache.cc | 177 |
4 files changed, 434 insertions, 50 deletions
diff --git a/configure.ac b/configure.ac index f6c238db..8466f202 100644 --- a/configure.ac +++ b/configure.ac @@ -61,6 +61,16 @@ boost/flyweight.hpp dnl boost/format.hpp dnl boost/functional/hash.hpp dnl boost/function.hpp dnl +boost/iostreams/copy.hpp dnl +boost/iostreams/device/file.hpp dnl +boost/iostreams/filter/gzip.hpp dnl +boost/iostreams/filtering_stream.hpp dnl +boost/iostreams/invert.hpp dnl +boost/iostreams/operations.hpp dnl +boost/iostreams/read.hpp dnl +boost/iostreams/seek.hpp dnl +boost/iostreams/stream.hpp dnl +boost/iostreams/write.hpp dnl boost/lambda/bind.hpp dnl boost/make_shared.hpp dnl boost/multi_index_container.hpp dnl @@ -76,6 +86,9 @@ boost/unordered_set.hpp dnl , [AC_MSG_FAILURE([Boost install not found, too old, or incomplete; install libboost-dev.])]) +AX_BOOST_BASE() +AX_BOOST_IOSTREAMS + PKG_CHECK_MODULES(SQLITE3, sqlite3) HAVE_GTK=1 @@ -121,7 +134,9 @@ then fi CXXFLAGS="$CXXFLAGS $LOG4CXX_CFLAGS $SIGC_CFLAGS $CWIDGET_CFLAGS $ept_CFLAGS $SQLITE3_CFLAGS" -LIBS="$LIBS $LOG4CXX_LIBS $SIGC_LIBS $CWIDGET_LIBS $ept_LIBS $SQLITE3_LIBS" +LIBS="$LIBS $LOG4CXX_LIBS $SIGC_LIBS $CWIDGET_LIBS $ept_LIBS $SQLITE3_LIBS $BOOST_IOSTREAMS_LIB" +CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" +LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" if test x$HAVE_GTK = x1 then diff --git a/m4/ax_boost_base.m4 b/m4/ax_boost_base.m4 new file mode 100644 index 00000000..369acd6c --- /dev/null +++ b/m4/ax_boost_base.m4 @@ -0,0 +1,191 @@ +dnl @synopsis AX_BOOST([MINIMUM-VERSION]) +dnl +dnl Test for the Boost C++ libraries of a particular version (or newer) +dnl +dnl If no path to the installed boost library is given the macro +dnl searchs under /usr, /usr/local, and /opt, and evaluates the +dnl $BOOST_ROOT environment variable. Further documentation is +dnl available at <http://randspringer.de/boost/index.html>. +dnl +dnl This macro calls: +dnl +dnl AC_SUBST(BOOST_CPPFLAGS) / AC_SUBST(BOOST_LDFLAGS) +dnl +dnl And sets: +dnl +dnl HAVE_BOOST +dnl +dnl @category InstalledPackages +dnl @category Cxx +dnl @author Thomas Porschberg <thomas@randspringer.de> +dnl @version 2006-06-15 +dnl @license AllPermissive +dnl +dnl Modified to make Boost the default and to fail if the user tries +dnl to disable it. + +AC_DEFUN([AX_BOOST_BASE], +[ +AC_ARG_WITH([boost], + AS_HELP_STRING([--with-boost@<:@=DIR@:>@], [modify the directory that is searched for Boost]), + [ + if test "$withval" = "no"; then + want_boost="no" + elif test "$withval" = "yes"; then + want_boost="yes" + ac_boost_path="" + else + want_boost="yes" + ac_boost_path="$withval" + fi + ], + [want_boost="yes"]) + +if test "x$want_boost" = "xyes"; then + boost_lib_version_req=ifelse([$1], ,1.20.0,$1) + boost_lib_version_req_shorten=`expr $boost_lib_version_req : '\([[0-9]]*\.[[0-9]]*\)'` + boost_lib_version_req_major=`expr $boost_lib_version_req : '\([[0-9]]*\)'` + boost_lib_version_req_minor=`expr $boost_lib_version_req : '[[0-9]]*\.\([[0-9]]*\)'` + boost_lib_version_req_sub_minor=`expr $boost_lib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'` + if test "x$boost_lib_version_req_sub_minor" = "x" ; then + boost_lib_version_req_sub_minor="0" + fi + WANT_BOOST_VERSION=`expr $boost_lib_version_req_major \* 100000 \+ $boost_lib_version_req_minor \* 100 \+ $boost_lib_version_req_sub_minor` + AC_MSG_CHECKING(for boostlib >= $boost_lib_version_req) + succeeded=no + + dnl first we check the system location for boost libraries + dnl this location ist chosen if boost libraries are installed with the --layout=system option + dnl or if you install boost with RPM + if test "$ac_boost_path" != ""; then + BOOST_LDFLAGS="-L$ac_boost_path/lib" + BOOST_CPPFLAGS="-I$ac_boost_path/include" + else + for ac_boost_path_tmp in /usr /usr/local /opt ; do + if test -d "$ac_boost_path_tmp/include/boost" && test -r "$ac_boost_path_tmp/include/boost"; then + BOOST_LDFLAGS="-L$ac_boost_path_tmp/lib" + BOOST_CPPFLAGS="-I$ac_boost_path_tmp/include" + break; + fi + done + fi + + CPPFLAGS_SAVED="$CPPFLAGS" + CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" + export CPPFLAGS + + LDFLAGS_SAVED="$LDFLAGS" + LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" + export LDFLAGS + + AC_LANG_PUSH(C++) + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ + @%:@include <boost/version.hpp> + ]], [[ + #if BOOST_VERSION >= $WANT_BOOST_VERSION + // Everything is okay + #else + # error Boost version is too old + #endif + ]])],[ + AC_MSG_RESULT(yes) + succeeded=yes + found_system=yes + ],[ + ]) + AC_LANG_POP([C++]) + + + + dnl if we found no boost with system layout we search for boost libraries + dnl built and installed without the --layout=system option or for a staged(not installed) version + if test "x$succeeded" != "xyes"; then + _version=0 + if test "$ac_boost_path" != ""; then + BOOST_LDFLAGS="-L$ac_boost_path/lib" + if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then + for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do + _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'` + V_CHECK=`expr $_version_tmp \> $_version` + if test "$V_CHECK" = "1" ; then + _version=$_version_tmp + fi + VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'` + BOOST_CPPFLAGS="-I$ac_boost_path/include/boost-$VERSION_UNDERSCORE" + done + fi + else + for ac_boost_path in /usr /usr/local /opt ; do + if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then + for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do + _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'` + V_CHECK=`expr $_version_tmp \> $_version` + if test "$V_CHECK" = "1" ; then + _version=$_version_tmp + best_path=$ac_boost_path + fi + done + fi + done + + VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'` + BOOST_CPPFLAGS="-I$best_path/include/boost-$VERSION_UNDERSCORE" + BOOST_LDFLAGS="-L$best_path/lib" + + if test "x$BOOST_ROOT" != "x"; then + if test -d "$BOOST_ROOT" && test -r "$BOOST_ROOT" && test -d "$BOOST_ROOT/stage/lib" && test -r "$BOOST_ROOT/stage/lib"; then + version_dir=`expr //$BOOST_ROOT : '.*/\(.*\)'` + stage_version=`echo $version_dir | sed 's/boost_//' | sed 's/_/./g'` + stage_version_shorten=`expr $stage_version : '\([[0-9]]*\.[[0-9]]*\)'` + V_CHECK=`expr $stage_version_shorten \>\= $_version` + if test "$V_CHECK" = "1" ; then + AC_MSG_NOTICE(We will use a staged boost library from $BOOST_ROOT) + BOOST_CPPFLAGS="-I$BOOST_ROOT" + BOOST_LDFLAGS="-L$BOOST_ROOT/stage/lib" + fi + fi + fi + fi + + CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" + export CPPFLAGS + LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" + export LDFLAGS + + AC_LANG_PUSH(C++) + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ + @%:@include <boost/version.hpp> + ]], [[ + #if BOOST_VERSION >= $WANT_BOOST_VERSION + // Everything is okay + #else + # error Boost version is too old + #endif + ]])],[ + AC_MSG_RESULT(yes) + succeeded=yes + found_system=yes + ],[ + ]) + AC_LANG_POP([C++]) + fi + + if test "$succeeded" != "yes" ; then + if test "$_version" = "0" ; then + AC_MSG_ERROR([[We could not detect the boost libraries (version $boost_lib_version_req_shorten or higher). If you have a staged boost library (still not installed) please specify \$BOOST_ROOT in your environment and do not give a PATH to --with-boost option. If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.]]) + else + AC_MSG_NOTICE([Your boost libraries seems to old (version $_version).]) + fi + else + AC_SUBST(BOOST_CPPFLAGS) + AC_SUBST(BOOST_LDFLAGS) + AC_DEFINE(HAVE_BOOST,,[define if the Boost library is available]) + fi + + CPPFLAGS="$CPPFLAGS_SAVED" + LDFLAGS="$LDFLAGS_SAVED" +else + AC_MSG_ERROR([[Boost is required for compilation and may not be disabled.]]) +fi + +]) diff --git a/m4/ax_boost_iostreams.m4 b/m4/ax_boost_iostreams.m4 new file mode 100644 index 00000000..ba866ce9 --- /dev/null +++ b/m4/ax_boost_iostreams.m4 @@ -0,0 +1,99 @@ +dnl @synopsis AX_BOOST_IOSTREAMS +dnl +dnl Test for IOStreams library from the Boost C++ libraries. The macro +dnl requires a preceding call to AX_BOOST_BASE. Further documentation +dnl is available at <http://randspringer.de/boost/index.html>. +dnl +dnl This macro calls: +dnl +dnl AC_SUBST(BOOST_IOSTREAMS_LIB) +dnl +dnl And sets: +dnl +dnl HAVE_BOOST_IOSTREAMS +dnl +dnl @category InstalledPackages +dnl @category Cxx +dnl @author Thomas Porschberg <thomas@randspringer.de> +dnl @version 2006-06-15 +dnl @license AllPermissive +dnl +dnl Modified by me to make using iostreams the default and to fail if +dnl the user tries to disable it. Also changed linking to check for +dnl libboost_iostreams-mt instead of libboost_iostreams and to fail +dnl if the library can't be found. +dnl -- dburrows 2009-08-22 + +AC_DEFUN([AX_BOOST_IOSTREAMS], +[ + AC_ARG_WITH([boost-iostreams], + AS_HELP_STRING([--with-boost-iostreams@<:@=special-lib@:>@], + [use the IOStreams library from boost - it is possible to specify a certain library for the linker + e.g. --with-boost-iostreams=boost_iostreams-gcc-mt-d-1_33_1 ]), + [ + if test "$withval" = "no"; then + want_boost="no" + elif test "$withval" = "yes"; then + want_boost="yes" + ax_boost_user_iostreams_lib="" + else + want_boost="yes" + ax_boost_user_iostreams_lib="$withval" + fi + ], + [want_boost="yes"] + ) + + if test "x$want_boost" = "xyes"; then + AC_REQUIRE([AC_PROG_CC]) + CPPFLAGS_SAVED="$CPPFLAGS" + CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" + export CPPFLAGS + + LDFLAGS_SAVED="$LDFLAGS" + LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" + export LDFLAGS + + AC_CACHE_CHECK(whether the Boost::IOStreams library is available, + ax_cv_boost_iostreams, + [AC_LANG_PUSH([C++]) + AC_COMPILE_IFELSE(AC_LANG_PROGRAM([[@%:@include <boost/iostreams/filtering_stream.hpp> + @%:@include <boost/range/iterator_range.hpp> + ]], + [[std::string input = "Hello World!"; + namespace io = boost::iostreams; + io::filtering_istream in(boost::make_iterator_range(input)); + return 0; + ]]), + ax_cv_boost_iostreams=yes, ax_cv_boost_iostreams=no) + AC_LANG_POP([C++]) + ]) + if test "x$ax_cv_boost_iostreams" = "xyes"; then + AC_DEFINE(HAVE_BOOST_IOSTREAMS,,[define if the Boost::IOStreams library is available]) + BN=boost_iostreams + if test "x$ax_boost_user_iostreams_lib" = "x"; then + for ax_lib in $BN-mt $BN-$CC-mt $BN-$CC-mt-s \ + lib$BN-mt lib$BN-$CC-mt lib$BN-$CC-mt-s \ + $BN-mgw-mt $BN-mgw-mt-s ; do + AC_CHECK_LIB($ax_lib, main, [BOOST_IOSTREAMS_LIB="-l$ax_lib" AC_SUBST(BOOST_IOSTREAMS_LIB) link_iostreams="yes" break], + [link_iostreams="no"]) + done + else + for ax_lib in $ax_boost_user_iostreams_lib $BN-$ax_boost_user_iostreams_lib; do + AC_CHECK_LIB($ax_lib, main, + [BOOST_IOSTREAMS_LIB="-l$ax_lib" AC_SUBST(BOOST_IOSTREAMS_LIB) link_iostreams="yes" break], + [link_iostreams="no"]) + done + + fi + if test "x$link_iostreams" = "xno"; then + AC_MSG_ERROR([Could not link against $ax_lib !]) + fi + fi + + CPPFLAGS="$CPPFLAGS_SAVED" + LDFLAGS="$LDFLAGS_SAVED" + else + AC_MSG_ERROR([Boost IOStreams is required and may not be disabled.]) + fi +]) diff --git a/src/generic/util/file_cache.cc b/src/generic/util/file_cache.cc index 0f9496a0..7d00ba73 100644 --- a/src/generic/util/file_cache.cc +++ b/src/generic/util/file_cache.cc @@ -29,6 +29,16 @@ #include <cwidget/generic/util/ssprintf.h> #include <boost/format.hpp> +#include <boost/iostreams/copy.hpp> +#include <boost/iostreams/device/file.hpp> +#include <boost/iostreams/filter/gzip.hpp> +#include <boost/iostreams/filtering_stream.hpp> +#include <boost/iostreams/invert.hpp> +#include <boost/iostreams/operations.hpp> +#include <boost/iostreams/read.hpp> +#include <boost/iostreams/seek.hpp> +#include <boost/iostreams/stream.hpp> +#include <boost/iostreams/write.hpp> #include <boost/make_shared.hpp> #include <loggers.h> @@ -39,6 +49,7 @@ using namespace aptitude::sqlite; namespace cw = cwidget; +namespace io = boost::iostreams; namespace aptitude { @@ -277,16 +288,46 @@ insert into globals(TotalBlobSize) values(0); \ void putItem(const std::string &key, const std::string &path) { - cw::threads::mutex::lock l(store_mutex); - - LOG_INFO(Loggers::getAptitudeDownloadCache(), - "Caching " << path << " as " << key); - + // NOTE: We wait until we've finished compressing the input + // file to take the store mutex. try { + // Before anything else, we need to compress the input + // file to a temporary location. Without this step, + // there's no way to know the size of the compressed + // data, but we need that size in order to insert it + // into the cache database. + temp::dir td("aptitudeCache"); + temp::name tn(td, "compressed"); + + const std::string compressed_path(tn.get_name()); + + // The size of the input file -- used only for logging + // so we can see how well it was compressed. + std::streamsize input_size = -1; + + // Compress the input file. + // + // TODO: make the compression level an option. Maybe + // also support other algorithms, although that needs a + // schema change (an extra column in the blobs table + // giving the compressor that was used). + { + io::filtering_ostream compressed_out(io::zlib_compressor(9) | io::file_sink(compressed_path)); + + input_size = io::copy(io::file(path), compressed_out); + } + + if(input_size < 0) + throw FileCacheException((boost::format("Unable to compress \"%s\" to \"%s\".") + % path % compressed_path).str()); + + LOG_TRACE(Loggers::getAptitudeDownloadCache(), + "Compressed \"" << path << "\" to \"" << compressed_path << "\""); + // Here's the plan: // - // 1) Open the file and get its size with fstat(). + // 1) Open the compressed file and get its size. // 2) If the file is too large to ever cache, return // immediately (don't cache it). // 3) In an sqlite transaction: @@ -299,31 +340,53 @@ insert into globals(TotalBlobSize) values(0); \ // Step 1) + // + // Using Unix I/O instead of Boost IOStreams or + // std::ifstream because my attempts to use the latter + // two failed utterly: it seems to be impossible to + // determine a file's size using a high-level interface. FileFd fd; - if(!fd.Open(path, FileFd::ReadOnly)) + if(!fd.Open(compressed_path, FileFd::ReadOnly)) { - std::string msg = cw::util::sstrerror(errno); + const int err = errno; throw FileCacheException((boost::format("Can't open \"%s\" to store it in the cache: %s") - % path % msg).str()); + % compressed_path % cw::util::sstrerror(err)).str()); } struct stat buf; if(fstat(fd.Fd(), &buf) != 0) { - std::string msg = cw::util::sstrerror(errno); - throw FileCacheException((boost::format("Can't determine the size of \"%s\" to store it in the cache: %s") - % path % msg).str()); + const int err = errno; + throw FileCacheException((boost::format("Can't determine the size of \"%s\" to store it in the cache: %s.") + % compressed_path % cw::util::sstrerror(err)).str()); } + off_t compressed_size = buf.st_size; + + if(compressed_size == 0 && input_size > 0) + throw FileCacheException("Sanity-check failed: a non-empty file was compressed to zero bytes!."); + // Step 2) - if(buf.st_size > max_size) + if(compressed_size > max_size) { LOG_INFO(Loggers::getAptitudeDownloadCache(), - "Refusing to cache \"" << path << "\" as \"" << key - << "\": its size " << buf.st_size << " is greater than the cache size limit " << max_size); + "Refusing to cache \"" << compressed_path << "\" as \"" << key + << "\": its size " << compressed_size + << " is greater than the cache size limit " << max_size); return; } + cw::threads::mutex::lock l(store_mutex); + + LOG_INFO(Loggers::getAptitudeDownloadCache(), + "Caching " << compressed_path << " as " << key + << " (size: " << input_size << " -> " + << compressed_size << " [" + << (compressed_size == 0 + ? (input_size == 0 ? boost::format("100%%") : boost::format("VANISHED?")) + : (boost::format("%.2f%%") % (((double)(100 * compressed_size)) / input_size))) + << "])"); + store->exec("begin transaction"); // Step 3) @@ -337,11 +400,11 @@ insert into globals(TotalBlobSize) values(0); \ sqlite3_int64 total_size = get_total_size_statement->get_int64(0); get_total_size_statement->exec(); - if(total_size + buf.st_size > max_size) + if(total_size + compressed_size > max_size) { LOG_TRACE(Loggers::getAptitudeDownloadCache(), boost::format("The new cache size %ld exceeds the maximum size %ld; dropping old entries.") - % (total_size + buf.st_size) % max_size); + % (total_size + compressed_size) % max_size); bool first = true; sqlite3_int64 last_cache_id_dropped = -1; @@ -353,7 +416,7 @@ insert into globals(TotalBlobSize) values(0); \ sqlite::db::statement_proxy read_entries_statement = store->get_cached_statement("select CacheId, BlobSize from cache order by CacheId"); - while(total_size + buf.st_size - amount_dropped > max_size && + while(total_size + compressed_size - amount_dropped > max_size && read_entries_statement->step()) { first = false; @@ -384,7 +447,7 @@ insert into globals(TotalBlobSize) values(0); \ // Step 3.c) { LOG_TRACE(Loggers::getAptitudeDownloadCache(), - boost::format("Inserting \"%s\" into the blobs table.") % path); + boost::format("Inserting \"%s\" into the blobs table.") % compressed_path); // The blob has to be inserted before the // cache entry, so the foreign key constraints @@ -395,7 +458,7 @@ insert into globals(TotalBlobSize) values(0); \ { sqlite::db::statement_proxy insert_blob_statement = store->get_cached_statement("insert into blobs (Data) values (zeroblob(?))"); - insert_blob_statement->bind_int64(1, buf.st_size); + insert_blob_statement->bind_int64(1, compressed_size); insert_blob_statement->exec(); } @@ -421,7 +484,7 @@ insert into globals(TotalBlobSize) values(0); \ sqlite::db::statement_proxy insert_cache_statement = store->get_cached_statement("insert into cache (BlobId, BlobSize, Key) values (?, ?, ?)"); insert_cache_statement->bind_int64(1, inserted_blob_row); - insert_cache_statement->bind_int64(2, buf.st_size); + insert_cache_statement->bind_int64(2, compressed_size); insert_cache_statement->bind_string(3, key); insert_cache_statement->exec(); } @@ -433,12 +496,10 @@ insert into globals(TotalBlobSize) values(0); \ "Data", inserted_blob_row); - int amount_to_write(buf.st_size); + int amount_to_write(compressed_size); int blob_offset = 0; static const int block_size = 16384; - // This can safely be static, since we acquire - // a mutex before we enter this routine. - static char buf[block_size]; + char buf[block_size]; while(amount_to_write > 0) { int curr_amt; @@ -449,11 +510,11 @@ insert into globals(TotalBlobSize) values(0); \ int amt_read = read(fd.Fd(), buf, curr_amt); if(amt_read == 0) - throw FileCacheException((boost::format("Unexpected end of file while reading %s into the cache.") % path).str()); + throw FileCacheException((boost::format("Unexpected end of file while reading %s into the cache.") % compressed_path).str()); else if(amt_read < 0) { std::string errmsg(cw::util::sstrerror(errno)); - throw FileCacheException((boost::format("Error while reading %s into the cache: %s.") % path % errmsg).str()); + throw FileCacheException((boost::format("Error while reading %s into the cache: %s.") % compressed_path % errmsg).str()); } blob_data->write(blob_offset, buf, curr_amt); @@ -493,12 +554,21 @@ insert into globals(TotalBlobSize) values(0); \ boost::format("Can't cache \"%s\" as \"%s\": %s") % path % key % ex.errmsg()); } + catch(std::exception &ex) + { + LOG_WARN(Loggers::getAptitudeDownloadCache(), + boost::format("Can't cache \"%s\" as \"%s\": %s") + % path % key % ex.what()); + } } temp::name getItem(const std::string &key) { cw::threads::mutex::lock l(store_mutex); + LOG_TRACE(Loggers::getAptitudeDownloadCache(), + boost::format("Looking up \"%s\" in the cache.") % key); + // Here's the plan. // // 1) In an sqlite transaction: @@ -522,6 +592,9 @@ insert into globals(TotalBlobSize) values(0); \ if(!find_cache_entry_statement->step()) // 1.a.i: no matching entry { + LOG_TRACE(Loggers::getAptitudeDownloadCache(), + boost::format("No entry for \"%s\" found in the cache.") % key); + store->exec("rollback"); return temp::name(); } @@ -549,8 +622,14 @@ insert into globals(TotalBlobSize) values(0); \ temp::dir d("aptitudeCacheExtract"); temp::name rval(d, "extracted"); + int extracted_size = -1; { - FileFd outfile(rval.get_name(), FileFd::WriteEmpty, 0644); + // Decompress the data as it's written to the + // output file. + io::filtering_ostream outfile(io::zlib_decompressor() | io::file_sink(rval.get_name())); + if(!outfile.good()) + throw FileCacheException(((boost::format("Can't open \"%s\" for writing")) + % rval.get_name()).str()); boost::shared_ptr<sqlite::blob> blob_data = sqlite::blob::open(*store, @@ -561,13 +640,14 @@ insert into globals(TotalBlobSize) values(0); \ false); static const int block_size = 16384; - // Note: this is safe because we hold a mutex - // for the duration of this method. - static char buf[block_size]; + char buf[block_size]; int amount_to_read = blob_data->size(); int blob_offset = 0; + LOG_TRACE(Loggers::getAptitudeDownloadCache(), + boost::format("Extracting %d bytes to \"%s\".") % amount_to_read % rval.get_name()); + // Copy the blob into the temporary file. while(amount_to_read > 0) { @@ -579,27 +659,19 @@ insert into globals(TotalBlobSize) values(0); \ curr_amt = block_size; blob_data->read(blob_offset, buf, curr_amt); - int amt_written = write(outfile.Fd(), buf, curr_amt); - - if(amt_written < 0) - { - std::string errmsg(cw::util::sstrerror(errno)); - throw FileCacheException((boost::format("Can't open \"%s\" for writing: %s") - % rval.get_name() % errmsg).str()); - } - else if(amt_written == 0) - { - throw FileCacheException((boost::format("Unable to write to \"%s\".") - % rval.get_name()).str()); - } - else - { - blob_offset += amt_written; - amount_to_read -= amt_written; - } + std::streamsize amt_written = io::write(outfile, buf, curr_amt); + + blob_offset += amt_written; + amount_to_read -= amt_written; } + + extracted_size = blob_data->size(); } + LOG_INFO(Loggers::getAptitudeDownloadCache(), + boost::format("Extracted %d bytes corresponding to \"%s\" to \"%s\".") + % extracted_size % key % rval.get_name()); + store->exec("commit"); return rval; } @@ -631,6 +703,13 @@ insert into globals(TotalBlobSize) values(0); \ % key % ex.errmsg()); return temp::name(); } + catch(std::exception &ex) + { + LOG_WARN(Loggers::getAptitudeDownloadCache(), + boost::format("Can't get the cache entry for \"%s\": %s") + % key % ex.what()); + return temp::name(); + } } }; |