Update wrt-installer_0.0.54
[framework/web/wrt-installer.git] / src / wrt-installer / plugin_utils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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    plugin-utils.cpp
18  * @author
19  * @version 1.0
20  * @brief   Header file for plugin util
21  */
22
23 #include "plugin_utils.h"
24 #include <dpl/semaphore.h>
25 #include <dpl/exception.h>
26 #include <dpl/log/log.h>
27 #include <dpl/wrt-dao-ro/global_config.h>
28
29 using namespace WrtDB;
30
31 namespace PluginUtils {
32 const char PLUGIN_INSTALL_SEMAPHORE[] = "/.wrt_plugin_install_lock";
33
34 static DPL::Semaphore semaphore(PLUGIN_INSTALL_SEMAPHORE);
35
36 bool lockPluginInstallation()
37 {
38     Try {
39         semaphore.Lock();
40         return true;
41     }
42     Catch(DPL::Semaphore::Exception::Base){
43         return false;
44     }
45 }
46
47 bool unlockPluginInstallation()
48 {
49     Try {
50         semaphore.Unlock();
51         return true;
52     }
53     Catch(DPL::Semaphore::Exception::Base){
54         return false;
55     }
56 }
57
58 bool checkPluginInstallationRequired()
59 {
60     std::string installRequest =
61         std::string(GlobalConfig::GetPluginInstallInitializerName());
62
63     FileState::Type installationRequest =
64         checkFile(installRequest);
65
66     switch (installationRequest) {
67         case FileState::FILE_EXISTS:
68             return true;
69         case FileState::FILE_NOT_EXISTS:
70             return false;
71         default:
72         LogWarning("Opening installation request file failed");
73         return false;
74     }
75 }
76
77 bool removeInstallationRequiredFlag()
78 {
79     std::string installRequest =
80         std::string(GlobalConfig::GetPluginInstallInitializerName());
81
82     return removeFile(installRequest);
83 }
84
85 //checks if file exists and is regular file
86 FileState::Type checkFile(const std::string& filename)
87 {
88     struct stat tmp;
89
90     if (-1 == stat(filename.c_str(), &tmp)) {
91         if (ENOENT == errno) {
92             return FileState::FILE_NOT_EXISTS;
93         }
94         return FileState::FILE_READ_DATA_ERROR;
95     } else if (!S_ISREG(tmp.st_mode)) {
96         return FileState::FILE_EXISTS_NOT_REGULAR;
97     }
98     return FileState::FILE_EXISTS;
99 }
100
101 bool removeFile(const std::string& filename)
102 {
103     if (0 != unlink(filename.c_str())) {
104         return false;
105     }
106
107     return true;
108 }
109 } //namespace PluginUtils