2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_FileLockImpl.cpp
19 * @brief This is the implementation file for _FileLockImpl class.
25 #include <sys/types.h>
29 #include <unique_ptr.h>
31 #include <FBaseResult.h>
32 #include <FBaseSysLog.h>
34 #include <FBase_NativeError.h>
35 #include "FIo_FileImpl.h"
36 #include "FIo_FileLockImpl.h"
38 using namespace Tizen::Base;
40 namespace Tizen { namespace Io
43 _FileLockImpl::_FileLockImpl(void)
45 , __lockType(FileLockType(-1))
52 _FileLockImpl::~_FileLockImpl(void)
54 if (__pFileImpl != null)
56 __pFileImpl->__pFileLockImpl = null;
57 int fd = fileno(__pFileImpl->GetFilePointer());
58 struct stat64 statbuf;
59 SysTryReturnVoidResult(NID_IO, fstat64(fd, &statbuf) == 0, E_SYSTEM,
60 "[E_SYSTEM] Failed to get file status. errno: %d (%s)", errno, strerror(errno));
63 if (__lockType == FILE_LOCK_SHARED)
65 lock.l_type = F_RDLCK;
67 else if (__lockType == FILE_LOCK_EXCLUSIVE)
69 lock.l_type = F_WRLCK;
71 lock.l_whence = SEEK_SET;
72 lock.l_start = __offset;
75 SysTryReturnVoidResult(NID_IO, fcntl(fd, F_UNLCK, &lock) == 0, E_SYSTEM,
76 "[E_SYSTEM] Failed to release the lock. errno: %d (%s)", errno, strerror(errno));
81 _FileLockImpl::Construct(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
83 SysTryReturnResult(NID_IO, pFileImpl != null, E_SYSTEM, "File instance is null.");
85 __pFileImpl = const_cast< _FileImpl* >(pFileImpl);
86 __lockType = lockType;
94 _FileLockImpl::GetInstance(FileLock& fileLock)
96 return fileLock.__pFileLockImpl;
100 _FileLockImpl::GetInstance(const FileLock& fileLock)
102 return fileLock.__pFileLockImpl;
106 _FileLockImpl::IsShared(void) const
108 SetLastResult(E_SUCCESS);
109 return (__lockType == FILE_LOCK_SHARED) ? true : false;
113 _FileLockImpl::IsExclusive(void) const
115 SetLastResult(E_SUCCESS);
116 return (__lockType == FILE_LOCK_EXCLUSIVE) ? true : false;
120 _FileLockImpl::IsValid(void) const
122 struct stat64 statbuf;
123 SysTryReturn(NID_IO, __pFileImpl != null && (fstat64(fileno(__pFileImpl->GetFilePointer()), &statbuf) == 0),
124 false, E_SYSTEM, "[E_SYSTEM} File lock is invalid.");
125 SetLastResult(E_SUCCESS);
130 _FileLockImpl::CreateFileLockInstanceN(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
132 std::unique_ptr<FileLock> pFileLock(new (std::nothrow) FileLock());
133 SysTryReturn(NID_IO, pFileLock != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
134 _FileLockImpl* pFileLockImpl = _FileLockImpl::GetInstance(*pFileLock);
136 result r = pFileLockImpl->Construct(pFileImpl, lockType, offset, size, pid);
137 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
139 SetLastResult(E_SUCCESS);
140 return pFileLock.release();