(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; int fd;
struct timeval tv;
char *tmpbase;
char *tmpname;
char *XXXXXX;
char *ret_path;
int count;
if (strchr (prefix, PATH_SEP) == NULL) if (strchr (prefix, PATH_SEP) != NULL)
{ p1 = g_strdup (prefix);
/* Add prefix first to find the position of XXXXXX */
tmpbase = g_build_filename (mc_tmpdir (), prefix, NULL);
}
else 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); p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
ret_path = tmpname; g_free (p1);
XXXXXX = &tmpname[strlen (tmpbase)];
g_free (tmpbase);
/* Get some more or less random data. */ fd = g_mkstemp (p2);
gettimeofday (&tv, NULL); if (fd >= 0)
value += (tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); *pname_vpath = vfs_path_from_str (p2);
else
for (count = 0; count < TMP_MAX; ++count)
{ {
unsigned long v = value; *pname_vpath = NULL;
int fd; fd = -1;
/* 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;
} }
/* Unsuccessful. Free the filename. */ g_free (p2);
g_free (ret_path);
*pname_vpath = NULL;
return -1; return fd;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */