8 #include "ucompose.hpp"
10 Tar_Helper::Tar_Helper(std::string file, std::ios::openmode mode)
15 if (mode & std::ios::in)
17 else if (mode & std::ios::out)
25 char *f = strdup(file.c_str());
26 int retval = tar_open (&t, f, NULL, m, perms, TAR_GNU);
31 if (mode & std::ios::in)
33 std::list<std::string> files = getFilenames();
34 tmpoutdir = String::ucompose("%1/%2.%3/", Glib::get_tmp_dir(), files.front(), getpid());
35 File::create_dir(tmpoutdir);
42 bool Tar_Helper::saveFile(std::string filename, std::string destfile)
44 char *f = strdup(filename.c_str());
47 b = strdup(File::get_basename(filename,true).c_str());
49 b = strdup(destfile.c_str());
50 int retval = tar_append_file(t, f, b);
58 void Tar_Helper::Close()
62 if (openmode & std::ios::out)
68 File::erase_dir(tmpoutdir);
72 std::string Tar_Helper::getFirstFile(bool &broken)
74 std::list<std::string> files = getFilenames();
75 return getFile(files.front(), broken);
78 std::string Tar_Helper::getFile(std::string filename, bool &broken)
80 char buf[T_BLOCKSIZE];
81 lseek(t->fd, 0, SEEK_SET);
84 //cycle through looking to find a file by that name.
85 while ((i = th_read(t)) == 0)
87 char *f = th_get_pathname(t);
88 if (strcmp(f, filename.c_str()) == 0)
93 int size = th_get_size(t);
94 for (int i = size; i > 0; i -= T_BLOCKSIZE)
96 k = tar_block_read(t, buf);
110 int size = th_get_size(t);
111 char *data = (char* )malloc(size);
112 size_t bytesread = 0;
113 for (int i = size; i > 0; i -= T_BLOCKSIZE)
115 k = tar_block_read(t, buf);
116 if (k != T_BLOCKSIZE)
121 memcpy (&data[bytesread], buf, i > T_BLOCKSIZE ? T_BLOCKSIZE : i);
122 bytesread += (i > T_BLOCKSIZE) ? T_BLOCKSIZE : i;
124 std::string outfile = tmpoutdir + filename;
125 FILE *fileptr = fopen(outfile.c_str(), "w");
126 fwrite(data, 1, size, fileptr);
133 std::list<std::string> Tar_Helper::getFilenames()
135 std::list<std::string> result;
137 char buf[T_BLOCKSIZE];
138 lseek(t->fd, 0, SEEK_SET);
139 //cycle through looking to find files with that extension.
140 while ((i = th_read(t)) == 0)
142 char *f = th_get_pathname(t);
145 int size = th_get_size(t);
146 for (int i = size; i > 0; i -= T_BLOCKSIZE)
148 k = tar_block_read(t, buf);
149 if (k != T_BLOCKSIZE)
155 std::list<std::string> Tar_Helper::getFilenamesWithExtension(std::string ext)
157 std::list<std::string> result;
159 char buf[T_BLOCKSIZE];
160 lseek(t->fd, 0, SEEK_SET);
161 //cycle through looking to find files with that extension.
162 while ((i = th_read(t)) == 0)
164 char *f = th_get_pathname(t);
165 if (File::nameEndsWith(f, ext) == true)
168 int size = th_get_size(t);
169 for (int i = size; i > 0; i -= T_BLOCKSIZE)
171 k = tar_block_read(t, buf);
172 if (k != T_BLOCKSIZE)
179 Tar_Helper::~Tar_Helper()