summaryrefslogtreecommitdiff
path: root/riscos/filename.c
blob: 1265c890350b8e82b2ffa40a9539d477e1aac8b3 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *		  http://www.opensource.org/licenses/gpl-license
 * Copyright 2005 Richard Wilson <info@tinct.net>
 */

/** \file
 * Provides a central method of obtaining unique filenames.
 *
 * A maximum of 2^24 files can be allocated at any point in time.
 */

#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "oslib/osfile.h"
#include "netsurf/riscos/filename.h"
#include "netsurf/utils/log.h"

#define FULL_WORD (unsigned int)4294967295

struct directory {
	char prefix[10];		/** directory prefix, eg '00.11.52.' */
  	unsigned int low_used;		/** first 32 files, 1 bit per file */
  	unsigned int high_used;		/** last 32 files, 1 bit per file */
  	unsigned int low_persistent;	/** first 32 files, 1 bit per file */
  	unsigned int high_persistent;	/** last 32 files, 1 bit per file */
	struct directory *next;		/** next directory (sorted by prefix) */
};


static struct directory *root = NULL;
static char ro_filename_buffer[12];
static char ro_filename_directory[256];

static struct directory *ro_filename_create_directory(const char *prefix);


/**
 * Request a new, unique, filename.
 *
 * \param  persistent  keep the filename allocated across sessions
 * \return a pointer to a shared buffer containing the new filename
 */
char *ro_filename_request(bool persistent) {
	struct directory *dir;
	int i = -1;
	
	for (dir = root; dir; dir = dir->next)
		if ((dir->low_used & dir->high_used) != FULL_WORD) {
		  	if (dir->low_used != FULL_WORD) {
				for (i = 0; (dir->low_used & (1 << i)); i++);
		  	} else {
				for (i = 0; (dir->high_used & (1 << i)); i++);
		  		i += 32;
		  	}
			break;
		}
	if (i == -1) {
		/* no available slots - create a new directory */
		dir = ro_filename_create_directory(NULL);
		if (!dir) {
			LOG(("Failed to create a new directory."));
			return NULL;
		}
		i = 63;
	}
	if (i < 32) {
		dir->low_used |= (1 << i);
		if (persistent) 
			dir->low_persistent |= (1 << i);
	} else {
		dir->high_used |= (1 << (i - 32));
		if (persistent) 
			dir->high_persistent |= (1 << (i - 32));
	}
	sprintf(ro_filename_buffer, "%s%i", dir->prefix, i);
	return ro_filename_buffer;
}


/**
 * Releases a filename for future use.
 *
 * \param  filename  the filename to release
 */
void ro_filename_release(const char *filename) {
  	char *last;
	char dir_prefix[16];
	int i;
	struct directory *dir;
	
	/* extract the prefix */
	sprintf(dir_prefix, filename);
	for (i = 0, last = dir_prefix; i < 3; i++)
		while (*last++ != '.');
	i = atoi(last);
	last[0] = '\0';
	
	/* modify the correct directory entry */
	for (dir = root; dir; dir = dir->next)
		if (!strcmp(dir->prefix, dir_prefix)) {
		  	if (i < 32) {
		  		dir->low_used &= ~(1 << i);
		  		dir->low_persistent &= ~(1 << i);
		  	} else {
		  		dir->high_used &= ~(1 << (i - 32));
		  		dir->high_persistent &= ~(1 << (i - 32));
		  	}
			return;
		}
}


/**
 * Initialise the filename provider and load the previous session state.
 */
