Skip to contentSkip to conversation

Makedevs: add recursive option

PATCH Superseded Angelo Compagnucci 3 participants 7 messages Apr 11, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover Makedevs: add recursive option

This patch series add the recursive option to makedevs.

With recursive option will be possible to apply owner/permission recursively to folders.

Angelo Compagnucci (2):
package/makedevs: add recursive option docs/manual: documentation for recursive makedevs syntax

 docs/manual/makedev-syntax.txt | 10 +++++++++-
 package/makedevs/makedevs.c    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/2 package/makedevs: add recursive option

This patch adds the option to change owner/permission of a folder recursively.

---
Changelog:

v1 -> v2:
- Code refactoring to avoid nested functions as suggested by Peter Korsgaard

 package/makedevs/makedevs.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
package/makedevs/makedevs.c +31 -0
index ab90b93..ee0f9fb 100644
@@ -34,8 +34,12 @@
 #ifndef __APPLE__
 #include <sys/sysmacros.h>     /* major() and minor() */
 #endif
+#include <ftw.h>
 
 const char *bb_applet_name;
+uid_t recursive_uid;
+gid_t recursive_gid;
+unsigned int recursive_mode;
 
 void bb_verror_msg(const char *s, va_list p)
 {
@@ -332,6 +336,7 @@ void bb_show_usage(void)
 	fprintf(stderr, "Where name is the file name,  type can be one of:\n");
 	fprintf(stderr, "      f       A regular file\n");
 	fprintf(stderr, "      d       Directory\n");
+	fprintf(stderr, "      r       Directory recursively\n");
 	fprintf(stderr, "      c       Character special device file\n");
 	fprintf(stderr, "      b       Block special device file\n");
 	fprintf(stderr, "      p       Fifo (named pipe)\n");
@@ -364,6 +369,23 @@ void bb_show_usage(void)
 	exit(1);
 }
 
+bb_recursive(const char *fpath, const struct stat *sb,
+		int tflag, struct FTW *ftwbuf){
+
+	if (chown(fpath, recursive_uid, recursive_gid) == -1) {
+		bb_perror_msg("chown failed for %s", fpath);
+		return -1;
+	}
+	if (recursive_mode != -1) {
+		if (chmod(fpath, recursive_mode) < 0) {
+			bb_perror_msg("chmod failed for %s", fpath);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int opt;
@@ -474,6 +496,15 @@ int main(int argc, char **argv)
 				ret = EXIT_FAILURE;
 				goto loop;
 			}
+		} else if (type == 'r') {
+			recursive_uid = uid;
+			recursive_gid = gid;
+			recursive_mode = mode;
+			if (nftw(full_name, bb_recursive, 20, FTW_MOUNT | FTW_PHYS) < 0) {
+				bb_perror_msg("line %d: recursive failed for %s", linenum, full_name);
+				ret = EXIT_FAILURE;
+				goto loop;
+			}
 		} else
 		{
 			dev_t rdev;
Signature · 1 line
1.9.1
#2 lore raw

PATCH v2 1/2 package/makedevs: add recursive option

Angelo, All,

On 2015-04-11 15:40 +0200, Angelo Compagnucci spake thusly:

Quoted 2 lines

This patch adds the option to change owner/permission of a folder recursively.

In *nix terminology, we usually refer to a 'directory' not a 'folder'. 'Folder' is a Windows-ism. ;-)

Quoted 1 line

Tested with a simple dirtectory tree, and manually running through fakeroot that saved its internal DB, then manually inspected the DB dump to contain what was expected.

Thanks! :-)

Regards,
Yann E. MORIN.

Quoted 82 lines

---
Changelog:

v1 -> v2:
- Code refactoring to avoid nested functions as suggested by Peter Korsgaard

 package/makedevs/makedevs.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
package/makedevs/makedevs.c +31 -0
index ab90b93..ee0f9fb 100644
@@ -34,8 +34,12 @@
 #ifndef __APPLE__
 #include <sys/sysmacros.h>     /* major() and minor() */
 #endif
+#include <ftw.h>
 
 const char *bb_applet_name;
+uid_t recursive_uid;
+gid_t recursive_gid;
+unsigned int recursive_mode;
 
 void bb_verror_msg(const char *s, va_list p)
 {
@@ -332,6 +336,7 @@ void bb_show_usage(void)
 	fprintf(stderr, "Where name is the file name,  type can be one of:\n");
 	fprintf(stderr, "      f       A regular file\n");
 	fprintf(stderr, "      d       Directory\n");
+	fprintf(stderr, "      r       Directory recursively\n");
 	fprintf(stderr, "      c       Character special device file\n");
 	fprintf(stderr, "      b       Block special device file\n");
 	fprintf(stderr, "      p       Fifo (named pipe)\n");
@@ -364,6 +369,23 @@ void bb_show_usage(void)
 	exit(1);
 }
 
