1
1

sfs.c: reimplemented cached file list using GSList.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
Этот коммит содержится в:
Andrew Borodin 2010-12-29 16:53:11 +03:00
родитель 5b7c5f09f4
Коммит 2e839a6ec6

Просмотреть файл

@ -66,7 +66,8 @@
#define F_FULLMATCH 8 #define F_FULLMATCH 8
#define COPY_CHAR \ #define COPY_CHAR \
if ((size_t) (t-pad) > sizeof(pad)) { \ if ((size_t) (t - pad) > sizeof(pad)) \
{ \
g_free (pqname); \ g_free (pqname); \
return -1; \ return -1; \
} \ } \
@ -74,25 +75,28 @@
*t++ = *s; *t++ = *s;
#define COPY_STRING(a) \ #define COPY_STRING(a) \
if ((t-pad)+strlen(a)>sizeof(pad)) { \ if ((t - pad) + strlen(a) > sizeof(pad)) \
{ \
g_free (pqname); \ g_free (pqname); \
return -1; \ return -1; \
} else { \ } \
else \
{ \
strcpy (t, a); \ strcpy (t, a); \
t+= strlen(a); \ t += strlen (a); \
} }
/*** file scope type declarations ****************************************************************/ /*** file scope type declarations ****************************************************************/
struct cachedfile typedef struct cachedfile
{ {
char *name, *cache; char *name;
struct cachedfile *next; char *cache;
}; } cachedfile;
/*** file scope variables ************************************************************************/ /*** file scope variables ************************************************************************/
static struct cachedfile *head; static GSList *head;
static struct vfs_class vfs_sfs_ops; static struct vfs_class vfs_sfs_ops;
static int sfs_no = 0; static int sfs_no = 0;
@ -101,6 +105,16 @@ static char *sfs_command[MAXFS];
static int sfs_flags[MAXFS]; static int sfs_flags[MAXFS];
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
static int
cachedfile_compare (const void *a, const void *b)
{
const cachedfile *cf = (const cachedfile *) a;
const char *name = (const char *) b;
return strcmp (name, cf->name);
}
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static int static int
@ -196,39 +210,34 @@ sfs_vfmake (struct vfs_class *me, const char *name, char *cache)
static const char * static const char *
sfs_redirect (struct vfs_class *me, const char *name) sfs_redirect (struct vfs_class *me, const char *name)
{ {
struct cachedfile *cur = head; GSList *cur;
cachedfile *cf;
char *cache; char *cache;
int handle; int handle;
while (cur) cur = g_slist_find_custom (head, name, cachedfile_compare);
if (cur != NULL)
{ {
if (!strcmp (name, cur->name)) cf = (cachedfile *) cur->data;
{ vfs_stamp (&vfs_sfs_ops, cf);
vfs_stamp (&vfs_sfs_ops, cur); return cf->cache;
return cur->cache; }
}
cur = cur->next;
}
handle = vfs_mkstemps (&cache, "sfs", name); handle = vfs_mkstemps (&cache, "sfs", name);
if (handle == -1) if (handle == -1)
{
return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US"; return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
}
close (handle); close (handle);
if (!sfs_vfmake (me, name, cache)) if (sfs_vfmake (me, name, cache) == 0)
{ {
cur = g_new (struct cachedfile, 1); cf = g_new (cachedfile, 1);
cur->name = g_strdup (name); cf->name = g_strdup (name);
cur->cache = cache; cf->cache = cache;
cur->next = head; head = g_slist_prepend (head, cf);
head = cur;
vfs_stamp_create (&vfs_sfs_ops, head);
vfs_stamp_create (&vfs_sfs_ops, (cachedfile *) head->data);
return cache; return cache;
} }
@ -319,18 +328,13 @@ sfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
static vfsid static vfsid
sfs_getid (struct vfs_class *me, const char *path) sfs_getid (struct vfs_class *me, const char *path)
{ {
struct cachedfile *cur = head; GSList *cur;
(void) me; (void) me;
while (cur) cur = g_slist_find_custom (head, path, cachedfile_compare);
{
if (!strcmp (path, cur->name))
break;
cur = cur->next;
}
return (vfsid) cur; return (vfsid) (cur != NULL ? cur->data : NULL);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -338,23 +342,21 @@ sfs_getid (struct vfs_class *me, const char *path)
static void static void
sfs_free (vfsid id) sfs_free (vfsid id)
{ {
struct cachedfile *which = (struct cachedfile *) id; struct cachedfile *which;
struct cachedfile *cur, *prev; GSList *cur;
for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next) which = (struct cachedfile *) id;
; cur = g_slist_find (head, which);
if (!cur) if (cur == NULL)
vfs_die ("Free of thing which is unknown to me\n"); vfs_die ("Free of thing which is unknown to me\n");
unlink (cur->cache);
if (prev) which = (struct cachedfile *) cur->data;
prev->next = cur->next; unlink (which->cache);
else g_free (which->cache);
head = cur->next; g_free (which->name);
g_free (which);
g_free (cur->cache); head = g_slist_delete_link (head, cur);
g_free (cur->name);
g_free (cur);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -362,15 +364,12 @@ sfs_free (vfsid id)
static void static void
sfs_fill_names (struct vfs_class *me, fill_names_f func) sfs_fill_names (struct vfs_class *me, fill_names_f func)
{ {
struct cachedfile *cur = head; GSList *cur;
(void) me; (void) me;
while (cur) for (cur = head; cur != NULL; cur = g_slist_next (cur))
{ func (((cachedfile *) cur->data)->name);
(*func) (cur->name);
cur = cur->next;
}
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */