Fixed preinstall widget wasn't installed after factory reset
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_commons.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       task_commons.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include "task_commons.h"
23 #include <unistd.h>
24 #include <sstream>
25 #include <ftw.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <dpl/wrt-dao-ro/global_config.h>
29 #include <dpl/log/log.h>
30 #include <dpl/exception.h>
31 #include <dpl/errno_string.h>
32 #include <dpl/utils/file_utils.h>
33
34 namespace Jobs {
35 namespace WidgetInstall {
36
37 namespace {
38
39 const char * const TEMPORARY_PATH_POSTFIX = "temp";
40 const mode_t TEMPORARY_PATH_MODE = 0775;
41
42 int lambdaDeleteFile(const char *fpath,
43                      const struct stat* /*sb*/,
44                      int tflag,
45                      struct FTW* /*ftwbuf*/)
46 {
47     switch (tflag) {
48     case FTW_D:
49     case FTW_DNR:
50     case FTW_DP:
51         LogInfo("Removing old temporary directory" << fpath);
52         return rmdir(fpath);
53         break;
54     default:
55         LogInfo("Unlinking old temporary file" << fpath);
56         return unlink(fpath);
57         break;
58     }
59     return 0;
60 }
61
62 } // namespace
63
64 void removeTemporaryDir(const std::string& dir)
65 {
66     LogError("[GenerateConfig Task] Aborting... (removing temporary dir: " <<
67              dir << " )");
68
69     static const int maxDepth = 1024;
70     struct stat fileInfo;
71     if (stat(dir.c_str(), &fileInfo) == 0) {
72         nftw(dir.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH);
73     }
74 }
75
76 std::string createTempPath()
77 {
78     LogInfo("Step: Creating temporary path");
79
80    // Temporary path
81    std::ostringstream tempPathBuilder;
82
83    tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
84    tempPathBuilder << "/";
85    tempPathBuilder << TEMPORARY_PATH_POSTFIX;
86    tempPathBuilder << "_";
87
88    timeval tv;
89    gettimeofday(&tv, NULL);
90    tempPathBuilder <<
91    (static_cast<unsigned long long>(tv.tv_sec) * 1000000ULL +
92     static_cast<unsigned long long>(tv.tv_usec));
93
94    std::string tempPath = tempPathBuilder.str();
95
96    // Remove old path if any
97    struct stat fileInfo;
98
99    // FIXME: what if there are more then maxDepth recursive directories
100    static const int maxDepth = 1024;
101    if (stat(tempPath.c_str(), &fileInfo) == 0) {
102        int error = nftw(
103                tempPath.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH);
104
105        if (error == -1) {
106            ThrowMsg(DPL::CommonException::InternalError,
107                     DPL::GetErrnoString());
108        }
109    }
110    // Create new path
111    FileUtils::MakePath(tempPath, TEMPORARY_PATH_MODE);
112
113    return tempPath;
114 }
115
116 void createTempPath(const std::string& path)
117 {
118     FileUtils::MakePath(path, TEMPORARY_PATH_MODE);
119 }
120
121 } // WidgetInstall
122 } // Jobs