1 /*
2 * Copyright (C) 2025 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 #include "credential_base.h"
16
17 using namespace Hdc;
18
19 namespace HdcCredentialBase {
GetPathSep()20 char GetPathSep()
21 {
22 #ifdef _WIN32
23 const char sep = '\\';
24 #else
25 const char sep = '/';
26 #endif
27 return sep;
28 }
29
RemoveDir(const std::string & dir)30 int RemoveDir(const std::string& dir)
31 {
32 DIR *pdir = opendir(dir.c_str());
33 if (pdir == nullptr) {
34 WRITE_LOG(LOG_FATAL, "opendir failed, error is:%s", strerror(errno));
35 return -1;
36 }
37 struct dirent *ent;
38 struct stat st;
39 while ((ent = readdir(pdir)) != nullptr) {
40 if (ent->d_name[0] == '.') {
41 continue;
42 }
43 std::string subpath = dir + GetPathSep() + ent->d_name;
44 if (lstat(subpath.c_str(), &st) == -1) {
45 WRITE_LOG(LOG_WARN, "lstat failed subpath:%s", ent->d_name);
46 continue;
47 }
48 if (S_ISDIR(st.st_mode)) {
49 if (RemoveDir(subpath) == -1) {
50 closedir(pdir);
51 return -1;
52 }
53 rmdir(subpath.c_str());
54 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
55 if (unlink(subpath.c_str()) == -1) {
56 WRITE_LOG(LOG_FATAL, "Failed to unlink file or symlink, error is :%s", strerror(errno));
57 }
58 } else {
59 WRITE_LOG(LOG_DEBUG, "lstat st_mode:%07o", st.st_mode);
60 }
61 }
62 if (rmdir(dir.c_str()) == -1) {
63 closedir(pdir);
64 return -1;
65 }
66 closedir(pdir);
67 return 0;
68 }
69
RemovePath(const std::string & path)70 int RemovePath(const std::string& path)
71 {
72 struct stat st;
73 if (lstat(path.c_str(), &st) == -1) {
74 WRITE_LOG(LOG_WARN, "lstat failed path:%s", strerror(errno));
75 return -1;
76 }
77 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
78 if (unlink(path.c_str()) == -1) {
79 WRITE_LOG(LOG_FATAL, "Failed to unlink file or symlink,, error is :%s", strerror(errno));
80 }
81 } else if (S_ISDIR(st.st_mode)) {
82 if (path == "." || path == "..") {
83 return 0;
84 }
85 int rc = RemoveDir(path);
86 return rc;
87 }
88 WRITE_LOG(LOG_DEBUG, "Directory removed successfully.");
89 return 0;
90 }
91
StringFormat(const char * const formater,...)92 const std::string StringFormat(const char* const formater, ...)
93 {
94 va_list vaArgs;
95 va_start(vaArgs, formater);
96 std::string ret = StringFormat(formater, vaArgs);
97 va_end(vaArgs);
98 return ret;
99 }
100
StringFormat(const char * const formater,va_list & vaArgs)101 const std::string StringFormat(const char* const formater, va_list& vaArgs)
102 {
103 std::vector<char> args(MAX_SIZE_IOBUF_STABLE);
104 const int retSize = vsnprintf_s(
105 args.data(), MAX_SIZE_IOBUF_STABLE, (args.size() >= 1) ? (args.size() - 1) : 0, formater, vaArgs);
106 if (retSize < 0) {
107 return std::string("");
108 } else {
109 return std::string(args.data(), retSize);
110 }
111 }
112
CreatePathWithMode(const char * path,mode_t mode)113 bool CreatePathWithMode(const char* path, mode_t mode)
114 {
115 if (::mkdir(path, mode) != 0) {
116 WRITE_LOG(LOG_FATAL, "Failed to create directory ,error is :%s", strerror(errno));
117 return false;
118 }
119 if (::chmod(path, mode) != 0) {
120 WRITE_LOG(LOG_FATAL, "Failed to set directory permissions, error is :%s", strerror(errno));
121 return false;
122 }
123 WRITE_LOG(LOG_DEBUG, "Directory created successfully.");
124 return true;
125 }
126
IsUserDir(const std::string & dir)127 bool IsUserDir(const std::string& dir)
128 {
129 int userId;
130 try {
131 userId = std::stoi(dir);
132 } catch (const std::invalid_argument&) {
133 userId = 0;
134 }
135 return userId >= MIN_USER_ID && userId <= MAX_USER_ID;
136 }
137 } // namespace HdcCredentialBase