2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <sys/types.h>
21 #include <systemd/sd-login.h>
22 #include <ScopeMutex.h>
23 #include <ServerUtil.h>
29 static GMutex __pathMutex;
30 static GDBusConnection* __dbusConnection = NULL;
32 EXPORT_API bool util::is_normal_user(uid_t uid)
40 EXPORT_API bool util::is_container_user(uid_t uid)
45 EXPORT_API bool util::is_system(uid_t uid)
47 return (!is_normal_user(uid) && !is_container_user(uid));
50 EXPORT_API std::string util::get_system_path(enum tzplatform_variable id, const std::string& path)
52 ScopeMutex sm(&__pathMutex);
54 const char* rawPath = tzplatform_mkpath(id, path.c_str());
55 IF_FAIL_RETURN_TAG(rawPath, EMPTY_STR, _E, "Path creation failed");
57 return std::string(rawPath);
60 EXPORT_API std::string util::get_user_path(uid_t uid, enum tzplatform_variable id, const std::string& path)
62 IF_FAIL_RETURN_TAG(!util::is_system(uid), EMPTY_STR, _E, "Invalid UID");
64 ScopeMutex sm(&__pathMutex);
66 tzplatform_context* context = NULL;
67 tzplatform_context_create(&context);
68 IF_FAIL_RETURN_TAG(context, EMPTY_STR, _E, "tzplatform_context_create() failed");
70 if (tzplatform_context_set_user(context, uid) != E_NONE) {
71 _E("tzplatform_context_set_user() failed");
72 tzplatform_context_destroy(context);
76 const char* rawPath = tzplatform_context_mkpath(context, id, path.c_str());
79 _E("Path creation failed");
80 tzplatform_context_destroy(context);
84 std::string outPath(rawPath);
86 tzplatform_context_destroy(context);
91 EXPORT_API std::vector<std::string> util::get_files(const std::string& dirPath, bool symlink)
93 static GMutex dirMutex;
96 struct dirent* entry = NULL;
99 std::vector<std::string> fileNames;
101 ScopeMutex sm(&dirMutex);
103 dir = opendir(dirPath.c_str());
104 IF_FAIL_RETURN_TAG(dir, fileNames, _E, "Failed to open: %s", dirPath.c_str());
107 entry = readdir(dir);
111 filePath = dirPath + "/" + entry->d_name;
113 if (stat(filePath.c_str(), &fileStat) != 0)
116 if (lstat(filePath.c_str(), &fileStat) != 0)
120 if (!S_ISREG(fileStat.st_mode))
123 fileNames.push_back(entry->d_name);
130 EXPORT_API void util::set_dbus_connection(GDBusConnection* conn)
132 __dbusConnection = conn;
135 EXPORT_API GDBusConnection* util::get_dbus_connection()
137 return __dbusConnection;