summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_file/ecore_file.c
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2009-04-11 07:10:12 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2009-04-11 07:10:12 +0000
commiteab4fea0304679a050a5cffc05867f61bdf4d8f3 (patch)
tree7a7a837275082bbb561c5e6f1e3db439fa9e2e8c /legacy/ecore/src/lib/ecore_file/ecore_file.c
parent33183982b0783b33ae623542cc17c99eb349277e (diff)
move batch directory creation to ecore_file.
as suggested by raster, this could be abstracted into ecore-file and other applications could use it as well. SVN revision: 39965
Diffstat (limited to '')
-rw-r--r--legacy/ecore/src/lib/ecore_file/ecore_file.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/legacy/ecore/src/lib/ecore_file/ecore_file.c b/legacy/ecore/src/lib/ecore_file/ecore_file.c
index 60b476c8a0..a34e004a93 100644
--- a/legacy/ecore/src/lib/ecore_file/ecore_file.c
+++ b/legacy/ecore/src/lib/ecore_file/ecore_file.c
@@ -6,6 +6,7 @@
6# include <config.h> 6# include <config.h>
7#endif 7#endif
8 8
9#include <Ecore_Str.h>
9#include <stdio.h> 10#include <stdio.h>
10#include <string.h> 11#include <string.h>
11#include <sys/types.h> 12#include <sys/types.h>
@@ -174,6 +175,100 @@ ecore_file_mkdirs(const char **dirs)
174} 175}
175 176
176/** 177/**
178 * Create complete list of sub-directories in a batch (optimized).
179 *
180 * @param base the base directory to act on, will be created if does
181 * not exists.
182 * @param subdirs list of directories, null terminated. These are
183 * created similarly to ecore_file_mkdir(), so same mode and whole
184 * path to that point must exists. So if creating base/a/b/c,
185 * provide subdirs with "a", "a/b" and "a/b/c" in that order!
186 *
187 * @return number of successfull directories created, -1 if subdirs or
188 * base is NULL or invalid.
189 *
190 * @see ecore_file_mkdir() and ecore_file_mkpaths()
191 */
192EAPI int
193ecore_file_mksubdirs(const char *base, const char **subdirs)
194{
195#ifndef HAVE_ATFILE_SOURCE
196 char buf[PATH_MAX];
197 int baselen;
198#else
199 int fd;
200 DIR *dir;
201#endif
202 int i;
203
204 if (!subdirs) return -1;
205 if ((!base) || (base[0] == '\0')) return -1;
206
207 if ((!ecore_file_is_dir(base)) && (!ecore_file_mkpath(base)))
208 return 0;
209
210#ifndef HAVE_ATFILE_SOURCE
211 baselen = ecore_strlcpy(buf, base, sizeof(buf));
212 if ((baselen < 1) || (baselen + 1 >= (int)sizeof(buf)))
213 return 0;
214
215 if (buf[baselen - 1] != '/')
216 {
217 buf[baselen] = '/';
218 baselen++;
219 }
220#else
221 dir = opendir(base);
222 if (!dir)
223 return 0;
224 fd = dirfd(dir);
225#endif
226
227 i = 0;
228 for (; *subdirs != NULL; subdirs++)
229 {
230 struct stat st;
231
232#ifndef HAVE_ATFILE_SOURCE
233 ecore_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen);
234 printf("no atfile: %s\n", buf);
235 if (stat(buf, &st) == 0)
236#else
237 printf("atfile: %s/%s\n", base, *subdirs);
238 if (fstatat(fd, *subdirs, &st, 0) == 0)
239#endif
240 {
241 if (S_ISDIR(st.st_mode))
242 {
243 i++;
244 continue;
245 }
246 }
247 else
248 {
249 if (errno == ENOENT)
250 {
251#ifndef HAVE_ATFILE_SOURCE
252 if (mkdir(buf, default_mode) == 0)
253#else
254 if (mkdirat(fd, *subdirs, default_mode) == 0)
255#endif
256 {
257 i++;
258 continue;
259 }
260 }
261 }
262 }
263
264#ifdef HAVE_ATFILE_SOURCE
265 closedir(dir);
266#endif
267
268 return i;
269}
270
271/**
177 * Delete the given dir 272 * Delete the given dir
178 * @param dir The name of the directory to delete 273 * @param dir The name of the directory to delete
179 * @return 1 on success, 0 on failure 274 * @return 1 on success, 0 on failure