Przeglądaj źródła

1、梳理数据库内存释放的bug
2、将文件接口独立出来

guohui 2 lat temu
rodzic
commit
e6b836cd84
8 zmienionych plików z 409 dodań i 326 usunięć
  1. 1 0
      pro/CMakeLists.txt
  2. 1 2
      pro/src/appweb_handle.c
  3. 1 1
      pro/src/cascade.c
  4. 113 0
      pro/src/file.c
  5. 20 0
      pro/src/file.h
  6. 28 94
      pro/src/paras.c
  7. 244 150
      pro/src/sqlite_handle.c
  8. 1 79
      pro/src/sys.c

+ 1 - 0
pro/CMakeLists.txt

@@ -74,6 +74,7 @@ set(PDU_SRC_LIST
 	${PROJECT_SOURCE_DIR}/src/cascade.c
     ${PROJECT_SOURCE_DIR}/src/cascade_slave.c
 	${PROJECT_SOURCE_DIR}/src/upg.c
+	${PROJECT_SOURCE_DIR}/src/file.c
 	${PROJECT_SOURCE_DIR}/src/main.c
 
 	${PROJECT_SOURCE_DIR}/src/language_hashmap.c

+ 1 - 2
pro/src/appweb_handle.c

@@ -15,7 +15,6 @@
 #include "websocket_handle.h"
 
 
-#define WEB_DEBUG 1
 
 typedef void (*http_fn_t)(void *arg);
 
@@ -2380,7 +2379,7 @@ static void communicationManagement(void *conn)
         int ipv=strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6;
         
         int mode = atoi(info2->mode);
-        if(ipv==IP_V4 && mode==IP_STATIC) { //ipv4, static
+        if(ipv==IP_V4) {
             info->mode = mode;
             
             if(info2->ipaddr)   strcpy(info->ip_address, info2->ipaddr);

+ 1 - 1
pro/src/cascade.c

@@ -8,7 +8,7 @@
 #include "sqlite_handle.h"
 #include "paras.h"
 
-#if 0
+#if 1
 #define LOGD            log_d
 #define LOGE            log_e
 #define LOGW            log_w

+ 113 - 0
pro/src/file.c

@@ -0,0 +1,113 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "file.h"
+
+
+#if 0
+#define LOGE  log_e
+#define LOGW  log_w
+#define LOGD  log_d
+#else
+#define LOGD  printf
+#define LOGW  printf
+#define LOGE  printf
+#endif
+
+
+int file_length(char* path)
+{
+    int r;
+    struct stat st;
+
+    r = stat(path, &st);
+    if (r) {
+        return -1;
+    }
+
+    return st.st_size;
+}
+
+int file_save(char *path, void *buf, int len)
+{
+    int r;
+    FILE *fp;
+    
+    fp = fopen(path, "w");
+    if(!fp) {
+        LOGE("file_save, fopen %s failed\n", path);
+        return -1;
+    }
+    r = fwrite(buf, 1, len, fp);
+    if(r!=len) {
+        LOGE("file_save %s failed, %d, %d\n", path, r, len);
+        r = -1;
+    }
+    else {
+        r = 0;
+    }
+    fclose(fp);
+
+    return r;
+}
+
+int file_load(char* path, file_data_t *fdat)
+{
+    int r,flen;
+    FILE* fp;
+
+    flen = file_length(path);
+    if(flen<=0) {
+        LOGE("____ file_load, %s failed\n", path);
+        return -1;
+    }
+
+    fdat->buf = (char*)malloc(flen+1);
+    if(!fdat->buf) {
+        LOGE("____ file_load, %s malloc %d failed\n", path, flen);
+        return -1;
+    }
+    fdat->len = flen;
+
+    fp = fopen(path, "r");
+    if (!fp) {
+        LOGE("file_load, fopen %s failed\n", path); free(fdat->buf);
+        free(fdat->buf);
+        return -1;
+    }
+    r = fread(fdat->buf, 1, flen, fp);
+    if (r != flen) {
+        LOGE("file_load %s failed, %d, %d\n", path, r, flen);
+        fclose(fp); free(fdat->buf);
+        r = -1;
+    }
+    else {
+        fdat->buf[flen] = 0; r = 0;
+    }
+    fclose(fp);
+
+    return r;
+}
+
+
+int file_remove(char *path)
+{
+    return remove(path);
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 20 - 0
pro/src/file.h

@@ -0,0 +1,20 @@
+#ifndef __FILE_Hx__
+#define __FILE_Hx__
+
+#include <stdlib.h>
+#include <stdio.h>
+
+
+typedef struct {
+    char *buf;
+    int  len;
+}file_data_t;
+
+
+int file_length(char* path);
+int file_save(char *path, void *buf, int len);
+int file_load(char* path, file_data_t *fdat);
+int file_remove(char *path);
+
+
+#endif

+ 28 - 94
pro/src/paras.c

@@ -1,111 +1,44 @@
-#include "paras.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include "sys.h"
-
-
-//#define LOG log_print
-#define LOG printf
+#include "file.h"
+#include "paras.h"
 
 
 all_paras_t allParas;
-////////////////////////////////////////////////////////////
-static int file_length(char* path)
-{
-    int r;
-    struct stat st;
-
-    r = stat(path, &st);
-    if (r) {
-        return -1;
-    }
-
-    return st.st_size;
-}
-
-static int file_save(char *path, void *buf, int len)
+static int do_check(void *para)
 {
-    int r;
-    FILE *fp;
-    
-    fp = fopen(path, "wb");
-    if(!fp) {
-        LOG("file_save, fopen %s failed\n", path);
-        return -1;
-    }
-    r = fwrite(buf, 1, len, fp);
-    if(r!=len) {
-        LOG("file_save %s failed, %d, %d\n", path, r, len);
-        r = -1;
-    }
-    else {
-        r = 0;
-    }
-    fclose(fp);
-
-    return r;
-}
-static int file_load(char* path, void* buf, int len)
-{
-    int r;
-    FILE* fp;
-
-    fp = fopen(path, "rb");
-    if (!fp) {
-        LOG("file_load, fopen %s failed\n", path);
-        return -1;
-    }
-    r = fread(buf, 1, len, fp);
-    if (r != len) {
-        LOG("file_load %s failed, %d, %d\n", path, r, len);
-        r = -1;
-    }
-    else {
-        r = 0;
-    }
-    fclose(fp);
-
-    return r;
-}
-static int file_remove(char *path)
-{
-    return remove(path);
-}
-
-static int do_check(all_paras_t *paras)
-{
-    int i,j;
-    
-
     return 0;
 }
-///////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////
 int paras_load(void)
 {
     int i,r=-1;
-    int flen,rlen;
+    file_data_t fdat;
     char path[PATH_MAX];
     NetworkInfo_t *p=&allParas.dev._network_info;
 
     allParas = DFLT_PARAS;
     sys_get_path(path, PARAS_FILE);
 
- #if 1   
-    flen = file_length(path);
-    if(flen==sizeof(allParas)) {
-        rlen = file_load(path, &allParas, flen);
-        if(rlen==flen) {
-            r = do_check(&allParas);
+#if 1
+    r = file_load(path, &fdat);
+    if(r==0) {
+        if(fdat.len==sizeof(allParas)) {
+            r = do_check(fdat.buf);
             if(r) {
                 allParas = DFLT_PARAS;
             }
+            else {
+                allParas = *((all_paras_t*)fdat.buf);
+            }
         }
+        free(fdat.buf);
     }
 #endif
 
     if(r) {
         paras_save();
-        //sys_set_net(p, IP_V4);
     }
     else {
         sys_get_net(p, IP_V4);
@@ -117,29 +50,30 @@ int paras_load(void)
 
 int paras_save(void)
 {
-    int r,flen;
+    int r;
+    file_data_t fdat;
     char path[PATH_MAX];
-    all_paras_t *p;
     
     r = do_check(&allParas);
     if(r) {
         return -1;
     }
+
     sys_get_path(path, PARAS_FILE);
-    flen = file_length(path);
-
-    if(flen==sizeof(allParas)) {
-        p = malloc(sizeof(allParas));
-        if(p) {
-            file_load(path, p, flen);
-            if(memcmp(p, &allParas, sizeof(allParas))==0) {
-                free(p); return -1;
+    r = file_load(path, &fdat);
+    if(r==0) {
+        if(fdat.len==sizeof(allParas)) {
+            if(memcmp(fdat.buf, &allParas, sizeof(allParas))==0) {
+                r = -1;
+            }
+            else {
+                r = file_save(path, &allParas, sizeof(allParas));
             }
-            free(p);
         }
+        free(fdat.buf);
     }
-    
-    return file_save(path, &allParas, sizeof(allParas));
+
+    return r;
 }
 
 

Plik diff jest za duży
+ 244 - 150
pro/src/sqlite_handle.c


+ 1 - 79
pro/src/sys.c

@@ -1,5 +1,3 @@
-#include <stdlib.h>
-#include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -11,6 +9,7 @@
 #include "upgrade.h"
 #include "paras.h"
 #include "cfg.h"
+#include "file.h"
 #include "sqlite_handle.h"
 
 #if 0
@@ -36,11 +35,6 @@
 #define FILE_DNS            "/etc/dnsmasq.conf"
 
 
-typedef struct {
-    char *buf;
-    int  len;
-}file_data_t;
-
 typedef struct {
 	char *pbuf;
     int  dlen;
@@ -477,78 +471,6 @@ static int ip_check(char* addr)
 
 
 ///////////////////////////////////////////////
-static int file_length(char* path)
-{
-    int r;
-    struct stat st;
-
-    r = stat(path, &st);
-    if (r) {
-        return -1;
-    }
-
-    return st.st_size;
-}
-
-static int file_save(char *path, void *buf, int len)
-{
-    int r;
-    FILE *fp;
-    
-    fp = fopen(path, "w");
-    if(!fp) {
-        LOGE("file_save, fopen %s failed\n", path);
-        return -1;
-    }
-    r = fwrite(buf, 1, len, fp);
-    if(r!=len) {
-        LOGE("file_save %s failed, %d, %d\n", path, r, len);
-        r = -1;
-    }
-    else {
-        r = 0;
-    }
-    fclose(fp);
-
-    return r;
-}
-
-static int file_load(char* path, file_data_t *fdat)
-{
-    int r,flen;
-    FILE* fp;
-
-    flen = file_length(path);
-    if(flen<=0) {
-        LOGE("____ file_load, %s failed\n", path);
-        return -1;
-    }
-
-    fdat->buf = (char*)malloc(flen+1);
-    if(!fdat->buf) {
-        LOGE("____ file_load, %s malloc %d failed\n", path, flen);
-        return -1;
-    }
-    fdat->len = flen;
-
-    fp = fopen(path, "r");
-    if (!fp) {
-        LOGE("file_load, fopen %s failed\n", path); free(fdat->buf);
-        return -1;
-    }
-    r = fread(fdat->buf, 1, flen, fp);
-    if (r != flen) {
-        LOGE("file_load %s failed, %d, %d\n", path, r, flen); free(fdat->buf);
-        r = -1;
-    }
-    else {
-        fdat->buf[flen] = 0; r = 0;
-    }
-    fclose(fp);
-
-    return r;
-}
-
 typedef struct {
     char lo[1000];
     char e0[1000];