235 lines
5.1 KiB
C
235 lines
5.1 KiB
C
//
|
|
// Created by xajhu on 2021/7/2 0002.
|
|
//
|
|
#include <string.h>
|
|
#include <uv.h>
|
|
#include <sys/vfs.h>
|
|
#include <zlog.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <sys/sendfile.h>
|
|
#include <linux/if.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/if_ether.h>
|
|
|
|
#include "user_errno.h"
|
|
#include "misc.h"
|
|
|
|
const char *basename_v2(const char *path) {
|
|
const char *tail = strrchr(path, '/');
|
|
return tail ? tail + 1 : path;
|
|
}
|
|
|
|
int dirname_v2(const char *path, char *dir) {
|
|
const char *tail = strrchr(path, '/');
|
|
|
|
if (tail) {
|
|
memcpy(dir, path, tail - path);
|
|
dir[tail - path] = 0;
|
|
} else {
|
|
strcpy(dir, "./");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
unsigned long long get_partition_free_size(const char *pPartPath) {
|
|
struct statfs myStatfs;
|
|
unsigned long long freeSize;
|
|
|
|
if (statfs(pPartPath, &myStatfs) == -1) {
|
|
return 0;
|
|
}
|
|
|
|
freeSize = myStatfs.f_bsize * myStatfs.f_bfree;
|
|
|
|
return freeSize;
|
|
}
|
|
|
|
int copy_file(const char *pSrc, const char *pDest) {
|
|
int fdSrc, fdDest;
|
|
struct stat st;
|
|
ssize_t sz;
|
|
|
|
if (stat(pSrc, &st) != 0) {
|
|
dzlog_error("Get File %s Size Error\n", pSrc);
|
|
return (-ERR_GET_FILE_SIZE);
|
|
}
|
|
|
|
fdSrc = open(pSrc, O_RDONLY);
|
|
|
|
if (fdSrc < 0) {
|
|
dzlog_error("Open File %s Error\n", pSrc);
|
|
return (-ERR_OPEN_FILE);
|
|
}
|
|
|
|
fdDest = open(pDest, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
if (fdDest < 0) {
|
|
close(fdSrc);
|
|
dzlog_error("Open File %s Error\n", pDest);
|
|
return (-ERR_OPEN_FILE);
|
|
}
|
|
|
|
sz = sendfile(fdDest, fdSrc, NULL, st.st_size);
|
|
|
|
if (sz != st.st_size) {
|
|
dzlog_error("Copy File Size Error: %zd, %ld\n", sz, st.st_size);
|
|
close(fdSrc);
|
|
close(fdDest);
|
|
return (-ERR_COPY_FILE);
|
|
}
|
|
|
|
fsync(fdDest);
|
|
|
|
close(fdSrc);
|
|
close(fdDest);
|
|
|
|
return (0);
|
|
}
|
|
|
|
char *bin2hex(char *p, const unsigned char *cp, unsigned int count) {
|
|
static const char hex_asc[] = "0123456789abcdef";
|
|
while (count) {
|
|
unsigned char c = *cp++;
|
|
/* put lowercase hex digits */
|
|
*p++ = (char)(0x20 | hex_asc[c >> 4]);
|
|
*p++ = (char)(0x20 | hex_asc[c & 0xf]);
|
|
count--;
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
int shell_with_output(const char *pCmd, char **pResult) {
|
|
FILE *pFile = NULL;
|
|
unsigned int uRdSize;
|
|
char *pCmdOut;
|
|
|
|
*pResult = NULL;
|
|
|
|
if (pCmd == NULL || strlen(pCmd) == 0) {
|
|
return (-ERR_INPUT_PARAMS);
|
|
}
|
|
|
|
pFile = popen(pCmd, "r");
|
|
|
|
if (pFile == NULL) {
|
|
return (-ERR_OPEN_FILE);
|
|
}
|
|
|
|
*pResult = (char *)malloc(4096);
|
|
pCmdOut = *pResult;
|
|
|
|
uRdSize = fread(pCmdOut, sizeof(char), 4096, pFile);
|
|
|
|
pCmdOut[uRdSize] = 0;
|
|
|
|
if (pCmdOut[strlen(pCmdOut) - 1] == '\n') {
|
|
pCmdOut[strlen(pCmdOut) - 1] = 0;
|
|
}
|
|
|
|
pclose(pFile);
|
|
return ERR_SUCCESS;
|
|
}
|
|
|
|
int file_exists(const char *pPath) {
|
|
if ((access(pPath, F_OK)) == -1) {
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
const char *get_cur_process_dir() {
|
|
static char g_exePath[4096] = {0};
|
|
|
|
size_t bufSize = 4096L;
|
|
|
|
if (strlen((const char *)g_exePath) == 0) {
|
|
memset(g_exePath, 0, 4096);
|
|
uv_cwd(g_exePath, &bufSize);
|
|
}
|
|
|
|
return (const char *)g_exePath;
|
|
}
|
|
|
|
int str_to_mac(const char *str, unsigned char mac[6]) {
|
|
int i;
|
|
char *s, *e;
|
|
|
|
if ((mac == NULL) || (str == NULL)) {
|
|
return -ERR_INPUT_PARAMS;
|
|
}
|
|
|
|
s = (char *)str;
|
|
for (i = 0; i < 6; ++i) {
|
|
mac[i] = s ? strtoul(s, &e, 16) : 0;
|
|
if (s) {
|
|
s = (*e) ? e + 1 : e;
|
|
}
|
|
}
|
|
return ERR_SUCCESS;
|
|
}
|
|
|
|
int str_to_ipaddr(const char *pIp, unsigned int *ipAddr) {
|
|
struct in_addr addr;
|
|
int ret = inet_aton(pIp, &addr);
|
|
|
|
if (ret != 0) {
|
|
*ipAddr = addr.s_addr;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int get_nic_info(const char *pName, unsigned int *pIp, unsigned int *pNetmask, unsigned int *pGateway,
|
|
unsigned char *pMac) {
|
|
int sock;
|
|
struct ifreq ifr;
|
|
int err = ERR_SUCCESS;
|
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (sock < 0) {
|
|
dzlog_error("Get local NIC information failed\n");
|
|
return -ERR_SYS_INIT;
|
|
}
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
strcpy(ifr.ifr_name, pName);
|
|
|
|
if (pIp) {
|
|
if (ioctl(sock, SIOCGIFADDR, &ifr) != 0) {
|
|
err = ERR_MISC_GET_IPADDR;
|
|
} else {
|
|
*pIp = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
|
|
}
|
|
}
|
|
|
|
if (pNetmask) {
|
|
if (ioctl(sock, SIOCGIFNETMASK, &ifr) == 0) {
|
|
*pNetmask = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
|
|
} else {
|
|
err = ERR_MISC_GET_NETMASK;
|
|
}
|
|
}
|
|
|
|
if (pGateway) {
|
|
if (ioctl(sock, SIOCGIFBRDADDR, &ifr) == 0) {
|
|
*pGateway = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
|
|
} else {
|
|
err = ERR_MISC_GET_GATEWAY;
|
|
}
|
|
}
|
|
|
|
if (pMac) {
|
|
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
|
|
memcpy(pMac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
|
|
} else {
|
|
err = ERR_MISC_GET_MACADDR;
|
|
}
|
|
}
|
|
|
|
close(sock);
|
|
return err;
|
|
} |