Add util functions for creating platform paths 65/118665/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 13 Mar 2017 12:51:02 +0000 (21:51 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 13 Mar 2017 12:51:02 +0000 (21:51 +0900)
Change-Id: I47ca401661e2b8d3ed0297fe305a5ce1e65bb3e9
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/PathUtil.h [new file with mode: 0644]
src/server/PathUtil.cpp [new file with mode: 0644]
src/server/PlatformDatabase.cpp

diff --git a/include/PathUtil.h b/include/PathUtil.h
new file mode 100644 (file)
index 0000000..88fcbde
--- /dev/null
@@ -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 <string>
+#include <tzplatform_config.h>
+#include <ContextTypes.h>
+
+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 (file)
index 0000000..afc04e7
--- /dev/null
@@ -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 <ScopeMutex.h>
+#include <ClientBase.h>
+#include <ServiceBase.h>
+#include <PathUtil.h>
+
+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;
+}
index 9e02b9d..a182539 100644 (file)
  */
 
 #include <sqlite3.h>
-#include <tzplatform_config.h>
-#include <ScopeMutex.h>
 #include <Tuple.h>
 #include <ClientBase.h>
+#include <PathUtil.h>
 #include <PlatformDatabase.h>
 
 #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);
        }
 }