Modify AppRegistry for multi-process
authorHyunbin Lee <hyunbin.lee@samsung.com>
Fri, 9 Aug 2013 06:55:49 +0000 (15:55 +0900)
committerHyunbin Lee <hyunbin.lee@samsung.com>
Wed, 14 Aug 2013 05:11:37 +0000 (14:11 +0900)
Change-Id: I542d56149d3e3df163f08fe680a1c8392492b9cc
Signed-off-by: Hyunbin Lee <hyunbin.lee@samsung.com>
src/app/FApp_AppRegistryImpl.cpp
src/app/FApp_AppRegistryImpl.h
src/io/FIo_FileUtil.cpp
src/io/FIo_RegistryCore.cpp

index 96db27e..1d17ea0 100644 (file)
  * @brief      This is the implementation for the _AppRegistryImpl class.
  */
 
-#include <FAppAppRegistry.h>
+#include <FBaseSysLog.h>
+#include <FIoFile.h>
+#include <FIoFileAttributes.h>
 #include <FIoRegistry.h>
+#include "FAppAppRegistry.h"
 
-#include <FBaseSysLog.h>
 #include "FApp_AppRegistryImpl.h"
 #include "FApp_AppInfo.h"
 
@@ -32,227 +34,190 @@ using namespace Tizen::Base;
 namespace Tizen { namespace App
 {
 
-
 _AppRegistryImpl::_AppRegistryImpl(void)
        : __pRegistry(null)
        , __sectionName(L"__ApplicationStates")
 {
 }
 
-
 _AppRegistryImpl::~_AppRegistryImpl(void)
 {
-       delete __pRegistry;
 }
 
-
 result
 _AppRegistryImpl::Construct(void)
 {
-       result r = E_SUCCESS;
-
-       SysAssertf(__pRegistry == null,
-                       "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
-
        const String& packageId = _AppInfo::GetPackageId();
-       String regName = _AppInfo::GetAppRootPath() + L"data/";
-
        SysAssertf(!packageId.IsEmpty(), "Empty package.");
-       r = regName.Append(packageId);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] String appending has failed.", GetErrorMessage(r));
-
-       __pRegistry = new (std::nothrow) Registry();
-       SysTryCatch(NID_APP, __pRegistry != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
-                          "[E_OUT_OF_MEMORY] Failed while instantiating registry.");
-
-       r = __pRegistry->Construct(regName, true);
-       SysTryCatch(NID_APP, !IsFailed(r), , r, "[%ls] Constructing the registry %s has failed.", regName.GetPointer(),
-                          GetErrorMessage(r));
-
-       r = __pRegistry->AddSection(__sectionName);
-       if (r == E_SECTION_ALREADY_EXIST)
-       {
-               // section may exist already
-               r = E_SUCCESS;
-       }
-
-       SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding section to registry has failed.", GetErrorMessage(r));
 
-       return r;
-
-CATCH:
-       delete __pRegistry;
-       __pRegistry = null;
+       __regPath = _AppInfo::GetAppRootPath() + L"data/";
+       result r = __regPath.Append(packageId);
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
 
        SysLog(NID_APP, "Exit.");
-
        return r;
 }
 
-
 result
 _AppRegistryImpl::Add(const String& key, const String& value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->AddValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Add(const String& key, int value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->AddValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Add(const String& key, double value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->AddValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Set(const String& key, const String& value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->SetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Set(const String& key, int value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->SetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Set(const String& key, double value)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->SetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Save(void)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
-
-       r = __pRegistry->Flush();
-
-       SysTryLog(NID_APP, !IsFailed(r), "[%s] Saving value to the registry has failed.", GetErrorMessage(r));
-
-       return r;
+    return E_SUCCESS;
 }
 
-
 result
 _AppRegistryImpl::Remove(const String& key)
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->RemoveValue(__sectionName, key);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Get(const String& key, String& value) const
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->GetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Get(const String& key, int& value) const
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->GetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
-
 result
 _AppRegistryImpl::Get(const String& key, double& value) const
 {
-       SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
-
-       result r = E_SUCCESS;
+       result r = Load();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
 
        r = __pRegistry->GetValue(__sectionName, key, value);
-
        SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
 
+       Unload();
        return r;
 }
 
+result
+_AppRegistryImpl::Load(void) const
+{
+       __pRegistry = new (std::nothrow) Registry();
+       SysTryReturnResult(NID_APP, __pRegistry != null, E_OUT_OF_MEMORY, "Instantiating registry has failed.");
+
+       result r = __pRegistry->Construct(__regPath, "a+");
+       SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed.", __regPath.GetPointer());
+
+       r = __pRegistry->AddSection(__sectionName);
+       if (r == E_SECTION_ALREADY_EXIST)
+       {
+               r = E_SUCCESS;
+       }
+       SysAssertf(!IsFailed(r), "Adding section to registry has failed.");
+
+       return E_SUCCESS;
+}
+
+void
+_AppRegistryImpl::Unload(void) const
+{
+       delete __pRegistry;
+}
+
+}} // Tizen::App
 
-} } // Tizen::App
index 99a25ca..7c22cfb 100644 (file)
@@ -54,7 +54,7 @@ public:
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A string value
-        * @exception   E_SUCCESS                               The method was successful.
+        * @exception   E_SUCCESS                               The method is successful.
         * @exception   E_INVALID_ARG                   The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_ALREADY_EXIST             The key has already been used in the application preferences.
         * @exception   E_OUT_OF_MEMORY                 Insufficient memory.
@@ -69,7 +69,7 @@ public:
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   An integer value
-        * @exception   E_SUCCESS                               The method was successful.
+        * @exception   E_SUCCESS                               The method is successful.
         * @exception   E_INVALID_ARG                   The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_ALREADY_EXIST             The key has already been used in the application preferences.
         * @exception   E_OUT_OF_MEMORY                 Insufficient memory.
@@ -84,7 +84,7 @@ public:
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A floating point value
-        * @exception   E_SUCCESS                               The method was successful.
+        * @exception   E_SUCCESS                               The method is successful.
         * @exception   E_INVALID_ARG                   The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_ALREADY_EXIST             The key has already been used in the application preferences.
         * @exception   E_OUT_OF_MEMORY                 Insufficient memory.
@@ -99,7 +99,7 @@ public:
         * @return      An error code
         * @param[in]   key             A key corresponding to the value
         * @param[in]   value   A string value
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         * @exception   E_OUT_OF_MEMORY         Insufficient memory.
@@ -114,7 +114,7 @@ public:
         * @return      An error code
         * @param[in]   key     A key corresponding to the value
         * @param[in]   value   An integer value
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         * @exception   E_OUT_OF_MEMORY         Insufficient memory.
@@ -129,7 +129,7 @@ public:
         * @return      An error code
         * @param[in]   key     A key corresponding to the value
         * @param[in]   value   A floating point value
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         * @exception   E_OUT_OF_MEMORY         Insufficient memory.
@@ -143,7 +143,7 @@ public:
         *
         * @since               1.0
         * @return      An error code
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         */
        result Save(void);
 
@@ -153,7 +153,7 @@ public:
         * @since               1.0
         * @return      An error code
         * @param[in]   key             The key of the value to be removed
-        * @exception   E_SUCCESS                               The method was successful.
+        * @exception   E_SUCCESS                               The method is successful.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         */
        result Remove(const Tizen::Base::String& key);
@@ -165,7 +165,7 @@ public:
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   A string value to be retrieved
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         */
@@ -178,7 +178,7 @@ public:
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   An integer value to be retrieved
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         */
@@ -191,7 +191,7 @@ public:
         * @return      An error code
         * @param[in]   key             The key of the value to retrieve
         * @param[out]  value   A floating point value to be retrieved
-        * @exception   E_SUCCESS                       The method was successful.
+        * @exception   E_SUCCESS                       The method is successful.
         * @exception   E_INVALID_ARG           The length of the specified string for a section or entry is smaller than or equal to zero. Or, a @c null value or string with '\0' and '\n' was passed.
         * @exception   E_KEY_NOT_FOUND         The specified key was not used in the application preferences.
         */
@@ -216,9 +216,8 @@ private:
         * Allocates memory for an _AppRegistryImpl object.
         *
         * @since       1.0
-        *
         * @return      An error code
-        * @exception   E_SUCCESS       The method was successful.
+        * @exception   E_SUCCESS       The method is successful.
         */
        result Construct(void);
 
@@ -227,9 +226,26 @@ private:
         */
        _AppRegistryImpl& operator =(const _AppRegistryImpl& source);
 
+       /**
+        * Loads the registry file.
+        *
+        * @since               2.2
+        * @return              An error code
+        * @exception   E_SUCCESS       The method is successful.
+        */
+       result Load(void) const;
+
+       /*
+        * Unloads the registry file.
+        *
+        * @since               2.2
+        */
+       void Unload(void) const;
+
 private:
-       Tizen::Io::Registry* __pRegistry;
+       mutable Tizen::Io::Registry* __pRegistry;
        const Tizen::Base::String __sectionName;
+       Tizen::Base::String __regPath;
 
        friend class AppRegistry;
 }; // _AppRegistryImpl
index 7b869e3..fd426c3 100644 (file)
@@ -330,10 +330,11 @@ _FileUtil::GetAttributes(const String& filePath, FileAttributes& attribute)
                           GetErrorMessage(GetLastResult()));
 
        struct stat64 statbuf;
