2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file directory_api.cpp
18 * @author Soyoung Kim(sy037.kim@samsung.com)
20 * @brief directory api - implementation file
24 #include <directory_api.h>
29 #include <dpl/foreach.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/errno_string.h>
32 #include <installer_log.h>
34 namespace DirectoryApi {
35 bool DirectoryCopy(std::string source, std::string dest)
37 DIR* dir = opendir(source.c_str());
43 struct dirent *dEntryResult;
48 return_code = readdir_r(dir, &dEntry, &dEntryResult);
49 if (dEntryResult != NULL && return_code == 0) {
50 std::string fileName = dEntry.d_name;
51 std::string fullName = source + "/" + fileName;
53 if (stat(fullName.c_str(), &statInfo) != 0) {
58 if (S_ISDIR(statInfo.st_mode)) {
59 if (("." == fileName) || (".." == fileName)) {
62 std::string destFolder = dest + "/" + fileName;
63 WrtUtilMakeDir(destFolder);
65 if (!DirectoryCopy(fullName, destFolder)) {
71 std::string destFile = dest + "/" + fileName;
72 std::ifstream infile(fullName);
73 std::ofstream outfile(destFile);
74 outfile << infile.rdbuf();
79 if (-1 == TEMP_FAILURE_RETRY(chown(destFile.c_str(),
83 _E("Failed to change owner [%s]", DPL::GetErrnoString(error).c_str());
86 } while (dEntryResult != NULL && return_code == 0);