26 #include <ibrcommon/Logger.h>
36 #include <archive_entry.h>
42 #define BUFF_SIZE 8192
64 char* cast_buf = (
char*) buffer;
66 std::ostream &os = *(std::ostream*)ostream_ptr;
67 os.write(cast_buf, length);
75 std::istream &is = *(std::istream*)istream_ptr;
88 void TarUtils::read(
const ibrcommon::File &extract_folder, std::istream &input )
91 struct archive_entry *entry;
94 a = archive_read_new();
95 archive_read_support_filter_all(a);
96 archive_read_support_compression_all(a);
97 archive_read_support_format_tar(a);
101 while ((ret = archive_read_next_header(a, &entry)) == ARCHIVE_OK )
103 ibrcommon::File filename = extract_folder.get(archive_entry_pathname(entry));
104 ibrcommon::File path = filename.getParent();
105 ibrcommon::File::createDirectory(path);
106 fd = open(filename.getPath().c_str(),O_CREAT|O_WRONLY,0600);
107 if(fd < 0)
throw ibrcommon::IOException(
"cannot open file " + path.getPath());
108 archive_read_data_into_fd(a,fd);
112 archive_read_free(a);
117 bool processed =
false;
121 a = archive_write_new();
122 archive_write_set_format_ustar(a);
125 for(std::set<ObservedFile>::const_iterator of_iter = files_to_send.begin(); of_iter != files_to_send.end(); ++of_iter)
128 const ibrcommon::File &file = of.
getFile();
130 struct archive_entry *entry;
131 entry = archive_entry_new();
132 archive_entry_set_size(entry, file.size());
134 if(file.isDirectory())
136 archive_entry_set_filetype(entry, AE_IFDIR);
137 archive_entry_set_perm(entry, 0755);
141 archive_entry_set_filetype(entry, AE_IFREG);
142 archive_entry_set_perm(entry, 0644);
145 archive_entry_set_pathname(entry, rel_filename(root, of).c_str());
149 clock_gettime(CLOCK_REALTIME, &ts);
150 archive_entry_set_atime(entry, ts.tv_sec, ts.tv_nsec);
151 archive_entry_set_birthtime(entry, ts.tv_sec, ts.tv_nsec);
152 archive_entry_set_ctime(entry, ts.tv_sec, ts.tv_nsec);
153 archive_entry_set_mtime(entry, ts.tv_sec, ts.tv_nsec);
155 archive_write_header(a, entry);
180 if( (ret = archive_write_data(a, buff, len)) < 0)
182 IBRCOMMON_LOGGER_TAG(
"TarUtils", error) <<
"archive_write_data failed" << IBRCOMMON_LOGGER_ENDL;
189 }
catch (
const std::bad_cast&) { };
198 std::ifstream fs(file.getPath().c_str());
207 if( (ret = archive_write_data(a, buff, fs.gcount())) < 0)
209 IBRCOMMON_LOGGER_TAG(
"TarUtils", error) <<
"archive write failed" << IBRCOMMON_LOGGER_ENDL;
214 }
catch (
const ibrcommon::IOException &e) {
216 IBRCOMMON_LOGGER_TAG(
"TarUtils", error) <<
"archive write failed: " << e.what() << IBRCOMMON_LOGGER_ENDL;
218 archive_entry_free(entry);
219 archive_write_close(a);
220 archive_write_free(a);
225 archive_entry_free(entry);
227 archive_write_close(a);
228 archive_write_free(a);
234 const std::string file_path = f.
getFile().getPath();
237 const std::string parent_path = parent.
getFile().getPath();
240 if (parent.
getFile().isRoot())
return file_path.substr(parent_path.length(), file_path.length() - parent_path.length());
243 return file_path.substr(parent_path.length() + 1, file_path.length() - parent_path.length() - 1);
ssize_t __tar_utils_read_callback(struct archive *, void *istream_ptr, const void **buffer)
FileHandle open(const FATFile &file) const
static void read(const ibrcommon::File &extract_folder, std::istream &input)
size_t read(unsigned char *buffer, size_t buf_size)
static void write(std::ostream &output, const io::ObservedFile &root, const std::set< ObservedFile > &files_to_send)
int __tar_utils_close_callback(struct archive *, void *)
ssize_t __tar_utils_write_callback(struct archive *, void *ostream_ptr, const void *buffer, size_t length)
int __tar_utils_open_callback(struct archive *, void *)
const FatImageReader & getReader() const
const ibrcommon::File & getFile() const