2017-11-04 10:52:33 +00:00
|
|
|
/* $Id: tmpfileplus.c $ */
|
|
|
|
/*
|
|
|
|
* $Date: 2016-06-01 03:31Z $
|
|
|
|
* $Revision: 2.0.0 $
|
|
|
|
* $Author: dai $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012-16 David Ireland, DI Management Services Pty Ltd
|
|
|
|
* <http://www.di-mgt.com.au/contact/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NAME
|
|
|
|
* tmpfileplus - create a unique temporary file
|
|
|
|
*
|
|
|
|
* SYNOPSIS
|
|
|
|
* FILE *tmpfileplus(const char *dir, const char *prefix, char **pathname, int keep)
|
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* The tmpfileplus() function opens a unique temporary file in binary
|
|
|
|
* read/write (w+b) mode. The file is opened with the O_EXCL flag,
|
|
|
|
* guaranteeing that the caller is the only user. The filename will consist
|
|
|
|
* of the string given by `prefix` followed by 10 random characters. If
|
|
|
|
* `prefix` is NULL, then the string "tmp." will be used instead. The file
|
|
|
|
* will be created in an appropriate directory chosen by the first
|
|
|
|
* successful attempt in the following sequence:
|
|
|
|
*
|
|
|
|
* a) The directory given by the `dir` argument (so the caller can specify
|
|
|
|
* a secure directory to take precedence).
|
|
|
|
*
|
|
|
|
* b) The directory name in the environment variables:
|
|
|
|
*
|
|
|
|
* (i) "TMP" [Windows only]
|
|
|
|
* (ii) "TEMP" [Windows only]
|
|
|
|
* (iii) "TMPDIR" [Unix only]
|
|
|
|
*
|
|
|
|
* c) `P_tmpdir` as defined in <stdio.h> [Unix only] (in Windows, this is
|
|
|
|
* usually "\", which is no good).
|
|
|
|
*
|
|
|
|
* d) The current working directory.
|
|
|
|
*
|
|
|
|
* If a file cannot be created in any of the above directories, then the
|
|
|
|
* function fails and NULL is returned.
|
|
|
|
*
|
|
|
|
* If the argument `pathname` is not a null pointer, then it will point to
|
|
|
|
* the full pathname of the file. The pathname is allocated using `malloc`
|
|
|
|
* and therefore should be freed by `free`.
|
|
|
|
*
|
|
|
|
* If `keep` is nonzero and `pathname` is not a null pointer, then the file
|
|
|
|
* will be kept after it is closed. Otherwise the file will be
|
|
|
|
* automatically deleted when it is closed or the program terminates.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* RETURN VALUE
|
|
|
|
* The tmpfileplus() function returns a pointer to the open file stream,
|
|
|
|
* or NULL if a unique file cannot be opened.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ERRORS
|
|
|
|
* ENOMEM Not enough memory to allocate filename.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tmpfileplus.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/* Non-ANSI include files that seem to work in both MSVC and Linux */
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* MSVC nags to enforce ISO C++ conformant function names with leading "_",
|
|
|
|
* so we define our own function names to avoid whingeing compilers...
|
|
|
|
*/
|
|
|
|
#define OPEN_ _open
|
|
|
|
#define FDOPEN_ _fdopen
|
|
|
|
#else
|
|
|
|
#define OPEN_ open
|
|
|
|
#define FDOPEN_ fdopen
|
|
|
|
#endif
|
|
|
|
|
2017-12-02 16:05:39 +00:00
|
|
|
#include "m_random.h"
|
|
|
|
#include "cmdlib.h"
|
2017-12-02 16:33:43 +00:00
|
|
|
#include "zstring.h"
|
2017-11-04 10:52:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define RANDCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
#define NRANDCHARS (sizeof(RANDCHARS) - 1)
|
|
|
|
|
2017-12-02 16:05:39 +00:00
|
|
|
static FRandom pr_tmpfile;
|
|
|
|
|
2017-11-04 10:52:33 +00:00
|
|
|
/** Replace each byte in string s with a random character from TEMPCHARS */
|
|
|
|
static char *set_randpart(char *s)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned int r;
|
|
|
|
|
2017-12-02 16:05:39 +00:00
|
|
|
|
2017-11-04 10:52:33 +00:00
|
|
|
for (i = 0; i < strlen(s); i++)
|
|
|
|
{
|
2017-12-02 16:05:39 +00:00
|
|
|
r = pr_tmpfile() % NRANDCHARS;
|
2017-11-04 10:52:33 +00:00
|
|
|
s[i] = (RANDCHARS)[r];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try and create a randomly-named file in directory `tmpdir`.
|
|
|
|
* If successful, allocate memory and set `tmpname_ptr` to full filepath, and return file pointer;
|
|
|
|
* otherwise return NULL.
|
|
|
|
* If `keep` is zero then create the file as temporary and it should not exist once closed.
|
|
|
|
*/
|
2017-12-02 16:33:43 +00:00
|
|
|
static FILE *mktempfile_internal(const char *tmpdir, const char *pfx, FString *tmpname_ptr, int keep)
|
2017-11-04 10:52:33 +00:00
|
|
|
/* PRE:
|
|
|
|
* pfx is not NULL and points to a valid null-terminated string
|
|
|
|
* tmpname_ptr is not NULL.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int fd;
|
|
|
|
char randpart[] = "1234567890";
|
|
|
|
int i;
|
|
|
|
int oflag, pmode;
|
|
|
|
|
|
|
|
/* In Windows, we use the _O_TEMPORARY flag with `open` to ensure the file is deleted when closed.
|
2017-12-03 11:23:43 +00:00
|
|
|
* In Unix, we use the remove function after opening the file. (This does not work in Windows,
|
|
|
|
* which does not allow an open file to be deleted.)
|
2017-11-04 10:52:33 +00:00
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* MSVC flags */
|
2017-11-04 11:12:13 +00:00
|
|
|
oflag = _O_BINARY|_O_CREAT|_O_RDWR;
|
2017-11-04 10:52:33 +00:00
|
|
|
if (!keep)
|
|
|
|
oflag |= _O_TEMPORARY;
|
|
|
|
pmode = _S_IREAD | _S_IWRITE;
|
|
|
|
#else
|
|
|
|
/* Standard POSIX flags */
|
2017-11-04 11:12:13 +00:00
|
|
|
oflag = O_CREAT|O_RDWR;
|
2017-11-04 10:52:33 +00:00
|
|
|
pmode = S_IRUSR|S_IWUSR;
|
|
|
|
#endif
|
|
|
|
|
2017-12-02 16:05:39 +00:00
|
|
|
if (!tmpdir || !DirExists(tmpdir)) {
|
2017-11-04 10:52:33 +00:00
|
|
|
errno = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-02 16:33:43 +00:00
|
|
|
FString tempname;
|
|
|
|
|
2017-11-04 10:52:33 +00:00
|
|
|
/* If we don't manage to create a file after 10 goes, there is something wrong... */
|
|
|
|
for (i = 0; i < 10; i++)
|
|
|
|
{
|
2017-12-02 16:33:43 +00:00
|
|
|
tempname.Format("%s/%s%s", tmpdir, pfx, set_randpart(randpart));
|
|
|
|
fd = OPEN_(tempname, oflag, pmode);
|
2017-11-04 10:52:33 +00:00
|
|
|
if (fd != -1) break;
|
|
|
|
}
|
|
|
|
if (fd != -1)
|
|
|
|
{ /* Success, so return user a proper ANSI C file pointer */
|
|
|
|
fp = FDOPEN_(fd, "w+b");
|
|
|
|
errno = 0;
|
2017-12-03 11:23:43 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
/* [Unix only] And make sure the file will be deleted once closed */
|
|
|
|
if (!keep) remove(tempname);
|
|
|
|
#endif
|
|
|
|
|
2017-11-04 10:52:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* We failed */
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-02 16:33:43 +00:00
|
|
|
if (tmpname_ptr) *tmpname_ptr = tempname;
|
2017-11-04 10:52:33 +00:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************/
|
|
|
|
/* EXPORTED FUNCTIONS */
|
|
|
|
/**********************/
|
|
|
|
|
2017-12-02 16:33:43 +00:00
|
|
|
FILE *tmpfileplus(const char *dir, const char *prefix, FString *pathname, int keep)
|
2017-11-04 10:52:33 +00:00
|
|
|
{
|
2017-12-02 16:33:43 +00:00
|
|
|
FILE *fp = mktempfile_internal(dir, (prefix ? prefix : "tmp."), pathname, keep);
|
|
|
|
if (!fp && pathname) *pathname = "";
|
2017-11-04 10:52:33 +00:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|