Initialize Tizen 2.3
[framework/web/wrt-installer.git] / src_mobile / 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 <cerrno>
24 #include <directory_api.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <fstream>
29 #include <dpl/foreach.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/errno_string.h>
32 #include <installer_log.h>
33
34 namespace DirectoryApi {
35 bool DirectoryCopy(std::string source, std::string dest)
36 {
37     DIR* dir = opendir(source.c_str());
38     if (NULL == dir) {
39         return false;
40     }
41
42     struct dirent dEntry;
43     struct dirent *dEntryResult;
44     int return_code;
45
46     do {
47         struct stat statInfo;
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;
52
53             if (stat(fullName.c_str(), &statInfo) != 0) {
54                 closedir(dir);
55                 return false;
56             }
57
58             if (S_ISDIR(statInfo.st_mode)) {
59                 if (("." == fileName) || (".." == fileName)) {
60                     continue;
61                 }
62                 std::string destFolder = dest + "/" + fileName;
63                 WrtUtilMakeDir(destFolder);
64
65                 if (!DirectoryCopy(fullName, destFolder)) {
66                     closedir(dir);
67                     return false;
68                 }
69             }
70
71             std::string destFile = dest + "/" + fileName;
72             std::ifstream infile(fullName);
73             std::ofstream outfile(destFile);
74             outfile << infile.rdbuf();
75             outfile.close();
76             infile.close();
77
78             errno = 0;
79             if (-1 == TEMP_FAILURE_RETRY(chown(destFile.c_str(),
80                                                statInfo.st_uid,
81                                                statInfo.st_gid))) {
82                 int error = errno;
83                 _E("Failed to change owner [%s]", DPL::GetErrnoString(error).c_str());
84             }
85         }
86     } while (dEntryResult != NULL && return_code == 0);
87     closedir(dir);
88     return true;
89 }
90 }