Files
ascend-docker-runtime/cli/src/utils.c
2020-08-11 22:52:36 +08:00

120 lines
2.5 KiB
C

/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend-docker-cli工具实用函数模块
*/
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include "securec.h"
#include "logging.h"
int IsStrEqual(const char *s1, const char *s2)
{
return (!strcmp(s1, s2));
}
int StrHasPrefix(const char *str, const char *prefix)
{
return (!strncmp(str, prefix, strlen(prefix)));
}
int MkDir(const char *dir, int mode)
{
return mkdir(dir, mode);
}
int VerifyPathInfo(const struct PathInfo* pathInfo)
{
if (pathInfo == NULL || pathInfo->dst == NULL || pathInfo->src == NULL) {
return -1;
}
return 0;
}
int CheckDirExists(const char *dir)
{
DIR *ptr = opendir(dir);
if (NULL == ptr) {
return -1;
}
closedir(ptr);
return 0;
}
int GetParentPathStr(const char *path, char *parent, size_t bufSize)
{
char *ptr = strrchr(path, '/');
if (ptr == NULL) {
return 0;
}
int len = (int)strlen(path) - (int)strlen(ptr);
if (len < 1) {
return 0;
}
errno_t ret = strncpy_s(parent, bufSize, path, len);
if (ret != EOK) {
return -1;
}
return 0;
}
int MakeDirWithParent(const char *path, mode_t mode)
{
if (*path == '\0' || *path == '.') {
return 0;
}
if (CheckDirExists(path) == 0) {
return 0;
}
char parentPath[BUF_SIZE] = {0};
GetParentPathStr(path, parentPath, BUF_SIZE);
if (strlen(parentPath) > 0 && MakeDirWithParent(parentPath, mode) < 0) {
return -1;
}
int ret = MkDir(path, mode);
if (ret < 0) {
return -1;
}
return 0;
}
int CreateFile(const char *path, mode_t mode)
{
/* directory */
char parentDir[BUF_SIZE] = {0};
GetParentPathStr(path, parentDir, BUF_SIZE);
int ret = MakeDirWithParent(parentDir, DEFAULT_DIR_MODE);
if (ret < 0) {
LogError("error: failed to make parent dir for file: %s", path);
return -1;
}
char resolvedPath[PATH_MAX] = {0};
if (realpath(path, resolvedPath) == NULL && errno != ENOENT) {
LogError("error: failed to resolve path %s.", path);
return -1;
}
int fd = open(resolvedPath, O_NOFOLLOW | O_CREAT, mode);
if (fd < 0) {
LogError("error: cannot create file: %s.", resolvedPath);
return -1;
}
close(fd);
return 0;
}