/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>

#include <gal/util/e-util.h>

#include "e-mktemp.h"

#define d(x) x

/* define to put temporary files in ~/evolution/cache/tmp */
#define TEMP_HOME (1)

/* how old things need to be to expire */
#define TEMP_EXPIRE (60*60*2)
/* dont scan more often than this */
#define TEMP_SCAN (60)

static int
expire_dir_rec(const char *base, time_t now)
{
	DIR *dir;
	struct dirent *d;
	GString *path;
	size_t len;
	struct stat st;
	int count = 0;

	printf("expire dir '%s'\n", base);

	dir = opendir(base);
	if (dir == NULL)
		return 0;

	path = g_string_new(base);
	len = path->len;

	while ( (d = readdir(dir)) ) {
		if (strcmp(d->d_name, ".") == 0
		    || strcmp(d->d_name, "..") == 0)
			continue;

		g_string_truncate(path, len);
		g_string_append_printf(path, "/%s", d->d_name);
		d(printf("Checking '%s' for expiry\n", path->str));

		if (stat(path->str, &st) == 0
		    && st.st_atime + TEMP_EXPIRE < now) {
			if (S_ISDIR(st.st_mode)) {
				if (expire_dir_rec(path->str, now) == 0) {
					printf("Removing dir '%s'\n", path->str);
					rmdir(path->str);
				} else {
					count++;
				}
			} else if (unlink(path->str) == -1) {
				printf("expiry failed: %s\n", strerror(errno));
				count++;
			} else {
				printf("expired %s\n", path->str);
			}
		} else {
			count++;
		}
	}
	g_string_free(path, TRUE);
	closedir(dir);

	printf("expire dir '%s' %d remaining files\n", base, count);

	return count;
}

static GString *
get_dir (gboolean make)
{
	GString *path;
	time_t now = time(0);
	static time_t last = 0;
	
#ifdef TEMP_HOME
	path = g_string_new (g_get_home_dir());
	g_string_append_printf(path, "/.evolution/cache/tmp");
	if (make && e_mkdir_hier(path->str, 0777) == -1) {
		g_string_free(path, TRUE);
		path = NULL;
	}
#else
	path = g_string_new("/tmp/evolution-");
	g_string_append_printf (path, "%d", (int) getuid ());
	if (make) {
		int ret;
		
		/* shoot now, ask questions later */
		ret = mkdir (path->str, S_IRWXU);
		if (ret == -1) {
			if (errno == EEXIST) {
				struct stat st;
				
				if (stat (path->str, &st) == -1) {
					/* reset errno */
					errno = EEXIST;
					g_string_free (path, TRUE);
					return NULL;
				}
				
				/* make sure this is a directory and belongs to us... */
				if (!S_ISDIR (st.st_mode) || st.st_uid != getuid ()) {
					/* eek! this is bad... */
					g_string_free (path, TRUE);
					return NULL;
				}
			} else {
				/* some other error...do not pass go, do not collect $200 */
				g_string_free (path, TRUE);
				return NULL;
			}
		}
	}
#endif	

	if (path)
		printf("temp dir '%s'\n", path->str);

	/* fire off an expirey attempt no more often than TEMP_SCAN seconds */
	if (path && (last+TEMP_SCAN) < now) {
		last = now;
		expire_dir_rec(path->str, now);
	}

	return path;
}

const char *
e_mktemp (const char *template)
{
	GString *path;
	char *ret;
	
	path = get_dir (TRUE);
	if (!path)
		return NULL;
	
	g_string_append_c (path, '/');
	if (template)
		g_string_append (path, template);
	else
		g_string_append (path, "unknown-XXXXXX");
	
	ret = mktemp (path->str);
	g_string_free(path, ret == NULL);
	
	return ret;
}


int
e_mkstemp (const char *template)
{
	GString *path;
	int fd;
	
	path = get_dir (TRUE);
	if (!path)
		return -1;
	
	g_string_append_c (path, '/');
	if (template)
		g_string_append (path, template);
	else
		g_string_append (path, "unknown-XXXXXX");
	
	fd = mkstemp (path->str);
	g_string_free(path, TRUE);
	
	return fd;
}


const char *
e_mkdtemp (const char *template)
{
	GString *path;
	char *tmpdir;
	
	path = get_dir (TRUE);
	if (!path)
		return NULL;
	
	g_string_append_c (path, '/');
	if (template)
		g_string_append (path, template);
	else
		g_string_append (path, "unknown-XXXXXX");
	
#ifdef HAVE_MKDTEMP
	tmpdir = mkdtemp (path->str);
#else
	tmpdir = mktemp (path->str);
	if (tmpdir) {
		if (mkdir (tmpdir, S_IRWXU) == -1)
			tmpdir = NULL;
	}
#endif
	g_string_free(path, tmpdir == NULL);
	
	return tmpdir;
}
ption value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15'>dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2'>dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0'>dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2'>dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2</option>
<option value='main' selected='selected'>main</option>
<option value='master'>master</option>
<option value='svn_head'>svn_head</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD Ports (https://github.com/freebsd/freebsd-ports)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/about/'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/refs/'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/stats/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>
<select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel'>devel</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32'>electron32</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32/files'>files</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc'>patch-chrome_browser_accessibility_page__colors.cc</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/devel/electron32/files/patch-chrome_browser_accessibility_page__colors.cc?showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>