Update wrt-commons_0.2.53
[framework/web/wrt-commons.git] / modules / utils / src / file_utils.cpp
1 /*
2  * Copyright (c) 2011 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    file_utils.cpp
18  * @author  Bartosz Janiak (b.janiak@samsung.com)
19  * @version 1.0
20  */
21
22
23 #include <sys/stat.h>
24 #include <cerrno>
25 #include <cstring>
26 #include <cstdlib>
27 #include <dirent.h>
28 #include <unistd.h>
29
30 #include <dpl/exception.h>
31 #include <dpl/errno_string.h>
32 #include <dpl/utils/file_utils.h>
33
34 #include <dpl/wrt-dao-ro/path_builder.h>
35
36 namespace {
37 int try_mkdir(const char* path,
38         mode_t mode)
39 {
40     struct stat st;
41     int err = 0;
42
43     if (::stat(path, &st) != 0) {
44         if (::mkdir(path, mode) != 0) {
45             err = -1;
46         }
47     } else if (!S_ISDIR(st.st_mode)) {
48         errno = ENOTDIR;
49         err = -1;
50     }
51
52     return err;
53 }
54
55 int mkpath(const char* path,
56         mode_t mode)
57 {
58     char* copy = ::strdup(path);
59     if (NULL == copy) {
60         return -1;
61     }
62
63     int err = 0;
64     char* ptr = copy;
65     char* slash = NULL;
66
67     while ((0 == err) && (NULL != (slash = ::strchr(ptr, '/')))) {
68         if (slash != ptr) {
69             *slash = '\0';
70             err = try_mkdir(copy, mode);
71             *slash = '/';
72         }
73         ptr = slash + 1;
74     }
75
76     if (0 == err) {
77         err = try_mkdir(path, mode);
78     }
79
80     ::free(copy);
81     return err;
82 }
83
84 int RmNode(const char* path);
85
86 int RmDir(const char* path)
87 {
88     DIR* dir = ::opendir(path);
89     if (NULL == dir) {
90         return -1;
91     }
92
93     struct dirent* entry = NULL;
94     do {
95         errno = 0;
96         if (NULL != (entry = ::readdir(dir))) {
97             if (!::strncmp(entry->d_name, ".", 1) ||
98                 !::strncmp(entry->d_name, "..", 2)) {
99                 continue;
100             }
101             std::string fullPath = WrtDB::PathBuilder(path)
102                     .Append(entry->d_name)
103                     .GetFullPath();
104             if (RmNode(fullPath.c_str()) != 0) {
105                 int error = errno;
106                 TEMP_FAILURE_RETRY(::closedir(dir));
107                 errno = error;
108                 return -1;
109             }
110         }
111     }
112     while (NULL != entry);
113
114     int error = errno;
115     if (TEMP_FAILURE_RETRY(::closedir(dir)) != 0) {
116         return -1;
117     }
118     errno = error;
119
120     return (errno == 0 ? ::rmdir(path) : -1);
121 }
122
123 int RmNode(const char* path)
124 {
125     struct stat st;
126     if (::lstat(path, &st) != 0) {
127         return -1;
128     }
129     return (S_ISDIR(st.st_mode) ? RmDir(path) : ::unlink(path));
130 }
131 }
132
133 namespace FileUtils {
134 bool FileExists(const DPL::String& absolutePath)
135 {
136     struct stat statStruct;
137     if (stat(DPL::ToUTF8String(absolutePath).c_str(), &statStruct) == 0) {
138         return S_ISREG(statStruct.st_mode);
139     } else {
140         return false;
141     }
142 }
143
144 void MakePath(const std::string& path,
145         mode_t mode)
146 {
147     if (mkpath(path.c_str(), mode) == -1) {
148         ThrowMsg(CreateDirectoryException, "Cannot make path");
149     }
150 }
151
152 void RemoveDir(const std::string& path)
153 {
154     if (RmDir(path.c_str()) != 0) {
155         ThrowMsg(RemoveDirectoryException, DPL::GetErrnoString());
156     }
157 }
158 }