From 4edee0f7f61fc6812c0c241024deaa0f330f2fac Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 30 Apr 2024 21:43:14 -0400 Subject: [PATCH] Allow importing from a single app .json. --- src/ssb.import.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/ssb.import.c b/src/ssb.import.c index f6a9dd17..8be109fd 100644 --- a/src/ssb.import.c +++ b/src/ssb.import.c @@ -231,28 +231,35 @@ static void _tf_ssb_import_app_json(tf_ssb_t* ssb, uv_loop_t* loop, JSContext* c void tf_ssb_import(tf_ssb_t* ssb, const char* user, const char* path) { - uv_fs_t req = { 0 }; - int r = uv_fs_scandir(tf_ssb_get_loop(ssb), &req, path, 0, NULL); - if (r >= 0) + if (strlen(path) > strlen(".json") && strcasecmp(path + strlen(path) - strlen(".json"), ".json") == 0) { - uv_dirent_t ent; - while (uv_fs_scandir_next(&req, &ent) == 0) - { - size_t len = strlen(path) + strlen(ent.name) + 2; - char* full_path = tf_malloc(len); - snprintf(full_path, len, "%s/%s", path, ent.name); - if (strlen(ent.name) > strlen(".json") && strcasecmp(ent.name + strlen(ent.name) - strlen(".json"), ".json") == 0) - { - _tf_ssb_import_app_json(ssb, tf_ssb_get_loop(ssb), tf_ssb_get_context(ssb), user, full_path); - } - tf_free(full_path); - } + _tf_ssb_import_app_json(ssb, tf_ssb_get_loop(ssb), tf_ssb_get_context(ssb), user, path); } else { - tf_printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); + uv_fs_t req = { 0 }; + int r = uv_fs_scandir(tf_ssb_get_loop(ssb), &req, path, 0, NULL); + if (r >= 0) + { + uv_dirent_t ent; + while (uv_fs_scandir_next(&req, &ent) == 0) + { + size_t len = strlen(path) + strlen(ent.name) + 2; + char* full_path = tf_malloc(len); + snprintf(full_path, len, "%s/%s", path, ent.name); + if (strlen(ent.name) > strlen(".json") && strcasecmp(ent.name + strlen(ent.name) - strlen(".json"), ".json") == 0) + { + _tf_ssb_import_app_json(ssb, tf_ssb_get_loop(ssb), tf_ssb_get_context(ssb), user, full_path); + } + tf_free(full_path); + } + } + else + { + tf_printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); + } + uv_fs_req_cleanup(&req); } - uv_fs_req_cleanup(&req); } static char* _tf_ssb_import_read_current_file_from_zip(unzFile zip, size_t* size)