Merge "Update Downloaded/Preloaded information" into tizen_2.1
[platform/framework/native/appfw.git] / src / io / FIo_NormalRegistry.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_NormalRegistry.cpp
20  * @brief       This is the implementation file for _NormalRegistry class.
21  */
22
23 #include <unistd.h>
24 #include <new>
25
26 #include <FBaseInteger.h>
27 #include <FBaseDouble.h>
28 #include <FBaseFloat.h>
29 #include <FBaseUuId.h>
30 #include <FBaseByteBuffer.h>
31 #include <FBaseColIEnumerator.h>
32 #include <FBaseUtilStringTokenizer.h>
33 #include <FBaseResult.h>
34 #include <FBaseSysLog.h>
35 #include <FIoFile.h>
36 #include <FIoRegistry.h>
37
38 #include <FBase_StringConverter.h>
39 #include <FIo_FileImpl.h>
40 #include <FIo_NormalRegistry.h>
41 #include <FIo_SecureIoUtil.h>
42
43 using namespace Tizen::Base;
44
45 namespace Tizen { namespace Io
46 {
47
48 _NormalRegistry::_NormalRegistry(void)
49 {
50 }
51
52 _NormalRegistry::~_NormalRegistry(void)
53 {
54         if (_write == true && _FileImpl::IsFileExist(_regPath) == true && _update == true)
55         {
56                 this->Flush();
57         }
58
59         _sectionList.RemoveAll(true);
60
61         delete[] _pBuffer; //clear existing buffer
62         _pBuffer = null;
63         _length = 0;
64 }
65
66 result
67 _NormalRegistry::Construct(const String& regPath, const char* pOpenMode)
68 {
69         SysAssertf(_constructed == false, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
70
71         bool isValidOpenMode = this->VerifyRegistryOpenMode(pOpenMode);
72         SysTryReturnResult(NID_IO, isValidOpenMode == true, E_INVALID_ARG, "The specified openMode is invalid. (%s)", pOpenMode);
73
74         result r = this->Load(regPath, pOpenMode);
75         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to load the registry file (%ls).",
76                         GetErrorMessage(r), regPath.GetPointer());
77
78         if (_pBuffer && _length >= SECURE_REG_HEADER_SIZE_V1)
79         {
80                 r = _SecureIoUtil::CheckSecureRegistryHeader(&_pBuffer, &_length, regPath, false);
81                 SysTryReturnResult(NID_IO, !IsFailed(r), E_INVALID_ARG, "Can not open the secure file in normal mode.");
82         }
83
84         r = this->Parse();
85         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to parse the registry file (%ls).",
86                         GetErrorMessage(r), regPath.GetPointer());
87
88         _constructed = true;
89         _regPath.Clear();
90         _regPath.Append(regPath);
91
92         return r;
93 }
94
95 result
96 _NormalRegistry::Flush(void)
97 {
98         result r = this->PrepareToWrite();
99         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to prepare to write.", GetErrorMessage(r));
100
101         r = this->Write();
102         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write registry file.", GetErrorMessage(r));
103
104         return r;
105 }
106
107 }} // Tizen::Io
108