Skip to contentSkip to conversation

getcifsacl: Add support to accept more paths

PATCH In review Kenneth D'souza 2 participants 2 messages Apr 17, 2019 – Apr 18, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getcifsacl: Add support to accept more paths

Accept more than one path on the getcifsacl command line.

---
 getcifsacl.c | 82 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 36 deletions(-)
getcifsacl.c +46 -36
index fc78881..df3099d 100644
@@ -340,14 +340,54 @@ getcifsacl_usage(const char *prog)
 	fprintf(stderr, "\nRefer to getcifsacl(1) manpage for details\n");
 }
 
+static void
+getcifsacl(const char *filename , bool raw)
+{
+	ssize_t attrlen;
+	size_t bufsize = BUFSIZE;
+        char *attrval;
+        int failed = 0;
+cifsacl:
+	if (bufsize >= XATTR_SIZE_MAX) {
+		printf("buffer to allocate exceeds max size of %d\n",
+				XATTR_SIZE_MAX);
+		exit(1);
+	}
+
+	attrval = malloc(bufsize * sizeof(char));
+	if (!attrval) {
+		printf("error allocating memory for attribute value buffer\n");
+		exit(1);
+	}
+
+	attrlen = getxattr(filename, ATTRNAME, attrval, bufsize);
+	if (attrlen == -1) {
+		if (errno == ERANGE) {
+			free(attrval);
+			bufsize += BUFSIZE;
+			goto cifsacl;
+                } else
+                {
+                  fprintf(stderr, "Failed to getxattr %s: %s\n", filename, strerror(errno) );
+                    failed = -1;
+                }
+        }
+
+        if (failed == 0)
+        {
+        printf("# filename: %s\n", filename);
+        parse_sec_desc((struct cifs_ntsd *)attrval, attrlen, raw);
+	printf("\n");
+        }
+        free(attrval);
+
+}
+
 int
 main(const int argc, char *const argv[])
 {
 	int c, ret = 0;
 	bool raw = false;
-	ssize_t attrlen;
-	size_t bufsize = BUFSIZE;
-	char *filename, *attrval;
 	execname = basename(argv[0]);
 
 	if (argc < 2) {
@@ -374,8 +414,7 @@ main(const int argc, char *const argv[])
 		printf("you must specify a filename after options.\n");
 		printf("Usage: getcifsacl [option] <file_name>\n");
 		goto out;
-	} else
-		filename = argv[optind];
+	}
 
 	if (!raw && !plugin_loaded) {
 		ret = init_plugin(&plugin_handle);
@@ -386,38 +425,9 @@ main(const int argc, char *const argv[])
 			plugin_loaded = true;
 	}
 
-cifsacl:
-	if (bufsize >= XATTR_SIZE_MAX) {
-		printf("buffer to allocate exceeds max size of %d\n",
-				XATTR_SIZE_MAX);
-		ret = -1;
-		goto out;
-	}
-
-	attrval = malloc(bufsize * sizeof(char));
-	if (!attrval) {
-		printf("error allocating memory for attribute value buffer\n");
-		ret = -1;
-		goto out;
-	}
-
-	attrlen = getxattr(filename, ATTRNAME, attrval, bufsize);
-	if (attrlen == -1) {
-		if (errno == ERANGE) {
-			free(attrval);
-			bufsize += BUFSIZE;
-			goto cifsacl;
-		} else {
-			fprintf(stderr, "getxattr failed on %s: %s\n", filename, strerror(errno) );
-			free(attrval);
-			ret = -1;
-			goto out;
-		}
-	}
-
-	parse_sec_desc((struct cifs_ntsd *)attrval, attrlen, raw);
+	for(; optind < argc; optind++)
+		getcifsacl(argv[optind], raw);
 
-	free(attrval);
 out:
 	if (plugin_loaded)
 		exit_plugin(plugin_handle);
Signature · 1 line
2.20.1
#1 lore raw

ср, 17 апр. 2019 г. в 10:19, Kenneth D'souza <[email protected]>:

Quoted 122 lines

