|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Egor Pugin |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * 3. Neither the name of the copyright holder nor the names of |
| 13 | + * its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "archive.h" |
| 29 | + |
| 30 | +#ifdef _WIN32 |
| 31 | +#include <libarchive/archive.h> |
| 32 | +#include <libarchive/archive_entry.h> |
| 33 | +#else |
| 34 | +#include <archive.h> |
| 35 | +#include <archive_entry.h> |
| 36 | +#endif |
| 37 | + |
| 38 | +#if !defined(_WIN32) && !defined(__APPLE__) |
| 39 | +#include <linux/limits.h> |
| 40 | +#endif |
| 41 | + |
| 42 | +bool pack_files(const path &fn, const Files &files, const path &root_dir) |
| 43 | +{ |
| 44 | + bool result = true; |
| 45 | + auto a = archive_write_new(); |
| 46 | + archive_write_add_filter_gzip(a); |
| 47 | + archive_write_set_format_pax_restricted(a); |
| 48 | + archive_write_open_filename(a, fn.string().c_str()); |
| 49 | + for (auto &f : files) |
| 50 | + { |
| 51 | + if (!fs::exists(f)) |
| 52 | + { |
| 53 | + result = false; |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // skip symlinks too |
| 58 | + if (!fs::is_regular_file(f)) |
| 59 | + continue; |
| 60 | + |
| 61 | + auto sz = fs::file_size(f); |
| 62 | + auto e = archive_entry_new(); |
| 63 | + archive_entry_set_pathname(e, fs::relative(f, root_dir).string().c_str()); |
| 64 | + archive_entry_set_size(e, sz); |
| 65 | + archive_entry_set_filetype(e, AE_IFREG); |
| 66 | + archive_entry_set_perm(e, 0644); |
| 67 | + archive_write_header(a, e); |
| 68 | + auto fp = fopen(f.string().c_str(), "rb"); |
| 69 | + if (!fp) |
| 70 | + { |
| 71 | + archive_entry_free(e); |
| 72 | + result = false; |
| 73 | + continue; |
| 74 | + } |
| 75 | + char buff[8192]; |
| 76 | + while (auto len = fread(buff, 1, sizeof(buff), fp)) |
| 77 | + archive_write_data(a, buff, len); |
| 78 | + fclose(fp); |
| 79 | + archive_entry_free(e); |
| 80 | + } |
| 81 | + archive_write_close(a); |
| 82 | + archive_write_free(a); |
| 83 | + return result; |
| 84 | +} |
| 85 | + |
| 86 | +Files unpack_file(const path &fn, const path &dst) |
| 87 | +{ |
| 88 | + if (!fs::exists(dst)) |
| 89 | + fs::create_directories(dst); |
| 90 | + |
| 91 | + Files files; |
| 92 | + |
| 93 | + auto a = archive_read_new(); |
| 94 | + archive_read_support_filter_all(a); |
| 95 | + archive_read_support_format_all(a); |
| 96 | + auto r = archive_read_open_filename(a, fn.string().c_str(), 10240); |
| 97 | + if (r != ARCHIVE_OK) |
| 98 | + throw std::runtime_error(archive_error_string(a)); |
| 99 | + archive_entry *entry; |
| 100 | + while (archive_read_next_header(a, &entry) == ARCHIVE_OK) |
| 101 | + { |
| 102 | + // act on regular files only |
| 103 | + auto type = archive_entry_filetype(entry); |
| 104 | + if (type != AE_IFREG) |
| 105 | + continue; |
| 106 | + |
| 107 | + path f = dst / archive_entry_pathname(entry); |
| 108 | + path fdir = f.parent_path(); |
| 109 | + if (!fs::exists(fdir)) |
| 110 | + fs::create_directories(fdir); |
| 111 | + path filename = f.filename(); |
| 112 | + if (filename == "." || filename == "..") |
| 113 | + continue; |
| 114 | + |
| 115 | + auto fn = fs::absolute(f).string(); |
| 116 | + // do not use fopen() because MSVC's file streams support UTF-16 characters |
| 117 | + // and we won't be able to unpack files under some tricky dir names |
| 118 | + std::ofstream o(fn, std::ios::out | std::ios::binary); |
| 119 | + if (!o) |
| 120 | + { |
| 121 | + // TODO: probably remove this and linux/limit.h header when server will be using hash paths |
| 122 | +#ifdef _WIN32 |
| 123 | + if (fn.size() >= MAX_PATH) |
| 124 | + continue; |
| 125 | +#elif defined(__APPLE__) |
| 126 | +#else |
| 127 | + if (fn.size() >= PATH_MAX) |
| 128 | + continue; |
| 129 | +#endif |
| 130 | + throw std::runtime_error("Cannot open file: " + f.string()); |
| 131 | + } |
| 132 | + while (1) |
| 133 | + { |
| 134 | + const void *buff; |
| 135 | + size_t size; |
| 136 | + int64_t offset; |
| 137 | + auto r = archive_read_data_block(a, &buff, &size, &offset); |
| 138 | + if (r == ARCHIVE_EOF) |
| 139 | + break; |
| 140 | + if (r < ARCHIVE_OK) |
| 141 | + throw std::runtime_error(archive_error_string(a)); |
| 142 | + o.write((const char *)buff, size); |
| 143 | + } |
| 144 | + files.insert(f); |
| 145 | + } |
| 146 | + archive_read_close(a); |
| 147 | + archive_read_free(a); |
| 148 | + |
| 149 | + return files; |
| 150 | +} |
0 commit comments