From 32f6ed3d788fc6f811111607261e255741675cb1 Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Thu, 24 Oct 2013 11:45:19 +0200 Subject: [PATCH] [DPL] FileLock [Issue#] N_SE-55833 [Problem] There is no file locking wrapper [Cause] This is needs in depending repos. [Solution] Introduce file lock. [Verification] N/A - build repository, - verification should be done by reviewers based on wrt-installer changes, Change-Id: I27cdfb5ea168b62ad5430585785599f3e65c53e1 --- modules/core/config.cmake | 2 + modules/core/include/dpl/file_lock.h | 57 ++++++++++++++++++++ modules/core/src/file_lock.cpp | 102 +++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 modules/core/include/dpl/file_lock.h create mode 100644 modules/core/src/file_lock.cpp diff --git a/modules/core/config.cmake b/modules/core/config.cmake index 1a8625e..6e665d4 100644 --- a/modules/core/config.cmake +++ b/modules/core/config.cmake @@ -36,6 +36,7 @@ SET(DPL_CORE_SOURCES ${PROJECT_SOURCE_DIR}/modules/core/src/exception.cpp ${PROJECT_SOURCE_DIR}/modules/core/src/fast_delegate.cpp ${PROJECT_SOURCE_DIR}/modules/core/src/file_input.cpp + ${PROJECT_SOURCE_DIR}/modules/core/src/file_lock.cpp ${PROJECT_SOURCE_DIR}/modules/core/src/file_output.cpp ${PROJECT_SOURCE_DIR}/modules/core/src/lexical_cast.cpp ${PROJECT_SOURCE_DIR}/modules/core/src/mutable_task_list.cpp @@ -95,6 +96,7 @@ SET(DPL_CORE_HEADERS ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/exception.h ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/fast_delegate.h ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/file_input.h + ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/file_lock.h ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/file_output.h ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/foreach.h ${PROJECT_SOURCE_DIR}/modules/core/include/dpl/generic_event.h diff --git a/modules/core/include/dpl/file_lock.h b/modules/core/include/dpl/file_lock.h new file mode 100644 index 0000000..4771d89 --- /dev/null +++ b/modules/core/include/dpl/file_lock.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file file_lock.h + * @author Tomasz Iwanek (t.iwanek@samsung.com) + * @version 1.0 + * @brief This implementation fo file locking + */ +#ifndef DPL_FILE_LOCK_H +#define DPL_FILE_LOCK_H + +#include +#include + +#include +#include + +namespace DPL { +class FileLock : private Noncopyable +{ + protected: + int m_fd; + std::string m_filename; + size_t m_mode; + + public: + FileLock(const std::string & filename, bool blocking = true, size_t mode = 0666); + ~FileLock(); + + bool lock(bool blocking = true); + bool unlock(); + bool isHoldingLock() const; + int getFd() const; +}; + +class FileBasedMutex : public FileLock +{ +public: + FileBasedMutex(const std::string & id, bool blocking = true); +}; + +} // namespace DPL + +#endif // DPL_FILE_LOCK_H diff --git a/modules/core/src/file_lock.cpp b/modules/core/src/file_lock.cpp new file mode 100644 index 0000000..0106df4 --- /dev/null +++ b/modules/core/src/file_lock.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file file_input.cpp + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of named input pipe + */ + +#include + +#include + +namespace DPL { + +FileLock::FileLock(const std::string & filename, bool blocking, size_t mode) : m_filename(filename), m_mode(mode) +{ + lock(blocking); +} + +FileLock::~FileLock() +{ + if(isHoldingLock()) + { + unlock(); + } +} + +bool FileLock::lock(bool blocking) +{ + if(isHoldingLock()) return true; + + int ret = 0; + + m_fd = open(m_filename.c_str(), O_RDONLY | O_CREAT, m_mode); + + if (m_fd == -1) { + return false; + } + + if(blocking) + { + ret = flock(m_fd, LOCK_EX); + } + else + { + ret = flock(m_fd, LOCK_EX | LOCK_NB); + } + + if (ret == -1) { + close(m_fd); + m_fd = -1; + return false; + } + + return true; +} + +bool FileLock::unlock() +{ + if(!isHoldingLock()) return true; + + int ret = 0; + ret = flock(m_fd, LOCK_UN); + if(ret == -1) + { + return false; + } + close(m_fd); + m_fd = -1; + return true; +} + +bool FileLock::isHoldingLock() const +{ + return (m_fd > 0); +} + +int FileLock::getFd() const +{ + return m_fd; +} + +FileBasedMutex::FileBasedMutex(const std::string & id, bool blocking) : FileLock(std::string("/tmp/.dpl_lock.") + id, blocking) +{ +} + +} // namespace DPL + -- 2.7.4