[Release] wrt-installer_0.1.53
[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 <dirent.h>
26 #include <fstream>
27 #include <dpl/log/log.h>
28 #include <dpl/foreach.h>
29 #include <dpl/utils/wrt_utility.h>
30
31 namespace DirectoryApi {
32 bool DirectoryCopy(std::string source, std::string dest)
33 {
34     DIR* dir = opendir(source.c_str());
35     if (NULL == dir) {
36         return false;
37     }
38
39     struct dirent dEntry;
40     struct dirent *dEntryResult;
41     int return_code;
42
43     do {
44         struct stat statInfo;
45         return_code = readdir_r(dir, &dEntry, &dEntryResult);
46         if (dEntryResult != NULL && return_code == 0) {
47             std::string fileName = dEntry.d_name;
48             std::string fullName = source + "/" + fileName;
49
50             if (stat(fullName.c_str(), &statInfo) != 0) {
51                 closedir(dir);
52                 return false;
53             }
54
55             if (S_ISDIR(statInfo.st_mode)) {
56                 if (("." == fileName) || (".." == fileName)) {
57                     continue;
58                 }
59                 std::string destFolder = dest + "/" + fileName;
60                 WrtUtilMakeDir(destFolder);
61
62                 if (!DirectoryCopy(fullName, destFolder)) {
63                     closedir(dir);
64                     return false;
65                 }
66             }
67
68             std::string destFile = dest + "/" + fileName;
69             std::ifstream infile(fullName);
70             std::ofstream outfile(destFile);
71             outfile << infile.rdbuf();
72             outfile.close();
73             infile.close();
74         }
75     } while (dEntryResult != NULL && return_code == 0);
76     closedir(dir);
77     return true;
78 }
79 }