lib/fileutils: xalloc stuff in library-like code

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2014-06-18 12:57:42 +02:00
parent 65322517eb
commit f272b32c57
2 changed files with 10 additions and 6 deletions

View File

@ -7,10 +7,11 @@ static inline FILE *xfmkstemp(char **tmpname, char *dir)
{
int fd;
FILE *ret;
fd = xmkstemp(tmpname, dir);
if (fd == -1) {
if (fd == -1)
return NULL;
}
if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
close(fd);
return NULL;

View File

@ -12,7 +12,6 @@
#include "c.h"
#include "fileutils.h"
#include "pathnames.h"
#include "xalloc.h"
/* Create open temporary file in safe way. Please notice that the
* file permissions are -rw------- by default. */
@ -21,7 +20,7 @@ int xmkstemp(char **tmpname, char *dir)
char *localtmp;
char *tmpenv;
mode_t old_mode;
int fd;
int fd, rc;
/* Some use cases must be capable of being moved atomically
* with rename(2), which is the reason why dir is here. */
@ -31,11 +30,15 @@ int xmkstemp(char **tmpname, char *dir)
tmpenv = getenv("TMPDIR");
if (tmpenv)
xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
program_invocation_short_name);
else
xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
program_invocation_short_name);
if (rc < 0)
return -1;
old_mode = umask(077);
fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
umask(old_mode);