about pikafs,support pack given files and unpack a *.pack file to the given path

This commit is contained in:
shanjiayang 2023-03-07 23:28:54 +08:00
parent 3f1ebc12eb
commit f62d7b0261
3 changed files with 115 additions and 1 deletions

View File

@ -11,7 +11,8 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=vm.dir_print_arg"
// "--gtest_filter=vm.dir_print_arg"
"--gtest_filter=packtool.unpack"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -493,6 +493,39 @@ PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
return PIKA_RES_ERR_ARG_NO_FOUND;
}
/**
* @brief .pack pack library_bytes
*
* @param pikafs_FILE** fp pikafs_FILE
* @param Arg** f_arg
* @param char* pack_name pack
* @return PIKA_RES_OK when success, otherwise failed;
* @note if failed *fp if freed
*
*/
PIKA_RES _getPack_libraryBytes(pikafs_FILE** fp, Arg** f_arg, char* pack_name) {
if (NULL == pack_name) {
return PIKA_RES_ERR_INVALID_PTR;
}
*fp = (pikafs_FILE*)pikaMalloc(sizeof(pikafs_FILE));
if (NULL == *fp) {
pika_platform_printf("Error: malloc failed \r\n");
return PIKA_RES_ERR_OUT_OF_RANGE;
}
memset(*fp, 0, sizeof(pikafs_FILE));
*f_arg = arg_loadFile(NULL, pack_name);
if (NULL == *f_arg) {
pika_platform_printf("Error: Could not load file \'%s\'\r\n", pack_name);
pikaFree(*fp, sizeof(pikafs_FILE));
// fp == NULL;
return PIKA_RES_ERR_IO_ERROR;
}
return PIKA_RES_OK;
}
int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes) {
int module_num = _getModuleNum(library_bytes);
if (module_num < 0) {
@ -543,6 +576,62 @@ int LibObj_loadLibraryFile(LibObj* self, char* lib_file_name) {
return PIKA_RES_OK;
}
/**
* @brief unpack *.pack file to Specified path
*
* @param pack_name the name of *.pack file
* @param out_path output path
* @return
*/
PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path) {
PIKA_RES stat = PIKA_RES_OK;
Arg* file_arg = NULL;
uint8_t* library_bytes = NULL;
pikafs_FILE* fptr = NULL;
stat = _getPack_libraryBytes(&fptr, &file_arg, pack_name);
if (PIKA_RES_OK == stat) {
library_bytes = arg_getBytes(file_arg);
}
else {
return stat;
}
int module_num = _getModuleNum(library_bytes);
if (module_num < 0) {
return (PIKA_RES)module_num;
}
Args buffs = { 0 };
char* output_file_path = NULL;
FILE* new_fp = NULL;
for (int i = 0; i < module_num; ++i) {
char* name = NULL;
uint8_t* addr = NULL;
size_t size = 0;
_loadModuleDataWithIndex(library_bytes, module_num, i, &name, &addr,
&size);
output_file_path = strsPathJoin(&buffs, out_path, name);
new_fp = pika_platform_fopen(output_file_path, "wb+");
if (NULL != new_fp) {
pika_platform_fwrite(addr, size, 1, new_fp);
pika_platform_fclose(new_fp);
pika_platform_printf("extract %s to %s\r\n", name, output_file_path);
}
else {
pika_platform_printf("can't open %s\r\n", output_file_path);
break;
}
}
arg_deinit(file_arg);
strsDeinit(&buffs);
pikaFree(fptr, sizeof(pikafs_FILE));
return PIKA_RES_OK;
}
size_t pika_fputs(char* str, FILE* fp) {
size_t size = strGetSize(str);
return pika_platform_fwrite(str, 1, size, fp);
@ -960,6 +1049,28 @@ pikafs_FILE* pikafs_fopen(char* file_name, char* mode) {
return f;
}
pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name) {
pikafs_FILE* f = NULL;
Arg* file_arg = NULL;
PIKA_RES stat = PIKA_RES_OK;
uint8_t* library_bytes = NULL;
stat = _getPack_libraryBytes(&f, &file_arg, pack_name);
if (PIKA_RES_OK == stat) {
library_bytes = arg_getBytes(file_arg);
}
else {
return NULL;
}
if (PIKA_RES_OK !=
_loadModuleDataWithName(library_bytes, file_name, &f->addr, &f->size)) {
return NULL;
}
arg_deinit(file_arg);
return f;
}
/*
* @brief read file
* @param buf the buffer to read

View File

@ -19,6 +19,7 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name);
void LibObj_listModules(LibObj* self);
int LibObj_saveLibraryFile(LibObj* self, char* output_file_name);
int LibObj_loadLibraryFile(LibObj* self, char* input_file_name);
PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path);
int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root);
PikaMaker* New_PikaMaker(void);
void pikaMaker_setPWD(PikaMaker* self, char* pwd);
@ -55,6 +56,7 @@ typedef struct {
} pikafs_FILE;
pikafs_FILE* pikafs_fopen(char* file_name, char* mode);
pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name);
int pikafs_fread(void* buf, size_t size, size_t count, pikafs_FILE* file);
int pikafs_fwrite(void* buf, size_t size, size_t count, pikafs_FILE* file);
int pikafs_fclose(pikafs_FILE* file);