Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Feb 2011 12:31:49 -0700
From:      Shawn Webb <lattera@gmail.com>
To:        Miroslav Lachman <000.fbsd@quip.cz>
Cc:        FreeBSD-current <freebsd-current@freebsd.org>
Subject:   Re: setfacl Recursive Functionality
Message-ID:  <AANLkTimfyhi4wz59-1pyMRkUE6K0PCZmK9br-E=9psCq@mail.gmail.com>
In-Reply-To: <4D518BC4.3080905@quip.cz>
References:  <AANLkTi=%2BWtmRz07m=Cg7hbXJGw7eWRHC1ASGeufTSLBB@mail.gmail.com> <4D518BC4.3080905@quip.cz>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
Attached is the patch. I'm also storing my FreeBSD patches at
https://github.com/lattera/patches

Thanks,

Shawn

On Tue, Feb 8, 2011 at 11:30 AM, Miroslav Lachman <000.fbsd@quip.cz> wrote:

> Shawn Webb wrote:
>
>> I've just finished a patch to add recursive functionality to setfacl.
>> Before
>> I officially submit it, I'd like a few suggestions on how to improve the
>> patch.
>>
>> The part I'm worried about involves the #define directive at top. I'm not
>> sure what ramifications using that define might have. I needed it for my
>> remove_invalid_inherit() function to work.
>>
>
> Can it be extended to getfacl too? I am waiting for recursive functionality
> for a long time. It is available on linux and maybe some other OSes.
>
> Miroslav Lachman
>

[-- Attachment #2 --]
--- /usr/src/bin/getfacl/getfacl.c	2011-02-03 12:11:00.851493061 -0700
+++ bin/getfacl/getfacl.c	2011-02-08 12:26:52.072186480 -0700
@@ -47,6 +47,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <dirent.h>
 
 int	more_than_one = 0;
 
@@ -260,8 +261,56 @@
 }
 
 static int
+recurse_directory(char *path, acl_type_t type, int hflag, int iflag,
+    int nflag, int qflag, int vflag, int rflag)
+{
+	DIR *dirp;
+	struct dirent *ent;
+	struct stat sb;
+	char newpath[PATH_MAX+1];
+	int	carried_error = 0, error;
+	
+	if (stat(path, &sb) == -1) {
+		warn("%s: stat() failed", path);
+		return -1;
+	}
+	
+	error = print_acl(path, type, hflag, iflag, nflag, qflag, vflag);
+	
+	if (rflag == 0 || S_ISDIR(sb.st_mode) == 0)
+		return error;
+	
+	if (error == -1)
+		carried_error = -1;
+	
+	dirp = opendir(path);
+	while ((ent = readdir(dirp)) != NULL) {
+		if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
+			continue;
+		
+		snprintf(newpath, PATH_MAX, "%s/%s", path, ent->d_name); 
+		
+		if (stat(newpath, &sb) == -1) {
+			warn("%s: stat() failed", newpath);
+			continue;
+		}
+		
+		if (S_ISDIR(sb.st_mode))
+			error = recurse_directory(newpath, type, hflag, iflag,
+			    nflag, qflag, vflag, rflag);
+		else
+			error = print_acl(newpath, type, hflag, iflag, nflag, qflag, vflag);
+		if (error == -1)
+			carried_error = -1;
+	}
+	closedir(dirp);
+	
+	return carried_error;
+}
+
+static int
 print_acl_from_stdin(acl_type_t type, int hflag, int iflag, int nflag,
-    int qflag, int vflag)
+    int qflag, int vflag, int rflag)
 {
 	char	*p, pathname[PATH_MAX];
 	int	carried_error = 0;
@@ -269,8 +318,8 @@
 	while (fgets(pathname, (int)sizeof(pathname), stdin)) {
 		if ((p = strchr(pathname, '\n')) != NULL)
 			*p = '\0';
-		if (print_acl(pathname, type, hflag, iflag, nflag,
-		    qflag, vflag) == -1) {
+		if (recurse_directory(pathname, type, hflag, iflag, nflag,
+		    qflag, vflag, rflag) == -1) {
 			carried_error = -1;
 		}
 	}
@@ -284,14 +333,15 @@
 	acl_type_t	type = ACL_TYPE_ACCESS;
 	int	carried_error = 0;
 	int	ch, error, i;
-	int	hflag, iflag, qflag, nflag, vflag;
+	int	hflag, iflag, qflag, nflag, vflag, rflag;
 
 	hflag = 0;
 	iflag = 0;
 	qflag = 0;
 	nflag = 0;
 	vflag = 0;
-	while ((ch = getopt(argc, argv, "dhinqv")) != -1)
+	rflag = 0;
+	while ((ch = getopt(argc, argv, "Rdhinqv")) != -1)
 		switch(ch) {
 		case 'd':
 			type = ACL_TYPE_DEFAULT;
@@ -311,6 +361,9 @@
 		case 'v':
 			vflag = 1;
 			break;
+		case 'R':
+			rflag = 1;
+			break;
 		default:
 			usage();
 			return(-1);
@@ -320,19 +373,19 @@
 
 	if (argc == 0) {
 		error = print_acl_from_stdin(type, hflag, iflag, nflag,
-		    qflag, vflag);
+		    qflag, vflag, rflag);
 		return(error ? 1 : 0);
 	}
 
 	for (i = 0; i < argc; i++) {
 		if (!strcmp(argv[i], "-")) {
 			error = print_acl_from_stdin(type, hflag, iflag, nflag,
-			    qflag, vflag);
+			    qflag, vflag, rflag);
 			if (error == -1)
 				carried_error = -1;
 		} else {
-			error = print_acl(argv[i], type, hflag, iflag, nflag,
-			    qflag, vflag);
+			error = recurse_directory(argv[i], type, hflag, iflag, nflag,
+			    qflag, vflag, rflag);
 			if (error == -1)
 				carried_error = -1;
 		}
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTimfyhi4wz59-1pyMRkUE6K0PCZmK9br-E=9psCq>