Make the user path creation function atomic 87/135687/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 26 Jun 2017 01:10:33 +0000 (10:10 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 26 Jun 2017 05:01:41 +0000 (05:01 +0000)
Change-Id: I2ceb68ccb4bc13d1a8a96c30ef14b871e2111725
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/ServerUtil.cpp

index 86db4b1..5c62463 100644 (file)
@@ -47,18 +47,12 @@ EXPORT_API bool util::isSystemUid(uid_t uid)
 
 EXPORT_API std::string util::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");
-       }
+       ScopeMutex sm(&__pathMutex);
 
-       return outPath;
+       const char* rawPath = tzplatform_mkpath(id, path.c_str());
+       IF_FAIL_RETURN_TAG(rawPath, EMPTY_STR, _E, "Path creation failed");
+
+       return std::string(rawPath);
 }
 
 EXPORT_API std::string util::getUserPath(enum tzplatform_variable id, const std::string& path)
@@ -70,6 +64,8 @@ EXPORT_API std::string util::getUserPath(uid_t uid, enum tzplatform_variable id,
 {
        IF_FAIL_RETURN_TAG(!util::isSystemUid(uid), EMPTY_STR, _E, "Invalid UID");
 
+       ScopeMutex sm(&__pathMutex);
+
        tzplatform_context* context = NULL;
        tzplatform_context_create(&context);
        IF_FAIL_RETURN_TAG(context, EMPTY_STR, _E, "tzplatform_context_create() failed");
@@ -80,18 +76,18 @@ EXPORT_API std::string util::getUserPath(uid_t uid, enum tzplatform_variable id,
                return EMPTY_STR;
        }
 
-       std::string outPath;
+       const char* rawPath = tzplatform_context_mkpath(context, id, path.c_str());
 
-       {
-               ScopeMutex sm(&__pathMutex);
-               const char* rawPath = tzplatform_context_mkpath(context, id, path.c_str());
-               if (rawPath)
-                       outPath = rawPath;
-               else
-                       _E("Path creation failed");
+       if (!rawPath) {
+               _E("Path creation failed");
+               tzplatform_context_destroy(context);
+               return EMPTY_STR;
        }
 
+       std::string outPath(rawPath);
+
        tzplatform_context_destroy(context);
+
        return outPath;
 }