tar_archive.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. }
  34. return MTAR_ESUCCESS;
  35. }
  36. const struct mtar_ops filesystem_ops = {
  37. .read = mtar_storage_file_read,
  38. .write = mtar_storage_file_write,
  39. .seek = mtar_storage_file_seek,
  40. .close = mtar_storage_file_close,
  41. };
  42. TarArchive* tar_archive_alloc(Storage* storage) {
  43. furi_check(storage);
  44. TarArchive* archive = malloc(sizeof(TarArchive));
  45. archive->storage = storage;
  46. archive->unpack_cb = NULL;
  47. return archive;
  48. }
  49. bool tar_archive_open(TarArchive* archive, const char* path, TarOpenMode mode) {
  50. furi_assert(archive);
  51. FS_AccessMode access_mode;
  52. FS_OpenMode open_mode;
  53. int mtar_access = 0;
  54. switch(mode) {
  55. case TAR_OPEN_MODE_READ:
  56. mtar_access = MTAR_READ;
  57. access_mode = FSAM_READ;
  58. open_mode = FSOM_OPEN_EXISTING;
  59. break;
  60. case TAR_OPEN_MODE_WRITE:
  61. mtar_access = MTAR_WRITE;
  62. access_mode = FSAM_WRITE;
  63. open_mode = FSOM_CREATE_ALWAYS;
  64. break;
  65. default:
  66. return false;
  67. }
  68. File* stream = storage_file_alloc(archive->storage);
  69. if(!storage_file_open(stream, path, access_mode, open_mode)) {
  70. storage_file_free(stream);
  71. return false;
  72. }
  73. mtar_init(&archive->tar, mtar_access, &filesystem_ops, stream);
  74. return true;
  75. }
  76. void tar_archive_free(TarArchive* archive) {
  77. furi_assert(archive);
  78. if(mtar_is_open(&archive->tar)) {
  79. mtar_close(&archive->tar);
  80. }
  81. }
  82. void tar_archive_set_file_callback(TarArchive* archive, tar_unpack_file_cb callback, void* context) {
  83. furi_assert(archive);
  84. archive->unpack_cb = callback;
  85. archive->unpack_cb_context = context;
  86. }
  87. static int tar_archive_entry_counter(mtar_t* tar, const mtar_header_t* header, void* param) {
  88. UNUSED(tar);
  89. UNUSED(header);
  90. int32_t* counter = param;
  91. (*counter)++;
  92. return 0;
  93. }
  94. int32_t tar_archive_get_entries_count(TarArchive* archive) {
  95. int32_t counter = 0;
  96. if(mtar_foreach(&archive->tar, tar_archive_entry_counter, &counter) != MTAR_ESUCCESS) {
  97. counter = -1;
  98. }
  99. return counter;
  100. }
  101. bool tar_archive_dir_add_element(TarArchive* archive, const char* dirpath) {
  102. furi_assert(archive);
  103. return (mtar_write_dir_header(&archive->tar, dirpath) == MTAR_ESUCCESS);
  104. }
  105. bool tar_archive_finalize(TarArchive* archive) {
  106. furi_assert(archive);
  107. return (mtar_finalize(&archive->tar) == MTAR_ESUCCESS);
  108. }
  109. bool tar_archive_store_data(
  110. TarArchive* archive,
  111. const char* path,
  112. const uint8_t* data,
  113. const int32_t data_len) {
  114. furi_assert(archive);
  115. return (
  116. tar_archive_file_add_header(archive, path, data_len) &&
  117. tar_archive_file_add_data_block(archive, data, data_len) &&
  118. tar_archive_file_finalize(archive));
  119. }
  120. bool tar_archive_file_add_header(TarArchive* archive, const char* path, const int32_t data_len) {
  121. furi_assert(archive);
  122. return (mtar_write_file_header(&archive->tar, path, data_len) == MTAR_ESUCCESS);
  123. }
  124. bool tar_archive_file_add_data_block(
  125. TarArchive* archive,
  126. const uint8_t* data_block,
  127. const int32_t block_len) {
  128. furi_assert(archive);
  129. return (mtar_write_data(&archive->tar, data_block, block_len) == block_len);
  130. }
  131. bool tar_archive_file_finalize(TarArchive* archive) {
  132. furi_assert(archive);
  133. return (mtar_end_data(&archive->tar) == MTAR_ESUCCESS);
  134. }
  135. typedef struct {
  136. TarArchive* archive;
  137. const char* work_dir;
  138. } TarArchiveDirectoryOpParams;
  139. static int archive_extract_foreach_cb(mtar_t* tar, const mtar_header_t* header, void* param) {
  140. TarArchiveDirectoryOpParams* op_params = param;
  141. TarArchive* archive = op_params->archive;
  142. string_t fname;
  143. bool skip_entry = false;
  144. if(archive->unpack_cb) {
  145. skip_entry = !archive->unpack_cb(
  146. header->name, header->type == MTAR_TDIR, archive->unpack_cb_context);
  147. }
  148. if(skip_entry) {
  149. FURI_LOG_W(TAG, "filter: skipping entry \"%s\"", header->name);
  150. return 0;
  151. }
  152. if(header->type == MTAR_TDIR) {
  153. string_init(fname);
  154. path_concat(op_params->work_dir, header->name, fname);
  155. bool create_res = storage_simply_mkdir(archive->storage, string_get_cstr(fname));
  156. string_clear(fname);
  157. return create_res ? 0 : -1;
  158. }
  159. if(header->type != MTAR_TREG) {
  160. FURI_LOG_W(TAG, "not extracting unsupported type \"%s\"", header->name);
  161. return 0;
  162. }
  163. string_init(fname);
  164. path_concat(op_params->work_dir, header->name, fname);
  165. FURI_LOG_I(TAG, "Extracting %d bytes to '%s'", header->size, header->name);
  166. File* out_file = storage_file_alloc(archive->storage);
  167. uint8_t* readbuf = malloc(FILE_BLOCK_SIZE);
  168. bool failed = false;
  169. uint8_t n_tries = FILE_OPEN_NTRIES;
  170. do {
  171. while(
  172. (n_tries-- > 0) &&
  173. !storage_file_open(out_file, string_get_cstr(fname), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  174. FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", string_get_cstr(fname), n_tries);
  175. osDelay(FILE_OPEN_RETRY_DELAY);
  176. continue;
  177. }
  178. if(!storage_file_is_open(out_file)) {
  179. failed = true;
  180. break;
  181. }
  182. while(!mtar_eof_data(tar)) {
  183. int32_t readcnt = mtar_read_data(tar, readbuf, FILE_BLOCK_SIZE);
  184. if(!readcnt || !storage_file_write(out_file, readbuf, readcnt)) {
  185. failed = true;
  186. break;
  187. }
  188. }
  189. } while(false);
  190. storage_file_free(out_file);
  191. free(readbuf);
  192. string_clear(fname);
  193. return failed ? -1 : 0;
  194. }
  195. bool tar_archive_unpack_to(TarArchive* archive, const char* destination) {
  196. furi_assert(archive);
  197. TarArchiveDirectoryOpParams param = {
  198. .archive = archive,
  199. .work_dir = destination,
  200. };
  201. FURI_LOG_I(TAG, "Restoring '%s'", destination);
  202. return (mtar_foreach(&archive->tar, archive_extract_foreach_cb, &param) == MTAR_ESUCCESS);
  203. };
  204. bool tar_archive_add_file(
  205. TarArchive* archive,
  206. const char* fs_file_path,
  207. const char* archive_fname,
  208. const int32_t file_size) {
  209. furi_assert(archive);
  210. uint8_t* file_buffer = malloc(FILE_BLOCK_SIZE);
  211. bool success = false;
  212. File* src_file = storage_file_alloc(archive->storage);
  213. uint8_t n_tries = FILE_OPEN_NTRIES;
  214. do {
  215. while((n_tries-- > 0) &&
  216. !storage_file_open(src_file, fs_file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  217. FURI_LOG_W(TAG, "Failed to open '%s', reties: %d", fs_file_path, n_tries);
  218. osDelay(FILE_OPEN_RETRY_DELAY);
  219. continue;
  220. }
  221. if(!storage_file_is_open(src_file) ||
  222. !tar_archive_file_add_header(archive, archive_fname, file_size)) {
  223. break;
  224. }
  225. uint16_t bytes_read = 0;
  226. while((bytes_read = storage_file_read(src_file, file_buffer, FILE_BLOCK_SIZE))) {
  227. success = tar_archive_file_add_data_block(archive, file_buffer, bytes_read);
  228. if(!success) {
  229. break;
  230. }
  231. }
  232. success = success && tar_archive_file_finalize(archive);
  233. } while(false);
  234. storage_file_free(src_file);
  235. free(file_buffer);
  236. return success;
  237. }
  238. bool tar_archive_add_dir(TarArchive* archive, const char* fs_full_path, const char* path_prefix) {
  239. furi_assert(archive);
  240. furi_check(path_prefix);
  241. File* directory = storage_file_alloc(archive->storage);
  242. FileInfo file_info;
  243. FURI_LOG_I(TAG, "Backing up '%s', '%s'", fs_full_path, path_prefix);
  244. char* name = malloc(MAX_NAME_LEN);
  245. bool success = false;
  246. do {
  247. if(!storage_dir_open(directory, fs_full_path)) {
  248. break;
  249. }
  250. while(true) {
  251. if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
  252. success = true; /* empty dir / no more files */
  253. break;
  254. }
  255. string_t element_name, element_fs_abs_path;
  256. string_init(element_name);
  257. string_init(element_fs_abs_path);
  258. path_concat(fs_full_path, name, element_fs_abs_path);
  259. if(strlen(path_prefix)) {
  260. path_concat(path_prefix, name, element_name);
  261. } else {
  262. string_init_set(element_name, name);
  263. }
  264. if(file_info.flags & FSF_DIRECTORY) {
  265. success = tar_archive_dir_add_element(archive, string_get_cstr(element_name)) &&
  266. tar_archive_add_dir(
  267. archive,
  268. string_get_cstr(element_fs_abs_path),
  269. string_get_cstr(element_name));
  270. } else {
  271. success = tar_archive_add_file(
  272. archive,
  273. string_get_cstr(element_fs_abs_path),
  274. string_get_cstr(element_name),
  275. file_info.size);
  276. }
  277. string_clear(element_name);
  278. string_clear(element_fs_abs_path);
  279. if(!success) {
  280. break;
  281. }
  282. }
  283. } while(false);
  284. free(name);
  285. storage_file_free(directory);
  286. return success;
  287. }