1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include <unistd.h>
17f6603c60Sopenharmony_ci#include <stdlib.h>
18f6603c60Sopenharmony_ci#include <string.h>
19f6603c60Sopenharmony_ci#include <errno.h>
20f6603c60Sopenharmony_ci#include <dirent.h>
21f6603c60Sopenharmony_ci#include <fcntl.h>
22f6603c60Sopenharmony_ci#include <sys/stat.h>
23f6603c60Sopenharmony_ci#include "log.h"
24f6603c60Sopenharmony_ci#include "libfs.h"
25f6603c60Sopenharmony_ci#include "KernelConstants.h"
26f6603c60Sopenharmony_ci
27f6603c60Sopenharmony_ci
28f6603c60Sopenharmony_ciint MakeDir(const char *dirname)
29f6603c60Sopenharmony_ci{
30f6603c60Sopenharmony_ci    if (access(dirname, F_OK) == 0) {
31f6603c60Sopenharmony_ci        LOG("dir:%s exists", dirname);
32f6603c60Sopenharmony_ci        return 1;
33f6603c60Sopenharmony_ci    } else {
34f6603c60Sopenharmony_ci        int rt = mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IXOTH);
35f6603c60Sopenharmony_ci        if (rt) {
36f6603c60Sopenharmony_ci            LOG("create dir failed! path=%s, errno=%d:%s", dirname, errno, strerror(errno));
37f6603c60Sopenharmony_ci            return -1;
38f6603c60Sopenharmony_ci        }
39f6603c60Sopenharmony_ci    }
40f6603c60Sopenharmony_ci    return 0;
41f6603c60Sopenharmony_ci}
42f6603c60Sopenharmony_ci
43f6603c60Sopenharmony_ciint RemoveDir(const char *dirname)
44f6603c60Sopenharmony_ci{
45f6603c60Sopenharmony_ci    char subDir[MAX_PATH_SIZE];
46f6603c60Sopenharmony_ci    struct stat dirStat = {0};
47f6603c60Sopenharmony_ci
48f6603c60Sopenharmony_ci    if (stat(dirname, &dirStat) < 0) {
49f6603c60Sopenharmony_ci        LOG("get directory stat error, errno=%d:%s", errno, strerror(errno));
50f6603c60Sopenharmony_ci        return -1;
51f6603c60Sopenharmony_ci    }
52f6603c60Sopenharmony_ci
53f6603c60Sopenharmony_ci    if (S_ISDIR(dirStat.st_mode)) {
54f6603c60Sopenharmony_ci        DIR *pDir = opendir(dirname);
55f6603c60Sopenharmony_ci        struct dirent *entry;
56f6603c60Sopenharmony_ci        while ((entry = readdir(pDir))) {
57f6603c60Sopenharmony_ci            char *fname = entry->d_name;
58f6603c60Sopenharmony_ci            if (!strcmp(fname, ".") || !strcmp(fname, "..")) {
59f6603c60Sopenharmony_ci                continue; // skip . and ..
60f6603c60Sopenharmony_ci            }
61f6603c60Sopenharmony_ci            // MAX_PATH_SIZE is the max length of allowed path string, so it safe to use sprintf here.
62f6603c60Sopenharmony_ci            sprintf(subDir, "%s/%s", dirname, fname);
63f6603c60Sopenharmony_ci            RemoveDir(subDir);   // remove sub dir or file
64f6603c60Sopenharmony_ci        }
65f6603c60Sopenharmony_ci        closedir(pDir);
66f6603c60Sopenharmony_ci        if (rmdir(dirname) == -1) {  // delete empty dir
67f6603c60Sopenharmony_ci            LOG("delete empty directory failed, path=%s, errno=%d:%s", dirname, errno, strerror(errno));
68f6603c60Sopenharmony_ci            return -1;
69f6603c60Sopenharmony_ci        }
70f6603c60Sopenharmony_ci    } else {
71f6603c60Sopenharmony_ci        if (remove(dirname) == -1) {
72f6603c60Sopenharmony_ci            LOG("remove regular file failed, path=%s, errno=%d:%s", dirname, errno, strerror(errno));
73f6603c60Sopenharmony_ci            return -1;
74f6603c60Sopenharmony_ci        }
75f6603c60Sopenharmony_ci    }
76f6603c60Sopenharmony_ci
77f6603c60Sopenharmony_ci    return 0;
78f6603c60Sopenharmony_ci}
79f6603c60Sopenharmony_ci
80f6603c60Sopenharmony_ciint RemoveFile(const char *fpath)
81f6603c60Sopenharmony_ci{
82f6603c60Sopenharmony_ci    if (!remove(fpath)) {
83f6603c60Sopenharmony_ci        LOG("remove file success");
84f6603c60Sopenharmony_ci    } else {
85f6603c60Sopenharmony_ci        LOG("remove file failed! path=%s: errno=%d:%s", fpath, errno, strerror(errno));
86f6603c60Sopenharmony_ci        return -1;
87f6603c60Sopenharmony_ci    }
88f6603c60Sopenharmony_ci    return 0;
89f6603c60Sopenharmony_ci}
90f6603c60Sopenharmony_ci
91f6603c60Sopenharmony_ciint CopyFile(const char *srcFile, const char *dstFile)
92f6603c60Sopenharmony_ci{
93f6603c60Sopenharmony_ci    int rt = 0;
94f6603c60Sopenharmony_ci    FILE *srcFp = fopen(srcFile, "rb");
95f6603c60Sopenharmony_ci    if (srcFp == nullptr) {
96f6603c60Sopenharmony_ci        LOG("Cannot open source file %s: errno=%d,%s \n", srcFile, errno, strerror(errno));
97f6603c60Sopenharmony_ci        return -1;
98f6603c60Sopenharmony_ci    }
99f6603c60Sopenharmony_ci    FILE *dstFp = fopen(dstFile, "wb");
100f6603c60Sopenharmony_ci    if (dstFp == nullptr) {
101f6603c60Sopenharmony_ci        LOG("Cannot create dest file %s: errno=%d,%s \n", dstFile, errno, strerror(errno));
102f6603c60Sopenharmony_ci        fclose(srcFp);
103f6603c60Sopenharmony_ci        return -1;
104f6603c60Sopenharmony_ci    }
105f6603c60Sopenharmony_ci
106f6603c60Sopenharmony_ci    const int BUF_SIZE = 100 * 1024;
107f6603c60Sopenharmony_ci    char buffer[BUF_SIZE];
108f6603c60Sopenharmony_ci    size_t bytes;
109f6603c60Sopenharmony_ci    while ((bytes = fread(buffer, 1, sizeof(buffer), srcFp)) > 0) {
110f6603c60Sopenharmony_ci        if (fwrite(buffer, 1, bytes, dstFp) != bytes) {
111f6603c60Sopenharmony_ci            LOG("write to dest file failed: errno=%d,%s \n", errno, strerror(errno));
112f6603c60Sopenharmony_ci            rt = -1;
113f6603c60Sopenharmony_ci            break;
114f6603c60Sopenharmony_ci        }
115f6603c60Sopenharmony_ci    }
116f6603c60Sopenharmony_ci    fclose(srcFp);
117f6603c60Sopenharmony_ci    fclose(dstFp);
118f6603c60Sopenharmony_ci    return rt;
119f6603c60Sopenharmony_ci}
120f6603c60Sopenharmony_ci
121f6603c60Sopenharmony_cichar* GetCurrentPath()
122f6603c60Sopenharmony_ci{
123f6603c60Sopenharmony_ci    static char path1[MAX_PATH_SIZE];
124f6603c60Sopenharmony_ci    char *path = getcwd(path1, MAX_PATH_SIZE);
125f6603c60Sopenharmony_ci    LOG("current Path = %s,path1=%s", path, path1);
126f6603c60Sopenharmony_ci    return path1;
127f6603c60Sopenharmony_ci}
128