Browse Source

fix file size align in pya (v7)

lyon1998 2 years ago
parent
commit
429adcef77

+ 1 - 1
port/linux/.vscode/launch.json

@@ -18,7 +18,7 @@
                 // "--gtest_filter=vm.run_file_subsrc"
                 // "--gtest_filter=vm.run_file"
                 // "--gtest_filter=stddata.encode_decode"
-                "--gtest_filter=packtool.packread"
+                "--gtest_filter=packtool.packfiles_txt"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 2 - 0
port/linux/package/pikascript/pikascript-lib/PikaCV/PikaCV_Image.c

@@ -125,6 +125,8 @@ void PikaCV_Image_read(PikaObj* self, char* path) {
     }
     Arg* data_arg = arg_loadFile(NULL, path);
     if (NULL == data_arg) {
+        pika_platform_printf("PikaCV_Image_read: failed to load file: %s\n",
+                             path);
         return;
     }
     Args buffs = {0};

+ 31 - 0
port/linux/test/packtool-test.cpp

@@ -24,6 +24,37 @@ TEST(packtool, packfiles) {
     EXPECT_EQ(ret, PIKA_RES_OK);
 }
 
+TEST(packtool, packfiles_txt) {
+    PikaMaker* maker = New_PikaMaker();
+    PIKA_RES ret = PIKA_RES_OK;
+
+    pikaMaker_linkRawWithPath(maker, "test/assets/test.txt", "txt-file");
+    pikaMaker_linkRawWithPath(maker, "test/assets/widget_config.ini",
+                              "/widget_config.ini");
+
+    // create "./test/out/packout" path if not exist
+    ret = pikaMaker_linkCompiledModulesFullPath(maker,
+                                                "./test/out/packout/ptxt.pack");
+
+    pikaMaker_deinit(maker);
+    EXPECT_EQ(ret, PIKA_RES_OK);
+
+    pikafs_FILE* pack_file =
+        pikafs_fopen_pack("test/out/packout/ptxt.pack", "widget_config.ini");
+    EXPECT_TRUE(pack_file != NULL);
+    while (1) {
+        char buf = 0;
+        size_t n = pikafs_fread(&buf, 1, 1, pack_file);
+        if (n <= 0) {
+            break;
+        }
+        /* the txt file should not contain '\0' */
+        EXPECT_NE(buf, 0);
+    }
+    pikafs_fclose(pack_file);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
 TEST(packtool, packread_) {
     size_t n = 0;
     // Arg* fileArg = NULL;

+ 14 - 8
src/PikaCompiler.c

@@ -193,6 +193,8 @@ PIKA_RES pikaCompileFileWithOutputName(char* output_file_name,
     Args buffs = {0};
     Arg* input_file_arg = arg_loadFile(NULL, input_file_name);
     if (NULL == input_file_arg) {
+        pika_platform_printf("Error: Could not load file '%s'\n",
+                             input_file_name);
         return PIKA_RES_ERR_IO_ERROR;
     }
     char* lines = (char*)arg_getBytes(input_file_arg);
@@ -273,7 +275,7 @@ int LibObj_staticLinkFileWithPath(LibObj* self,
     /* read file */
     Arg* input_file_arg = arg_loadFile(NULL, input_file_name);
     if (NULL == input_file_arg) {
-        pika_platform_printf("error: can't open file %s\r\n", input_file_name);
+        pika_platform_printf("Error: can't open file %s\r\n", input_file_name);
         return -1;
     }
     char* module_name = strsGetLastToken(&buffs, input_file_name, '/');
@@ -362,8 +364,8 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach,
     Args buffs = {0};
     if (arg_isObject(argEach)) {
         PikaObj* module_obj = arg_getPtr(argEach);
-        uint32_t bytecode_size =
-            align_by(obj_getInt(module_obj, "bytesize"), 4);
+        uint32_t bytecode_size = obj_getInt(module_obj, "bytesize");
+        uint32_t bytecode_size_align = align_by(bytecode_size, 4);
         char* module_name = obj_getStr(module_obj, "name");
         module_name = strsReplace(&buffs, module_name, "|", ".");
         uint32_t name_size = strGetSize(module_name);
@@ -377,9 +379,10 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach,
 
         pika_platform_memcpy(block_buff, module_name,
                              name_size + 1); /* add '\0' after name */
+        /* should write the size without align */
         pika_platform_memcpy(
-            block_buff + linker->block_size - sizeof(bytecode_size),
-            &bytecode_size, sizeof(bytecode_size));
+            block_buff + linker->block_size - sizeof(bytecode_size_align),
+            &bytecode_size, sizeof(bytecode_size_align));
 
         /* write the block to file */
         linker_fwrite(linker, (uint8_t*)block_buff, linker->block_size);
@@ -548,8 +551,8 @@ static PIKA_RES _loadModuleDataWithIndex(uint8_t* library_bytes,
             *(uint32_t*)(file_info_block + block_size - sizeof(uint32_t));
         bytecode_ptr = bytecode_ptr_next;
         offset += block_size;
-        /* next module ptr */
-        bytecode_ptr_next += module_size;
+        /* next module ptr, align by 4 bytes */
+        bytecode_ptr_next += align_by(module_size, 4);
     }
     *name_p = module_name;
     *addr_p = bytecode_ptr;
@@ -1087,6 +1090,7 @@ int pikaMaker_getDependencies(PikaMaker* self, char* module_name) {
     Arg* file_arg = arg_loadFile(NULL, file_path);
     uint8_t offset_befor = 0;
     if (NULL == file_arg) {
+        pika_platform_printf("Error: Could not load file '%s'\n", file_path);
         res = 1;
         goto __exit;
     }
@@ -1469,7 +1473,9 @@ pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name) {
     return f;
 
 __exit:
-    arg_deinit(f->farg);
+    if (NULL != f->farg) {
+        arg_deinit(f->farg);
+    }
 __getpack_err:
     pikaFree(f, sizeof(pikafs_FILE));
 __malloc_err:

+ 1 - 1
src/PikaCompiler.h

@@ -50,7 +50,7 @@ PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
                                  uint8_t** addr_p,
                                  size_t* size_p);
 
-#define LIB_VERSION_NUMBER 6
+#define LIB_VERSION_NUMBER 7
 #define LIB_INFO_BLOCK_SIZE 32
 #define PIKA_APP_MAGIC_CODE_OFFSET 0
 #define PIKA_APP_MODULE_SIZE_OFFSET 1

+ 1 - 0
src/PikaParser.c

@@ -3058,6 +3058,7 @@ char* pika_file2Target(Args* outBuffs,
     Arg* file_arg = arg_loadFile(NULL, filename);
     pika_assert(NULL != file_arg);
     if (NULL == file_arg) {
+        pika_platform_printf("Error: Can not open file: %s\r\n", filename);
         return NULL;
     }
     char* lines = (char*)arg_getBytes(file_arg);

+ 4 - 1
src/PikaPlatform.c

@@ -31,7 +31,10 @@
 #if defined(_WIN32) && !defined(CROSS_BUILD)
 #include <Windows.h>
 #include <io.h>
-#include <direct.h>
+#endif
+
+#if defined(_WIN32)
+#include <dirent.h>
 #endif
 
 #if defined(__linux) || PIKA_LINUX_COMPATIBLE

+ 1 - 1
src/PikaPlatform.h

@@ -101,7 +101,7 @@ extern "C" {
 #endif
 
 #if defined(_WIN32)
-#pragma warning(disable: 4113)
+#pragma warning(disable : 4113)
 #endif
 
 /* OS */

+ 2 - 0
src/PikaVM.c

@@ -3902,6 +3902,7 @@ VMParameters* pikaVM_runByteCodeFile(PikaObj* self, char* filename) {
     Arg* file_arg = arg_loadFile(NULL, filename);
     pika_assert(NULL != file_arg);
     if (NULL == file_arg) {
+        pika_platform_printf("Error: can not open file '%s'\n", filename);
         return NULL;
     }
     uint8_t* lines = arg_getBytes(file_arg);
@@ -3917,6 +3918,7 @@ VMParameters* pikaVM_runSingleFile(PikaObj* self, char* filename) {
     Arg* file_arg = arg_loadFile(NULL, filename);
     pika_assert(NULL != file_arg);
     if (NULL == file_arg) {
+        pika_platform_printf("Error: can not open file '%s'\n", filename);
         return NULL;
     }
     char* lines = (char*)arg_getBytes(file_arg);

+ 1 - 1
src/PikaVersion.h

@@ -2,4 +2,4 @@
 #define PIKA_VERSION_MINOR 13
 #define PIKA_VERSION_MICRO 0
 
-#define PIKA_EDIT_TIME "2023/11/19 14:45:23"
+#define PIKA_EDIT_TIME "2023/12/03 01:48:58"

+ 5 - 6
src/dataArg.c

@@ -665,7 +665,6 @@ Arg* arg_loadFile(Arg* self, char* filename) {
     pika_platform_memset(file_buff, 0, PIKA_READ_FILE_BUFF_SIZE);
     FILE* input_file = pika_platform_fopen(filename, "rb");
     if (NULL == input_file) {
-        pika_platform_printf("Error: Couldn't open file '%s'\n", filename);
         if (NULL != res) {
             arg_deinit(res);
         }
@@ -677,21 +676,21 @@ Arg* arg_loadFile(Arg* self, char* filename) {
 
     if (file_size >= PIKA_READ_FILE_BUFF_SIZE) {
         pika_platform_printf("Error: Not enough buff for input file.\r\n");
+        pika_platform_printf("Info: file size: %d\r\n", file_size);
+        pika_platform_printf("Info: buff size: %d\r\n",
+                             PIKA_READ_FILE_BUFF_SIZE);
         arg_deinit(res);
         res = NULL;
         goto __exit;
     }
-    /* add '\0' to the end of the string, will copy content from file_buff to
-     * res  */
-    res = arg_setBytes(res, "", (uint8_t*)file_buff, file_size + 1);
-    // return res;
+    /* should not add '\0' to the end of the string */
+    res = arg_setBytes(res, "", (uint8_t*)file_buff, file_size);
 __exit:
     pika_platform_free(file_buff);
     if (NULL != input_file) {
         pika_platform_fclose(input_file);
     }
     return res;
-    // return NULL;
 }
 
 void arg_deinit(Arg* self) {

BIN
tools/pikaCompiler/rust-msc-latest-win10.exe