From 1ec1fbe35b3393c913dc7ebd2df4f33c1aed7dbd Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 8 Oct 2012 17:35:03 +0400 Subject: [PATCH] (mc_mkstemps): use g_mkstemp() to generate name of temporary file. Signed-off-by: Andrew Borodin --- lib/vfs/interface.c | 72 ++++++++++----------------------------------- 1 file changed, 16 insertions(+), 56 deletions(-) diff --git a/lib/vfs/interface.c b/lib/vfs/interface.c index 24874c3f1..c083c2e80 100644 --- a/lib/vfs/interface.c +++ b/lib/vfs/interface.c @@ -777,72 +777,32 @@ mc_lseek (int fd, off_t offset, int whence) int mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix) { - static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - static unsigned long value; - struct timeval tv; - char *tmpbase; - char *tmpname; - char *XXXXXX; - char *ret_path; - int count; + char *p1, *p2; + int fd; - if (strchr (prefix, PATH_SEP) == NULL) - { - /* Add prefix first to find the position of XXXXXX */ - tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL); - } + if (strchr (prefix, PATH_SEP) != NULL) + p1 = g_strdup (prefix); else { - tmpbase = g_strdup (prefix); + /* Add prefix first to find the position of XXXXXX */ + p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL); } - tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL); - ret_path = tmpname; - XXXXXX = &tmpname[strlen (tmpbase)]; - g_free (tmpbase); + p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL); + g_free (p1); - /* Get some more or less random data. */ - gettimeofday (&tv, NULL); - value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); - - for (count = 0; count < TMP_MAX; ++count) + fd = g_mkstemp (p2); + if (fd >= 0) + *pname_vpath = vfs_path_from_str (p2); + else { - unsigned long v = value; - int fd; - - /* Fill in the random bits. */ - XXXXXX[0] = letters[v % 62]; - v /= 62; - XXXXXX[1] = letters[v % 62]; - v /= 62; - XXXXXX[2] = letters[v % 62]; - v /= 62; - XXXXXX[3] = letters[v % 62]; - v /= 62; - XXXXXX[4] = letters[v % 62]; - v /= 62; - XXXXXX[5] = letters[v % 62]; - - fd = open (tmpname, O_RDWR | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR); - if (fd >= 0) - { - /* Successfully created. */ - *pname_vpath = vfs_path_from_str (ret_path); - g_free (ret_path); - return fd; - } - - /* This is a random value. It is only necessary that the next - TMP_MAX values generated by adding 7777 to VALUE are different - with (module 2^32). */ - value += 7777; + *pname_vpath = NULL; + fd = -1; } - /* Unsuccessful. Free the filename. */ - g_free (ret_path); - *pname_vpath = NULL; + g_free (p2); - return -1; + return fd; } /* --------------------------------------------------------------------------------------------- */