00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "config.h"
00020
00021 #include "EDoc/DirectoryArchive.h"
00022 #include "EDoc/exceptions.h"
00023 #include "EDoc/utils.h"
00024
00025 #ifdef HAVE_STRING_H
00026 #include <string.h>
00027 #endif
00028
00029 #ifdef HAVE_SYS_STAT_H
00030 #include <sys/stat.h>
00031 #endif
00032
00033 #ifdef HAVE_DIRENT_H
00034 #include <dirent.h>
00035 #endif
00036
00037 #ifdef HAVE_ERRNO_H
00038 #include <errno.h>
00039 #endif
00040
00041
00042 namespace EDoc
00043 {
00044
00045 static bool IsDirectory(std::string dirname)
00046 {
00047 struct stat buffer;
00048 int result = stat(dirname.c_str(), &buffer);
00049 if (result == -1)
00050 {
00051 return false;
00052 }
00053 return buffer.st_mode & S_IFDIR;
00054 }
00055
00056 static std::list<ManagedFile> GetAllFiles(const ManagedFile& arch,
00057 std::string subdir)
00058 {
00059 DIR* dir;
00060 std::list<ManagedFile> ret;
00061
00062
00063 std::string full_dir = arch.GetWorkFileName() + subdir;
00064 dir = opendir(full_dir.c_str());
00065 if (!dir)
00066 {
00067 std::ostringstream error_stream;
00068 error_stream << "Failed to open the directory."
00069 << " Name: " << full_dir;
00070 EDOC_THROW_EXCEPTION(EDoc::FileIOException, strerror(errno),
00071 error_stream.str());
00072 }
00073
00074 for (struct dirent* entry = readdir(dir); entry; entry = readdir(dir))
00075 {
00076
00077
00078
00079
00080 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
00081 {
00082 continue;
00083 }
00084
00085 std::string filename = full_dir + "/" + entry->d_name;
00086 if (IsDirectory(filename))
00087 {
00088 Append(ret, GetAllFiles(arch, subdir + "/" + entry->d_name));
00089 }
00090 else
00091 {
00092
00093
00094
00095
00096 std::list<std::string> id = arch.GetIdentifier();
00097 id.push_back(subdir + "/" + entry->d_name);
00098 ret.push_back(ManagedFile(id, filename, NULL));
00099 }
00100 }
00101
00102 closedir(dir);
00103
00104 return ret;
00105 }
00106
00107 bool DirectoryArchive::HandlesType(const ManagedFile& dirname)
00108 {
00109 return IsDirectory(dirname.GetWorkFileName());
00110 }
00111
00112 std::list<ManagedFile> DirectoryArchive::ReadExtractAll(
00113 const ManagedFile& dirname)
00114 {
00115 return GetAllFiles(dirname, "");
00116 }
00117
00118 ManagedFile DirectoryArchive::WriteOpen(const ManagedFile& arch,
00119 std::string item_filename)
00120 {
00121 std::list<std::string> id = arch.GetIdentifier();
00122 id.push_back(item_filename);
00123
00124 return ManagedFile(id, arch.GetWorkFileName() + "/" + item_filename, NULL);
00125 }
00126
00127 }
00128