bool ro_filename_initialise(void) {
	char s[16];
  	struct directory *dir;
	FILE *fp;
	int version;

	/* create the 'CACHE_FILENAME_PREFIX' structure */
	xosfile_create_dir("<Wimp$ScrapDir>.WWW", 0);
	xosfile_create_dir("<Wimp$ScrapDir>.WWW.NetSurf", 0);
	xosfile_create_dir("<Wimp$ScrapDir>.WWW.NetSurf.Cache", 0);

	/* load the persistent file list */
	fp = fopen("Choices:WWW.NetSurf.Filename", "r");
	if (!fp) {
		LOG(("Unable to open filename record for reading."));
		return true;
	}

	if (!fgets(s, 16, fp)) {
	  	fclose(fp);
		return false;
	}
	version = atoi(s);
	if (version != 100) {
	  	LOG(("Invalid or unsupported filename record."));
	  	fclose(fp);
		return false;
	}
	while (fgets(s, 16, fp)) {
		if (s[strlen(s) - 1] == '\n')
			s[strlen(s) - 1] = '\0';
		dir = ro_filename_create_directory(s);
		if (!dir) {
			LOG(("Unable to load filename record for prefix '%s'.",
					s));
		  	fclose(fp);
			return false;
		}
		if (!fgets(s, 16, fp)) {
		  	fclose(fp);
			return false;
		}
		dir->low_used = dir->low_persistent = (unsigned int)
				strtoul(s, (char **)NULL, 10);
		if (!fgets(s, 16, fp)) {
		  	fclose(fp);
			return false;
		}
		dir->high_used = dir->high_persistent = (unsigned int)
				strtoul(s, (char **)NULL, 10);
	}
  	fclose(fp);

	ro_filename_finalise();
	return true;
}


/**
 * Finalise the filename provider and save the previous session state.
 */
bool ro_filename_finalise(void) {
	struct directory *dir;
	FILE *fp;

	fp = fopen("<Choices$Write>.WWW.NetSurf.Filename", "w");
	if (!fp) {
		LOG(("Unable to open filename record for writing."));
		return false;
	}
	fprintf(fp, "100\n");

	/* dump the persistent files in the format of:
	 * [prefix]\n[low used word]\n[high used word]\n */
	for (dir = root; dir; dir = dir->next)
		if ((dir->low_persistent | dir->high_persistent) != 0)
			fprintf(fp, "%s\n%u\n%u\n",
					dir->prefix,
					dir->low_persistent,
					dir->high_persistent);
	fclose(fp);
	return true;
}


/**
 * Deletes all files in the cache directory that are not accounted for.
 */
void ro_filename_flush(void) {
	// todo: delete any files that aren't known of
	// todo: delete any empty references (?)
}


/**
 * Creates a new directory.
 *
 * \param  prefix  the prefix to use, or NULL to allocate a new one
 * \return a new directory structure, or NULL on memory exhaustion
 * 
 * Empty directories are never deleted, except by an explicit call to
 * ro_filename_flush().
 */
static struct directory *ro_filename_create_directory(const char *prefix) {
	char *last_1, *last_2;
	int result, index;
	struct directory *old_dir, *new_dir, *prev_dir = NULL;
	char dir_prefix[16];
	
	/* get the lowest unique prefix, or use the provided one */
	if (prefix) {
		strcpy(dir_prefix, prefix);
	} else {
		sprintf(dir_prefix, "00.00.00.");
		for (index = 1, old_dir = root; old_dir;
				index++, old_dir = old_dir->next) {
			if (strcmp(old_dir->prefix, dir_prefix))
				break;
			sprintf(dir_prefix, "%.2i.%.2i.%.2i.",
					((index >> 12) & 63),
					((index >> 6) & 63),
					((index >> 0) & 63));
		}
	}
	
	/* allocate a new directory */
	new_dir = (struct directory *)calloc(1,
			sizeof(struct directory));
	if (!new_dir) {
		LOG(("No memory for calloc()"));
		return NULL;
	}
	strcpy(new_dir->prefix, dir_prefix);

	/* link into the tree, sorted by prefix. */
	for (old_dir = root; old_dir; prev_dir = old_dir,
			old_dir = old_dir->next) {
		 result = strcmp(old_dir->prefix, dir_prefix);
		 if (result == 0) {
			free(new_dir);
			return old_dir;
		 } else if (result > 0)
			break;
	}
	if (!prev_dir) {
		new_dir->next = root;
		root = new_dir;
	} else {
		new_dir->next = prev_dir->next;
		prev_dir->next = new_dir;
	}
	
	/* create the directory structure */
	sprintf(ro_filename_directory, "%s.", CACHE_FILENAME_PREFIX);
	last_1 = ro_filename_directory + strlen(CACHE_FILENAME_PREFIX) + 1;
	last_2 = new_dir->prefix;
	for (int i = 0; i < 3; i++) {
		*last_1++ = *last_2++;
		while (*last_2 != '.')
			*last_1++ = *last_2++;
		last_1[0] = '\0';
		xosfile_create_dir(ro_filename_directory, 0);
	}
	return new_dir;
}