提交 1135c557 编写于 作者: X xionglei6

init: Optimize the code

Signed-off-by: Nxionglei6 <xionglei6@huawei.com>
上级 99705c08
......@@ -33,23 +33,13 @@ int GetControlFile(const char *pathName)
if (pathName == NULL) {
return -1;
}
char *name = (char *)calloc(1, strlen(pathName) + 1);
INIT_CHECK(name != NULL, return -1);
if (strncpy_s(name, strlen(pathName) + 1, pathName, strlen(pathName)) < 0) {
INIT_LOGE("Failed strncpy_s err=%d", errno);
free(name);
return -1;
}
if (StringReplaceChr(name, '/', '_') < 0) {
free(name);
char path[PATH_MAX] = { 0 };
if (snprintf_s(path, sizeof(path), sizeof(path) - 1, OHOS_FILE_ENV_PREFIX "%s", pathName) == -1) {
return -1;
}
char path[PATH_MAX] = { 0 };
if (snprintf_s(path, sizeof(path), sizeof(path) - 1, OHOS_FILE_ENV_PREFIX"%s", name) == -1) {
free(name);
if (StringReplaceChr(path, '/', '_') < 0) {
return -1;
}
free(name);
INIT_LOGI("Environment path is %s ", path);
const char *val = getenv(path);
if (val == NULL) {
......
......@@ -35,20 +35,8 @@ static int CreateFile(ServiceFile *file)
{
INIT_CHECK(file != NULL && file->fileName != NULL, return -1);
char path[PATH_MAX] = {0};
char *realPath = GetRealPath(file->fileName);
if (realPath == NULL && errno == ENOENT) {
if (strncpy_s(path, strlen(file->fileName) + 1, file->fileName, strlen(file->fileName)) != 0) {
return -1;
}
} else if (realPath != NULL) {
if (strncpy_s(path, strlen(realPath) + 1, realPath, strlen(realPath)) != 0) {
free(realPath);
return -1;
}
free(realPath);
} else {
INIT_LOGE("GetRealPath failed err=%d", errno);
return -1;
if (realpath(file->fileName, path) == NULL ) {
INIT_LOGE("Failed realpath err=%d", errno);
}
INIT_LOGI("File path =%s . file flags =%d, file perm =%u ", path, file->flags, file->perm);
if (file->fd >= 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册