Add FileLock class 35/29335/8
authorAleksander Zdyb <a.zdyb@samsung.com>
Wed, 26 Nov 2014 08:23:06 +0000 (09:23 +0100)
committerAleksander Zdyb <a.zdyb@samsung.com>
Wed, 3 Dec 2014 11:07:25 +0000 (12:07 +0100)
A class creating advisory lock on provided file.
The file must exist and be openable by the process.

Change-Id: Ia5fa4ef51e1a413beb81efd56f807e1434145691

src/common/CMakeLists.txt
src/common/exceptions/DatabaseBusyException.h [new file with mode: 0644]
src/common/exceptions/FileLockAcquiringException.h [new file with mode: 0644]
src/common/lock/FileLock.cpp [new file with mode: 0644]
src/common/lock/FileLock.h [new file with mode: 0644]
src/service/main/pointers.h

index e8974cb..a8e0632 100644 (file)
@@ -28,6 +28,7 @@ INCLUDE_DIRECTORIES(
 SET(COMMON_SOURCES
     ${COMMON_PATH}/config/PathConfig.cpp
     ${COMMON_PATH}/containers/BinaryQueue.cpp
+    ${COMMON_PATH}/lock/FileLock.cpp
     ${COMMON_PATH}/log/log.cpp
     ${COMMON_PATH}/plugin/PluginManager.cpp
     ${COMMON_PATH}/protocol/ProtocolAdmin.cpp
diff --git a/src/common/exceptions/DatabaseBusyException.h b/src/common/exceptions/DatabaseBusyException.h
new file mode 100644 (file)
index 0000000..78c90cb
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2014 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        src/common/exceptions/DatabaseBusyException.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       This file defines exception thrown when database is busy
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/DatabaseException.h>
+
+namespace Cynara {
+
+class DatabaseBusyException : public DatabaseException {
+public:
+    DatabaseBusyException() : m_message("Database busy") {};
+    virtual ~DatabaseBusyException() {};
+
+    const std::string &message(void) const {
+        return m_message;
+    }
+
+private:
+    std::string m_message;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_DATABASEBUSYEXCEPTION_H_ */
diff --git a/src/common/exceptions/FileLockAcquiringException.h b/src/common/exceptions/FileLockAcquiringException.h
new file mode 100644 (file)
index 0000000..44bc54a
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014 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        src/common/exceptions/FileLockAcquiringException.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       This file defines exception thrown when file lock cannot be acquired
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_
+
+#include <cstring>
+#include <string>
+
+#include <exceptions/Exception.h>
+
+namespace Cynara {
+
+class FileLockAcquiringException {
+public:
+    FileLockAcquiringException(const int errNumber) : m_errno(errNumber) {
+        m_message = "File lock acquiring error [" + std::to_string(errorNumber()) + "]"
+                    + " <" + errorString() + ">";
+    };
+    virtual ~FileLockAcquiringException() {};
+
+    const std::string &message(void) const {
+        return m_message;
+    }
+
+    int errorNumber(void) const {
+        return m_errno;
+    }
+
+    const std::string errorString(void) const {
+        return strerror(m_errno);
+    }
+
+private:
+    std::string m_message;
+    int m_errno;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_FILELOCKACQUIRINGEXCEPTION_H_ */
diff --git a/src/common/lock/FileLock.cpp b/src/common/lock/FileLock.cpp
new file mode 100644 (file)
index 0000000..b8bd34a
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2014 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        src/common/lock/FileLock.cpp
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       A class for acquiring and holding file lock
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <exceptions/CannotCreateFileException.h>
+#include <exceptions/FileLockAcquiringException.h>
+#include <log/log.h>
+
+#include "FileLock.h"
+
+namespace Cynara {
+
+FileLock::FileLock(const std::string &lockFilename) : m_lockFilename(lockFilename) {
+    m_fd = ::open(m_lockFilename.c_str(), O_RDONLY);
+
+    if (m_fd < 0) {
+        LOGE("Could not open lock file <%s>", lockFilename.c_str());
+        throw FileLockAcquiringException(errno);
+    }
+
+    LOGD("File lock file opened");
+}
+
+FileLock::~FileLock() {
+    ::close(m_fd);
+}
+
+bool FileLock::tryLock(void) {
+    int lock = TEMP_FAILURE_RETRY(::flock(m_fd, LOCK_EX | LOCK_NB));
+
+    if (lock == 0) {
+        LOGI("File lock acquired");
+        return true;
+    } else if (errno == EWOULDBLOCK) {
+        LOGI("File lock NOT acquired");
+        return false;
+    }
+
+    throw FileLockAcquiringException(errno);
+}
+
+void FileLock::lock(void) {
+    int lock = TEMP_FAILURE_RETRY(::flock(m_fd, LOCK_EX));
+
+    if (lock == -1)
+        throw FileLockAcquiringException(errno);
+
+    LOGI("File lock acquired");
+}
+
+} /* namespace Cynara */
diff --git a/src/common/lock/FileLock.h b/src/common/lock/FileLock.h
new file mode 100644 (file)
index 0000000..53f048f
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2014 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        src/common/lock/FileLock.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       A class for acquiring and holding file lock
+ */
+
+#ifndef SRC_COMMON_LOCK_FILELOCK_H_
+#define SRC_COMMON_LOCK_FILELOCK_H_
+
+#include <string>
+
+namespace Cynara {
+
+class FileLock {
+public:
+    explicit FileLock(const std::string &lockFilename);
+    ~FileLock();
+    bool tryLock(void);
+    void lock(void);
+
+private:
+    std::string m_lockFilename;
+    int m_fd;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_LOCK_FILELOCK_H_ */
index f32c4ab..db734ae 100644 (file)
@@ -45,6 +45,9 @@ typedef std::shared_ptr<Storage> StoragePtr;
 class StorageBackend;
 typedef std::shared_ptr<StorageBackend> StorageBackendPtr;
 
+class FileLock;
+typedef std::unique_ptr<FileLock> FileLockUniquePtr;
+
 } // namespace Cynara
 
 #endif /* SRC_SERVICE_MAIN_POINTERS_H_ */