Refactor enmasse() and recurse() to reflect depth
The HLP-changes to sbase have been a great addition of functionality, but they kind of "polluted" the enmasse() and recurse() prototypes. As this will come in handy in the future, knowing at which "depth" you are inside a recursing function is an important functionality. Instead of having a special HLP-flag passed to enmasse, each sub- function needs to provide it on its own and can calculate results based on the current depth (for instance, 'H' implies 'P' at depth > 0). A special case is recurse(), because it actually depends on the follow-type. A new flag "recurse_follow" brings consistency into what used to be spread across different naming conventions (fflag, HLP_flag, ...). This also fixes numerous bugs with the behaviour of HLP in the tools using it.
This commit is contained in:
		
							
								
								
									
										10
									
								
								libutil/cp.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								libutil/cp.c
									
									
									
									
									
								
							| @@ -24,7 +24,7 @@ int cp_status = 0; | ||||
| int cp_HLPflag = 'L'; | ||||
|  | ||||
| int | ||||
| cp(const char *s1, const char *s2, char ff) | ||||
| cp(const char *s1, const char *s2, int depth) | ||||
| { | ||||
| 	FILE *f1, *f2; | ||||
| 	char *ns1, *ns2; | ||||
| @@ -39,9 +39,11 @@ cp(const char *s1, const char *s2, char ff) | ||||
| 	if (cp_vflag) | ||||
| 		printf("'%s' -> '%s'\n", s1, s2); | ||||
|  | ||||
| 	r = ff == 'P' ? lstat(s1, &st) : stat(s1, &st); | ||||
| 	r = (cp_HLPflag == 'P' || (cp_HLPflag == 'H' && depth > 0)) ? | ||||
| 	    lstat(s1, &st) : stat(s1, &st); | ||||
| 	if (r < 0) { | ||||
| 		weprintf("%s %s:", ff == 'P' ? "lstat" : "stat", s1); | ||||
| 		weprintf("%s %s:", (cp_HLPflag == 'P' || | ||||
| 		         (cp_HLPflag == 'H' && depth > 0)) ? "lstat" : "stat", s1); | ||||
| 		cp_status = 1; | ||||
| 		return 0; | ||||
| 	} | ||||
| @@ -83,7 +85,7 @@ cp(const char *s1, const char *s2, char ff) | ||||
| 					eprintf("%s/%s: filename too long\n", | ||||
| 						s2, d->d_name); | ||||
| 				} | ||||
| 				fnck(ns1, ns2, cp, ff == 'H' ? 'P' : ff); | ||||
| 				fnck(ns1, ns2, cp, depth + 1); | ||||
| 			} | ||||
| 		} | ||||
| 		closedir(dp); | ||||
|   | ||||
| @@ -9,7 +9,7 @@ | ||||
| #include "../util.h" | ||||
|  | ||||
| void | ||||
| enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), char ff) | ||||
| enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int)) | ||||
| { | ||||
| 	char *buf, *dir; | ||||
| 	int i, len; | ||||
| @@ -18,7 +18,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), cha | ||||
| 	size_t dlen; | ||||
|  | ||||
| 	if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) { | ||||
| 		fnck(argv[0], argv[1], fn, ff); | ||||
| 		fnck(argv[0], argv[1], fn, 0); | ||||
| 		return; | ||||
| 	} else { | ||||
| 		dir = (argc == 1) ? "." : argv[--argc]; | ||||
| @@ -35,7 +35,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), cha | ||||
| 			eprintf("%s/%s: filename too long\n", dir, | ||||
| 			        basename(argv[i])); | ||||
| 		} | ||||
| 		fnck(argv[i], buf, fn, ff); | ||||
| 		fnck(argv[i], buf, fn, 0); | ||||
| 	} | ||||
| 	free(buf); | ||||
| } | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
|  | ||||
| void | ||||
| fnck(const char *a, const char *b, | ||||
|      int (*fn)(const char *, const char *, char), char ff) | ||||
|      int (*fn)(const char *, const char *, int), int depth) | ||||
| { | ||||
| 	struct stat sta, stb; | ||||
|  | ||||
| @@ -16,6 +16,6 @@ fnck(const char *a, const char *b, | ||||
| 		eprintf("%s -> %s: same file\n", a, b); | ||||
| 	} | ||||
|  | ||||
| 	if (fn(a, b, ff) < 0) | ||||
| 	if (fn(a, b, depth) < 0) | ||||
| 		eprintf("%s -> %s:", a, b); | ||||
| } | ||||
|   | ||||
| @@ -10,25 +10,29 @@ | ||||
|  | ||||
| #include "../util.h" | ||||
|  | ||||
| int recurse_follow = 'P'; | ||||
|  | ||||
| void | ||||
| recurse(const char *path, void (*fn)(const char *, int), int follow) | ||||
| recurse(const char *path, void (*fn)(const char *, int), int depth) | ||||
| { | ||||
| 	char buf[PATH_MAX]; | ||||
| 	struct dirent *d; | ||||
| 	struct stat lst, st; | ||||
| 	DIR *dp; | ||||
|  | ||||
| 	if (lstat(path, &lst) < 0 || stat(path, &st) < 0 || | ||||
| 	    !(S_ISDIR(lst.st_mode) || | ||||
| 	    (follow != 'P' && S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode)))) | ||||
| 	if (lstat(path, &lst) < 0) | ||||
| 		eprintf("lstat %s:", path); | ||||
| 	if (stat(path, &st) < 0) | ||||
| 		eprintf("stat %s:", path); | ||||
| 	if (!S_ISDIR(lst.st_mode) && !(S_ISLNK(lst.st_mode) && S_ISDIR(st.st_mode) && | ||||
| 	    !(recurse_follow == 'P' || (recurse_follow == 'H' && depth > 0)))) | ||||
| 		return; | ||||
|  | ||||
| 	if (!(dp = opendir(path))) | ||||
| 		eprintf("opendir %s:", path); | ||||
|  | ||||
| 	while ((d = readdir(dp))) { | ||||
| 		if (strcmp(d->d_name, ".") == 0 || | ||||
| 		    strcmp(d->d_name, "..") == 0) | ||||
| 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) | ||||
| 			continue; | ||||
| 		if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) | ||||
| 			eprintf("path too long\n"); | ||||
| @@ -37,7 +41,7 @@ recurse(const char *path, void (*fn)(const char *, int), int follow) | ||||
| 				eprintf("path too long\n"); | ||||
| 		if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf)) | ||||
| 			eprintf("path too long\n"); | ||||
| 		fn(buf, follow == 'H' ? 'P' : follow); | ||||
| 		fn(buf, depth + 1); | ||||
| 	} | ||||
|  | ||||
| 	closedir(dp); | ||||
|   | ||||
| @@ -10,10 +10,10 @@ int rm_rflag = 0; | ||||
| int rm_status = 0; | ||||
|  | ||||
| void | ||||
| rm(const char *path, int flag) | ||||
| rm(const char *path, int depth) | ||||
| { | ||||
| 	if (rm_rflag) | ||||
| 		recurse(path, rm, 'P'); | ||||
| 		recurse(path, rm, depth); | ||||
| 	if (remove(path) < 0) { | ||||
| 		if (!rm_fflag) | ||||
| 			weprintf("remove %s:", path); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user