2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FIo_FileLockImpl.cpp
18 * @brief This is the implementation file for _FileLockImpl class.
24 #include <sys/types.h>
28 #include <unique_ptr.h>
30 #include <FBaseResult.h>
31 #include <FBaseSysLog.h>
33 #include <FBase_NativeError.h>
34 #include "FIo_FileImpl.h"
35 #include "FIo_FileLockImpl.h"
37 using namespace Tizen::Base;
39 namespace Tizen { namespace Io
42 _FileLockImpl::_FileLockImpl(void)
44 , __lockType(FileLockType(-1))
51 _FileLockImpl::~_FileLockImpl(void)
53 if (__pFileImpl != null)
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));
62 if (__lockType == FILE_LOCK_SHARED)
64 lock.l_type = F_RDLCK;
66 else if (__lockType == FILE_LOCK_EXCLUSIVE)
68 lock.l_type = F_WRLCK;
70 lock.l_whence = SEEK_SET;
71 lock.l_start = __offset;
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));
80 _FileLockImpl::Construct(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
82 SysTryReturnResult(NID_IO, pFileImpl != null, E_SYSTEM, "File instance is null.");
84 __pFileImpl = const_cast< _FileImpl* >(pFileImpl);
85 __lockType = lockType;
93 _FileLockImpl::GetInstance(FileLock& fileLock)
95 return fileLock.__pFileLockImpl;
99 _FileLockImpl::GetInstance(const FileLock& fileLock)
101 return fileLock.__pFileLockImpl;
105 _FileLockImpl::IsShared(void) const
107 SetLastResult(E_SUCCESS);
108 return (__lockType == FILE_LOCK_SHARED) ? true : false;
112 _FileLockImpl::IsExclusive(void) const
114 SetLastResult(E_SUCCESS);
115 return (__lockType == FILE_LOCK_EXCLUSIVE) ? true : false;
119 _FileLockImpl::IsValid(void) const
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);
129 _FileLockImpl::CreateFileLockInstanceN(const _FileImpl* pFileImpl, FileLockType lockType, int offset, int size, int pid)
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);
135 result r = pFileLockImpl->Construct(pFileImpl, lockType, offset, size, pid);
136 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
138 SetLastResult(E_SUCCESS);
139 return pFileLock.release();