diff options
author | Vincent Torri <vincent.torri@gmail.com> | 2020-10-08 11:58:11 +0100 |
---|---|---|
committer | Carsten Haitzler (Rasterman) <raster@rasterman.com> | 2020-10-08 11:58:11 +0100 |
commit | eacee53c2e795a1c5e187558ccced2d66390d761 (patch) | |
tree | 03d374e1ed454827d64874c3548f564f1df1201b | |
parent | 509e3fcc7a65918f6bcf8a664353b45af457cfa6 (diff) | |
download | efl-eacee53c2e795a1c5e187558ccced2d66390d761.tar.gz |
Evil : move mkstemp(s) and mkdtemp in eina_file directly
Summary:
Also replace all mkstemp(s) and mkdtemp with the eina_file functions in
the source
Test Plan: run eina_file test
Reviewers: raster
Subscribers: cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12170
-rw-r--r-- | src/examples/eet/eet-data-cipher_decipher.c | 29 | ||||
-rw-r--r-- | src/lib/ecore_con/efl_net_dialer_http.eo | 2 | ||||
-rw-r--r-- | src/lib/ecore_con/efl_net_dialer_websocket.eo | 2 | ||||
-rw-r--r-- | src/lib/eina/eina_file_common.c | 94 | ||||
-rw-r--r-- | src/lib/eina/eina_file_posix.c | 76 | ||||
-rw-r--r-- | src/lib/eina/eina_file_win32.c | 176 | ||||
-rw-r--r-- | src/lib/eina/eina_main.c | 12 | ||||
-rw-r--r-- | src/lib/eina/eina_private.h | 7 | ||||
-rw-r--r-- | src/lib/eina/meson.build | 5 | ||||
-rw-r--r-- | src/lib/evil/evil_stdlib.c | 109 | ||||
-rw-r--r-- | src/lib/evil/evil_stdlib.h | 18 | ||||
-rw-r--r-- | src/modules/elementary/access_output/mod.c | 13 | ||||
-rw-r--r-- | src/tests/eet/eet_test_cipher.c | 14 | ||||
-rw-r--r-- | src/tests/eet/eet_test_file.c | 58 | ||||
-rw-r--r-- | src/tests/eet/eet_test_identity.c | 17 | ||||
-rw-r--r-- | src/tests/eet/eet_test_image.c | 26 | ||||
-rw-r--r-- | src/tests/elua/elua_lib.c | 16 | ||||
-rw-r--r-- | src/tests/evil/evil_test_stdlib.c | 88 |
18 files changed, 361 insertions, 401 deletions
diff --git a/src/examples/eet/eet-data-cipher_decipher.c b/src/examples/eet/eet-data-cipher_decipher.c index 4ff94d5782..fb6574064b 100644 --- a/src/examples/eet/eet-data-cipher_decipher.c +++ b/src/examples/eet/eet-data-cipher_decipher.c @@ -17,46 +17,46 @@ main(void) const char *key = "This is a crypto key"; const char *key_bad = "This is another crypto key"; - char *file = strdup("/tmp/eet_cipher_example_XXXXXX"); Eet_File *ef; char *test; int size; int tmpfd; + Eina_Tmpstr *tmpf = NULL; eet_init(); - if (-1 == (tmpfd = mkstemp(file)) || !!close(tmpfd)) + if (-1 == (tmpfd = eina_file_mkstemp("eet_cipher_example_XXXXXX", &tmpf)) || !!close(tmpfd)) { fprintf( stderr, "ERROR: could not create temporary file (%s) : %s\n", - file, strerror(errno)); + tmpf, strerror(errno)); goto panic; } /* Crypt an eet file. */ - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); if (!ef) { fprintf( - stderr, "ERROR: could not access file (%s).\n", file); + stderr, "ERROR: could not access file (%s).\n", tmpf); goto error; } if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key)) { fprintf( - stderr, "ERROR: could not access file (%s).\n", file); + stderr, "ERROR: could not access file (%s).\n", tmpf); goto error; } eet_close(ef); /* Decrypt an eet file. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); if (!ef) { fprintf( - stderr, "ERROR: could not access file (%s).\n", file); + stderr, "ERROR: could not access file (%s).\n", tmpf); goto error; } @@ -65,7 +65,7 @@ main(void) { fprintf( stderr, "ERROR: could decript contents on file %s, with key %s.\n", - file, key); + tmpf, key); goto error; } @@ -86,11 +86,11 @@ main(void) eet_close(ef); /* Decrypt an eet file, now using our BAD key!! */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); if (!ef) { fprintf( - stderr, "ERROR: could not access file (%s).\n", file); + stderr, "ERROR: could not access file (%s).\n", tmpf); goto error; } @@ -102,18 +102,19 @@ main(void) fprintf( stderr, "ERROR: something is wrong with the contents of %s, as" " we accessed it with a different key and it decripted our" - " information right.\n", file); + " information right.\n", tmpf); goto error; } eet_close(ef); error: - if (unlink(file) != 0) + if (unlink(tmpf) != 0) { fprintf( - stderr, "ERROR: could not unlink file (%s).\n", file); + stderr, "ERROR: could not unlink file (%s)%d.\n", tmpf, errno); } + eina_tmpstr_del(tmpf); panic: eet_shutdown(); diff --git a/src/lib/ecore_con/efl_net_dialer_http.eo b/src/lib/ecore_con/efl_net_dialer_http.eo index 00bff4131b..f194d7bdc6 100644 --- a/src/lib/ecore_con/efl_net_dialer_http.eo +++ b/src/lib/ecore_con/efl_net_dialer_http.eo @@ -310,7 +310,7 @@ class @beta Efl.Net.Dialer_Http extends Efl.Loop_Consumer implements Efl.Net.Dia If a new, empty session is to be used, start with an empty or non-existent file such as created with - mkstemp() or tmpfile(). Alternatively use an + eina_file_mkstemp() or tmpfile(). Alternatively use an empty string ("") to store it in memory. If you want to start from a pre-existing cookie jar diff --git a/src/lib/ecore_con/efl_net_dialer_websocket.eo b/src/lib/ecore_con/efl_net_dialer_websocket.eo index 612f504d46..1d4d98fb44 100644 --- a/src/lib/ecore_con/efl_net_dialer_websocket.eo +++ b/src/lib/ecore_con/efl_net_dialer_websocket.eo @@ -273,7 +273,7 @@ class @beta Efl.Net.Dialer_Websocket extends Efl.Loop_Consumer implements Efl.Ne If a new, empty session is to be used, start with an empty or non-existent file such as one created with - mkstemp() or tmpfile(). Alternatively use an + eina_file_mkstemp() or tmpfile(). Alternatively use an empty string ("") to keep it in memory. If you want to start from a pre-existent cookie jar diff --git a/src/lib/eina/eina_file_common.c b/src/lib/eina/eina_file_common.c index 0380ea7a85..70ac5384fb 100644 --- a/src/lib/eina/eina_file_common.c +++ b/src/lib/eina/eina_file_common.c @@ -28,7 +28,7 @@ #include <errno.h> #ifdef _WIN32 -# include <evil_private.h> /* windows.h fcntl mkstemps mkdtemp */ +# include <evil_private.h> /* windows.h */ #endif #define COPY_BLOCKSIZE (4 * 1024 * 1024) @@ -993,98 +993,6 @@ eina_file_copy(const char *src, const char *dst, Eina_File_Copy_Flags flags, Ein return success; } -EAPI int -eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path) -{ - char buffer[PATH_MAX]; - const char *XXXXXX = NULL, *sep; - int fd, len; -#ifndef _WIN32 - mode_t old_umask; -#endif - - EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, -1); - - sep = strchr(templatename, '/'); -#ifdef _WIN32 - if (!sep) sep = strchr(templatename, '\\'); -#endif - if (sep) - { - len = eina_strlcpy(buffer, templatename, sizeof(buffer)); - } - else - { - len = eina_file_path_join(buffer, sizeof(buffer), - eina_environment_tmp_get(), templatename); - } - - /* - * Unix: - * Make sure temp file is created with secure permissions, - * http://man7.org/linux/man-pages/man3/mkstemp.3.html#NOTES - * - * Windows: - * no secure permissions anyway and the umask use below makes - * the file read-only. - */ -#ifndef _WIN32 - old_umask = umask(S_IRWXG|S_IRWXO); -#endif - if ((XXXXXX = strstr(buffer, "XXXXXX.")) != NULL) - { - int suffixlen = buffer + len - XXXXXX - 6; - fd = mkstemps(buffer, suffixlen); - } - else - fd = mkstemp(buffer); -#ifndef _WIN32 - umask(old_umask); -#endif - - if (fd < 0) - { - if (path) *path = NULL; - return -1; - } - - if (path) *path = eina_tmpstr_add(buffer); - return fd; -} - -EAPI Eina_Bool -eina_file_mkdtemp(const char *templatename, Eina_Tmpstr **path) -{ - char buffer[PATH_MAX]; - char *tmpdirname, *sep; - - EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, EINA_FALSE); - - sep = strchr(templatename, '/'); -#ifdef _WIN32 - if (!sep) sep = strchr(templatename, '\\'); -#endif - if (sep) - { - eina_strlcpy(buffer, templatename, sizeof(buffer)); - } - else - { - eina_file_path_join(buffer, sizeof(buffer), - eina_environment_tmp_get(), templatename); - } - - tmpdirname = mkdtemp(buffer); - if (tmpdirname == NULL) - { - if (path) *path = NULL; - return EINA_FALSE; - } - - if (path) *path = eina_tmpstr_add(tmpdirname); - return EINA_TRUE; -} - /*============================================================================* * Global * *============================================================================*/ diff --git a/src/lib/eina/eina_file_posix.c b/src/lib/eina/eina_file_posix.c index 14460b2f01..72f0e510fe 100644 --- a/src/lib/eina/eina_file_posix.c +++ b/src/lib/eina/eina_file_posix.c @@ -1509,3 +1509,79 @@ skip3: } #endif } + +EAPI int +eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path) +{ + char buffer[PATH_MAX]; + const char *XXXXXX = NULL, *sep; + int fd, len; + mode_t old_umask; + + EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, -1); + + sep = strchr(templatename, '/'); + if (sep) + { + len = eina_strlcpy(buffer, templatename, sizeof(buffer)); + } + else + { + len = eina_file_path_join(buffer, sizeof(buffer), + eina_environment_tmp_get(), templatename); + } + + /* + * Unix: + * Make sure temp file is created with secure permissions, + * http://man7.org/linux/man-pages/man3/mkstemp.3.html#NOTES + */ + old_umask = umask(S_IRWXG|S_IRWXO); + if ((XXXXXX = strstr(buffer, "XXXXXX.")) != NULL) + { + int suffixlen = buffer + len - XXXXXX - 6; + fd = mkstemps(buffer, suffixlen); + } + else + fd = mkstemp(buffer); + umask(old_umask); + + if (fd < 0) + { + if (path) *path = NULL; + return -1; + } + + if (path) *path = eina_tmpstr_add(buffer); + return fd; +} + +EAPI Eina_Bool +eina_file_mkdtemp(const char *templatename, Eina_Tmpstr **path) +{ + char buffer[PATH_MAX]; + char *tmpdirname, *sep; + + EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, EINA_FALSE); + + sep = strchr(templatename, '/'); + if (sep) + { + eina_strlcpy(buffer, templatename, sizeof(buffer)); + } + else + { + eina_file_path_join(buffer, sizeof(buffer), + eina_environment_tmp_get(), templatename); + } + + tmpdirname = mkdtemp(buffer); + if (tmpdirname == NULL) + { + if (path) *path = NULL; + return EINA_FALSE; + } + + if (path) *path = eina_tmpstr_add(tmpdirname); + return EINA_TRUE; +} diff --git a/src/lib/eina/eina_file_win32.c b/src/lib/eina/eina_file_win32.c index 9fa8e96a4d..b5c0c418c0 100644 --- a/src/lib/eina/eina_file_win32.c +++ b/src/lib/eina/eina_file_win32.c @@ -24,6 +24,7 @@ #include <sys/stat.h> #include <evil_private.h> +#include <fcntl.h> #include "eina_config.h" #include "eina_private.h" @@ -397,6 +398,114 @@ _eina_file_sep_find(char *s) return NULL; } +static unsigned char _eina_file_random_uchar(unsigned char *c) +{ + /* + * Helper function for mktemp. + * + * Only characters from 'a' to 'z' and '0' to '9' are considered + * because on Windows, file system is case insensitive. That means + * 36 possible values. + * To increase randomness, we consider the greatest multiple of 36 + * within 255 : 7*36 = 252, that is, values from 0 to 251 and choose + * a random value in this interval. + */ + do { + BCryptGenRandom(_eina_bcrypt_provider, c, sizeof(UCHAR), 0); + } while (*c > 251); + + *c = '0' + *c % 36; + if (*c > '9') + *c += 'a' - '9' - 1; + + return *c; +} + +static int +_eina_file_mkstemp_init(char *__template, size_t *length, int suffixlen) +{ + if (!__template || (suffixlen < 0)) + { + errno = EINVAL; + return 0; + } + + *length = strlen(__template); + if ((*length < (6 + (size_t)suffixlen)) + || (strncmp(__template + *length - 6 - suffixlen, "XXXXXX", 6) != 0)) + { + errno = EINVAL; + return 0; + } + return 1; +} + +static void +_eina_file_tmpname(char *__template, size_t length, int suffixlen) +{ + unsigned char *suffix; + + suffix = (unsigned char *)(__template + length - 6 - suffixlen); + *suffix = _eina_file_random_uchar(suffix); + suffix++; + *suffix = _eina_file_random_uchar(suffix); + suffix++; + *suffix = _eina_file_random_uchar(suffix); + suffix++; + *suffix = _eina_file_random_uchar(suffix); + suffix++; + *suffix = _eina_file_random_uchar(suffix); + suffix++; + *suffix = _eina_file_random_uchar(suffix); + suffix++; +} + +static int +_eina_file_mkstemps(char *__template, int suffixlen) +{ + size_t length; + int i; + + if (!_eina_file_mkstemp_init(__template, &length, suffixlen)) + return -1; + + for (i = 0; i < 32768; i++) + { + int fd; + + _eina_file_tmpname(__template, length, suffixlen); + + fd = _open(__template, + _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, + _S_IREAD | _S_IWRITE); + if (fd >= 0) + return fd; + } + + errno = EEXIST; + return -1; +} + +static char * +_eina_file_mkdtemp(char *__template) +{ + size_t length; + int i; + + if (!_eina_file_mkstemp_init(__template, &length, 0)) + return NULL; + + for (i = 0; i < 32768; i++) + { + _eina_file_tmpname(__template, length, 0); + if (CreateDirectory(__template, NULL) == TRUE) + return __template; + } + + return NULL; +} + + /** * @endcond */ @@ -1093,3 +1202,70 @@ eina_file_statat(void *container EINA_UNUSED, Eina_File_Direct_Info *info, Eina_ return 0; } + +EAPI int +eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path) +{ + char buffer[PATH_MAX]; + const char *XXXXXX = NULL, *sep; + int fd, len; + + EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, -1); + + sep = strchr(templatename, '/'); + if (!sep) sep = strchr(templatename, '\\'); + if (sep) + { + len = eina_strlcpy(buffer, templatename, sizeof(buffer)); + } + else + { + len = eina_file_path_join(buffer, sizeof(buffer), + eina_environment_tmp_get(), templatename); + } + + if ((XXXXXX = strstr(buffer, "XXXXXX.")) != NULL) + fd = _eina_file_mkstemps(buffer, buffer + len - XXXXXX - 6); + else + fd = _eina_file_mkstemps(buffer, 0); + + if (fd < 0) + { + if (path) *path = NULL; + return -1; + } + + if (path) *path = eina_tmpstr_add(buffer); + return fd; +} + +EAPI Eina_Bool +eina_file_mkdtemp(const char *templatename, Eina_Tmpstr **path) +{ + char buffer[PATH_MAX]; + char *tmpdirname, *sep; + + EINA_SAFETY_ON_NULL_RETURN_VAL(templatename, EINA_FALSE); + + sep = strchr(templatename, '/'); + if (!sep) sep = strchr(templatename, '\\'); + if (sep) + { + eina_strlcpy(buffer, templatename, sizeof(buffer)); + } + else + { + eina_file_path_join(buffer, sizeof(buffer), + eina_environment_tmp_get(), templatename); + } + + tmpdirname = _eina_file_mkdtemp(buffer); + if (tmpdirname == NULL) + { + if (path) *path = NULL; + return EINA_FALSE; + } + + if (path) *path = eina_tmpstr_add(tmpdirname); + return EINA_TRUE; +} diff --git a/src/lib/eina/eina_main.c b/src/lib/eina/eina_main.c index 9549ab96c3..e30a850f95 100644 --- a/src/lib/eina/eina_main.c +++ b/src/lib/eina/eina_main.c @@ -120,6 +120,10 @@ EAPI Eina_Inlist *_eina_tracking = NULL; extern Eina_Lock _sysmon_lock; #endif +#ifdef _WIN32 +BCRYPT_ALG_HANDLE _eina_bcrypt_provider; +#endif + /* place module init/shutdown functions here to avoid other modules * calling them by mistake. */ @@ -292,6 +296,10 @@ eina_init(void) return ++_eina_main_count; #ifdef _WIN32 + if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&_eina_bcrypt_provider, + BCRYPT_RNG_ALGORITHM, + NULL, 0))) + return 0; #else int fd = open("/dev/urandom", O_RDONLY); if (fd >= 0) @@ -397,6 +405,10 @@ eina_shutdown(void) _mt_enabled = 0; } #endif + +#ifdef _WIN32 + BCryptCloseAlgorithmProvider(_eina_bcrypt_provider, 0); +#endif } return _eina_main_count; diff --git a/src/lib/eina/eina_private.h b/src/lib/eina/eina_private.h index 9e2954e001..9d4da033ce 100644 --- a/src/lib/eina/eina_private.h +++ b/src/lib/eina/eina_private.h @@ -25,6 +25,11 @@ #include <xlocale.h> #endif +#ifdef _WIN32 +# include <windows.h> +# include <bcrypt.h> +#endif + #include "eina_magic.h" #include "eina_iterator.h" #include "eina_accessor.h" @@ -152,6 +157,7 @@ Eina_Stringshare *eina_file_sanitize(const char *path); void eina_freeq_main_set(Eina_FreeQ *fq); #ifdef _WIN32 +extern BCRYPT_ALG_HANDLE _eina_bcrypt_provider; typedef _locale_t locale_t; #endif locale_t _eina_c_locale_get(void); @@ -159,4 +165,3 @@ locale_t _eina_c_locale_get(void); #include "eina_inline_private.h" #endif /* EINA_PRIVATE_H_ */ - diff --git a/src/lib/eina/meson.build b/src/lib/eina/meson.build index 222d81b3a0..3bd869eed9 100644 --- a/src/lib/eina/meson.build +++ b/src/lib/eina/meson.build @@ -369,6 +369,11 @@ eina_ext_deps += [iconv] execinfo = cc.find_library('execinfo', required: false) eina_ext_deps += [execinfo] +if sys_windows == true + bcrypt = cc.find_library('bcrypt', required: true) + eina_ext_deps += [bcrypt] +endif + eina_lib = library('eina', eina_src, c_args : package_c_args, include_directories : config_dir, diff --git a/src/lib/evil/evil_stdlib.c b/src/lib/evil/evil_stdlib.c index d6de5a4fcd..ea8f3356b7 100644 --- a/src/lib/evil/evil_stdlib.c +++ b/src/lib/evil/evil_stdlib.c @@ -76,115 +76,6 @@ unsetenv(const char *name) * Files related functions * */ -static int -_mkstemp_init(char *__template, char **suffix, size_t *length, DWORD *val, - size_t suffixlen) -{ - *length = strlen(__template); - if ((*length < (6 + suffixlen)) - || (strncmp(__template + *length - 6 - suffixlen, "XXXXXX", 6) != 0)) - { - errno = EINVAL; - return 0; - } - - *suffix = __template + *length - 6 - suffixlen; - - *val = GetTickCount(); - *val += GetCurrentProcessId(); - - return 1; -} - -static int -_mkstemp(char *suffix, int val) -{ - const char lookup[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - DWORD v = val; - - suffix[0] = lookup[v % 62]; - v /= 62; - suffix[1] = lookup[v % 62]; - v /= 62; - suffix[2] = lookup[v % 62]; - v /= 62; - suffix[3] = lookup[v % 62]; - v /= 62; - suffix[4] = lookup[v % 62]; - v /= 62; - suffix[5] = lookup[v % 62]; - - val += 7777; - - return val; -} - -EAPI char * -mkdtemp(char *__template) -{ - char *suffix; - DWORD val; - size_t length; - int i; - - if (!__template) - { - errno = EINVAL; - return NULL; - } - - if (!_mkstemp_init(__template, &suffix, &length, &val, 0)) - return NULL; - - for (i = 0; i < 32768; i++) - { - val = _mkstemp(suffix, val); - - if (_mkdir(__template) == 0) - return __template; - - if (errno == EFAULT || - errno == ENOSPC || - errno == ENOMEM || - errno == ENOENT || - errno == ENOTDIR || - errno == EPERM || - errno == EROFS) - return NULL; - } - - errno = EEXIST; - return NULL; -} - -int -mkstemps(char *__template, int suffixlen) -{ - char *suffix; - DWORD val; - size_t length; - int i; - - if (!__template || (suffixlen < 0)) - return 0; - - if (!_mkstemp_init(__template, &suffix, &length, &val, (size_t) suffixlen)) - return -1; - - for (i = 0; i < 32768; i++) - { - int fd; - - val = _mkstemp(suffix, val); - - fd = _open(__template, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE); - if (fd >= 0) - return fd; - } - - errno = EEXIST; - return -1; -} char * realpath(const char *file_name, char *resolved_name) diff --git a/src/lib/evil/evil_stdlib.h b/src/lib/evil/evil_stdlib.h index eae2fb8809..cb35a570b0 100644 --- a/src/lib/evil/evil_stdlib.h +++ b/src/lib/evil/evil_stdlib.h @@ -68,24 +68,6 @@ EAPI int unsetenv(const char *name); */ /** - * @brief create an unique temporary directory - * - * @since 1.8.0 - */ -EAPI char *mkdtemp(char *__template); - -/** - * @brief Create a unique temporary file name with a suffix. - * - * @param __template Template of the file to create. - * @param suffixlen Length of the suffix following the 'XXXXXX' placeholder. - * @return A file descriptor on success, -1 otherwise. - * - * @since 1.10.0 - */ -EAPI int mkstemps(char *__template, int suffixlen); - -/** * @brief Return an absolute or full path name for a specified relative path name. * * @param file_name The absolute path name. diff --git a/src/modules/elementary/access_output/mod.c b/src/modules/elementary/access_output/mod.c index f51b512a47..cabf0a6a55 100644 --- a/src/modules/elementary/access_output/mod.c +++ b/src/modules/elementary/access_output/mod.c @@ -19,7 +19,7 @@ static void (*cb_func) (void *data); static void *cb_data; static Ecore_Exe *espeak = NULL; static Ecore_Event_Handler *exe_exit_handler = NULL; -static char *tmpf = NULL; +static Eina_Tmpstr *tmpf = NULL; static int tmpfd = -1; static Eina_Bool @@ -32,7 +32,7 @@ _exe_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) if (tmpf) { unlink(tmpf); - free(tmpf); + eina_tmpstr_del(tmpf); tmpf = NULL; close(tmpfd); tmpfd = -1; @@ -70,15 +70,12 @@ out_read(const char *txt) { if (!tmpf) { - char buf[PATH_MAX]; mode_t cur_umask; - snprintf(buf, sizeof(buf), "/tmp/.elm-speak-XXXXXX"); cur_umask = umask(S_IRWXO | S_IRWXG); - tmpfd = mkstemp(buf); + tmpfd = eina_file_mkstemp("elm-speak-XXXXXX", &tmpf); umask(cur_umask); - if (tmpfd >= 0) tmpf = strdup(buf); - else return; + if (tmpfd < 0) return; } if (write(tmpfd, txt, strlen(txt)) < 0) perror("write to tmpfile (espeak)"); } @@ -117,7 +114,7 @@ out_cancel(void) if (tmpf) { unlink(tmpf); - free(tmpf); + eina_tmpstr_del(tmpf); tmpf = NULL; close(tmpfd); tmpfd = -1; diff --git a/src/tests/eet/eet_test_cipher.c b/src/tests/eet/eet_test_cipher.c index c06c505b4c..09c09f2d64 100644 --- a/src/tests/eet/eet_test_cipher.c +++ b/src/tests/eet/eet_test_cipher.c @@ -19,15 +19,15 @@ EFL_START_TEST(eet_test_cipher_decipher_simple) const char *key_bad = "This is another crypto key"; Eet_File *ef; char *test; - char *file = strdup("/tmp/eet_suite_testXXXXXX"); + Eina_Tmpstr *tmpf = NULL; int size; int tmpfd; - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); /* Crypt an eet file. */ - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); fail_if(!ef); fail_if(!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, @@ -36,7 +36,7 @@ EFL_START_TEST(eet_test_cipher_decipher_simple) eet_close(ef); /* Decrypt an eet file. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); test = eet_read_cipher(ef, "keys/tests", &size, key); @@ -48,7 +48,7 @@ EFL_START_TEST(eet_test_cipher_decipher_simple) eet_close(ef); /* Decrypt an eet file. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); test = eet_read_cipher(ef, "keys/tests", &size, key_bad); @@ -58,7 +58,9 @@ EFL_START_TEST(eet_test_cipher_decipher_simple) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + + eina_tmpstr_del(tmpf); } EFL_END_TEST diff --git a/src/tests/eet/eet_test_file.c b/src/tests/eet/eet_test_file.c index 9423dc8aed..23233d7757 100644 --- a/src/tests/eet/eet_test_file.c +++ b/src/tests/eet/eet_test_file.c @@ -19,19 +19,17 @@ EFL_START_TEST(eet_test_file_simple_write) Eet_Entry *entry; Eet_File *ef; char *test; - char *file; void *m; int size; int tmpfd; + Eina_Tmpstr *tmpf = NULL; - file = strdup("/tmp/eet_suite_testXXXXXX"); - - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); fail_if(eet_mode_get(NULL) != EET_FILE_MODE_INVALID); - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); fail_if(!ef); fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 1)); @@ -46,7 +44,7 @@ EFL_START_TEST(eet_test_file_simple_write) eet_close(ef); /* Test read from buffer */ - f = eina_file_open(file, EINA_FALSE); + f = eina_file_open(tmpf, EINA_FALSE); fail_if(!f); m = eina_file_map_all(f, EINA_FILE_WILLNEED); @@ -83,7 +81,7 @@ EFL_START_TEST(eet_test_file_simple_write) eina_file_close(f); /* Test read of simple file */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); test = eet_read(ef, "keys/tests", &size); @@ -105,7 +103,7 @@ EFL_START_TEST(eet_test_file_simple_write) eet_close(ef); /* Test eet cache system */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); test = eet_read(ef, "keys/tests", &size); @@ -116,8 +114,9 @@ EFL_START_TEST(eet_test_file_simple_write) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST @@ -128,14 +127,12 @@ EFL_START_TEST(eet_test_file_data) Eet_Dictionary *ed; Eet_File *ef; char **list; - char *file; Eet_Data_Descriptor_Class eddc; Eet_Test_Ex_Type etbt; int size; int test; int tmpfd; - - file = strdup("/tmp/eet_suite_testXXXXXX"); + Eina_Tmpstr *tmpf = NULL; eet_test_ex_set(&etbt, 0); etbt.list = eina_list_prepend(etbt.list, eet_test_ex_set(NULL, 1)); @@ -165,14 +162,14 @@ EFL_START_TEST(eet_test_file_data) eet_build_ex_descriptor(edd, EINA_FALSE); - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); /* Insert an error in etbt. */ etbt.i = 0; /* Save the encoded data in a file. */ - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0)); @@ -193,7 +190,7 @@ EFL_START_TEST(eet_test_file_data) /* Attempt to replace etbt by the correct one. */ etbt.i = EET_TEST_INT; - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0)); @@ -207,7 +204,7 @@ EFL_START_TEST(eet_test_file_data) eet_close(ef); /* Read back the data. */ - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY2, &etbt, 0)); @@ -273,8 +270,9 @@ EFL_START_TEST(eet_test_file_data) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST @@ -286,11 +284,9 @@ EFL_START_TEST(eet_test_file_data_dump) Eet_Test_Ex_Type etbt; Eet_File *ef; char *string1; - char *file; int test; int tmpfd; - - file = strdup("/tmp/eet_suite_testXXXXXX"); + Eina_Tmpstr *tmpf = NULL; eet_test_ex_set(&etbt, 0); etbt.list = eina_list_prepend(etbt.list, eet_test_ex_set(NULL, 1)); @@ -318,11 +314,11 @@ EFL_START_TEST(eet_test_file_data_dump) eet_build_ex_descriptor(edd, EINA_FALSE); - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); /* Save the encoded data in a file. */ - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); fail_if(!ef); fail_if(!eet_data_write(ef, edd, EET_TEST_FILE_KEY1, &etbt, 0)); @@ -330,7 +326,7 @@ EFL_START_TEST(eet_test_file_data_dump) eet_close(ef); /* Use dump/undump in the middle */ - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); string1 = NULL; @@ -341,7 +337,7 @@ EFL_START_TEST(eet_test_file_data_dump) eet_close(ef); /* Test the correctness of the reinsertion. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); result = eet_data_read(ef, edd, EET_TEST_FILE_KEY1); @@ -372,14 +368,14 @@ EFL_START_TEST(eet_test_file_data_dump) fail_if(test != 0); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST EFL_START_TEST(eet_test_file_fp) { - char *file; Eet_Data_Descriptor_Class eddc; Eet_Data_Descriptor *edd_5FP; Eet_Data_Descriptor *edd_5DBL; @@ -388,8 +384,7 @@ EFL_START_TEST(eet_test_file_fp) Eet_5DBL *convert; Eet_5FP *build; int tmpfd; - - file = strdup("/tmp/eet_suite_testXXXXXX"); + Eina_Tmpstr *tmpf = NULL; EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Eet_5FP); edd_5FP = eet_data_descriptor_file_new(&eddc); @@ -415,10 +410,10 @@ EFL_START_TEST(eet_test_file_fp) origin.f1 = eina_f32p32_int_from(1); origin.f0 = 0; - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); fail_if(!eet_data_write(ef, edd_5FP, EET_TEST_FILE_KEY1, &origin, 1)); @@ -443,8 +438,9 @@ EFL_START_TEST(eet_test_file_fp) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST diff --git a/src/tests/eet/eet_test_identity.c b/src/tests/eet/eet_test_identity.c index b22d537021..4c54b7bcd4 100644 --- a/src/tests/eet/eet_test_identity.c +++ b/src/tests/eet/eet_test_identity.c @@ -60,18 +60,16 @@ EFL_START_TEST(eet_test_identity_simple) Eet_Key *k; FILE *noread; char *test; - char *file; int size; int fd; + Eina_Tmpstr *tmpf = NULL; - file = strdup("/tmp/eet_suite_testXXXXXX"); - - fail_if(-1 == (fd = mkstemp(file))); + fail_if(-1 == (fd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(fd)); fail_if(!(noread = fopen("/dev/null", "wb"))); /* Sign an eet file. */ - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); fail_if(!ef); fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 0)); @@ -85,7 +83,7 @@ EFL_START_TEST(eet_test_identity_simple) eet_close(ef); /* Open a signed file. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); /* check that the certificates match */ @@ -109,7 +107,7 @@ EFL_START_TEST(eet_test_identity_simple) eet_clearcache(); /* Corrupting the file. */ - fd = open(file, O_WRONLY | O_BINARY); + fd = open(tmpf, O_WRONLY | O_BINARY); fail_if(fd < 0); fail_if(lseek(fd, 200, SEEK_SET) != 200); @@ -122,11 +120,12 @@ EFL_START_TEST(eet_test_identity_simple) close(fd); /* Attempt to open a modified file. */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST diff --git a/src/tests/eet/eet_test_image.c b/src/tests/eet/eet_test_image.c index 5e35e67e54..585f39d86b 100644 --- a/src/tests/eet/eet_test_image.c +++ b/src/tests/eet/eet_test_image.c @@ -72,7 +72,6 @@ static const Eet_Test_Image test_alpha = { EFL_START_TEST(eet_test_image_normal) { Eet_File *ef; - char *file; unsigned int *data; int compress; int quality; @@ -82,14 +81,13 @@ EFL_START_TEST(eet_test_image_normal) unsigned int w; unsigned int h; int tmpfd; + Eina_Tmpstr *tmpf = NULL; - file = strdup("/tmp/eet_suite_testXXXXXX"); - - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); /* Save the encoded data in a file. */ - ef = eet_open(file, EET_FILE_MODE_READ_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_READ_WRITE); fail_if(!ef); result = eet_data_image_write(ef, @@ -219,7 +217,7 @@ EFL_START_TEST(eet_test_image_normal) eet_close(ef); /* Test read of image */ - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); result = eet_data_image_header_read(ef, @@ -424,14 +422,14 @@ EFL_START_TEST(eet_test_image_normal) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); + eina_tmpstr_del(tmpf); } EFL_END_TEST EFL_START_TEST(eet_test_image_small) { - char *file; unsigned int image[4]; unsigned int *data; Eet_File *ef; @@ -443,18 +441,17 @@ EFL_START_TEST(eet_test_image_small) Eet_Image_Encoding lossy; int result; int tmpfd; - - file = strdup("/tmp/eet_suite_testXXXXXX"); + Eina_Tmpstr *tmpf = NULL; image[0] = IM0; image[1] = IM1; image[2] = IM2; image[3] = IM3; - fail_if(-1 == (tmpfd = mkstemp(file))); + fail_if(-1 == (tmpfd = eina_file_mkstemp("/tmp/eet_suite_testXXXXXX", &tmpf))); fail_if(!!close(tmpfd)); - ef = eet_open(file, EET_FILE_MODE_WRITE); + ef = eet_open(tmpf, EET_FILE_MODE_WRITE); fail_if(!ef); result = eet_data_image_write(ef, "/images/test", image, 2, 2, 1, 9, 100, 0); @@ -462,7 +459,7 @@ EFL_START_TEST(eet_test_image_small) eet_close(ef); - ef = eet_open(file, EET_FILE_MODE_READ); + ef = eet_open(tmpf, EET_FILE_MODE_READ); fail_if(!ef); data = (unsigned int *)eet_data_image_read(ef, @@ -477,7 +474,7 @@ EFL_START_TEST(eet_test_image_small) eet_close(ef); - fail_if(unlink(file) != 0); + fail_if(unlink(tmpf) != 0); fail_if(data[0] != IM0); fail_if(data[1] != IM1); @@ -486,6 +483,7 @@ EFL_START_TEST(eet_test_image_small) free(data); + eina_tmpstr_del(tmpf); } EFL_END_TEST diff --git a/src/tests/elua/elua_lib.c b/src/tests/elua/elua_lib.c index f3d11d5023..b515d74c67 100644 --- a/src/tests/elua/elua_lib.c +++ b/src/tests/elua/elua_lib.c @@ -19,7 +19,6 @@ EFL_START_TEST(elua_api) { Elua_State *st; lua_State *lst; - char buf[] = "tmpXXXXXX"; FILE *f; int fd; char *cargv[2]; @@ -84,16 +83,16 @@ EFL_START_TEST(elua_api) fail_if(lua_type(lst, -1) != LUA_TFUNCTION); lua_pop(lst, 1); - fd = mkstemp(buf); + fd = eina_file_mkstemp("tmpXXXXXX", &tmpf); fail_if(fd < 0); f = fdopen(fd, "wb"); fail_if(!f); fprintf(f, "return 5\n"); fclose(f); - fail_if(!elua_util_file_run(st, buf)); + fail_if(!elua_util_file_run(st, tmpf)); fail_if(lua_tointeger(lst, -1) != 5); lua_pop(lst, 1); - fail_if(remove(buf)); + fail_if(remove(tmpf)); /* halfassed testing here, but not possible otherwise */ fail_if(elua_util_error_report(st, 0)); @@ -101,21 +100,21 @@ EFL_START_TEST(elua_api) fail_if(!elua_util_error_report(st, 5)); fail_if(lua_gettop(lst) > 0); - f = fopen(buf, "wb"); + f = fopen(tmpf, "wb"); fail_if(!f); fprintf(f, "return true"); fclose(f); - cargv[1] = buf; + cargv[1] = tmpf; fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit)); fail_if(quit != 1); - f = fopen(buf, "wb"); + f = fopen(tmpf, "wb"); fail_if(!f); fprintf(f, "return false"); fclose(f); fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit)); fail_if(quit != 0); - fail_if(remove(buf)); + fail_if(remove(tmpf)); /* elua API here tries accessing files by relative path, * prevent any unintentional file accesses in cwd @@ -139,6 +138,7 @@ EFL_START_TEST(elua_api) lua_pop(lst, 1); elua_state_free(st); + eina_tmpstr_del(tmpf); } EFL_END_TEST diff --git a/src/tests/evil/evil_test_stdlib.c b/src/tests/evil/evil_test_stdlib.c index c9f09813ef..57afb94fbd 100644 --- a/src/tests/evil/evil_test_stdlib.c +++ b/src/tests/evil/evil_test_stdlib.c @@ -169,86 +169,6 @@ EFL_START_TEST(evil_stdlib_unsetenv) } EFL_END_TEST -EFL_START_TEST(evil_stdlib_mkdtemp) -{ - char template[] = "file_XXXXXX"; - char *res; - - res = mkdtemp(template); - fail_if(res == NULL); - - fail_if(rmdir(res) < 0); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkdtemp_fail) -{ - char template[] = "file_XXX"; - char *res; - - res = mkdtemp(template); - fail_if(res != NULL); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkstemp) -{ - char template[] = "file_XXXXXX"; - int fd; - - fd = mkstemp(template); - fail_if(fd < 0); - - fail_if(close(fd) == -1); - - fail_if(unlink(template) == -1); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkstemp_fail) -{ - char template[] = "file_XXX"; - int fd; - - fd = mkstemp(template); - fail_if(fd >= 0); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkstemps) -{ - char template[] = "file_XXXXXX.ext"; - int fd; - - fd = mkstemps(template, 4); - fail_if(fd < 0); - - fail_if(close(fd) == -1); - - fail_if(unlink(template) == -1); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkstemps_fail_1) -{ - char template[] = "file_XXX.ext"; - int fd; - - fd = mkstemps(template, 4); - fail_if(fd >= 0); -} -EFL_END_TEST - -EFL_START_TEST(evil_stdlib_mkstemps_fail_2) -{ - char template[] = "file_XXX"; - int fd; - - fd = mkstemps(template, 4); - fail_if(fd >= 0); -} -EFL_END_TEST - EFL_START_TEST(evil_stdlib_realpath_1) { char buf[PATH_MAX]; @@ -302,14 +222,6 @@ void evil_test_stdlib(TCase *tc) tcase_add_test(tc, evil_stdlib_getenv_two_swapped); tcase_add_test(tc, evil_stdlib_unsetenv); - tcase_add_test(tc, evil_stdlib_mkdtemp); - tcase_add_test(tc, evil_stdlib_mkdtemp_fail); - tcase_add_test(tc, evil_stdlib_mkstemp); - tcase_add_test(tc, evil_stdlib_mkstemp_fail); - tcase_add_test(tc, evil_stdlib_mkstemps); - tcase_add_test(tc, evil_stdlib_mkstemps_fail_1); - tcase_add_test(tc, evil_stdlib_mkstemps_fail_2); - tcase_add_test(tc, evil_stdlib_realpath_1); tcase_add_test(tc, evil_stdlib_realpath_2); tcase_add_test(tc, evil_stdlib_realpath_3); |