diff --git a/libbbf_api/dmcommon.c b/libbbf_api/dmcommon.c index 62d99f18..4d64fede 100644 --- a/libbbf_api/dmcommon.c +++ b/libbbf_api/dmcommon.c @@ -1963,6 +1963,23 @@ void strip_lead_trail_whitespace(char *str) } } +int dm_buf_to_file(char *buf, const char *filename) +{ + FILE *file; + int ret = -1; + + if (buf == NULL || filename == NULL) + return ret; + + file = fopen(filename, "w"); + if (file) { + ret = fputs(buf, file); + fclose(file); + } + + return ret; +} + int dm_file_to_buf(const char *filename, void *buf, size_t buf_size) { FILE *file; diff --git a/libbbf_api/dmcommon.h b/libbbf_api/dmcommon.h index 69c1ec9b..3c098680 100644 --- a/libbbf_api/dmcommon.h +++ b/libbbf_api/dmcommon.h @@ -297,4 +297,5 @@ char *ioctl_get_ipv4(char *interface_name); char *get_ipv6(char *interface_name); bool validate_blob_message(struct blob_attr *src, struct blob_attr *dst); void strip_lead_trail_whitespace(char *str); +int dm_buf_to_file(char *buf, const char *filename); #endif diff --git a/libbbf_dm/dmdiagnostics.c b/libbbf_dm/dmdiagnostics.c index d21fce05..d87e085f 100644 --- a/libbbf_dm/dmdiagnostics.c +++ b/libbbf_dm/dmdiagnostics.c @@ -179,23 +179,47 @@ static long upload_file(const char *file_path, const char *url, const char *user { long res_code = 0; - CURL *curl = curl_easy_init(); - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_USERNAME, username); - curl_easy_setopt(curl, CURLOPT_PASSWORD, password); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT); - curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - - FILE *fp = fopen(file_path, "rb"); - if (fp) { - curl_easy_setopt(curl, CURLOPT_READDATA, fp); - curl_easy_perform(curl); - fclose(fp); + if (strncmp(url, FILE_URI, strlen(FILE_URI)) == 0) { + char dst_path[2046] = {0}; + snprintf(dst_path, sizeof(dst_path), "%s", url+strlen(FILE_URI)); + FILE *fp = fopen(file_path, "r"); + if (fp == NULL) { + return -1; } - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code); - curl_easy_cleanup(curl); + fseek(fp, 0, SEEK_END); + unsigned int length = ftell(fp); + fseek(fp, 0, SEEK_SET); + fclose(fp); + + char buff[length]; + memset(buff, 0, length); + + if (dm_file_to_buf(file_path, buff, length) > 0) { + if (dm_buf_to_file(buff, dst_path) < 0) + res_code = -1; + } else { + res_code = -1; + } + } else { + CURL *curl = curl_easy_init(); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_USERNAME, username); + curl_easy_setopt(curl, CURLOPT_PASSWORD, password); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT); + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); + + FILE *fp = fopen(file_path, "rb"); + if (fp) { + curl_easy_setopt(curl, CURLOPT_READDATA, fp); + curl_easy_perform(curl); + fclose(fp); + } + + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code); + curl_easy_cleanup(curl); + } } return res_code; diff --git a/libbbf_dm/dmtree/tr181/deviceinfo.c b/libbbf_dm/dmtree/tr181/deviceinfo.c index b3c95b00..7e208584 100644 --- a/libbbf_dm/dmtree/tr181/deviceinfo.c +++ b/libbbf_dm/dmtree/tr181/deviceinfo.c @@ -20,6 +20,7 @@ LIST_HEAD(process_list); static int process_count = 0; #define PROCPS_BUFSIZE 1024 +#define DEF_VENDOR_LOG_FILE "/tmp/.vend_log" struct process_entry { struct list_head list; @@ -1214,8 +1215,18 @@ static int operate_DeviceInfoVendorLogFile_Upload(char *refparam, struct dmctx * dmuci_get_value_by_section_string(((struct dmmap_dup *)data)->config_section, "log_file", &vlf_file_path); + if (DM_STRLEN(vlf_file_path) == 0) { + vlf_file_path = DEF_VENDOR_LOG_FILE; + char cmd[64] = {0}; + snprintf(cmd, sizeof(cmd), "logread > %s", DEF_VENDOR_LOG_FILE); + system(cmd); + } + int res = bbf_upload_log(url, user, pass, vlf_file_path, upload_command, upload_path); + if (file_exists(DEF_VENDOR_LOG_FILE)) + remove(DEF_VENDOR_LOG_FILE); + return res ? CMD_FAIL : CMD_SUCCESS; }