2008-10-08 06:55:10 +02:00
|
|
|
#include "dirvec.h"
|
2008-10-08 10:49:05 +02:00
|
|
|
#include "directory.h"
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-10-20 22:18:12 +02:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2009-01-03 14:53:02 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2008-12-28 21:02:30 +01:00
|
|
|
static GMutex *nr_lock = NULL;
|
2008-10-21 01:34:36 +02:00
|
|
|
|
2008-10-21 01:31:36 +02:00
|
|
|
static size_t dv_size(const struct dirvec *dv)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
return dv->nr * sizeof(struct directory *);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only used for sorting/searching a dirvec, not general purpose compares */
|
|
|
|
static int dirvec_cmp(const void *d1, const void *d2)
|
|
|
|
{
|
2008-10-08 10:49:05 +02:00
|
|
|
const struct directory *a = ((const struct directory * const *)d1)[0];
|
|
|
|
const struct directory *b = ((const struct directory * const *)d2)[0];
|
2009-01-15 00:09:18 +01:00
|
|
|
return g_utf8_collate(a->path, b->path);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
2008-12-28 21:02:30 +01:00
|
|
|
void dirvec_init(void)
|
|
|
|
{
|
|
|
|
g_assert(nr_lock == NULL);
|
|
|
|
nr_lock = g_mutex_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dirvec_deinit(void)
|
|
|
|
{
|
|
|
|
g_assert(nr_lock != NULL);
|
|
|
|
g_mutex_free(nr_lock);
|
|
|
|
nr_lock = NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-08 06:55:10 +02:00
|
|
|
void dirvec_sort(struct dirvec *dv)
|
|
|
|
{
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-08 10:49:05 +02:00
|
|
|
qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 15:24:34 +02:00
|
|
|
struct directory *dirvec_find(const struct dirvec *dv, const char *path)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
2008-10-26 21:28:16 +01:00
|
|
|
char *base;
|
2008-10-08 06:55:10 +02:00
|
|
|
int i;
|
2008-10-21 01:34:36 +02:00
|
|
|
struct directory *ret = NULL;
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-10-26 21:28:16 +01:00
|
|
|
base = g_path_get_basename(path);
|
2008-10-13 16:33:04 +02:00
|
|
|
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
for (i = dv->nr; --i >= 0; )
|
2008-10-26 21:28:16 +01:00
|
|
|
if (!strcmp(directory_get_name(dv->base[i]), base)) {
|
2008-10-21 01:34:36 +02:00
|
|
|
ret = dv->base[i];
|
|
|
|
break;
|
2008-10-20 22:18:12 +02:00
|
|
|
}
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-20 22:18:12 +02:00
|
|
|
|
2008-10-26 21:28:16 +01:00
|
|
|
g_free(base);
|
2008-10-21 01:34:36 +02:00
|
|
|
return ret;
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
int dirvec_delete(struct dirvec *dv, struct directory *del)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
2008-10-14 11:10:00 +02:00
|
|
|
size_t i;
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-14 11:10:00 +02:00
|
|
|
for (i = 0; i < dv->nr; ++i) {
|
2008-10-08 06:55:10 +02:00
|
|
|
if (dv->base[i] != del)
|
|
|
|
continue;
|
2008-10-08 11:07:58 +02:00
|
|
|
/* we _don't_ call directory_free() here */
|
2008-10-08 06:55:10 +02:00
|
|
|
if (!--dv->nr) {
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2009-01-03 14:53:02 +01:00
|
|
|
g_free(dv->base);
|
2008-10-08 06:55:10 +02:00
|
|
|
dv->base = NULL;
|
2008-10-21 01:34:36 +02:00
|
|
|
return i;
|
2008-10-08 06:55:10 +02:00
|
|
|
} else {
|
|
|
|
memmove(&dv->base[i], &dv->base[i + 1],
|
2008-10-14 11:10:00 +02:00
|
|
|
(dv->nr - i) * sizeof(struct directory *));
|
2009-01-03 14:53:02 +01:00
|
|
|
dv->base = g_realloc(dv->base, dv_size(dv));
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
2008-10-21 01:34:36 +02:00
|
|
|
break;
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
|
2008-10-21 01:34:36 +02:00
|
|
|
return i;
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:05 +02:00
|
|
|
void dirvec_add(struct dirvec *dv, struct directory *add)
|
2008-10-08 06:55:10 +02:00
|
|
|
{
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
++dv->nr;
|
2009-01-03 14:53:02 +01:00
|
|
|
dv->base = g_realloc(dv->base, dv_size(dv));
|
2008-10-08 06:55:10 +02:00
|
|
|
dv->base[dv->nr - 1] = add;
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dirvec_destroy(struct dirvec *dv)
|
|
|
|
{
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-21 01:34:36 +02:00
|
|
|
dv->nr = 0;
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-08 06:55:10 +02:00
|
|
|
if (dv->base) {
|
2009-01-03 14:53:02 +01:00
|
|
|
g_free(dv->base);
|
2008-10-08 06:55:10 +02:00
|
|
|
dv->base = NULL;
|
|
|
|
}
|
|
|
|
}
|
2008-10-21 01:30:30 +02:00
|
|
|
|
|
|
|
int dirvec_for_each(const struct dirvec *dv,
|
|
|
|
int (*fn)(struct directory *, void *), void *arg)
|
|
|
|
{
|
|
|
|
size_t i;
|
2008-10-21 01:35:11 +02:00
|
|
|
size_t prev_nr;
|
2008-10-21 01:30:30 +02:00
|
|
|
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock);
|
2008-10-21 01:35:11 +02:00
|
|
|
for (i = 0; i < dv->nr; ) {
|
2008-10-21 01:30:30 +02:00
|
|
|
struct directory *dir = dv->base[i];
|
|
|
|
|
|
|
|
assert(dir);
|
2008-10-21 01:35:11 +02:00
|
|
|
prev_nr = dv->nr;
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-21 01:30:30 +02:00
|
|
|
if (fn(dir, arg) < 0)
|
|
|
|
return -1;
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_lock(nr_lock); /* dv->nr may change in fn() */
|
2008-10-21 01:35:11 +02:00
|
|
|
if (prev_nr == dv->nr)
|
|
|
|
++i;
|
2008-10-21 01:30:30 +02:00
|
|
|
}
|
2008-12-28 21:02:30 +01:00
|
|
|
g_mutex_unlock(nr_lock);
|
2008-10-21 01:30:30 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|