Accept more than one path on the getcifsacl command line.

---
 getcifsacl.c | 82 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 46 insertions(+), 36 deletions(-)
getcifsacl.c +46 -37
index fc78881..df3099d 100644
@@ -340,14 +340,54 @@ getcifsacl_usage(const char *prog)
        fprintf(stderr, "\nRefer to getcifsacl(1) manpage for details\n");
 }
 
+static void
+getcifsacl(const char *filename , bool raw)
+{
+       ssize_t attrlen;
+       size_t bufsize = BUFSIZE;
+        char *attrval;
+        int failed = 0;
+cifsacl:
+       if (bufsize >= XATTR_SIZE_MAX) {
+               printf("buffer to allocate exceeds max size of %d\n",
+                               XATTR_SIZE_MAX);
+               exit(1);
+       }
+
+       attrval = malloc(bufsize * sizeof(char));
+       if (!attrval) {
+               printf("error allocating memory for attribute value buffer\n");
+               exit(1);
+       }
+
+       attrlen = getxattr(filename, ATTRNAME, attrval, bufsize);
+       if (attrlen == -1) {
+               if (errno == ERANGE) {
+                       free(attrval);
+                       bufsize += BUFSIZE;
+                       goto cifsacl;
+                } else
+                {
+                  fprintf(stderr, "Failed to getxattr %s: %s\n", filename, strerror(errno) );
+                    failed = -1;
+                }
+        }
+
+        if (failed == 0)
+        {
+        printf("# filename: %s\n", filename);
+        parse_sec_desc((struct cifs_ntsd *)attrval, attrlen, raw);
+       printf("\n");
+        }
+        free(attrval);
+
+}
+
 int
 main(const int argc, char *const argv[])
 {
        int c, ret = 0;
        bool raw = false;
-       ssize_t attrlen;
-       size_t bufsize = BUFSIZE;
-       char *filename, *attrval;
        execname = basename(argv[0]);
 
        if (argc < 2) {
@@ -374,8 +414,7 @@ main(const int argc, char *const argv[])
                printf("you must specify a filename after options.\n");
                printf("Usage: getcifsacl [option] <file_name>\n");
                goto out;
-       } else
-               filename = argv[optind];
+       }
 
        if (!raw && !plugin_loaded) {
                ret = init_plugin(&plugin_handle);
@@ -386,38 +425,9 @@ main(const int argc, char *const argv[])
                        plugin_loaded = true;
        }
 
-cifsacl:
-       if (bufsize >= XATTR_SIZE_MAX) {
-               printf("buffer to allocate exceeds max size of %d\n",
-                               XATTR_SIZE_MAX);
-               ret = -1;
-               goto out;
-       }
-
-       attrval = malloc(bufsize * sizeof(char));
-       if (!attrval) {
-               printf("error allocating memory for attribute value buffer\n");
-               ret = -1;
-               goto out;
-       }
-
-       attrlen = getxattr(filename, ATTRNAME, attrval, bufsize);
-       if (attrlen == -1) {
-               if (errno == ERANGE) {
-                       free(attrval);
-                       bufsize += BUFSIZE;
-                       goto cifsacl;
-               } else {
-                       fprintf(stderr, "getxattr failed on %s: %s\n", filename, strerror(errno) );
-                       free(attrval);
-                       ret = -1;
-                       goto out;
-               }
-       }
-
-       parse_sec_desc((struct cifs_ntsd *)attrval, attrlen, raw);
+       for(; optind < argc; optind++)
+               getcifsacl(argv[optind], raw);
 
-       free(attrval);
 out:
        if (plugin_loaded)
                exit_plugin(plugin_handle);
--
Quoted 2 lines

2.20.1

Fixed the code style and merged:
https://github.com/piastry/cifs-utils/commit/9beaa8c3c895ca8460d81fb54a6a0de2bb21a277

Also fixed the usage message separately:
https://github.com/piastry/cifs-utils/commit/f2955af017f604003e3c8c3efe0fb0fb85584cea

--
Best regards,
Pavel Shilovsky