2021-08-22 15:34:28 -04:00
|
|
|
#include "ssb.export.h"
|
|
|
|
|
2023-03-07 12:50:17 -05:00
|
|
|
#include "log.h"
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
2021-08-22 15:41:27 -04:00
|
|
|
#include "ssb.db.h"
|
2021-08-22 15:34:28 -04:00
|
|
|
#include "ssb.h"
|
|
|
|
|
2023-05-21 17:36:51 -04:00
|
|
|
#include "sqlite3.h"
|
|
|
|
#include "uv.h"
|
|
|
|
|
2021-08-22 15:34:28 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-02-24 11:12:35 -05:00
|
|
|
static void _write_file(const char* path, const void* blob, size_t size, bool force_add_trailing_newline)
|
2021-08-22 15:34:28 -04:00
|
|
|
{
|
|
|
|
FILE* file = fopen(path, "wb");
|
2021-10-10 17:51:38 -04:00
|
|
|
if (file)
|
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
fwrite(blob, 1, size, file);
|
2024-02-24 11:12:35 -05:00
|
|
|
if (force_add_trailing_newline)
|
|
|
|
{
|
|
|
|
fputc('\n', file);
|
|
|
|
}
|
2021-08-22 15:34:28 -04:00
|
|
|
fclose(file);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Failed to open %s for write: %s.\n", path, strerror(errno));
|
2021-08-22 15:34:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 12:48:21 -05:00
|
|
|
static void _make_dir(const char* path)
|
|
|
|
{
|
2022-05-16 18:30:14 -04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if (mkdir(path) && errno != EEXIST)
|
|
|
|
#else
|
2021-12-28 12:48:21 -05:00
|
|
|
if (mkdir(path, 0755) && errno != EEXIST)
|
2022-05-16 18:30:14 -04:00
|
|
|
#endif
|
2021-12-28 12:48:21 -05:00
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Failed to create directory %s: %s.\n", path, strerror(errno));
|
2021-12-28 12:48:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:09:05 -05:00
|
|
|
typedef struct _tf_export_t
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb;
|
|
|
|
const char* parent;
|
|
|
|
JSValue files;
|
|
|
|
uv_fs_t req;
|
|
|
|
bool done;
|
|
|
|
} tf_export_t;
|
|
|
|
|
|
|
|
static void _tf_ssb_export_scandir(uv_fs_t* req)
|
|
|
|
{
|
|
|
|
tf_export_t* export = req->data;
|
|
|
|
JSContext* context = tf_ssb_get_context(export->ssb);
|
|
|
|
uv_dirent_t ent;
|
|
|
|
while (uv_fs_scandir_next(req, &ent) == 0)
|
|
|
|
{
|
|
|
|
if (ent.type == UV_DIRENT_FILE)
|
|
|
|
{
|
|
|
|
JSValue found = JS_GetPropertyStr(context, export->files, ent.name);
|
|
|
|
if (JS_IsUndefined(found))
|
|
|
|
{
|
|
|
|
size_t len = strlen(export->parent) + strlen(ent.name) + 2;
|
|
|
|
char* path = tf_malloc(len);
|
|
|
|
snprintf(path, len, "%s/%s", export->parent, ent.name);
|
|
|
|
uv_fs_t req = { 0 };
|
|
|
|
int r = uv_fs_unlink(tf_ssb_get_loop(export->ssb), &req, path, NULL);
|
|
|
|
if (r)
|
|
|
|
{
|
2024-09-30 12:13:57 -04:00
|
|
|
tf_printf("Failed to unlink %s: %s.\n", path, uv_strerror(r));
|
2023-02-01 21:09:05 -05:00
|
|
|
}
|
|
|
|
uv_fs_req_cleanup(&req);
|
|
|
|
tf_free(path);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export->done = true;
|
|
|
|
}
|
|
|
|
|
2021-08-22 15:34:28 -04:00
|
|
|
void tf_ssb_export(tf_ssb_t* ssb, const char* key)
|
|
|
|
{
|
|
|
|
char user[256] = { 0 };
|
|
|
|
char path[256] = { 0 };
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sscanf(key, "/~%255[^/]/%255s", user, path) != 2)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Unable to export %s.\n", key);
|
2021-08-22 15:34:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-11 21:05:29 -05:00
|
|
|
char app_blob_id[k_blob_id_len] = { 0 };
|
2023-06-14 20:27:49 -04:00
|
|
|
sqlite3* db = tf_ssb_acquire_db_reader(ssb);
|
|
|
|
sqlite3_busy_timeout(db, 10000);
|
2021-08-22 15:34:28 -04:00
|
|
|
sqlite3_stmt* statement;
|
2023-10-23 21:27:35 -04:00
|
|
|
if (sqlite3_prepare(db, "SELECT value FROM properties WHERE id = ?1 AND key = 'path:' || ?2", -1, &statement, NULL) == SQLITE_OK)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2024-02-17 14:22:02 -05:00
|
|
|
if (sqlite3_bind_text(statement, 1, user, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, path, -1, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_ROW)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
int len = sqlite3_column_bytes(statement, 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (len >= (int)sizeof(app_blob_id))
|
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
len = sizeof(app_blob_id) - 1;
|
|
|
|
}
|
|
|
|
memcpy(app_blob_id, sqlite3_column_text(statement, 0), len);
|
|
|
|
app_blob_id[len] = '\0';
|
|
|
|
}
|
|
|
|
sqlite3_finalize(statement);
|
|
|
|
}
|
2023-06-14 20:27:49 -04:00
|
|
|
tf_ssb_release_db_reader(ssb, db);
|
2021-08-22 15:34:28 -04:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!*app_blob_id)
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Did not find app blob ID for %s.\n", key);
|
2021-08-22 15:34:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!tf_ssb_db_blob_get(ssb, app_blob_id, &blob, &size))
|
|
|
|
{
|
2023-03-07 12:50:17 -05:00
|
|
|
tf_printf("Did not find blob for %s: %s.\n", key, app_blob_id);
|
2021-08-22 15:34:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
char file_path[1024];
|
2021-12-28 12:48:21 -05:00
|
|
|
_make_dir("apps/");
|
2023-02-01 21:09:05 -05:00
|
|
|
snprintf(file_path, sizeof(file_path), "apps/%s", path);
|
2021-12-28 12:48:21 -05:00
|
|
|
_make_dir(file_path);
|
2023-02-01 21:09:05 -05:00
|
|
|
snprintf(file_path, sizeof(file_path), "apps/%s.json", path);
|
2021-08-22 15:34:28 -04:00
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue app = JS_ParseJSON(context, (const char*)blob, size, NULL);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(blob);
|
2021-08-22 15:34:28 -04:00
|
|
|
|
|
|
|
JSValue files = JS_GetPropertyStr(context, app, "files");
|
|
|
|
JSPropertyEnum* ptab = NULL;
|
|
|
|
uint32_t plen = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_GetOwnPropertyNames(context, &ptab, &plen, files, JS_GPN_STRING_MASK) == 0)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
JSPropertyDescriptor desc;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_GetOwnProperty(context, &desc, files, ptab[i].atom) == 1)
|
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
JSValue key = JS_AtomToString(context, ptab[i].atom);
|
|
|
|
const char* file_name = JS_ToCString(context, key);
|
|
|
|
const char* blob_id = JS_ToCString(context, desc.value);
|
|
|
|
|
|
|
|
uint8_t* file_blob = NULL;
|
|
|
|
size_t file_size = 0;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (tf_ssb_db_blob_get(ssb, blob_id, &file_blob, &file_size))
|
|
|
|
{
|
2023-02-01 21:09:05 -05:00
|
|
|
snprintf(file_path, sizeof(file_path), "apps/%s/%s", path, file_name);
|
2024-02-24 11:12:35 -05:00
|
|
|
_write_file(file_path, file_blob, file_size, false);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(file_blob);
|
2021-08-22 15:34:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_FreeCString(context, file_name);
|
|
|
|
JS_FreeValue(context, key);
|
|
|
|
JS_FreeCString(context, blob_id);
|
|
|
|
JS_FreeValue(context, desc.value);
|
2022-01-22 13:50:29 -05:00
|
|
|
JS_FreeValue(context, desc.setter);
|
|
|
|
JS_FreeValue(context, desc.getter);
|
2021-08-22 15:34:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2021-08-22 15:34:28 -04:00
|
|
|
JS_FreeAtom(context, ptab[i].atom);
|
|
|
|
}
|
|
|
|
js_free(context, ptab);
|
|
|
|
|
2023-02-01 21:09:05 -05:00
|
|
|
JSAtom files_atom = JS_NewAtom(context, "files");
|
|
|
|
JS_DeleteProperty(context, app, files_atom, 0);
|
|
|
|
JS_FreeAtom(context, files_atom);
|
|
|
|
|
2024-02-24 10:55:09 -05:00
|
|
|
JSValue tab = JS_NewString(context, "\t");
|
|
|
|
JSValue json = JS_JSONStringify(context, app, JS_NULL, tab);
|
|
|
|
JS_FreeValue(context, tab);
|
2023-02-01 21:09:05 -05:00
|
|
|
size_t length = 0;
|
|
|
|
const char* string = JS_ToCStringLen(context, &length, json);
|
|
|
|
snprintf(file_path, sizeof(file_path), "apps/%s.json", path);
|
2024-02-24 11:12:35 -05:00
|
|
|
_write_file(file_path, string, length, true);
|
2023-02-01 21:09:05 -05:00
|
|
|
JS_FreeCString(context, string);
|
|
|
|
JS_FreeValue(context, json);
|
|
|
|
|
2023-03-21 21:21:22 -04:00
|
|
|
snprintf(file_path, sizeof(file_path), "apps/%s", path);
|
2023-02-01 21:09:05 -05:00
|
|
|
tf_export_t export =
|
|
|
|
{
|
|
|
|
.parent = file_path,
|
|
|
|
.ssb = ssb,
|
|
|
|
.files = files,
|
|
|
|
.req =
|
|
|
|
{
|
|
|
|
.data = &export,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
int r = uv_fs_scandir(tf_ssb_get_loop(ssb), &export.req, file_path, 0, _tf_ssb_export_scandir);
|
|
|
|
if (r)
|
|
|
|
{
|
2024-09-30 12:13:57 -04:00
|
|
|
tf_printf("Failed to scan directory %s: %s.\n", file_path, uv_strerror(r));
|
2023-02-01 21:09:05 -05:00
|
|
|
}
|
|
|
|
while (!export.done)
|
|
|
|
{
|
|
|
|
uv_run(tf_ssb_get_loop(ssb), UV_RUN_ONCE);
|
|
|
|
}
|
|
|
|
uv_fs_req_cleanup(&export.req);
|
|
|
|
|
2021-08-22 15:34:28 -04:00
|
|
|
JS_FreeValue(context, files);
|
|
|
|
JS_FreeValue(context, app);
|
|
|
|
}
|