netsurf/utils/filename.c

550 lines
13 KiB
C
Raw Normal View History

/*
* Copyright 2006 Richard Wilson <info@tinct.net>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf 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; version 2 of the License.
*
* NetSurf 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, see <http://www.gnu.org/licenses/>.
*/
/** \file
* Provides a central method of obtaining unique filenames.
*
* A maximum of 2^24 files can be allocated at any point in time.
*/
#include <assert.h>
Merged revisions 5309-5406,5409-5422 via svnmerge from svn://svn.netsurf-browser.org/branches/vince/netsurf-fb ........ r5309 | vince | 2008-09-13 10:59:10 +0100 (Sat, 13 Sep 2008) | 2 lines first stab at framebuffer frontend ........ r5313 | vince | 2008-09-14 15:08:52 +0100 (Sun, 14 Sep 2008) | 2 lines add line plotters ........ r5314 | vince | 2008-09-14 15:28:12 +0100 (Sun, 14 Sep 2008) | 2 lines add rectangle plot to 16bpp plotters ........ r5315 | vince | 2008-09-14 19:58:57 +0100 (Sun, 14 Sep 2008) | 2 lines improve 16bpp image plot ........ r5316 | vince | 2008-09-15 00:35:32 +0100 (Mon, 15 Sep 2008) | 2 lines abstract the os specific framebuffer init ........ r5317 | vince | 2008-09-15 11:18:51 +0100 (Mon, 15 Sep 2008) | 2 lines first cut of linux frontend ........ r5318 | vince | 2008-09-15 12:01:00 +0100 (Mon, 15 Sep 2008) | 2 lines remove junk includes ........ r5319 | vince | 2008-09-15 12:09:02 +0100 (Mon, 15 Sep 2008) | 2 lines make plotters OS agnostic again ........ r5322 | vince | 2008-09-15 15:55:01 +0100 (Mon, 15 Sep 2008) | 2 lines Linux frontend operates ........ r5323 | vince | 2008-09-15 16:32:47 +0100 (Mon, 15 Sep 2008) | 2 lines abstract out OS specific input ........ r5326 | vince | 2008-09-15 19:21:01 +0100 (Mon, 15 Sep 2008) | 2 lines Improve linux mode setting ........ r5329 | vince | 2008-09-15 21:13:33 +0100 (Mon, 15 Sep 2008) | 2 lines improve text clipping ........ r5339 | vince | 2008-09-16 00:07:57 +0100 (Tue, 16 Sep 2008) | 2 lines possibly fix text clipping issue ........ r5342 | vince | 2008-09-16 00:39:36 +0100 (Tue, 16 Sep 2008) | 2 lines consolidate polygon plotters ........ r5344 | dsilvers | 2008-09-16 10:21:06 +0100 (Tue, 16 Sep 2008) | 1 line Fix up the framebuffer target makefile a bit more, add some config options for it ........ r5345 | dsilvers | 2008-09-16 10:22:19 +0100 (Tue, 16 Sep 2008) | 1 line Ensure the appropriate frontend is selected when building framebuffer ........ r5346 | dsilvers | 2008-09-16 10:27:16 +0100 (Tue, 16 Sep 2008) | 1 line Update build system to support targetting separate framebuffer frontends in different build trees, update executable to be nsfb-blah ........ r5350 | vince | 2008-09-16 17:15:04 +0100 (Tue, 16 Sep 2008) | 1 line Add -g to provide symbols for framebuffer link ........ r5351 | vince | 2008-09-16 17:17:09 +0100 (Tue, 16 Sep 2008) | 1 line framebuffer scheduler now works, plotters tweaked, gui tracks window redraw requirements better, keypresses not duplicated under linux fb ........ r5352 | dsilvers | 2008-09-16 17:38:53 +0100 (Tue, 16 Sep 2008) | 1 line Ensure we only allow one fetcher at a time ........ r5361 | vince | 2008-09-17 11:48:44 +0100 (Wed, 17 Sep 2008) | 2 lines initial cursor support ........ r5362 | vince | 2008-09-17 13:56:47 +0100 (Wed, 17 Sep 2008) | 2 lines add mouse handling ........ r5363 | vince | 2008-09-17 14:14:44 +0100 (Wed, 17 Sep 2008) | 2 lines add framebuffer resources ........ r5364 | vince | 2008-09-17 17:12:21 +0100 (Wed, 17 Sep 2008) | 2 lines add reasonable pointer ........ r5366 | vince | 2008-09-17 17:17:25 +0100 (Wed, 17 Sep 2008) | 2 lines fix pointer alpha ........ r5370 | vince | 2008-09-18 13:43:53 +0100 (Thu, 18 Sep 2008) | 2 lines warning squash and cleanup ready for trunk merge ........ r5375 | vince | 2008-09-19 14:58:43 +0100 (Fri, 19 Sep 2008) | 2 lines Working mouse navigation ........ r5377 | vince | 2008-09-20 14:06:22 +0100 (Sat, 20 Sep 2008) | 2 lines Improve scrolling ........ r5378 | vince | 2008-09-20 14:46:46 +0100 (Sat, 20 Sep 2008) | 2 lines fix redraw issues with scrolling ........ r5380 | vince | 2008-09-20 17:08:43 +0100 (Sat, 20 Sep 2008) | 3 lines Alter panning to use its own flag so it doesnt cause invalid redraw operations ........ r5381 | vince | 2008-09-20 21:52:45 +0100 (Sat, 20 Sep 2008) | 2 lines add dummy framebuffer ........ r5383 | vince | 2008-09-21 00:00:15 +0100 (Sun, 21 Sep 2008) | 2 lines fix segfault when cursor is off teh bottom of teh screen ........ r5384 | vince | 2008-09-21 00:06:08 +0100 (Sun, 21 Sep 2008) | 2 lines fix off by one in pointer fix ........ r5385 | vince | 2008-09-21 00:25:09 +0100 (Sun, 21 Sep 2008) | 2 lines when fixing bloody silly off by one errors remember to fix *both* references ........ r5387 | vince | 2008-09-21 00:38:13 +0100 (Sun, 21 Sep 2008) | 2 lines last try at stopping the pointer segfault ........ r5388 | vince | 2008-09-21 16:24:18 +0100 (Sun, 21 Sep 2008) | 2 lines improve vertical text clipping ........ r5392 | vince | 2008-09-21 23:11:51 +0100 (Sun, 21 Sep 2008) | 2 lines Improve text plotters ........ r5393 | vince | 2008-09-21 23:34:38 +0100 (Sun, 21 Sep 2008) | 2 lines fix 32bpp line plotting ........ r5394 | vince | 2008-09-22 00:00:03 +0100 (Mon, 22 Sep 2008) | 2 lines Fix off by one error in line plotting clipping ........ r5397 | vince | 2008-09-22 13:46:22 +0100 (Mon, 22 Sep 2008) | 2 lines Fix bitmap tileing ........ r5398 | vince | 2008-09-22 17:46:02 +0100 (Mon, 22 Sep 2008) | 2 lines enable knockout renderer ........ r5399 | vince | 2008-09-22 18:43:48 +0100 (Mon, 22 Sep 2008) | 2 lines ensure clipping region lies within window, caused by knockout renderer ........ r5400 | vince | 2008-09-22 19:20:25 +0100 (Mon, 22 Sep 2008) | 2 lines update cursor to one swiped from X windows ........ r5405 | vince | 2008-09-23 09:09:05 +0100 (Tue, 23 Sep 2008) | 2 lines fix vertical scroll limit ........ r5412 | dsilvers | 2008-09-23 10:53:14 +0100 (Tue, 23 Sep 2008) | 1 line Revert noisy fetcher patch ........ r5413 | dsilvers | 2008-09-23 10:58:00 +0100 (Tue, 23 Sep 2008) | 1 line Add header guards ........ r5414 | dsilvers | 2008-09-23 11:31:31 +0100 (Tue, 23 Sep 2008) | 1 line Tidy the region clipping slightly ........ r5416 | dsilvers | 2008-09-23 12:05:00 +0100 (Tue, 23 Sep 2008) | 1 line Rationalise how the framebuffer frontend finds resources and options ........ r5418 | dsilvers | 2008-09-23 13:59:00 +0100 (Tue, 23 Sep 2008) | 1 line Ensure options are overridden after load, and squash an fb_gui.c warning ........ r5419 | dsilvers | 2008-09-23 14:20:07 +0100 (Tue, 23 Sep 2008) | 1 line Support fb_mode and fb_device options ........ r5420 | dsilvers | 2008-09-23 14:21:48 +0100 (Tue, 23 Sep 2008) | 1 line Support option_fb_device in the able frontend ........ r5421 | dsilvers | 2008-09-23 14:25:17 +0100 (Tue, 23 Sep 2008) | 1 line Ensure target_blank is disabled ........ r5422 | dsilvers | 2008-09-23 14:39:00 +0100 (Tue, 23 Sep 2008) | 1 line Rework linux fb frontend to support scanning and opening all event devices for input ........ svn path=/trunk/netsurf/; revision=5423
2008-09-23 18:00:40 +04:00
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils/dirent.h"
#include "utils/filename.h"
#include "utils/log.h"
#include "utils/utils.h"
#define FULL_WORD (unsigned int)0xffffffffu
#define START_PREFIX ('0' + '0' * 10)
struct directory {
int numeric_prefix; /** numeric representation of prefix */
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 */
struct directory *next; /** next directory (sorted by prefix) */
};
static struct directory *root = NULL;
static char filename_buffer[12];
static char filename_directory[256];
static struct directory *filename_create_directory(const char *prefix);
static bool filename_flush_directory(const char *folder, int depth);
static bool filename_delete_recursive(char *folder);
/**
* Request a new, unique, filename.
*
* \return a pointer to a shared buffer containing the new filename,
* NULL on failure
*/
const char *filename_request(void)
{
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 = filename_create_directory(NULL);
if (dir == NULL) {
NSLOG(netsurf, INFO,
"Failed to create a new directory.");
return NULL;
}
i = 63;
}
if (i < 32)
dir->low_used |= (1 << i);
else
dir->high_used |= (1 << (i - 32));
sprintf(filename_buffer, "%s%.2i", dir->prefix, i);
return filename_buffer;
}
/**
* Claim a specific filename.
*
* \param filename the filename to claim
* \return whether the claim was successful
*/
bool filename_claim(const char *filename)
{
char dir_prefix[9];
int file;
struct directory *dir;
/* filename format is always '01/23/45/XX' */
strncpy(dir_prefix, filename, 9);
dir_prefix[8] = '\0';
file = (filename[10] + filename[9] * 10 - START_PREFIX);
/* create the directory */
dir = filename_create_directory(dir_prefix);
if (dir == NULL)
return false;
/* update the entry */
if (file < 32) {
if (dir->low_used & (1 << file))
return false;
dir->low_used |= (1 << file);
} else {
if (dir->high_used & (1 << (file - 32)))
return false;
dir->high_used |= (1 << (file - 32));
}
return true;
}
/**
* Releases a filename for future use.
*
* \param filename the filename to release
*/
void filename_release(const char *filename)
{
struct directory *dir;
int index, file;
/* filename format is always '01/23/45/XX' */
index = ((filename[7] + filename[6] * 10 - START_PREFIX) |
((filename[4] + filename[3] * 10 - START_PREFIX) << 6) |
((filename[1] + filename[0] * 10 - START_PREFIX) << 12));
file = (filename[10] + filename[9] * 10 - START_PREFIX);
/* modify the correct directory entry */
for (dir = root; dir; dir = dir->next) {
if (dir->numeric_prefix == index) {
if (file < 32)
dir->low_used &= ~(1 << file);
else
dir->high_used &= ~(1 << (file - 32));
return;
}
}
}
/**
* Initialise the filename provider.
*/
bool filename_initialise(void)
{
char *directory, *start;
int ret;
directory = strdup(TEMP_FILENAME_PREFIX);
if (directory == NULL)
return false;
for (start = directory; *start != '\0'; start++) {
if (*start == '/') {
*start = '\0';
NSLOG(netsurf, INFO, "Creating \"%s\"", directory);
ret = nsmkdir(directory, S_IRWXU);
if (ret != 0 && errno != EEXIST) {
NSLOG(netsurf, INFO,
"Failed to create directory \"%s\"",
directory);
free(directory);
return false;
}
*start = '/';
}
}
NSLOG(netsurf, INFO, "Temporary directory location: %s", directory);
ret = nsmkdir(directory, S_IRWXU);
free(directory);
if (ret != 0) {
return false;
}
return true;
}
/**
* Deletes all files in the cache directory that are not accounted for.
*/
void filename_flush(void)
{
while (filename_flush_directory(TEMP_FILENAME_PREFIX, 0));
}
/**
* Deletes some files in a directory that are not accounted for.
*
* A single call to this function may not delete all the files in
* a directory. It should be called until it returns false.
*
* \param folder the folder to search
* \param depth the folder depth
* \returns whether further calls may be needed
*/
bool filename_flush_directory(const char *folder, int depth)
{
DIR *parent;
struct dirent *entry;
bool changed = false;
bool del;
int number, i;
int prefix = 0;
unsigned int prefix_mask = (0x3f << 12);
char child[256];
const char *prefix_start = NULL;
struct directory *dir = NULL;
/* Maximum permissible depth is 3 */
assert(depth <= 3);
if (depth > 0) {
/* Not a top-level directory, so determine the prefix
* by removing the last /XX component */
prefix_start = folder + strlen(folder) - depth * 3 + 1;
}
/* Calculate the numeric prefix */
for (i = 0; i < depth; i++) {
number = prefix_start[1] + prefix_start[0] * 10 - START_PREFIX;
prefix |= (number << (12 - i * 6));
prefix_mask |= (0x3f << (12 - i * 6));
prefix_start += 3;
}
/* If we're flushing a leaf directory, find it in the list */
if (depth == 3) {
for (dir = root; dir; dir = dir->next) {
if (dir->numeric_prefix == prefix)
break;
}
if (dir == NULL)
return false;
}
parent = opendir(folder);
while ((entry = readdir(parent))) {
int written;
struct stat statbuf;
/* Ignore '.' and '..' */
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
written = snprintf(child, sizeof(child), "%s/%s",
folder, entry->d_name);
if (written == sizeof(child)) {
child[sizeof(child) - 1] = '\0';
}
if (stat(child, &statbuf) == -1) {
NSLOG(netsurf, INFO, "Unable to stat %s: %s", child,
strerror(errno));
continue;
}
/* first 3 depths are directories only, then files only */
if (depth < 3) {
/* Delete any unexpected files */
del = !S_ISDIR(statbuf.st_mode);
} else {
/* Delete any unexpected directories */
del = S_ISDIR(statbuf.st_mode);
}
/* check we are a file numbered '00' -> '63' */
if (del == false && (entry->d_name[0] >= '0') &&
(entry->d_name[0] <= '6') &&
(entry->d_name[1] >= '0') &&
(entry->d_name[1] <= '9') &&
(entry->d_name[2] == '\0')) {
number = atoi(entry->d_name);
if (number >= 0 && number <= 63) {
if (depth == 3) {
/* File: delete if not in bitfield */
if (number < 32)
del = !(dir->low_used &
(1 << number));
else
del = !(dir->high_used &
(1 << (number - 32)));
} else {
/* Directory: delete unless in list */
del = true;
/* Insert into numeric prefix */
prefix &= ~(0x3f << (12 - depth * 6));
prefix |= (number << (12 - depth * 6));
/* Find in dir list */
for (dir = root; dir; dir = dir->next) {
number = dir->numeric_prefix &
prefix_mask;
if (number == prefix) {
/* In list: retain */
del = false;
break;
}
}
}
} else {
/* Unexpected name: delete */
del = true;
}
} else {
/* Unexpected name: delete */
del = true;
}
/* continue if this is a file we want to retain */
if (del == false && (!S_ISDIR(statbuf.st_mode)))
continue;
/* delete or recurse */
if (del) {
if (S_ISDIR(statbuf.st_mode))
filename_delete_recursive(child);
if (remove(child))
NSLOG(netsurf, INFO, "Failed to remove '%s'",
child);
else
changed = true;
} else {
while (filename_flush_directory(child, depth + 1));
}
}
closedir(parent);
return changed;
}
/**
* Recursively deletes the contents of a directory
*
2014-11-13 02:27:13 +03:00
* \param folder the directory to delete
* \return true on success, false otherwise
*/
bool filename_delete_recursive(char *folder)
{
DIR *parent;
struct dirent *entry;
char child[256];
struct stat statbuf;
parent = opendir(folder);
while ((entry = readdir(parent))) {
int written;
/* Ignore '.' and '..' */
if (strcmp(entry->d_name, ".") == 0 ||
strcmp(entry->d_name, "..") == 0)
continue;
written = snprintf(child, sizeof(child), "%s/%s",
folder, entry->d_name);
if (written == sizeof(child)) {
child[sizeof(child) - 1] = '\0';
}
if (stat(child, &statbuf) == -1) {
NSLOG(netsurf, INFO, "Unable to stat %s: %s", child,
strerror(errno));
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
if (!filename_delete_recursive(child)) {
closedir(parent);
return false;
}
}
if (remove(child)) {
NSLOG(netsurf, INFO, "Failed to remove '%s'", child);
closedir(parent);
return false;
}
}
closedir(parent);
return true;
}
/**
* 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 or
* creation failure
*
* Empty directories are never deleted, except by an explicit call to
* filename_flush().
*/
static struct directory *filename_create_directory(const char *prefix)
{
char *last_1, *last_2;
int index;
struct directory *old_dir, *new_dir, *prev_dir = NULL;
char dir_prefix[16];
int i;
/* get the lowest unique prefix, or use the provided one */
if (prefix == NULL) {
for (index = 0, old_dir = root; old_dir;
index++, old_dir = old_dir->next) {
if (old_dir->numeric_prefix != index)
break;
prev_dir = old_dir;
}
sprintf(dir_prefix, "%.2i/%.2i/%.2i/",
((index >> 12) & 63),
((index >> 6) & 63),
((index >> 0) & 63));
prefix = dir_prefix;
} else {
/* prefix format is always '01/23/45/' */
index = ((prefix[7] + prefix[6] * 10 - START_PREFIX) |
((prefix[4] + prefix[3] * 10 - START_PREFIX) << 6) |
((prefix[1] + prefix[0] * 10 - START_PREFIX) << 12));
for (old_dir = root; old_dir; old_dir = old_dir->next) {
if (old_dir->numeric_prefix == index)
return old_dir;
else if (old_dir->numeric_prefix > index)
break;
prev_dir = old_dir;
}
}
/* allocate a new directory */
new_dir = malloc(sizeof(struct directory));
if (new_dir == NULL) {
NSLOG(netsurf, INFO, "No memory for malloc()");
return NULL;
}
strncpy(new_dir->prefix, prefix, 9);
new_dir->prefix[9] = '\0';
new_dir->low_used = new_dir->high_used = 0;
new_dir->numeric_prefix = index;
if (prev_dir == NULL) {
new_dir->next = root;
root = new_dir;
} else {
new_dir->next = prev_dir->next;
prev_dir->next = new_dir;
}
/* if the previous directory has the same parent then we can simply
* create the child. */
if (prev_dir && strncmp(prev_dir->prefix, new_dir->prefix, 6) == 0) {
new_dir->prefix[8] = '\0';
sprintf(filename_directory, "%s/%s",
TEMP_FILENAME_PREFIX,
new_dir->prefix);
new_dir->prefix[8] = '/';
if (!is_dir(filename_directory)) {
if (!nsmkdir(filename_directory, S_IRWXU))
return new_dir;
/* the user has probably deleted the parent directory
* whilst we are running if there is an error, so we
* don't report this yet and try to create the
* structure normally. */
NSLOG(netsurf, INFO,
"Failed to create optimised structure '%s'",
filename_directory);
}
}
/* create the directory structure */
sprintf(filename_directory, "%s/", TEMP_FILENAME_PREFIX);
last_1 = filename_directory + SLEN(TEMP_FILENAME_PREFIX) + 1;
last_2 = new_dir->prefix;
/* create each subdirectory, up to the maximum depth of 3 */
for (i = 0; i < 3 && *last_2; i++) {
*last_1++ = *last_2++;
while (*last_2 && *last_2 != '/')
*last_1++ = *last_2++;
if (*last_2) {
last_1[0] = '\0';
if (!is_dir(filename_directory)) {
if (nsmkdir(filename_directory, S_IRWXU)) {
NSLOG(netsurf, INFO,
"Failed to create directory '%s'",
filename_directory);
return NULL;
}
}
}
}
return new_dir;
}