Module Name:    othersrc
Committed By:   stacktic
Date:           Sun Apr 12 23:02:43 UTC 2009

Modified Files:
        othersrc/bin/fsu_ecp: fsu_ecp.c fsu_flist.c fsu_flist.h

Log Message:
Using LIST in fsu_list


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 othersrc/bin/fsu_ecp/fsu_ecp.c
cvs rdiff -u -r1.2 -r1.3 othersrc/bin/fsu_ecp/fsu_flist.c \
    othersrc/bin/fsu_ecp/fsu_flist.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/bin/fsu_ecp/fsu_ecp.c
diff -u othersrc/bin/fsu_ecp/fsu_ecp.c:1.4 othersrc/bin/fsu_ecp/fsu_ecp.c:1.5
--- othersrc/bin/fsu_ecp/fsu_ecp.c:1.4	Fri Apr  3 10:46:35 2009
+++ othersrc/bin/fsu_ecp/fsu_ecp.c	Sun Apr 12 23:02:43 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: fsu_ecp.c,v 1.4 2009/04/03 10:46:35 stacktic Exp $ */
+/* $NetBSD: fsu_ecp.c,v 1.5 2009/04/12 23:02:43 stacktic Exp $ */
 
 /*
  * Copyright (c) 2008 Arnaud Ysmal.  All Rights Reserved.
@@ -74,7 +74,7 @@
 	char *hl_from;
 	char **hl_to;
 	int hl_nlink;
-	SLIST_ENTRY(hardlink_s) next;
+	LIST_ENTRY(hardlink_s) next;
 };
 
 int
@@ -189,7 +189,7 @@
 copy_dir(struct ukfs *fs, const char *from, const char *to, int flags)
 {
 	const char *filename;
-	int res, rv;
+	int rv;
 	struct stat file_stat;
 	char to_p[PATH_MAX + 1];
 	size_t tlen, flen;
@@ -210,23 +210,15 @@
 			filename = strrchr(from, '/');
 			if (filename == NULL)
 				filename = from;
-			else {
-				if (filename[1] == '\0') {
-					for (rv = strlen(from) - 2;
-					     rv >= 0 && from[rv] != '/';
-					     --rv)
-						continue;
-				}
+			else
 				++filename;
-			}
-			
-			if (to_p[tlen - 1] != '/') {
-				if (filename[0] != '/') {
+
+			if (to_p[tlen - 1] == '/')
+				--tlen;
+			else if (to_p[tlen - 1] != '/' && filename[0] != '/') {
 					to_p[tlen] = '/';
 					to_p[tlen + 1] = '\0';
-				}
-			} else
-				--tlen;
+			}
 
 			flen = strlen(filename);
 			
@@ -240,22 +232,20 @@
 			return -1;
 		}
 	}
-
-	res = copy_dir_rec(fs, from, to_p, flags);
-
-	return res;
+	return copy_dir_rec(fs, from, to_p, flags);
 }
 
 static int
 copy_dir_rec(struct ukfs *fs, const char *from_p, char *to_p, int flags)
 {
-	FSU_FENT *root, *cur, *cur2, *tmp;
+	FSU_FENT *root, *cur, *cur2, *nextelt;
+	fsu_flist *flist;
 	size_t len;
 	int flist_options, res, rv, off, hl_supported, curlink;
 	struct hardlink_s *new;
 	char hlfrom[PATH_MAX + 1], hlto[PATH_MAX + 1];
 
-	SLIST_HEAD(, hardlink_s) hl_l = SLIST_HEAD_INITIALIZER(hl_l);
+	LIST_HEAD(, hardlink_s) hl_l = LIST_HEAD_INITIALIZER(hl_l);
 
 	curlink = res = 0;
 	hl_supported = 1;
@@ -273,20 +263,22 @@
 
 	len = strlen(to_p) - 1;
 	
-	root = fsu_flist_build(fs, from_p, flist_options);
-
-	if (root == NULL)
+	flist = fsu_flist_build(fs, from_p, flist_options);
+	if (flist == NULL)
 		return -1;
+	root = LIST_FIRST(flist);
 
 	off = root->pathlen;
-	for (cur = root->next; cur != NULL; cur = cur->next) {
-		if (cur->sb.st_nlink == 1)
+	LIST_FOREACH(cur, flist, next) {
+		if (cur == root || cur->sb.st_nlink == 1)
 			continue;
+
 		new = malloc(sizeof(struct hardlink_s));
 		if (new == NULL) {
 			warn("malloc");
 			return -1;
 		}
+		new->hl_from = cur->path;
 		new->hl_nlink = cur->sb.st_nlink - 1;
 		new->hl_to = malloc(new->hl_nlink * sizeof(char *));
 		if (new->hl_to == NULL) {
@@ -294,31 +286,30 @@
 			free(new);
 			return -1;
 		}
+		memset(new->hl_to, 0, new->hl_nlink * sizeof(char *));
 
-		new->hl_from = cur->path;
-
-		for (curlink = 0, cur2 = cur; cur2->next != NULL;) {
-			if (cur2->next->sb.st_nlink == 1) {
-				cur2 = cur2->next;
+		for (curlink = 0, cur2 = cur; LIST_NEXT(cur2, next) != NULL;) {
+			nextelt = LIST_NEXT(cur2, next);
+			if (nextelt->sb.st_nlink == 1) {
+				cur2 = nextelt;
 				continue;
 			}
-			if (cur->sb.st_ino == cur2->next->sb.st_ino &&
-			    cur->sb.st_dev == cur2->next->sb.st_dev) {
-				
-				new->hl_to[curlink] = cur2->next->path;
+
+			if (cur->sb.st_ino == nextelt->sb.st_ino &&
+			    cur->sb.st_dev == nextelt->sb.st_dev) {
+				new->hl_to[curlink] = nextelt->path;
 				if (new->hl_to[curlink] == NULL)
-					warn("%s", cur2->next->path);
+					warn("%s", nextelt->path);
 				else
 					curlink++;
-				tmp = cur2->next;
-				cur2->next = cur2->next->next;
-				free(tmp);
+				LIST_REMOVE(nextelt, next);
+				free(nextelt);
 				if (curlink + 1 == cur->sb.st_nlink)
 					break;
 			} else
-				cur2 = cur2->next;
+				cur2 = nextelt;
 		}
-		SLIST_INSERT_HEAD(&hl_l, new, next);
+		LIST_INSERT_HEAD(&hl_l, new, next);
 	}
 	
 	if (flags & FSU_ECP_GET)
@@ -337,8 +328,9 @@
 		}
 	}
 
-	for (cur = root->next; cur != NULL; cur = cur->next) {
-		
+	LIST_FOREACH(cur, flist, next) {
+		if (cur == root)
+			continue;
 		rv = strlcat(to_p, cur->path + off, PATH_MAX+1);
 		if (rv != (int)(len + cur->pathlen - off + 1)) {
 			warn("%s%s", to_p, cur->path + off);
@@ -378,13 +370,13 @@
 		}
 		to_p[len + 1] = '\0';
 	}
-	
+
 	memcpy(hlfrom, to_p, len + 1);
 	memcpy(hlto, to_p, len + 1);
 
-	while (!SLIST_EMPTY(&hl_l)) {
-		new = SLIST_FIRST(&hl_l);
-		SLIST_REMOVE_HEAD(&hl_l, next);
+	while (!LIST_EMPTY(&hl_l)) {
+		new = LIST_FIRST(&hl_l);
+		LIST_REMOVE(new, next);
 
 		hlfrom[len + 1] = '\0';
 		strlcat(hlfrom, new->hl_from + off, PATH_MAX+1);
@@ -421,7 +413,7 @@
 	}
 
 out:
-	fsu_flist_free(root);
+	fsu_flist_free(flist);
 
 	return res;
 }

Index: othersrc/bin/fsu_ecp/fsu_flist.c
diff -u othersrc/bin/fsu_ecp/fsu_flist.c:1.2 othersrc/bin/fsu_ecp/fsu_flist.c:1.3
--- othersrc/bin/fsu_ecp/fsu_flist.c:1.2	Fri Apr  3 10:46:36 2009
+++ othersrc/bin/fsu_ecp/fsu_flist.c	Sun Apr 12 23:02:43 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: fsu_flist.c,v 1.2 2009/04/03 10:46:36 stacktic Exp $ */
+/* $NetBSD: fsu_flist.c,v 1.3 2009/04/12 23:02:43 stacktic Exp $ */
 
 /*
  * Copyright (c) 2008 Arnaud Ysmal.  All Rights Reserved.
@@ -25,6 +25,7 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/syslimits.h>
 
@@ -49,13 +50,14 @@
 
 static int (*statfun)(struct ukfs *, const char *, struct stat *);
 
-FSU_FENT
+fsu_flist
 *fsu_flist_build(struct ukfs *fs, const char *rootp, int flags)
 {
-	FSU_FENT *cur, *first, *last, *child, *root;
+	FSU_FENT *cur, *prev, *child;
 	FSU_DIR *curdir;
 	DIR *rcurdir;
 	struct dirent *dent;
+	fsu_flist *head;
 
 	/*
 	 * If we are building a list from an image the struct ukfs pointer
@@ -73,12 +75,18 @@
 	else
 		statfun = flags & FSU_FLIST_STATLINK ? ukfs_lstat : ukfs_stat;
 
+	head = malloc(sizeof(fsu_flist));
+	if (head == NULL)
+		return NULL;
+	LIST_INIT(head);
 
-	root = fsu_flist_alloc_root(fs, rootp, flags);
-	if (root == NULL)
+	cur = fsu_flist_alloc_root(fs, rootp, flags);
+	if (cur == NULL)
 		return NULL;
 
-	for (cur = root; cur != NULL; cur = cur->next) {
+	LIST_INSERT_HEAD(head, cur, next);
+
+	LIST_FOREACH(cur, head, next) {
 		if (!S_ISDIR(cur->sb.st_mode))
 			continue;
 
@@ -90,7 +98,7 @@
 		if (curdir == NULL && rcurdir == NULL)
 			continue;
 
-		first = last = NULL;			
+		prev = cur;
 		for (;;) {
 			if (flags & FSU_FLIST_REALFS)
 				dent = readdir(rcurdir);
@@ -106,11 +114,9 @@
 			child = fsu_flist_alloc(fs, dent, cur, flags);
 			if (child == NULL)
 				continue;
-			
-			if (first == NULL)
-				first = last = child;
-			else
-				last = last->next = child;
+
+			LIST_INSERT_AFTER(prev, child, next);
+			prev = child;
 			
 			cur->childno++;	
 		}
@@ -119,16 +125,11 @@
 		else
 			fsu_closedir(curdir);
 
-		if (last != NULL) {
-			last->next = cur->next;
-			cur->next = first;
-		}
-
 		if (!(flags & FSU_FLIST_RECURSIVE))
 			break;
 	}
 
-	return root;
+	return head;
 }
 
 static FSU_FENT
@@ -175,7 +176,6 @@
 	}
 
 	child->filename = child->path + child->pathlen - dent->d_namlen;
-	child->next = NULL;
 	child->childno = 0;
 	
 	if (flags & FSU_FLIST_REALFS) {
@@ -190,7 +190,6 @@
 		fsu_flist_free_entry(child);
 		return NULL;
 	}
-
 	return child;
 }
 
@@ -207,7 +206,7 @@
 		return NULL;
 	}
 
-	root->parent = root->next = NULL;
+	root->parent = NULL;
 	root->childno = 0;
 	root->path = strdup(path);
 
@@ -232,15 +231,18 @@
 }
 
 void
-fsu_flist_free(FSU_FENT *root)
+fsu_flist_free(fsu_flist *list)
 {
 	FSU_FENT *cur;
 
-	while ((cur = root) != NULL) {
-		root = root->next;
+	while (!LIST_EMPTY(list)) {
+		cur = LIST_FIRST(list);
+		LIST_REMOVE(cur, next);
+
 		free(cur->path);
-		free(cur);		
+		free(cur);
 	}
+	free(list);
 }
 
 void
@@ -250,7 +252,6 @@
 	if (ent == NULL)
 		return;
 
-	ent->next = NULL;
 	free(ent->path);
 	free(ent);
 }
Index: othersrc/bin/fsu_ecp/fsu_flist.h
diff -u othersrc/bin/fsu_ecp/fsu_flist.h:1.2 othersrc/bin/fsu_ecp/fsu_flist.h:1.3
--- othersrc/bin/fsu_ecp/fsu_flist.h:1.2	Fri Apr  3 10:46:36 2009
+++ othersrc/bin/fsu_ecp/fsu_flist.h	Sun Apr 12 23:02:43 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: fsu_flist.h,v 1.2 2009/04/03 10:46:36 stacktic Exp $ */
+/* $NetBSD: fsu_flist.h,v 1.3 2009/04/12 23:02:43 stacktic Exp $ */
 
 /*
  * Copyright (c) 2008 Arnaud Ysmal.  All Rights Reserved.
@@ -37,16 +37,19 @@
 
 typedef struct fsu_fent_s {
 	struct fsu_fent_s *parent;
-	struct fsu_fent_s *next;
 	unsigned int childno;
 	char *path;
 	char *filename;
 	unsigned int pathlen;
 	struct stat sb;
+	LIST_ENTRY(fsu_fent_s) next;
 } FSU_FENT;
 
-FSU_FENT	*fsu_flist_build(struct ukfs *, const char *, int);
-void		fsu_flist_free(FSU_FENT *);
+LIST_HEAD(fsu_flist_s, fsu_fent_s);
+typedef struct fsu_flist_s fsu_flist;
+
+fsu_flist	*fsu_flist_build(struct ukfs *, const char *, int);
+void		fsu_flist_free(fsu_flist *);
 void		fsu_flist_free_entry(FSU_FENT *);
 
 #endif /* !_FSU_FLIST_H_ */

Reply via email to