degate  0.1.2
Public Member Functions | Private Member Functions
degate::ProjectArchiver Class Reference

Export a project directory as a ZIP archive. More...

#include <ProjectArchiver.h>

List of all members.

Public Member Functions

 ProjectArchiver ()
 ~ProjectArchiver ()
void export_data (boost::filesystem::path const &project_dir, boost::filesystem::path const &archive_file, boost::filesystem::path const &prepend_dir) const

Private Member Functions

void add_single_file (struct zip *zip_archive, boost::filesystem::path const &archive_file, boost::filesystem::path const &base_dir_path, boost::filesystem::path const &file, boost::filesystem::path const &prepend_dir) const
void add_directory (struct zip *zip_archive, boost::filesystem::path const &archive_file, boost::filesystem::path const &base_dir_path, boost::filesystem::path const &dir, boost::filesystem::path const &prepend_dir) const

Detailed Description

Export a project directory as a ZIP archive.

Definition at line 37 of file ProjectArchiver.h.


Constructor & Destructor Documentation

Definition at line 55 of file ProjectArchiver.h.

{}

Definition at line 56 of file ProjectArchiver.h.

{}

Member Function Documentation

void ProjectArchiver::add_directory ( struct zip *  zip_archive,
boost::filesystem::path const &  archive_file,
boost::filesystem::path const &  base_dir_path,
boost::filesystem::path const &  dir,
boost::filesystem::path const &  prepend_dir 
) const [private]

Definition at line 71 of file ProjectArchiver.cc.

References degate::get_filename_from_path(), degate::is_directory(), and degate::strip_path().

                                                                   {

  directory_iterator end_iter;

  for(directory_iterator iter(dir); iter != end_iter; ++iter) {

    path stripped = prepend_dir / strip_path(iter->path(), base_dir_path);

    if(is_directory(iter->path())) {

#if BOOST_FILESYSTEM_VERSION==3
      std::string rel_dir = get_filename_from_path(stripped.native());
#else
      std::string rel_dir = get_filename_from_path(stripped.native_file_string());
#endif
      std::string pattern = "scaling_";
      bool skip = rel_dir.length() >= pattern.length() && (rel_dir.compare(0, pattern.length(), pattern) == 0);

      if(!skip) {

        if(zip_add_dir(zip_archive, stripped.string().c_str()) < 0) {
          boost::format f("Cannot add directory %1% to zip archive %2%: %3%");
          f % iter->path() % archive_file % zip_strerror(zip_archive);
          throw ZipException(f.str());
        }

        add_directory(zip_archive, archive_file, base_dir_path, 
                      iter->path(), // already prefixed with dir
                      prepend_dir);
      }

    }
    else {
      add_single_file(zip_archive, archive_file, base_dir_path, iter->path(), prepend_dir);
    }
  }

}

Here is the call graph for this function:

void ProjectArchiver::add_single_file ( struct zip *  zip_archive,
boost::filesystem::path const &  archive_file,
boost::filesystem::path const &  base_dir_path,
boost::filesystem::path const &  file,
boost::filesystem::path const &  prepend_dir 
) const [private]

Definition at line 39 of file ProjectArchiver.cc.

References debug(), degate::strip_path(), and TM.

                                                                     {

  assert(zip_archive != NULL);
  struct zip_source *source;

  path stripped = prepend_dir / strip_path(file, base_dir_path);

  debug(TM, "Add file %s as %s to zip archive.",
        file.string().c_str(), stripped.string().c_str());

  if((source = zip_source_file(zip_archive,
                               file.string().c_str(), 0, 0)) == NULL) {
    boost::format f("Cannot add file %1% to zip archive %2%: %3%");
    f % file % archive_file % zip_strerror(zip_archive);
    throw ZipException(f.str());
  }

  if(zip_add(zip_archive,
             stripped.string().c_str(),
             source) < 0) {

    boost::format f("Cannot add file %1% to zip archive %2%: %3%");
    f % file % archive_file % zip_strerror(zip_archive);
    throw ZipException(f.str());
  }
}

Here is the call graph for this function:

void ProjectArchiver::export_data ( boost::filesystem::path const &  project_dir,
boost::filesystem::path const &  archive_file,
boost::filesystem::path const &  prepend_dir 
) const

Definition at line 115 of file ProjectArchiver.cc.

                                                                 {

  struct zip *zip_archive;
  int err;

  if((zip_archive = zip_open(archive_file.string().c_str(), ZIP_CREATE, &err)) == NULL) {
    char errstr[1024];
    zip_error_to_str(errstr, sizeof(errstr), err, errno);
    throw ZipException(errstr);
  }

  if(zip_add_dir(zip_archive, prepend_dir.string().c_str()) < 0) {
    boost::format f("Cannot add directory %1% to zip archive %2%: %3%");
    f % prepend_dir % archive_file % zip_strerror(zip_archive);
    throw ZipException(f.str());
  }

  try {
    add_directory(zip_archive, archive_file, project_dir, project_dir, prepend_dir);
  }
  catch(ZipException const& ex) {
    // rethrow exception, but free zip_archive resource first
    if(zip_archive != NULL) zip_close(zip_archive);
    throw;
  }

  if(zip_archive != NULL && zip_close(zip_archive) < 0) {
    boost::format f("Cannot write zip archive %1%: %2%");
    f % archive_file % zip_strerror(zip_archive);
    throw ZipException(f.str());
  }

}

The documentation for this class was generated from the following files: