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