[Release] wrt-installer_0.1.123
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / directory_api.cpp
1 /*
2  * Copyright (c) 2012 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    directory_api.cpp
18  * @author  Soyoung Kim(sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   directory api - implementation file
21  */
22
23 #include <directory_api.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <fstream>
28 #include <dpl/log/log.h>
29 #include <dpl/foreach.h>
30 #include <dpl/utils/wrt_utility.h>
31
32 namespace DirectoryApi {
33 bool DirectoryCopy(std::string source, std::string dest)
34 {
35     DIR* dir = opendir(source.c_str());
36     if (NULL == dir) {
37         return false;
38     }
39
40     struct dirent dEntry;
41     struct dirent *dEntryResult;
42     int return_code;
43
44     do {
45         struct stat statInfo;
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;
50
51             if (stat(fullName.c_str(), &statInfo) != 0) {
52                 closedir(dir);
53                 return false;
54             }
55
56             if (S_ISDIR(statInfo.st_mode)) {
57                 if (("." == fileName) || (".." == fileName)) {
58                     continue;
59                 }
60                 std::string destFolder = dest + "/" + fileName;
61                 WrtUtilMakeDir(destFolder);
62
63                 if (!DirectoryCopy(fullName, destFolder)) {
64                     closedir(dir);
65                     return false;
66                 }
67             }
68
69             std::string destFile = dest + "/" + fileName;
70             std::ifstream infile(fullName);
71             std::ofstream outfile(destFile);
72             outfile << infile.rdbuf();
73             outfile.close();
74             infile.close();
75
76             chown(destFile.c_str(), statInfo.st_uid, statInfo.st_gid);
77         }
78     } while (dEntryResult != NULL && return_code == 0);
79     closedir(dir);
80     return true;
81 }
82 }