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