Now we can run scripts from a .zip.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4206 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-03-08 23:59:11 +00:00
parent 50e48af7c4
commit 63dcab30c3
4 changed files with 78 additions and 14 deletions

View File

@ -24,6 +24,8 @@
#include <sqlite3.h>
#include "unzip.h"
#include "quickjs.h"
#include "quickjs-libc.h"
@ -123,6 +125,8 @@ typedef struct _tf_task_t
int _http_port;
int _https_port;
char _db_path[256];
char _zip_path[256];
unzFile _zip;
const char* _args;
promise_stack_t* _promise_stacks;
@ -305,19 +309,44 @@ bool tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
return false;
}
static const char* _task_loadFile(const char* fileName)
static const char* _task_loadFile(tf_task_t* task, const char* fileName)
{
char* result = NULL;
FILE* file = fopen(fileName, "rb");
if (file)
if (task->_zip)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
result = tf_malloc(fileSize + 1);
int bytes_read = fread(result, 1, fileSize, file);
result[bytes_read] = '\0';
fclose(file);
if (unzLocateFile(task->_zip, fileName, 1) == UNZ_OK)
{
unz_file_info64 info = { 0 };
if (unzGetCurrentFileInfo64(task->_zip, &info, NULL, 0, NULL, 0, NULL, 0) == UNZ_OK)
{
char* buffer = tf_malloc(info.uncompressed_size + 1);
if (unzOpenCurrentFile(task->_zip) == UNZ_OK)
{
if (unzReadCurrentFile(task->_zip, buffer, info.uncompressed_size) == (int)info.uncompressed_size)
{
buffer[info.uncompressed_size] = '\0';
result = buffer;
buffer = NULL;
}
unzCloseCurrentFile(task->_zip);
}
tf_free(buffer);
}
}
}
else
{
FILE* file = fopen(fileName, "rb");
if (file)
{
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
result = tf_malloc(fileSize + 1);
int bytes_read = fread(result, 1, fileSize, file);
result[bytes_read] = '\0';
fclose(file);
}
}
return result;
}
@ -338,7 +367,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName)
bool executed = false;
tf_trace_begin(task->_trace, "tf_task_execute");
const char* source = _task_loadFile(fileName);
const char* source = _task_loadFile(task, fileName);
tf_printf("Running script %s\n", fileName);
if (!*task->_scriptName)
{
@ -1471,7 +1500,7 @@ JSModuleDef* _tf_task_module_loader(JSContext* context, const char* module_name,
if (!source && task->_trusted)
{
source = (char*)_task_loadFile(module_name);
source = (char*)_task_loadFile(task, module_name);
length = source ? strlen(source) : 0;
}
@ -1838,6 +1867,21 @@ void tf_task_set_db_path(tf_task_t* task, const char* db_path)
snprintf(task->_db_path, sizeof(task->_db_path), "%s", db_path);
}
void tf_task_set_zip_path(tf_task_t* task, const char* zip_path)
{
if (task->_zip)
{
unzClose(task->_zip);
task->_zip = NULL;
}
snprintf(task->_zip_path, sizeof(task->_zip_path), "%s", zip_path);
if (zip_path)
{
task->_zip = unzOpen(zip_path);
tf_printf("Zip %s: %p\n", zip_path, task->_zip);
}
}
void tf_task_set_args(tf_task_t* task, const char* args)
{
task->_args = args;