00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022
00023 #include "EDocBFD/BFDArchive.h"
00024
00025 #include "EDocBFD/bfd_utils.h"
00026 #include "EDocBFD/exceptions.h"
00027 #include "EDoc/utils.h"
00028 #include "EDoc/persistence_data.h"
00029
00030 #ifdef HAVE_FCNTL_H
00031 #include <fcntl.h>
00032 #endif
00033
00034 #ifdef HAVE_UNISTD_H
00035 #include <unistd.h>
00036 #endif
00037
00038 #ifdef HAVE_ERRNO_H
00039 #include <errno.h>
00040 #endif
00041
00042 namespace EDocBFD
00043 {
00044
00045
00046 ExtractedBFDFile::ExtractedBFDFile(std::string temp_file_name_in) :
00047 sync(false),
00048 temp_file_name(temp_file_name_in)
00049 {
00050 EDOC_Fine("Create new BFD Read tempfile: " << temp_file_name);
00051 EDoc::Persistence::AddTempFile(temp_file_name);
00052 }
00053
00054 ExtractedBFDFile::ExtractedBFDFile(std::string bfd_file_name_in,
00055 std::string temp_file_name_in,
00056 std::string embedded_file_name_in) :
00057
00058 sync(true),
00059 bfd_file_name(bfd_file_name_in),
00060 temp_file_name(temp_file_name_in),
00061 embedded_file_name(embedded_file_name_in)
00062 {
00063 EDOC_Fine("Create new BFD Write tempfile: " << temp_file_name);
00064 EDoc::Persistence::AddTempFile(temp_file_name);
00065 }
00066
00067 ExtractedBFDFile::~ExtractedBFDFile()
00068 {
00069 if (sync)
00070 {
00071 EDOC_Fine("Syncing write tempfile: " << temp_file_name);
00072 EmbedFileInBinary(temp_file_name,
00073 embedded_file_name,
00074 bfd_file_name,
00075 EDOC_SECTION_NAME);
00076 }
00077
00078 EDOC_Fine("Erasing tempfile: " << temp_file_name);
00079 unlink(temp_file_name.c_str());
00080 EDoc::Persistence::RemoveTempFile(temp_file_name);
00081 }
00082
00083
00084
00085 BFDArchive::BFDArchive()
00086 {
00087 InitialiseBFD();
00088 }
00089
00090 bool BFDArchive::HandlesType(const EDoc::ManagedFile& filename)
00091 {
00092 try
00093 {
00094 struct bfd* abfd = OpenBFDFile(filename.GetWorkFileName());
00095 bfd_close(abfd);
00096 return abfd;
00097 }
00098 catch (BFDException& e)
00099 {
00100 return false;
00101 }
00102 }
00103
00104 std::list<EDoc::ManagedFile> BFDArchive::ReadExtractAll(
00105 const EDoc::ManagedFile& filename)
00106 {
00107 std::list<EDoc::ManagedFile> files;
00108 struct bfd* abfd = OpenBFDFile(filename.GetWorkFileName());
00109 EDOC_Debug("Opened BFD file: " << filename.GetFileNameRep());
00110 try
00111 {
00112 ProcessReadBFD(files, abfd, filename);
00113 bfd_close(abfd);
00114 }
00115 catch(...)
00116 {
00117 bfd_close(abfd);
00118 throw;
00119 }
00120
00121 return files;
00122 }
00123
00124 EDoc::ManagedFile BFDArchive::WriteOpen(const EDoc::ManagedFile& arch,
00125 std::string item_filename)
00126 {
00127 std::string outfile = EDoc::CreateTempFilename();
00128 int fd = open(outfile.c_str(), O_WRONLY, O_TRUNC);
00129 if (fd == -1)
00130 {
00131 EDOC_THROW_EXCEPTION(BFDException, "", "");
00132 }
00133 close(fd);
00134
00135 std::list<std::string> id = arch.GetIdentifier();
00136 id.push_back(item_filename);
00137
00138 return EDoc::ManagedFile(id, outfile, new ExtractedBFDFile(
00139 arch.GetWorkFileName(), outfile, item_filename));
00140 }
00141
00142 }
00143