/* * Copyright (c) 2011 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 task_commons.cpp * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com) * @version 1.0 */ #include "task_commons.h" #include #include #include #include #include #include #include #include #include #include namespace Jobs { namespace WidgetInstall { namespace { const char * const TEMPORARY_PATH_POSTFIX = "temp"; const mode_t TEMPORARY_PATH_MODE = 0775; int lambdaDeleteFile(const char *fpath, const struct stat* /*sb*/, int tflag, struct FTW* /*ftwbuf*/) { switch (tflag) { case FTW_D: case FTW_DNR: case FTW_DP: LogInfo("Removing old temporary directory" << fpath); return rmdir(fpath); break; default: LogInfo("Unlinking old temporary file" << fpath); return unlink(fpath); break; } return 0; } } // namespace void removeTemporaryDir(const std::string& dir) { LogError("[GenerateConfig Task] Aborting... (removing temporary dir: " << dir << " )"); static const int maxDepth = 1024; struct stat fileInfo; if (stat(dir.c_str(), &fileInfo) == 0) { nftw(dir.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH); } } std::string createTempPath() { LogInfo("Step: Creating temporary path"); // Temporary path std::ostringstream tempPathBuilder; tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath(); tempPathBuilder << "/"; tempPathBuilder << "widget"; tempPathBuilder << "/"; tempPathBuilder << TEMPORARY_PATH_POSTFIX; tempPathBuilder << "_"; timeval tv; gettimeofday(&tv, NULL); tempPathBuilder << (static_cast(tv.tv_sec) * 1000000ULL + static_cast(tv.tv_usec)); std::string tempPath = tempPathBuilder.str(); // Remove old path if any struct stat fileInfo; // FIXME: what if there are more then maxDepth recursive directories static const int maxDepth = 1024; if (stat(tempPath.c_str(), &fileInfo) == 0) { int error = nftw( tempPath.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH); if (error == -1) { ThrowMsg(DPL::CommonException::InternalError, DPL::GetErrnoString()); } } // Create new path FileUtils::MakePath(tempPath, TEMPORARY_PATH_MODE); return tempPath; } void createTempPath(const std::string& path) { FileUtils::MakePath(path, TEMPORARY_PATH_MODE); } } // WidgetInstall } // Jobs