tar_archive.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <m-string.h>
  5. #include <storage/storage.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef struct TarArchive TarArchive;
  10. typedef struct Storage Storage;
  11. typedef enum {
  12. TAR_OPEN_MODE_READ = 'r',
  13. TAR_OPEN_MODE_WRITE = 'w',
  14. TAR_OPEN_MODE_STDOUT = 's' /* to be implemented */
  15. } TarOpenMode;
  16. TarArchive* tar_archive_alloc(Storage* storage);
  17. bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode);
  18. void tar_archive_free(TarArchive* archive);
  19. /* High-level API - assumes archive is open */
  20. bool tar_archive_unpack_to(
  21. TarArchive* archive,
  22. const char* destination,
  23. Storage_name_converter converter);
  24. bool tar_archive_add_file(
  25. TarArchive* archive,
  26. const char* fs_file_path,
  27. const char* archive_fname,
  28. const int32_t file_size);
  29. bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix);
  30. int32_t tar_archive_get_entries_count(TarArchive* archive);
  31. bool tar_archive_unpack_file(
  32. TarArchive* archive,
  33. const char* archive_fname,
  34. const char* destination);
  35. /* Optional per-entry callback on unpacking - return false to skip entry */
  36. typedef bool (*tar_unpack_file_cb)(const char* name, bool is_directory, void* context);
  37. void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context);
  38. /* Low-level API */
  39. bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath);
  40. bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len);
  41. bool tar_archive_file_add_data_block(
  42. TarArchive* archive,
  43. const uint8_t* data_block,
  44. const int32_t block_len);
  45. bool tar_archive_file_finalize(TarArchive* archive);
  46. bool tar_archive_store_data(
  47. TarArchive* archive,
  48. const char* path,
  49. const uint8_t* data,
  50. const int32_t data_len);
  51. bool tar_archive_finalize(TarArchive* archive);
  52. #ifdef __cplusplus
  53. }
  54. #endif