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
23 #include <directory_api.h>
28 #include <dpl/log/log.h>
29 #include <dpl/foreach.h>
30 #include <dpl/utils/wrt_utility.h>
32 namespace DirectoryApi {
33 bool DirectoryCopy(std::string source, std::string dest)
35 DIR* dir = opendir(source.c_str());
41 struct dirent *dEntryResult;
46 return_code = readdir_r(dir, &dEntry, &dEntryResult);
47 if (dEntryResult != NULL && return_code == 0) {
48 std::string fileName = dEntry.d_name;
49 std::string fullName = source + "/" + fileName;
51 if (stat(fullName.c_str(), &statInfo) != 0) {
56 if (S_ISDIR(statInfo.st_mode)) {
57 if (("." == fileName) || (".." == fileName)) {
60 std::string destFolder = dest + "/" + fileName;
61 WrtUtilMakeDir(destFolder);
63 if (!DirectoryCopy(fullName, destFolder)) {
69 std::string destFile = dest + "/" + fileName;
70 std::ifstream infile(fullName);
71 std::ofstream outfile(destFile);
72 outfile << infile.rdbuf();
76 chown(destFile.c_str(), statInfo.st_uid, statInfo.st_gid);
78 } while (dEntryResult != NULL && return_code == 0);