<commit_before>/* Copyright (c) 2007-2008 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "hex-binary.h"
#include "randgen.h"
#include "hostpid.h"
#include "safe-mkstemp.h"

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	size_t prefix_len;
	struct stat st;
	unsigned char randbuf[8];
	int fd;

	prefix_len = str_len(prefix);
	for (;;) {
		do {
			random_fill_weak(randbuf, sizeof(randbuf));
			str_truncate(prefix, prefix_len);
			str_append(prefix,
				   binary_to_hex(randbuf, sizeof(randbuf)));
		} while (lstat(str_c(prefix), &st) == 0);

		if (errno != ENOENT) {
			i_error("stat(%s) failed: %m", str_c(prefix));
			return -1;
		}

		fd = open(str_c(prefix), O_RDWR | O_EXCL | O_CREAT, mode);
		if (fd != -1)
			break;

		if (errno != EEXIST) {
			if (errno != ENOENT && errno != EACCES)
				i_error("open(%s) failed: %m", str_c(prefix));
			return -1;
		}
	}
	if (uid != (uid_t)-1 || gid != (gid_t)-1) {
		if (fchown(fd, uid, gid) < 0) {
			i_error("fchown(%s) failed: %m", str_c(prefix));
			(void)close(fd);
			(void)unlink(str_c(prefix));
			return -1;<commit_msg>safe_mkstemp(): If fchown() fails, log the used uid/gid.<commit_after>/* Copyright (c) 2007-2008 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "hex-binary.h"
#include "randgen.h"
#include "hostpid.h"
#include "safe-mkstemp.h"

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int safe_mkstemp(string_t *prefix, mode_t mode, uid_t uid, gid_t gid)
{
	size_t prefix_len;
	struct stat st;
	unsigned char randbuf[8];
	int fd;

	prefix_len = str_len(prefix);
	for (;;) {
		do {
			random_fill_weak(randbuf, sizeof(randbuf));
			str_truncate(prefix, prefix_len);
			str_append(prefix,
				   binary_to_hex(randbuf, sizeof(randbuf)));
		} while (lstat(str_c(prefix), &st) == 0);

		if (errno != ENOENT) {
			i_error("stat(%s) failed: %m", str_c(prefix));
			return -1;
		}

		fd = open(str_c(prefix), O_RDWR | O_EXCL | O_CREAT, mode);
		if (fd != -1)
			break;

		if (errno != EEXIST) {
			if (errno != ENOENT && errno != EACCES)
				i_error("open(%s) failed: %m", str_c(prefix));
			return -1;
		}
	}
	if (uid != (uid_t)-1 || gid != (gid_t)-1) {
		if (fchown(fd, uid, gid) < 0) {
			i_error("fchown(%s, %ld, %ld) failed: %m",
				str_c(prefix),
				uid == (uid_t)-1 ? -1L : (long)uid,
				gid == (gid_t)-1 ? -1L : (long)gid);
			(void)close(fd);
			(void)unlink(str_c(prefix));
			return -1;
{'fraction_non_alphanumeric': 0.18644731688209948, 'fraction_numerical': 0.014884449667058363, 'mean_word_length': 3.8555133079847907, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '477258', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1091, 'n_words': 244}