From: Mu-Woong Lee Date: Mon, 13 Mar 2017 12:51:02 +0000 (+0900) Subject: Add util functions for creating platform paths X-Git-Tag: accepted/tizen/unified/20170414.163458^2~19 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a29128f15d7fb1e9d709cb8bcc1fb6b5b117475f;p=platform%2Fcore%2Fcontext%2Fcontext-common.git Add util functions for creating platform paths Change-Id: I47ca401661e2b8d3ed0297fe305a5ce1e65bb3e9 Signed-off-by: Mu-Woong Lee --- diff --git a/include/PathUtil.h b/include/PathUtil.h new file mode 100644 index 0000000..88fcbde --- /dev/null +++ b/include/PathUtil.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_PATH_UTIL_H__ +#define __CONTEXT_PATH_UTIL_H__ + +#include +#include +#include + +namespace ctx { + + class EXPORT_API PathUtil { + public: + static std::string getSystemPath(enum tzplatform_variable id, const std::string& path); + static std::string getUserPath(enum tzplatform_variable id, const std::string& path); + static std::string getUserPath(uid_t uid, enum tzplatform_variable id, const std::string& path); + + private: + PathUtil(); + }; + +} + +#endif /* __CONTEXT_PATH_UTIL_H__ */ diff --git a/src/server/PathUtil.cpp b/src/server/PathUtil.cpp new file mode 100644 index 0000000..afc04e7 --- /dev/null +++ b/src/server/PathUtil.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +using namespace ctx; + +static GMutex __pathMutex; + +PathUtil::PathUtil() +{ +} + +std::string PathUtil::getSystemPath(enum tzplatform_variable id, const std::string& path) +{ + std::string outPath; + + { + ScopeMutex sm(&__pathMutex); + const char* rawPath = tzplatform_mkpath(id, path.c_str()); + if (rawPath) + outPath = rawPath; + else + _E("Path creation failed"); + } + + return outPath; +} + +std::string PathUtil::getUserPath(enum tzplatform_variable id, const std::string& path) +{ + return getUserPath(ServiceBase::getActiveUser(), id, path); +} + +std::string PathUtil::getUserPath(uid_t uid, enum tzplatform_variable id, const std::string& path) +{ + IF_FAIL_RETURN_TAG(!ClientBase::isSystemUid(uid), EMPTY_STR, _E, "Invalid UID"); + + tzplatform_context* context = NULL; + tzplatform_context_create(&context); + IF_FAIL_RETURN_TAG(context, EMPTY_STR, _E, "tzplatform_context_create() failed"); + + if (tzplatform_context_set_user(context, uid) != E_NONE) { + _E("tzplatform_context_set_user() failed"); + tzplatform_context_destroy(context); + return EMPTY_STR; + } + + std::string outPath; + + { + ScopeMutex sm(&__pathMutex); + const char* rawPath = tzplatform_context_mkpath(context, id, path.c_str()); + if (rawPath) + outPath = rawPath; + else + _E("Path creation failed"); + } + + tzplatform_context_destroy(context); + return outPath; +} diff --git a/src/server/PlatformDatabase.cpp b/src/server/PlatformDatabase.cpp index 9e02b9d..a182539 100644 --- a/src/server/PlatformDatabase.cpp +++ b/src/server/PlatformDatabase.cpp @@ -15,60 +15,23 @@ */ #include -#include -#include #include #include +#include #include #define ROOT_UID ((uid_t)0) using namespace ctx; -static GMutex __pathMutex; - -static std::string __getSystemPath(const std::string& dbName) -{ - std::string path = "." + dbName + ".db"; - - { - ScopeMutex sm(&__pathMutex); - path = tzplatform_mkpath(TZ_SYS_DB, path.c_str()); - } - - return path; -} - -static std::string __getUserPath(const std::string& dbName, uid_t uid) +static std::string __getPath(const std::string& dbName, uid_t uid) { std::string path = "." + dbName + ".db"; - tzplatform_context* context = NULL; - tzplatform_context_create(&context); - IF_FAIL_RETURN_TAG(context, "", _E, "tzplatform_context_create() failed"); - - if (tzplatform_context_set_user(context, uid) != E_NONE) { - _E("tzplatform_context_set_user() failed"); - tzplatform_context_destroy(context); - return ""; - } - - { - ScopeMutex sm(&__pathMutex); - path = tzplatform_context_mkpath(context, TZ_USER_DB, path.c_str()); - } - - tzplatform_context_destroy(context); - - return path; -} - -static std::string __getPath(const std::string& dbName, uid_t uid) -{ if (ClientBase::isSystemUid(uid)) { - return __getSystemPath(dbName); + return PathUtil::getSystemPath(TZ_SYS_DB, path); } else { - return __getUserPath(dbName, uid); + return PathUtil::getUserPath(uid, TZ_USER_DB, path); } }