Merge "Remove unused code." into tizen_2.2
[platform/framework/native/appfw.git] / src / io / FIo_FileLockImpl.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 /**
17  * @file        FIo_FileLockImpl.cpp
18  * @brief       This is the implementation file for _FileLockImpl class.
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <new>
28 #include <unique_ptr.h>
29
30 #include <FBaseResult.h>
31 #include <FBaseSysLog.h>
32
33 #include <FBase_NativeError.h>
34 #include "FIo_FileImpl.h"
35 #include "FIo_FileLockImpl.h"
36
37 using namespace Tizen::Base;
38
39 namespace Tizen { namespace Io
40 {
41
42 _FileLockImpl::_FileLockImpl(void)
43         : __pFileImpl(null)
44         , __lockType(FileLockType(-1))
45         , __offset(-1)
46         , __size(-1)
47         , __pid(-1)
48 {
49 }
50
51 _FileLockImpl::~_FileLockImpl(void)
52 {
53         if (__pFileImpl != null)
54         {
55                 __pFileImpl->__pFileLockImpl = null;
56                 int fd = fileno(__pFileImpl->GetFilePointer());
57                 struct stat64 statbuf;
58                 SysTryReturnVoidResult(NID_IO, fstat64(fd, &statbuf) == 0, E_SYSTEM,
59                                 "[E_SYSTEM] Failed to get file status. errno: %d (%s)", errno, strerror(errno));
60                 // release the lock
61                 struct flock lock;
62                 if (__lockType == FILE_LOCK_SHARED)
63                 {
64                         lock.l_type = F_RDLCK;
65                 }
66                 else if (__lockType == FILE_LOCK_EXCLUSIVE)
67                 {
68                         lock.l_type = F_WRLCK;
69                 }
70                 lock.l_whence = SEEK_SET;
71                 lock.l_start = __offset;
72                 lock.l_len = __size;
73                 lock.l_pid = __pid;
74                 SysTryReturnVoidResult(NID_IO, fcntl(fd, F_UNLCK, &lock) == 0, E_SYSTEM,
75                                 "[E_SYSTEM] Failed to release the lock. errno: %d (%s)", errno, strerror(errno));
76         }
77 }
78
79 result
80 _FileLockImpl::Construct(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
81 {
82         SysTryReturnResult(NID_IO, pFileImpl != null, E_SYSTEM, "File instance is null.");
83
84         __pFileImpl = const_cast< _FileImpl* >(pFileImpl);
85         __lockType = lockType;
86         __offset = offset;
87         __size = size;
88         __pid = pid;
89         return E_SUCCESS;
90 }
91
92 _FileLockImpl*
93 _FileLockImpl::GetInstance(FileLock& fileLock)
94 {
95         return fileLock.__pFileLockImpl;
96 }
97
98 const _FileLockImpl*
99 _FileLockImpl::GetInstance(const FileLock& fileLock)
100 {
101         return fileLock.__pFileLockImpl;
102 }
103
104 bool
105 _FileLockImpl::IsShared(void) const
106 {
107         SetLastResult(E_SUCCESS);
108         return (__lockType == FILE_LOCK_SHARED) ? true : false;
109 }
110
111 bool
112 _FileLockImpl::IsExclusive(void) const
113 {
114         SetLastResult(E_SUCCESS);
115         return (__lockType == FILE_LOCK_EXCLUSIVE) ? true : false;
116 }
117
118 bool
119 _FileLockImpl::IsValid(void) const
120 {
121         struct stat64 statbuf;
122         SysTryReturn(NID_IO, __pFileImpl != null && (fstat64(fileno(__pFileImpl->GetFilePointer()), &statbuf) == 0),
123                         false, E_SYSTEM, "[E_SYSTEM} File lock is invalid.");
124         SetLastResult(E_SUCCESS);
125         return true;
126 }
127
128 FileLock*
129 _FileLockImpl::CreateFileLockInstanceN(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
130 {
131         std::unique_ptr<FileLock> pFileLock(new (std::nothrow) FileLock());
132         SysTryReturn(NID_IO, pFileLock != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
133         _FileLockImpl* pFileLockImpl = _FileLockImpl::GetInstance(*pFileLock);
134
135         result r = pFileLockImpl->Construct(pFileImpl, lockType, offset, size, pid);
136         SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
137
138         SetLastResult(E_SUCCESS);
139         return pFileLock.release();
140 }
141
142 }} // Tizen::Io