tar_archive.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* Optional per-entry callback on unpacking - return false to skip entry */
  32. typedef bool (*tar_unpack_file_cb)(const char* name, bool is_directory, void* context);
  33. void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context);
  34. /* Low-level API */
  35. bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath);
  36. bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len);
  37. bool tar_archive_file_add_data_block(
  38. TarArchive* archive,
  39. const uint8_t* data_block,
  40. const int32_t block_len);
  41. bool tar_archive_file_finalize(TarArchive* archive);
  42. bool tar_archive_store_data(
  43. TarArchive* archive,
  44. const char* path,
  45. const uint8_t* data,
  46. const int32_t data_len);
  47. bool tar_archive_finalize(TarArchive* archive);
  48. #ifdef __cplusplus
  49. }
  50. #endif