tar_archive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "tar_archive.h"
  2. #include <microtar.h>
  3. #include <storage/storage.h>
  4. #include <furi.h>
  5. #include <toolbox/path.h>
  6. #define TAG "TarArch"
  7. #define MAX_NAME_LEN 255
  8. #define FILE_BLOCK_SIZE 512
  9. #define FILE_OPEN_NTRIES 10
  10. #define FILE_OPEN_RETRY_DELAY 25
  11. typedef struct TarArchive {
  12. Storage* storage;
  13. mtar_t tar;
  14. tar_unpack_file_cb unpack_cb;
  15. void* unpack_cb_context;
  16. } TarArchive;
  17. /* API WRAPPER */
  18. static int mtar_storage_file_write(void* stream, const void* data, unsigned size) {
  19. uint16_t bytes_written = storage_file_write(stream, data, size);
  20. return (bytes_written == size) ? bytes_written : MTAR_EWRITEFAIL;
  21. }
  22. static int mtar_storage_file_read(void* stream, void* data, unsigned size) {
  23. uint16_t bytes_read = storage_file_read(stream, data, size);
  24. return (bytes_read == size) ? bytes_read : MTAR_EREADFAIL;
  25. }
  26. static int mtar_storage_file_seek(void* stream, unsigned offset) {
  27. bool res = storage_file_seek(stream, offset, true);
  28. return res ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
  29. }
  30. static int mtar_storage_file_close(void* stream) {
  31. if(stream) {
  32. storage_file_close(stream);
  33. storage_file_free(stream);
  34. }
  35. return MTAR_ESUCCESS;
  36. }
  37. const struct mtar_ops filesystem_ops = {
  38. .read = mtar_storage_file_read,
  39. .write = mtar_storage_file_write,
  40. .seek = mtar_storage_file_seek,
  41. .close = mtar_storage_file_close,
  42. };
  43. TarArchive* tar_archive_alloc(Storage* storage) {
  44. furi_check(storage);
  45. TarArchive* archive = malloc(sizeof(TarArchive));
  46. archive->storage = storage;
  47. archive->unpack_cb = NULL;
  48. return archive;
  49. }
  50. bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode) {
  51. furi_assert(archive);
  52. FS_AccessMode access_mode;
  53. FS_OpenMode open_mode;
  54. int mtar_access = 0;
  55. switch(mode) {
  56. case TAR_OPEN_MODE_READ:
  57. mtar_access = MTAR_READ;
  58. access_mode = FSAM_READ;
  59. open_mode = FSOM_OPEN_EXISTING;
  60. break;
  61. case TAR_OPEN_MODE_WRITE:
  62. mtar_access = MTAR_WRITE;
  63. access_mode = FSAM_WRITE;
  64. open_mode = FSOM_CREATE_ALWAYS;
  65. break;
  66. default:
  67. return false;
  68. }
  69. File* stream = storage_file_alloc(archive->storage);
  70. if(!storage_file_open(stream, path, access_mode, open_mode)) {
  71. storage_file_free(stream);
  72. return false;
  73. }
  74. mtar_init(&archive->tar, mtar_access, &filesystem_ops, stream);
  75. return true;
  76. }
  77. void tar_archive_free(TarArchive* archive) {
  78. furi_assert(archive);
  79. if(mtar_is_open(&archive->tar)) {
  80. mtar_close(&archive->tar);
  81. }
  82. free(archive);
  83. }
  84. void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context) {
  85. furi_assert(archive);
  86. archive->unpack_cb = callback;
  87. archive->unpack_cb_context = context;
  88. }
  89. static int tar_archive_entry_counter(mtar_t* tar, const mtar_header_t* header, void* param) {
  90. UNUSED(tar);
  91. UNUSED(header);
  92. int32_t* counter = param;
  93. (*counter)++;
  94. return 0;
  95. }
  96. int32_t tar_archive_get_entries_count(TarArchive* archive) {
  97. int32_t counter = 0;
  98. if(mtar_foreach(&archive->tar, tar_archive_entry_counter, &counter) != MTAR_ESUCCESS) {
  99. counter = -1;
  100. }
  101. return counter;
  102. }
  103. bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath) {
  104. furi_assert(archive);
  105. return (mtar_write_dir_header(&archive->tar, dirpath) == MTAR_ESUCCESS);
  106. }
  107. bool tar_archive_finalize(TarArchive* archive) {
  108. furi_assert(archive);
  109. return (mtar_finalize(&archive->tar) == MTAR_ESUCCESS);
  110. }
  111. bool tar_archive_store_data(
  112. TarArchive* archive,
  113. const char* path,
  114. const uint8_t* data,
  115. const int32_t data_len) {
  116. furi_assert(archive);
  117. return (
  118. tar_archive_file_add_header(archive, path, data_len) &&
  119. tar_archive_file_add_data_block(archive, data, data_len) &&
  120. tar_archive_file_finalize(archive));
  121. }
  122. bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len) {
  123. furi_assert(archive);
  124. return (mtar_write_file_header(&archive->tar, path, data_len) == MTAR_ESUCCESS);
  125. }
  126. bool tar_archive_file_add_data_block(
  127. TarArchive* archive,
  128. const uint8_t* data_block,
  129. const int32_t block_len) {
  130. furi_assert(archive);
  131. return (mtar_write_data(&archive->tar, data_block, block_len) == block_len);
  132. }
  133. bool tar_archive_file_finalize(TarArchive* archive) {
  134. furi_assert(archive);
  135. return (mtar_end_data(&archive->tar) == MTAR_ESUCCESS);
  136. }
  137. typedef struct {
  138. TarArchive* archive;
  139. const char* work_dir;
  140. Storage_name_converter converter;
  141. } TarArchiveDirectoryOpParams;
  142. static bool archive_extract_current_file(TarArchive* archive, const char* dst_path) {
  143. mtar_t* tar = &archive->tar;
  144. File* out_file = storage_file_alloc(archive->storage);
  145. uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
  146. bool success = true;
  147. uint8_t n_tries = FILE_OPEN_NTRIES;
  148. do {
  149. while(n_tries-- > 0) {
  150. if(storage_file_open(out_file, dst_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  151. break;
  152. }
  153. FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", dst_path, n_tries);
  154. storage_file_close(out_file);
  155. furi_delay_ms(FILE_OPEN_RETRY_DELAY);
  156. }
  157. if(!storage_file_is_open(out_file)) {
  158. success = false;
  159. break;
  160. }
  161. while(!mtar_eof_data(tar)) {
  162. int32_t readcnt = mtar_read_data(tar, readbuf, FILE_BLOCK_SIZE);
  163. if(!readcnt || !storage_file_write(out_file, readbuf, readcnt)) {
  164. success = false;
  165. break;
  166. }
  167. }
  168. } while(false);
  169. storage_file_free(out_file);
  170. free(readbuf);
  171. return success;
  172. }
  173. static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
  174. UNUSED(tar);
  175. TarArchiveDirectoryOpParams* op_params = param;
  176. TarArchive* archive = op_params->archive;
  177. bool skip_entry = false;
  178. if(archive->unpack_cb) {
  179. skip_entry = !archive->unpack_cb(
  180. header->name, header->type == MTAR_TDIR, archive->unpack_cb_context);
  181. }
  182. if(skip_entry) {
  183. FURI_LOG_W(TAG, "filter: skipping entry \"%s\"", header->name);
  184. return 0;
  185. }
  186. FuriString* full_extracted_fname;
  187. if(header->type == MTAR_TDIR) {
  188. full_extracted_fname = furi_string_alloc();
  189. path_concat(op_params->work_dir, header->name, full_extracted_fname);
  190. bool create_res =
  191. storage_simply_mkdir(archive->storage, furi_string_get_cstr(full_extracted_fname));
  192. furi_string_free(full_extracted_fname);
  193. return create_res ? 0 : -1;
  194. }
  195. if(header->type != MTAR_TREG) {
  196. FURI_LOG_W(TAG, "not extracting unsupported type \"%s\"", header->name);
  197. return 0;
  198. }
  199. FURI_LOG_D(TAG, "Extracting %d bytes to '%s'", header->size, header->name);
  200. FuriString* converted_fname = furi_string_alloc_set(header->name);
  201. if(op_params->converter) {
  202. op_params->converter(converted_fname);
  203. }
  204. full_extracted_fname = furi_string_alloc();
  205. path_concat(op_params->work_dir, furi_string_get_cstr(converted_fname), full_extracted_fname);
  206. bool success =
  207. archive_extract_current_file(archive, furi_string_get_cstr(full_extracted_fname));
  208. furi_string_free(converted_fname);
  209. furi_string_free(full_extracted_fname);
  210. return success ? 0 : -1;
  211. }
  212. bool tar_archive_unpack_to(
  213. TarArchive* archive,
  214. const char* destination,
  215. Storage_name_converter converter) {
  216. furi_assert(archive);
  217. TarArchiveDirectoryOpParams param = {
  218. .archive = archive,
  219. .work_dir = destination,
  220. .converter = converter,
  221. };
  222. FURI_LOG_I(TAG, "Restoring '%s'", destination);
  223. return (mtar_foreach(&archive->tar, archive_extract_foreach_cb, &param) == MTAR_ESUCCESS);
  224. };
  225. bool tar_archive_add_file(
  226. TarArchive* archive,
  227. const char* fs_file_path,
  228. const char* archive_fname,
  229. const int32_t file_size) {
  230. furi_assert(archive);
  231. uint8_t* file_buffer = malloc(FILE_BLOCK_SIZE);
  232. bool success = false;
  233. File* src_file = storage_file_alloc(archive->storage);
  234. uint8_t n_tries = FILE_OPEN_NTRIES;
  235. do {
  236. while(n_tries-- > 0) {
  237. if(storage_file_open(src_file, fs_file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  238. break;
  239. }
  240. FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", fs_file_path, n_tries);
  241. storage_file_close(src_file);
  242. furi_delay_ms(FILE_OPEN_RETRY_DELAY);
  243. }
  244. if(!storage_file_is_open(src_file) ||
  245. !tar_archive_file_add_header(archive, archive_fname, file_size)) {
  246. break;
  247. }
  248. success = true; // if file is empty, that's not an error
  249. uint16_t bytes_read = 0;
  250. while((bytes_read = storage_file_read(src_file, file_buffer, FILE_BLOCK_SIZE))) {
  251. success = tar_archive_file_add_data_block(archive, file_buffer, bytes_read);
  252. if(!success) {
  253. break;
  254. }
  255. }
  256. success = success && tar_archive_file_finalize(archive);
  257. } while(false);
  258. storage_file_free(src_file);
  259. free(file_buffer);
  260. return success;
  261. }
  262. bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix) {
  263. furi_assert(archive);
  264. furi_check(path_prefix);
  265. File* directory = storage_file_alloc(archive->storage);
  266. FileInfo file_info;
  267. FURI_LOG_I(TAG, "Backing up '%s', '%s'", fs_full_path, path_prefix);
  268. char* name = malloc(MAX_NAME_LEN);
  269. bool success = false;
  270. do {
  271. if(!storage_dir_open(directory, fs_full_path)) {
  272. break;
  273. }
  274. while(true) {
  275. if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
  276. success = true; /* empty dir / no more files */
  277. break;
  278. }
  279. FuriString* element_name = furi_string_alloc();
  280. FuriString* element_fs_abs_path = furi_string_alloc();
  281. path_concat(fs_full_path, name, element_fs_abs_path);
  282. if(strlen(path_prefix)) {
  283. path_concat(path_prefix, name, element_name);
  284. } else {
  285. furi_string_set(element_name, name);
  286. }
  287. if(file_info.flags & FSF_DIRECTORY) {
  288. success =
  289. tar_archive_dir_add_element(archive, furi_string_get_cstr(element_name)) &&
  290. tar_archive_add_dir(
  291. archive,
  292. furi_string_get_cstr(element_fs_abs_path),
  293. furi_string_get_cstr(element_name));
  294. } else {
  295. success = tar_archive_add_file(
  296. archive,
  297. furi_string_get_cstr(element_fs_abs_path),
  298. furi_string_get_cstr(element_name),
  299. file_info.size);
  300. }
  301. furi_string_free(element_name);
  302. furi_string_free(element_fs_abs_path);
  303. if(!success) {
  304. break;
  305. }
  306. }
  307. } while(false);
  308. free(name);
  309. storage_file_free(directory);
  310. return success;
  311. }
  312. bool tar_archive_unpack_file(
  313. TarArchive* archive,
  314. const char* archive_fname,
  315. const char* destination) {
  316. furi_assert(archive);
  317. furi_assert(archive_fname);
  318. furi_assert(destination);
  319. if(mtar_find(&archive->tar, archive_fname) != MTAR_ESUCCESS) {
  320. return false;
  321. }
  322. return archive_extract_current_file(archive, destination);
  323. }