Initialize Tizen 2.3
[framework/web/wrt-installer.git] / src_wearable / 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/exception.h>
30 #include <dpl/errno_string.h>
31 #include <dpl/utils/wrt_utility.h>
32 #include <widget_install/widget_install_errors.h>
33 #include <installer_log.h>
34
35 namespace Jobs {
36 namespace WidgetInstall {
37 namespace {
38 const char * const TEMPORARY_PATH_POSTFIX = "temp";
39 const mode_t TEMPORARY_PATH_MODE = 0775;
40 } // namespace
41
42 std::string createTempPath(bool preload)
43 {
44     _D("Step: Creating temporary path");
45
46     // Temporary path
47     std::ostringstream tempPathBuilder;
48
49     if (preload) {
50         tempPathBuilder << WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
51     } else {
52         tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
53     }
54     tempPathBuilder << WrtDB::GlobalConfig::GetTmpDirPath();
55     tempPathBuilder << "/";
56     tempPathBuilder << TEMPORARY_PATH_POSTFIX;
57     tempPathBuilder << "_";
58
59     timeval tv;
60     gettimeofday(&tv, NULL);
61     tempPathBuilder <<
62     (static_cast<unsigned long long>(tv.tv_sec) * 1000000ULL +
63      static_cast<unsigned long long>(tv.tv_usec));
64
65     std::string tempPath = tempPathBuilder.str();
66
67     // Remove old path if any
68     struct stat fileInfo;
69
70     if (stat(tempPath.c_str(), &fileInfo) == 0) {
71         if (!WrtUtilRemove(tempPath)) {
72             ThrowMsg(Exceptions::RemovingFolderFailure,
73                      "Failed to to remove temporary directory");
74         }
75     }
76     // Create new path
77     if (!WrtUtilMakeDir(tempPath, TEMPORARY_PATH_MODE)) {
78         ThrowMsg(Exceptions::FileOperationFailed,
79                  "Failed to create temporary directory");
80     }
81
82     return tempPath;
83 }
84
85 void createTempPath(const std::string& path)
86 {
87     if (!WrtUtilMakeDir(path, TEMPORARY_PATH_MODE)) {
88         ThrowMsg(Exceptions::FileOperationFailed,
89                  "Failed to create temporary directory");
90     }
91 }
92 } // WidgetInstall
93 } // Jobs