2022-04-13 23:50:25 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stdint.h>
|
2022-07-26 15:21:51 +03:00
|
|
|
#include <storage/storage.h>
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
typedef struct TarArchive TarArchive;
|
|
|
|
|
|
|
|
|
|
typedef struct Storage Storage;
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Tar archive open mode
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
typedef enum {
|
2024-06-30 13:38:48 +03:00
|
|
|
TarOpenModeRead = 'r',
|
|
|
|
|
TarOpenModeWrite = 'w',
|
|
|
|
|
/* read-only heatshrink compressed tar */
|
|
|
|
|
TarOpenModeReadHeatshrink = 'h',
|
2022-04-13 23:50:25 +03:00
|
|
|
} TarOpenMode;
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Get expected open mode for archive at the path.
|
|
|
|
|
* Used for automatic mode detection based on the file extension.
|
|
|
|
|
*
|
|
|
|
|
* @param[in] path Path to the archive
|
|
|
|
|
*
|
|
|
|
|
* @return open mode from TarOpenMode enum
|
|
|
|
|
*/
|
|
|
|
|
TarOpenMode tar_archive_get_mode_for_path(const char* path);
|
|
|
|
|
|
|
|
|
|
/** Tar archive constructor
|
|
|
|
|
*
|
|
|
|
|
* @param storage Storage API pointer
|
|
|
|
|
*
|
|
|
|
|
* @return allocated object
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
TarArchive* tar_archive_alloc(Storage* storage);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Open tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object
|
|
|
|
|
* @param[in] path Path to the tar archive
|
|
|
|
|
* @param mode Open mode
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Tar archive destructor
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
void tar_archive_free(TarArchive* archive);
|
|
|
|
|
|
|
|
|
|
/* High-level API - assumes archive is open */
|
2024-06-30 13:38:48 +03:00
|
|
|
|
|
|
|
|
/** Unpack tar archive to destination
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in read mode
|
|
|
|
|
* @param[in] destination Destination path
|
|
|
|
|
* @param converter Storage name converter
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-07-26 15:21:51 +03:00
|
|
|
bool tar_archive_unpack_to(
|
|
|
|
|
TarArchive* archive,
|
|
|
|
|
const char* destination,
|
|
|
|
|
Storage_name_converter converter);
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Add file to tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param[in] fs_file_path Path to the file on the filesystem
|
|
|
|
|
* @param[in] archive_fname Name of the file in the archive
|
|
|
|
|
* @param file_size Size of the file
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_add_file(
|
|
|
|
|
TarArchive* archive,
|
|
|
|
|
const char* fs_file_path,
|
|
|
|
|
const char* archive_fname,
|
|
|
|
|
const int32_t file_size);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Add directory to tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param fs_full_path Path to the directory on the filesystem
|
|
|
|
|
* @param path_prefix Prefix to add to the directory name in the archive
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Get number of entries in the archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object
|
|
|
|
|
*
|
|
|
|
|
* @return number of entries. -1 on error
|
|
|
|
|
*/
|
2022-04-19 11:03:28 +03:00
|
|
|
int32_t tar_archive_get_entries_count(TarArchive* archive);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Get read progress
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in read mode
|
|
|
|
|
* @param[in] processed Number of processed entries
|
|
|
|
|
* @param[in] total Total number of entries
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
|
|
|
|
bool tar_archive_get_read_progress(TarArchive* archive, int32_t* processed, int32_t* total);
|
|
|
|
|
|
|
|
|
|
/** Unpack single file from tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in read mode
|
|
|
|
|
* @param[in] archive_fname Name of the file in the archive
|
|
|
|
|
* @param[in] destination Destination path
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-09-28 21:13:12 +04:00
|
|
|
bool tar_archive_unpack_file(
|
|
|
|
|
TarArchive* archive,
|
|
|
|
|
const char* archive_fname,
|
|
|
|
|
const char* destination);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Optional per-entry callback on unpacking
|
|
|
|
|
* @param name Name of the file or directory
|
|
|
|
|
* @param is_directory True if the entry is a directory
|
|
|
|
|
* @param[in] context User context
|
|
|
|
|
* @return true to process the entry, false to skip
|
|
|
|
|
*/
|
2022-04-19 11:03:28 +03:00
|
|
|
typedef bool (*tar_unpack_file_cb)(const char* name, bool is_directory, void* context);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Set per-entry callback on unpacking
|
|
|
|
|
* @param archive Tar archive object
|
|
|
|
|
* @param callback Callback function
|
|
|
|
|
* @param[in] context User context
|
|
|
|
|
*/
|
2022-04-19 11:03:28 +03:00
|
|
|
void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context);
|
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
/* Low-level API */
|
2024-06-30 13:38:48 +03:00
|
|
|
|
|
|
|
|
/** Add tar archive directory header
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param[in] dirpath Path to the directory
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Add tar archive file header
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param[in] path Path to the file
|
|
|
|
|
* @param data_len Size of the file
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Add tar archive file data block
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param[in] data_block Data block
|
|
|
|
|
* @param block_len Size of the data block
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_file_add_data_block(
|
|
|
|
|
TarArchive* archive,
|
|
|
|
|
const uint8_t* data_block,
|
|
|
|
|
const int32_t block_len);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Finalize tar archive file
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_file_finalize(TarArchive* archive);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Store data in tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
* @param[in] path Path to the file
|
|
|
|
|
* @param[in] data Data to store
|
|
|
|
|
* @param data_len Size of the data
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_store_data(
|
|
|
|
|
TarArchive* archive,
|
|
|
|
|
const char* path,
|
|
|
|
|
const uint8_t* data,
|
|
|
|
|
const int32_t data_len);
|
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
/** Finalize tar archive
|
|
|
|
|
*
|
|
|
|
|
* @param archive Tar archive object. Must be opened in write mode
|
|
|
|
|
*
|
|
|
|
|
* @return true if successful
|
|
|
|
|
*/
|
2022-04-13 23:50:25 +03:00
|
|
|
bool tar_archive_finalize(TarArchive* archive);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2022-05-06 16:37:10 +03:00
|
|
|
#endif
|