Fix vendor log/config upload to local path

This commit is contained in:
suvendhu 2023-04-05 14:48:34 +05:30
parent 386d1cddb0
commit 4515b63198
4 changed files with 68 additions and 15 deletions

View file

@ -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;

View file

@ -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

View file

@ -179,6 +179,29 @@ static long upload_file(const char *file_path, const char *url, const char *user
{
long res_code = 0;
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;
}
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);
@ -197,6 +220,7 @@ static long upload_file(const char *file_path, const char *url, const char *user
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_code);
curl_easy_cleanup(curl);
}
}
return res_code;
}

View file

@ -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;
}