aboutsummaryrefslogtreecommitdiffstats
path: root/libibex/index.c
diff options
context:
space:
mode:
authorMichael Zucci <zucchi@src.gnome.org>2000-02-14 10:00:22 +0800
committerMichael Zucci <zucchi@src.gnome.org>2000-02-14 10:00:22 +0800
commitadd8821e99b4ea1b574ed469018ae224a542e6ae (patch)
tree3e9e02c153fb7300707a26a132ee66c9177d2369 /libibex/index.c
parente4bbdf696cc2f23e9b1fc288d37a44eb27d9426d (diff)
downloadgsoc2013-evolution-add8821e99b4ea1b574ed469018ae224a542e6ae.tar.gz
gsoc2013-evolution-add8821e99b4ea1b574ed469018ae224a542e6ae.tar.zst
gsoc2013-evolution-add8821e99b4ea1b574ed469018ae224a542e6ae.zip
Initial revision
svn path=/trunk/; revision=1768
Diffstat (limited to 'libibex/index.c')
-rw-r--r--libibex/index.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/libibex/index.c b/libibex/index.c
new file mode 100644
index 0000000000..aef891182e
--- /dev/null
+++ b/libibex/index.c
@@ -0,0 +1,101 @@
+/*
+ Copyright 2000 Helix Code Inc.
+*/
+/* index.c: high-level indexing ops */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "ibex_internal.h"
+
+/* Index a file, given its name. (Replace any previously indexed contents
+ * of this file with the new contents.)
+ */
+int
+ibex_index_file(ibex *ib, char *filename)
+{
+ int fd;
+ int status;
+ struct stat st;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ if (fstat(fd, &st) == -1)
+ {
+ close(fd);
+ return -1;
+ }
+ if (!S_ISREG(st.st_mode))
+ {
+ close(fd);
+ errno = EINVAL;
+ return -1;
+ }
+
+ ibex_unindex(ib, filename);
+ status = ibex_index_fd(ib, filename, fd, st.st_size);
+ close(fd);
+ return status;
+}
+
+/* Given a file descriptor and a name to refer to it by, index LEN
+ * bytes of data from it.
+ */
+int
+ibex_index_fd(ibex *ib, char *name, int fd, size_t len)
+{
+ char *buf;
+ int off = 0, nread, status;
+
+ buf = g_malloc(len);
+ do
+ {
+ nread = read(fd, buf + off, len - off);
+ if (nread == -1)
+ {
+ g_free(buf);
+ return -1;
+ }
+ off += nread;
+ }
+ while (off != len);
+
+ status = ibex_index_buffer(ib, name, buf, len, NULL);
+ g_free(buf);
+
+ return status;
+}
+
+void
+ibex_unindex(ibex *ib, char *name)
+{
+ ibex_file *ibf;
+
+ ibf = g_tree_lookup(ib->files, name);
+ if (ibf)
+ {
+ ibf->index = -1;
+ g_tree_remove(ib->files, name);
+ g_ptr_array_add(ib->oldfiles, ibf);
+ }
+}
+
+void
+ibex_rename(ibex *ib, char *oldname, char *newname)
+{
+ ibex_file *ibf;
+
+ ibf = g_tree_lookup(ib->files, oldname);
+ if (ibf)
+ {
+ g_tree_remove(ib->files, oldname);
+ g_free(ibf->name);
+ ibf->name = g_strdup(newname);
+ g_tree_insert(ib->files, ibf->name, ibf);
+ }
+}