+bb_recursive(const char *fpath, const struct stat *sb,
+		int tflag, struct FTW *ftwbuf){
+
+	if (chown(fpath, recursive_uid, recursive_gid) == -1) {
+		bb_perror_msg("chown failed for %s", fpath);
+		return -1;
+	}
+	if (recursive_mode != -1) {
+		if (chmod(fpath, recursive_mode) < 0) {
+			bb_perror_msg("chmod failed for %s", fpath);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int opt;
@@ -474,6 +496,15 @@ int main(int argc, char **argv)
 				ret = EXIT_FAILURE;
 				goto loop;
 			}
+		} else if (type == 'r') {
+			recursive_uid = uid;
+			recursive_gid = gid;
+			recursive_mode = mode;
+			if (nftw(full_name, bb_recursive, 20, FTW_MOUNT | FTW_PHYS) < 0) {
+				bb_perror_msg("line %d: recursive failed for %s", linenum, full_name);
+				ret = EXIT_FAILURE;
+				goto loop;
+			}
 		} else
 		{
 			dev_t rdev;
Signature · 6 lines
1.9.1

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#3 lore raw

PATCH v2 2/2 docs/manual: documentation for recursive makedevs syntax

In reply to Angelo Compagnucci

This patch adds the documentation for the new makedevs recursive syntax.

---
 docs/manual/makedev-syntax.txt | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
docs/manual/makedev-syntax.txt +9 -1
index e02b79d..afb4ae2 100644
@@ -24,6 +24,7 @@ There are a few non-trivial blocks:
 - +type+ is the type of the file, being one of:
   * f: a regular file
   * d: a directory
+  * r: a directory recursively
   * c: a character device file
   * b: a block device file
   * p: a named pipe
@@ -38,7 +39,14 @@ Let's say you want to change the permissions of a given file; using
 this syntax, you will need to put:
 
 ----
-/usr/bin/foobar f 644 0 0 - - -	- -
+/usr/bin/foobar f 644 0 0 - - - - -
+----
+
+Alternatively, if you want to change owner/permission of a folder
+recursively, you can put:
+
+----
+/usr/share/myapp r 750 myuser myuser - - - - -
 ----
 
 On the other hand, if you want to create the device file +/dev/hda+
Signature · 1 line
1.9.1
#4 lore raw

PATCH v2 2/2 docs/manual: documentation for recursive makedevs syntax

Angelo, All,

On 2015-04-11 15:40 +0200, Angelo Compagnucci spake thusly:

Quoted 21 lines

This patch adds the documentation for the new makedevs recursive syntax.

---
 docs/manual/makedev-syntax.txt | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
docs/manual/makedev-syntax.txt +1 -0
index e02b79d..afb4ae2 100644
@@ -24,6 +24,7 @@ There are a few non-trivial blocks:
 - +type+ is the type of the file, being one of:
   * f: a regular file
   * d: a directory
+  * r: a directory recursively
   * c: a character device file
   * b: a block device file
   * p: a named pipe

docs/manual/makedev-syntax.txt Let's say you want to change the permissions of a given file; using · line 45

Quoted 8 lines
docs/manual/makedev-syntax.txt +4 -1
@@ -38,7 +39,14 @@ Let's say you want to change the permissions of a given file; using
 this syntax, you will need to put:
 
 ----
-/usr/bin/foobar f 644 0 0 - - -	- -
+/usr/bin/foobar f 644 0 0 - - - - -
+----
+
+Alternatively, if you want to change owner/permission of a folder

s/folder/directory/

Otherwise:

Regards,
Yann E. MORIN.

Quoted 14 lines
docs/manual/makedev-syntax.txt +4 -0
+recursively, you can put:
+
+----
+/usr/share/myapp r 750 myuser myuser - - - - -
 ----
 
 On the other hand, if you want to create the device file +/dev/hda+
Signature · 6 lines
1.9.1

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#5 lore raw

v2 cover Makedevs: add recursive option

In reply to Angelo Compagnucci

Dear Angelo Compagnucci,

Quoted 5 lines · Angelo Compagnucci

This patch series add the recursive option to makedevs.

With recursive option will be possible to apply owner/permission recursively to folders.

Quoted 7 lines

Angelo Compagnucci (2):
package/makedevs: add recursive option docs/manual: documentation for recursive makedevs syntax

 docs/manual/makedev-syntax.txt | 10 +++++++++-
 package/makedevs/makedevs.c    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

Both patches applied, after s/folder/directory/, as suggested by Yann.

Thanks!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
#6 lore raw

v2 cover Makedevs: add recursive option

Dear Thoma Petazzoni,

Il 11/apr/2015 18:04, "Thomas Petazzoni" <
[email protected]> ha scritto:

Quoted 13 lines

Dear Angelo Compagnucci,

Quoted 9 lines · Angelo Compagnucci

This patch series add the recursive option to makedevs.

With recursive option will be possible to apply owner/permission recursively to folders.

Angelo Compagnucci (2):
package/makedevs: add recursive option docs/manual: documentation for recursive makedevs syntax

Quoted 5 lines
Quoted 3 lines
 docs/manual/makedev-syntax.txt | 10 +++++++++-
 package/makedevs/makedevs.c    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

Both patches applied, after s/folder/directory/, as suggested by Yann.

Kawabonga! Fantastic news!

Thank you all for your support!

Quoted 8 lines

Thanks!

Thomas
--
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/buildroot/attachments/20150411/bdcf94ff/attachment.html>