-       if (int ret = stat64(pFilePath.get(), &statbuf) == -1)
+       if (stat64(pFilePath.get(), &statbuf) == -1)
        {
                r = __ConvertNativeErrorToResult(errno);
-               SysSecureLogException(NID_IO, r, "[%s] Failed to get file (%s) status.", GetErrorMessage(r), pFilePath.get());
+               SysSecureLogException(NID_IO, r, "[%s] Failed to get file (%s) status. errno: %d (%s)",
+                               GetErrorMessage(r), pFilePath.get(), errno, strerror(errno));
                return r;
        }
 
index 2c916bb..03882c8 100644 (file)
@@ -992,20 +992,16 @@ _RegistryCore::Write(void)
                return E_SUCCESS;
        }
 
-       result r = E_SUCCESS;
-
-       String openMode(L"w+");
-       unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
-       SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+       result r = __pFileImpl->Seek(FILESEEKPOSITION_BEGIN, 0);
+       SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       //TODO: Secure mode is to be handled
-       r = pFileImpl->Construct(_regPath, "w+", null);
+       r = __pFileImpl->Truncate(0);
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       r = pFileImpl->Write(_pBuffer, _length);
+       r = __pFileImpl->Write(_pBuffer, _length);
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
-       r = pFileImpl->Flush();
+       r = __pFileImpl->Flush();
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
        return r;