(mc_mkstemps): use g_mkstemp() to generate name of temporary file.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2012-10-08 17:35:03 +04:00
parent d27a4f86ea
commit 8b3ed9bfdc

View File

@ -777,72 +777,32 @@ mc_lseek (int fd, off_t offset, int whence)
int int
mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix) mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
{ {
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char *p1, *p2;
static unsigned long value;
struct timeval tv;
char *tmpbase;
char *tmpname;
char *XXXXXX;
char *ret_path;
int count;
if (strchr (prefix, PATH_SEP) == NULL)
{
/* Add prefix first to find the position of XXXXXX */
tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
}
else
{
tmpbase = g_strdup (prefix);
}
tmpname = g_strconcat (tmpbase, "XXXXXX", suffix, (char *) NULL);
ret_path = tmpname;
XXXXXX = &tmpname[strlen (tmpbase)];
g_free (tmpbase);
/* 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)
{
unsigned long v = value;
int fd; int fd;
/* Fill in the random bits. */ if (strchr (prefix, PATH_SEP) != NULL)
XXXXXX[0] = letters[v % 62]; p1 = g_strdup (prefix);
v /= 62; else
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. */ /* Add prefix first to find the position of XXXXXX */
*pname_vpath = vfs_path_from_str (ret_path); p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
g_free (ret_path);
return fd;
} }
/* This is a random value. It is only necessary that the next p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
TMP_MAX values generated by adding 7777 to VALUE are different g_free (p1);
with (module 2^32). */
value += 7777;
}
/* Unsuccessful. Free the filename. */ fd = g_mkstemp (p2);
g_free (ret_path); if (fd >= 0)
*pname_vpath = vfs_path_from_str (p2);
else
{
*pname_vpath = NULL; *pname_vpath = NULL;
fd = -1;
}
return -1; g_free (p2);
return fd;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */