summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladislav Brovko <v.brovko@samsung.com>2013-03-27 22:40:28 +0900
committerCedric BAIL <cedric.bail@samsung.com>2013-03-27 22:45:34 +0900
commit7584a7e84dcbb11d52bf8091c46d230976bc458f (patch)
tree0686a47d74294bdfd346dad40caf3b66c498e8f9
parent6f6d425f0d8ed7eeb3251d4709c5228baab30983 (diff)
eina: add eina_file_mkdtemp and tests for Eina_File.
Test added for : eina_file_direct_ls_simple eina_file_ls_simple Signed-off-by: Cedric BAIL <cedric.bail@samsung.com>
-rw-r--r--AUTHORS1
-rw-r--r--src/lib/eina/eina_file.h35
-rw-r--r--src/lib/eina/eina_file_common.c28
-rw-r--r--src/tests/eina/eina_test_file.c122
4 files changed, 183 insertions, 3 deletions
diff --git a/AUTHORS b/AUTHORS
index cc47ce3d62..58b03ce2de 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -47,6 +47,7 @@ Jérôme Pinot <ngc891@gmail.com>
47Mike Blumenkrantz <michael.blumenkrantz@gmail.com> 47Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
48Patryk Kaczmarek <patryk.k@samsung.com> 48Patryk Kaczmarek <patryk.k@samsung.com>
49Igor Murzov <e-mail@date.by> 49Igor Murzov <e-mail@date.by>
50Vladislav Brovko <v.brovko@samsung.com>
50 51
51Eet 52Eet
52--- 53---
diff --git a/src/lib/eina/eina_file.h b/src/lib/eina/eina_file.h
index d0accf338c..959f089fda 100644
--- a/src/lib/eina/eina_file.h
+++ b/src/lib/eina/eina_file.h
@@ -323,7 +323,40 @@ EAPI Eina_Iterator *eina_file_stat_ls(const char *dir) EINA_WARN_UNUSED_RESULT E
323 */ 323 */
324EAPI int eina_file_statat(void *container, Eina_File_Direct_Info *info, Eina_Stat *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2, 3); 324EAPI int eina_file_statat(void *container, Eina_File_Direct_Info *info, Eina_Stat *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2, 3);
325 325
326EAPI int eina_file_mkstemp(const char *filename, Eina_Tmpstr **path); 326/**
327 * @brief Generate and create a uniquely named temporary file from template.
328 * Generated file is opened with the open(2) O_EXCL flag.
329 *
330 * @param [in] templatename is a string. The last six characters of templatename must be XXXXXX.
331 * @param [out] path Where to put the name of the created file. If not NULL
332 * should be released by eina_tmpstr_del.
333 * @return On success @c file descriptor of the temporary file is returned,
334 * On error @c -1 is returned, in which case errno is set appropriately.
335 *
336 * This function calls mkstemp, generates a unique temporary filename
337 * from template, creates and opens the file, and returns an open file
338 * descriptor for the file.
339 *
340 * @see eina_file_mkdtemp()
341 * @since 1.8
342 */
343EAPI int eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path);
344
345/**
346 * @brief Generate and create a uniquely named temporary directory from template.
347 *
348 * @param [in] templatename is a string. The last six characters of templatename must be XXXXXX.
349 * @param [out] path Where to put the name of the created directory. If not NULL
350 * should be released by eina_tmpstr_del.
351 * @return On success @c EINA_TRUE is returned, On error @c EINA_FALSE is returned,
352 * in which case errno is set appropriately.
353 *
354 * This function calls mkdtemp. The directory is then created with permissions 0700.
355 *
356 * @see eina_file_mkstemp()
357 * @since 1.8
358 */
359EAPI Eina_Bool eina_file_mkdtemp(const char *templatename, Eina_Tmpstr **path);
327 360
328/** 361/**
329 * @brief Get an iterator to list the content of a directory, with direct 362 * @brief Get an iterator to list the content of a directory, with direct
diff --git a/src/lib/eina/eina_file_common.c b/src/lib/eina/eina_file_common.c
index e17b185725..475f89da5a 100644
--- a/src/lib/eina/eina_file_common.c
+++ b/src/lib/eina/eina_file_common.c
@@ -571,7 +571,7 @@ eina_file_copy(const char *src, const char *dst, Eina_File_Copy_Flags flags, Ein
571} 571}
572 572
573EAPI int 573EAPI int
574eina_file_mkstemp(const char *filename, Eina_Tmpstr **path) 574eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path)
575{ 575{
576 char buffer[PATH_MAX]; 576 char buffer[PATH_MAX];
577 const char *tmpdir; 577 const char *tmpdir;
@@ -584,7 +584,7 @@ eina_file_mkstemp(const char *filename, Eina_Tmpstr **path)
584 tmpdir = (char *)evil_tmpdir_get(); 584 tmpdir = (char *)evil_tmpdir_get();
585#endif /* ! HAVE_EVIL */ 585#endif /* ! HAVE_EVIL */
586 586
587 snprintf(buffer, PATH_MAX, "%s/%s", tmpdir, filename); 587 snprintf(buffer, PATH_MAX, "%s/%s", tmpdir, templatename);
588 588
589 fd = mkstemp(buffer); 589 fd = mkstemp(buffer);
590 if (path) *path = eina_tmpstr_add(buffer); 590 if (path) *path = eina_tmpstr_add(buffer);
@@ -593,3 +593,27 @@ eina_file_mkstemp(const char *filename, Eina_Tmpstr **path)
593 593
594 return fd; 594 return fd;
595} 595}
596
597EAPI Eina_Bool
598eina_file_mkdtemp(const char *templatename, Eina_Tmpstr **path)
599{
600 char buffer[PATH_MAX];
601 const char *tmpdir;
602 char *tmpdirname;
603
604#ifndef HAVE_EVIL
605 tmpdir = getenv("TMPDIR");
606 if (!tmpdir) tmpdir = "/tmp";
607#else
608 tmpdir = (char *)evil_tmpdir_get();
609#endif /* ! HAVE_EVIL */
610
611 snprintf(buffer, PATH_MAX, "%s/%s", tmpdir, templatename);
612
613 tmpdirname = mkdtemp(buffer);
614 if (path) *path = eina_tmpstr_add(buffer);
615 if (tmpdirname == NULL)
616 return EINA_FALSE;
617
618 return EINA_TRUE;
619}
diff --git a/src/tests/eina/eina_test_file.c b/src/tests/eina/eina_test_file.c
index 60b3a3c361..a8bde8351b 100644
--- a/src/tests/eina/eina_test_file.c
+++ b/src/tests/eina/eina_test_file.c
@@ -23,11 +23,14 @@
23#include <stdlib.h> 23#include <stdlib.h>
24#include <stdio.h> 24#include <stdio.h>
25#include <string.h> 25#include <string.h>
26#include <unistd.h>
26 27
27#include "eina_suite.h" 28#include "eina_suite.h"
28#include "Eina.h" 29#include "Eina.h"
29#include "eina_safety_checks.h" 30#include "eina_safety_checks.h"
30 31
32static int default_dir_rights = 0777;
33
31#ifdef EINA_SAFETY_CHECKS 34#ifdef EINA_SAFETY_CHECKS
32struct log_ctx { 35struct log_ctx {
33 const char *msg; 36 const char *msg;
@@ -134,9 +137,128 @@ START_TEST(eina_file_split_simple)
134} 137}
135END_TEST 138END_TEST
136 139
140Eina_Tmpstr*
141get_full_path(const char* tmpdirname, const char* filename)
142{
143 char full_path[PATH_MAX] = "";
144 eina_str_join(full_path, sizeof(full_path), '/', tmpdirname, filename);
145 return eina_tmpstr_add(full_path);
146}
147
148Eina_Tmpstr*
149get_eina_test_file_tmp_dir()
150{
151 Eina_Tmpstr *tmp_dir;
152
153 Eina_Bool created = eina_file_mkdtemp("EinaFileTestXXXXXX", &tmp_dir);
154
155 if (!created)
156 {
157 return NULL;
158 }
159
160 return tmp_dir;
161}
162
163START_TEST(eina_file_direct_ls_simple)
164{
165 eina_init();
166
167 const char *good_dirs[] =
168 {
169 "eina_file_direct_ls_simple_dir",
170 "a.",
171 "$a$b",
172 "~$a@:-*$b!{}"
173 };
174 const int good_dirs_count = sizeof(good_dirs) / sizeof(const char *);
175 Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
176 fail_if(test_dirname == NULL);
177
178 for (int i = 0; i != good_dirs_count; ++i)
179 {
180 Eina_Tmpstr *dirname = get_full_path(test_dirname, good_dirs[i]);
181 // clean old test directories
182 rmdir(dirname);
183 fail_if(mkdir(dirname, default_dir_rights) != 0);
184
185 Eina_File_Direct_Info *dir_info;
186 Eina_Iterator *it = eina_file_direct_ls(test_dirname);
187 Eina_Bool found_dir = EINA_FALSE;
188
189 while (eina_iterator_next(it, (void **)&dir_info))
190 {
191 if (!strcmp(dir_info->path, dirname))
192 {
193 found_dir = EINA_TRUE;
194 }
195 }
196
197 eina_iterator_free(it);
198
199 fail_if(!found_dir);
200 fail_if(rmdir(dirname) != 0);
201
202 eina_tmpstr_del(dirname);
203 }
204 fail_if(rmdir(test_dirname) != 0);
205 eina_tmpstr_del(test_dirname);
206 eina_shutdown();
207}
208END_TEST
209
210START_TEST(eina_file_ls_simple)
211{
212 eina_init();
213
214 const char *good_dirs[] =
215 {
216 "eina_file_ls_simple_dir",
217 "b.",
218 "$b$a",
219 "~$b@:-*$a!{}"
220 };
221 const int good_dirs_count = sizeof(good_dirs) / sizeof(const char *);
222 Eina_Tmpstr *test_dirname = get_eina_test_file_tmp_dir();
223 fail_if(test_dirname == NULL);
224
225 for (int i = 0; i != good_dirs_count; ++i)
226 {
227 Eina_Tmpstr *dirname = get_full_path(test_dirname, good_dirs[i]);
228 // clean old test directories
229 rmdir(dirname);
230 fail_if(mkdir(dirname, default_dir_rights) != 0);
231
232 char *filename;
233 Eina_Iterator *it = eina_file_ls(test_dirname);
234 Eina_Bool found_dir = EINA_FALSE;
235
236 while (eina_iterator_next(it, (void **)&filename))
237 {
238 if (!strcmp(filename, dirname))
239 {
240 found_dir = EINA_TRUE;
241 }
242 }
243
244 eina_iterator_free(it);
245
246 fail_if(!found_dir);
247 fail_if(rmdir(dirname) != 0);
248
249 eina_tmpstr_del(dirname);
250 }
251 fail_if(rmdir(test_dirname) != 0);
252 eina_tmpstr_del(test_dirname);
253 eina_shutdown();
254}
255END_TEST
256
137void 257void
138eina_test_file(TCase *tc) 258eina_test_file(TCase *tc)
139{ 259{
140 tcase_add_test(tc, eina_file_split_simple); 260 tcase_add_test(tc, eina_file_split_simple);
261 tcase_add_test(tc, eina_file_direct_ls_simple);
262 tcase_add_test(tc, eina_file_ls_simple);
141} 263}
142 264