diff mpffs/find.c @ 34:95f61c3b430a

mpffs-rdutils: pathname search implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 16:20:28 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpffs/find.c	Sun Jun 30 16:20:28 2013 +0000
@@ -0,0 +1,91 @@
+/*
+ * This module contains the code for searching the FFS tree structure
+ * for a specified absolute pathname.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "types.h"
+#include "struct.h"
+
+extern int root_node_no;
+
+/*
+ * The following function iterates through the descendants of a directory
+ * object looking for a specific directory-member filename.
+ *
+ * Arguments:
+ * - index # of the first descendant (descendant pointer from the dir object)
+ * - filename to search for
+ *
+ * Returns: index # of the sought descendant object if found, 0 otherwise.
+ */
+find_dir_member(first_descend, srchname)
+	char *srchname;
+{
+	int ent;
+	struct objinfo obj;
+
+	for (ent = first_descend; ent != 0xFFFF; ent = obj.sibling) {
+		obj.entryno = ent;
+		get_index_entry(&obj);
+		if (!obj.type) /* skip deleted objects w/o further looking */
+			continue;
+		validate_chunk(&obj);
+		validate_obj_name(&obj);
+		if (!strcmp(obj.dataptr, srchname))
+			return(ent);
+	}
+	return(0);
+}
+
+/*
+ * The following function searches for a pathname from the root down.
+ * Returns the index # if found, otherwise exits with an error message
+ * indicating which step failed.
+ *
+ * Warning: the pathname in the argument buffer will be destroyed:
+ * 0s put in place of the slashes.
+ */
+find_pathname(pathname)
+	char *pathname;
+{
+	char *cur, *next;
+	int idx;
+	struct objinfo dir;
+
+	cur = pathname;
+	if (*cur == '/')
+		cur++;
+	for (idx = root_node_no; cur; cur = next) {
+		if (!*cur)
+			break;
+		next = index(cur, '/');
+		if (next == cur) {
+			fprintf(stderr,
+			"malformed pathname: multiple adjacent slashes\n");
+			exit(1);
+		}
+		if (next)
+			*next++ = '\0';
+		dir.entryno = idx;
+		get_index_entry(&dir);
+		if (dir.type != 0xF2) {
+			fprintf(stderr,
+			"pathname search error: encountered a non-directory\n");
+			exit(1);
+		}
+		idx = find_dir_member(dir.descend, cur);
+		if (!idx) {
+			fprintf(stderr,
+			"pathname search error: component name not found\n");
+			exit(1);
+		}
+	}
+	return(idx);
+}