summaryrefslogtreecommitdiff
path: root/src/libs/file/temp.c
blob: 3253e0d51071e2dfe97587b9c653a513795fe02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 */

// Contains code handling temporary files and dirs

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifdef WIN32
#	include <io.h>
#endif
#include <string.h>
#include <time.h>
#include "filintrn.h"
#include "libs/timelib.h"
#include "port.h"
#include "libs/compiler.h"
#include "libs/log.h"
#include "libs/memlib.h"

static char *tempDirName;
uio_DirHandle *tempDir;

static void
removeTempDir (void)
{
	rmdir (tempDirName);
}

// Try if the null-terminated path 'dir' to a directory is valid
// as temp path.
// On success, 'buf' will be filled with the path, with a trailing /,
// null-terminated, and 0 is returned.
// On failure, EINVAL, ENAMETOOLONG, or one of the errors access() can return
// is returned, and the contents of buf is unspecified.
static int
tryTempDir (char *buf, size_t buflen, const char *dir)
{
	size_t len;
	int haveSlash;
	
	if (dir == NULL)
		return EINVAL;
	
	if (dir[0] == '\0')
		return EINVAL;

	len = strlen (dir);
	haveSlash = (dir[len - 1] == '/'
#ifdef WIN32
			|| dir[len - 1] == '\\'
#endif
			);
	if ((haveSlash ? len : len + 1) >= buflen)
		return ENAMETOOLONG;
	
	strcpy (buf, dir);
#if 0
	//def WIN32
	{
		char *bufPtr;
		for (bufPtr = buf; *bufPtr != '\0'; bufPtr++)
		{
			if (*bufPtr == '\\')
				*bufPtr = '/';
		}
	}
#endif
	if (!haveSlash)
	{
		buf[len] = '/';
		len++;
		buf[len] = '\0';
	}
	if (access (buf, R_OK | W_OK) == -1)
		return errno;

	return 0;
}

static void
getTempDir (char *buf, size_t buflen) {
	char cwd[PATH_MAX];
	
	if (tryTempDir (buf, buflen, getenv("TMP")) &&
			tryTempDir (buf, buflen, getenv("TEMP")) &&
#if !defined(WIN32) || defined (__CYGWIN__)
			tryTempDir (buf, buflen, "/tmp/") &&
			tryTempDir (buf, buflen, "/var/tmp/") &&
#endif
			tryTempDir (buf, buflen, getcwd (cwd, sizeof cwd)))
	{
		log_add (log_Fatal, "Fatal Error: Cannot find a suitable location "
				"to store temporary files.");
		exit (EXIT_FAILURE);
	}
}

// Sets the global var 'tempDir'
static int
mountTempDir(const char *name) {
	static uio_AutoMount *autoMount[] = { NULL };
	uio_MountHandle *tempHandle;
	extern uio_Repository *repository;

	tempHandle = uio_mountDir (repository, "/tmp/",
			uio_FSTYPE_STDIO, NULL, NULL, name, autoMount,
			uio_MOUNT_TOP, NULL);
	if (tempHandle == NULL) {
		int saveErrno = errno;
		log_add (log_Fatal, "Fatal error: Couldn't mount temp dir '%s': "
				"%s", name, strerror (errno));
		errno = saveErrno;
		return -1;
	}

	tempDir = uio_openDir (repository, "/tmp", 0);
	if (tempDir == NULL) {
		int saveErrno = errno;
		log_add (log_Fatal, "Fatal error: Could not open temp dir: %s",
				strerror (errno));
		errno = saveErrno;
		return -1;
	}
	return 0;
}

#define NUM_TEMP_RETRIES 16
		// Number of files to try to open before giving up.
void
initTempDir (void) {
	size_t len;
	DWORD num;
	int i;
	char *tempPtr;
			// Pointer to the location in the tempDirName string where the
			// path to the temp dir ends and the dir starts.

	tempDirName = HMalloc (PATH_MAX);
	getTempDir (tempDirName, PATH_MAX - 21);
			// reserve 8 chars for dirname, 1 for slash, and 12 for filename
	len = strlen(tempDirName);

	num = ((DWORD) time (NULL));
//	num = GetTimeCounter () % 0xffffffff;
	tempPtr = tempDirName + len;
	for (i = 0; i < NUM_TEMP_RETRIES; i++)
	{
		sprintf (tempPtr, "%08x", num + i);
		if (createDirectory (tempDirName, 0700) == -1)
			continue;
		
		// Success, we've got a temp dir.
		tempDirName = HRealloc (tempDirName, len + 9);
		atexit (removeTempDir);
		if (mountTempDir (tempDirName) == -1)
			exit (EXIT_FAILURE);
		return;
	}
	
	// Failure, could not make a temporary directory.
	log_add (log_Fatal, "Fatal error: Cannot get a name for a temporary "
			"directory.");
	exit (EXIT_FAILURE);
}

void
unInitTempDir (void) {
	uio_closeDir(tempDir);
	// the removing of the dir is handled via atexit
}

// return the path to a file in the temp dir with the specified filename.
// returns a pointer to a static buffer.
char *
tempFilePath (const char *filename) {
	static char file[PATH_MAX];
	
	if (snprintf (file, PATH_MAX, "%s/%s", tempDirName, filename) == -1) {
		log_add (log_Fatal, "Path to temp file too long.");
		exit (EXIT_FAILURE);
	}
	return file;
}