113 bool extract(
const char* filename,
const char* destDir)
130 struct archive_entry* entry;
133 a = archive_read_new();
134 archive_read_support_format_all(a);
135 archive_read_support_filter_all(a);
136 r = archive_read_open_filename(a, filename, READ_OPEN_BLOCK_SIZE);
139 auto errNo = archive_errno(a);
140 fprintf(stderr,
"%s (%d)\n", archive_error_string(a), errNo);
141 archive_read_free(a);
159 std::filesystem::path destinationDir(destDir);
162 std::error_code absoluteError;
163 const auto absoluteDestination = std::filesystem::absolute(destinationDir, absoluteError);
166 auto wide = absoluteDestination.wstring();
167 if (wide.rfind(L
"\\\\?\\", 0) == 0)
169 destinationDir = std::filesystem::path(std::move(wide));
171 else if (wide.rfind(L
"\\\\", 0) == 0)
173 wide.insert(2, L
"?\\UNC\\");
174 destinationDir = std::filesystem::path(std::move(wide));
178 destinationDir = std::filesystem::path(L
"\\\\?\\" + wide);
185 destinationDir.make_preferred();
191 logwarn(
"extract: absolute() failed for destination [" + destinationDir.string()
192 +
"]: " + absoluteError.message() +
" - proceeding without long-path prefix");
198 r = archive_read_next_header(a, &entry);
199 if (r == ARCHIVE_EOF)
202 fprintf(stderr,
"%s\n", archive_error_string(a));
203 if (r < ARCHIVE_WARN)
205 archive_read_free(a);
215 std::string entryPathname = archive_entry_pathname(entry);
218 logerror(
"Refusing unsafe archive entry name: [" + entryPathname +
"]");
219 archive_read_free(a);
222 const std::filesystem::path relativeEntryPath(entryPathname);
227 std::filesystem::path entryDestinationPath = destinationDir / relativeEntryPath;
228 entryDestinationPath.make_preferred();
230 logdebug(
"Extracting, destination dir: [" + std::string(destDir) +
"]");
231 logdebug(
"Extracting, entry path name: [" + entryPathname +
"]");
232 logdebug(
"Extracting, destination path name: [" + entryDestinationPath.string() +
"]");
238 if (archive_entry_hardlink(entry) !=
nullptr || archive_entry_symlink(entry) !=
nullptr)
240 logerror(
"Refusing link archive entry: [" + entryPathname +
"]");
241 archive_read_free(a);
244 const auto fileType = archive_entry_filetype(entry);
245 if (fileType == AE_IFDIR)
247 std::string walkError;
248 if (!securely_create_directories(destinationDir, relativeEntryPath, walkError))
250 logerror(
"Failed creating extraction directory for [" + entryPathname +
"]: " + walkError);
251 archive_read_free(a);
257 if (fileType != AE_IFREG)
261 logerror(
"Refusing non-regular archive entry: [" + entryPathname +
"]");
262 archive_read_free(a);
267 std::string walkError;
268 if (!securely_create_directories(destinationDir, relativeEntryPath.parent_path(), walkError))
270 logerror(
"Failed creating extraction parent directory for [" + entryPathname +
"]: " + walkError);
271 archive_read_free(a);
278 std::error_code targetSymlinkEc;
279 if (is_symlink(entryDestinationPath, targetSymlinkEc))
281 logerror(
"Refusing symlink extraction target [" + entryDestinationPath.string() +
"]");
282 archive_read_free(a);
286 std::ofstream outputStream(entryDestinationPath, std::ios::binary | std::ios::trunc);
289 logerror(
"Failed opening extraction file [" + entryDestinationPath.string() +
"]");
290 archive_read_free(a);
298 la_int64_t expectedOffset = 0;
301 const void* buff =
nullptr;
303 la_int64_t offset = 0;
305 r = archive_read_data_block(a, &buff, &size, &offset);
306 if (r == ARCHIVE_EOF)
312 fprintf(stderr,
"%s\n", archive_error_string(a));
313 archive_read_free(a);
324 if (offset != expectedOffset)
326 logerror(
"Non-contiguous data extents for archive entry [" + entryPathname +
"] (expected offset "
327 + std::to_string(expectedOffset) +
", got " + std::to_string(offset) +
")");
328 archive_read_free(a);
332 outputStream.write(
static_cast<const char*
>(buff),
static_cast<std::streamsize
>(size));
335 logerror(
"Failed writing extraction file [" + entryDestinationPath.string() +
"]");
336 archive_read_free(a);
339 expectedOffset = offset +
static_cast<la_int64_t
>(size);
345 if (expectedOffset != archive_entry_size(entry))
347 logerror(
"Incomplete archive entry [" + entryPathname +
"] (declared "
348 + std::to_string(archive_entry_size(entry)) +
" bytes, got "
349 + std::to_string(expectedOffset) +
")");
350 archive_read_free(a);
355 outputStream.close();
358 logerror(
"Failed flushing extraction file [" + entryDestinationPath.string() +
"]");
359 archive_read_free(a);
364 archive_read_close(a);
365 archive_read_free(a);
376 path destArchivePath = path(destDir) / archiveName;
380 destArchivePath.replace_extension(TGZ_EXTENSION);
384 struct archive* a = archive_write_new();
386 if (archive_write_set_format_ustar(a) != ARCHIVE_OK)
return false;
388 if (archive_write_add_filter_gzip(a) != ARCHIVE_OK)
return false;
390 if (archive_write_open_filename(a, destArchivePath.string().c_str()) != ARCHIVE_OK)
return false;
393 auto root_entry = archive_entry_new();
395 auto rootDir = path(srcDir).filename();
396 archive_entry_set_pathname(root_entry, rootDir.string().c_str());
397 archive_entry_set_filetype(root_entry, AE_IFDIR);
398 if (archive_write_header(a, root_entry) != ARCHIVE_OK)
return false;
399 archive_entry_free(root_entry);
404 for (
const auto& it : recursive_directory_iterator(srcDir))
406 if (it.is_directory())
408 auto relativePath = relative(it.path(), srcDir);
409 auto dir_entry = archive_entry_new();
414 archive_entry_set_pathname(dir_entry, (rootDir / relativePath).generic_string().c_str());
415 archive_entry_set_filetype(dir_entry, AE_IFDIR);
416 stat(it.path().string().c_str(), &st);
417 archive_entry_copy_stat(dir_entry, &st);
418 if (archive_write_header(a, dir_entry) != ARCHIVE_OK)
return false;
419 archive_write_finish_entry(a);
420 archive_entry_free(dir_entry);
422 else if (it.is_regular_file())
425 auto relativePath = relative(it.path(), srcDir);
426 auto file_entry = archive_entry_new();
427 archive_entry_set_pathname(file_entry, (rootDir / relativePath).generic_string().c_str());
428 stat(it.path().string().c_str(), &st);
429 archive_entry_set_filetype(file_entry, AE_IFREG);
430 archive_entry_set_size(file_entry, st.st_size);
433 archive_entry_copy_stat(file_entry, &st);
434 if (archive_write_header(a, file_entry) != ARCHIVE_OK)
return false;
436 std::ifstream ifs(it.path(), std::ios::in | std::ios::binary);
437 if (!ifs.is_open())
return false;
438 char szBuffer[READ_BUFF_SIZE]{ 0 };
441 ifs.read(szBuffer,
sizeof(szBuffer));
442 auto readin = ifs.gcount();
447 if (archive_write_data(a, szBuffer, readin) != readin)
return false;
459 archive_write_finish_entry(a);
460 archive_entry_free(file_entry);
465 archive_write_close(a);
466 archive_write_free(a);
468 fileOut = destArchivePath.string();