diff mbox series

[LEDE-DEV,fstools] libfstools: fix foreachdir() to pass dir with a trailing slash

Message ID 20180413112614.21378-1-zajec5@gmail.com
State Accepted
Headers show
Series [LEDE-DEV,fstools] libfstools: fix foreachdir() to pass dir with a trailing slash | expand

Commit Message

Rafał Miłecki April 13, 2018, 11:26 a.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

Commit cc63723d886fd ("overlay: use lstat rather than stat and make sure
there are no trailing spaces") changed behavior of foreachdir() breaking
some callbacks. Before that modification all callbacks were getting
directory with a trailing slash. Above commit started removing them.

This broke handle_whiteout() which doesn't work at all since then. It
constructs file paths incorrectly: slash is missing between directory
and a file name. It seems noone noticed it for years because this issue
got hidden by switch2jffs() which also handles whiteouts with its system
command "cp -a" call.

Fix that regression by setting trailing slash back - right after calling
lstat(). Also to keep code simple just skip all entries that aren't
directories. This keeps conditions for removing/setting trailing slash
trivial. A side effect is not calling callbacks for files which is a
free bonus optimization.

Fixes: cc63723d886fd ("overlay: use lstat rather than stat and make sure there are no trailing spaces")
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 libfstools/overlay.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Comments

John Crispin April 16, 2018, 3:14 p.m. UTC | #1
On 13/04/18 13:26, Rafał Miłecki wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
>
> Commit cc63723d886fd ("overlay: use lstat rather than stat and make sure
> there are no trailing spaces") changed behavior of foreachdir() breaking
> some callbacks. Before that modification all callbacks were getting
> directory with a trailing slash. Above commit started removing them.
>
> This broke handle_whiteout() which doesn't work at all since then. It
> constructs file paths incorrectly: slash is missing between directory
> and a file name. It seems noone noticed it for years because this issue
> got hidden by switch2jffs() which also handles whiteouts with its system
> command "cp -a" call.
>
> Fix that regression by setting trailing slash back - right after calling
> lstat(). Also to keep code simple just skip all entries that aren't
> directories. This keeps conditions for removing/setting trailing slash
> trivial. A side effect is not calling callbacks for files which is a
> free bonus optimization.
>
> Fixes: cc63723d886fd ("overlay: use lstat rather than stat and make sure there are no trailing spaces")
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
> ---
>   libfstools/overlay.c | 14 ++++++++++++--
>   1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/libfstools/overlay.c b/libfstools/overlay.c
> index 1d69f5a..a41364c 100644
> --- a/libfstools/overlay.c
> +++ b/libfstools/overlay.c
> @@ -88,15 +88,25 @@ foreachdir(const char *dir, int (*cb)(const char*))
>   
>   	sprintf(globdir, "%s/*", dir);
>   
> +	/* Include GLOB_MARK as callbacks expect a trailing slash */
>   	if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
>   		for (j = 0; j < gl.gl_pathc; j++) {
>   			char *dir = gl.gl_pathv[j];
>   			int len = strlen(gl.gl_pathv[j]);
> +			int err;
>   
> -			if (len > 1 && dir[len - 1] == '/')
> +			/* Quick way of skipping files */
> +			if (dir[len - 1] != '/')
> +				continue;
> +
> +			/* lstat needs path without a trailing slash */
> +			if (len > 1)
>   				dir[len - 1] = '\0';
> +			err = lstat(gl.gl_pathv[j], &s);
> +			if (len > 1)
> +				dir[len - 1] = '/';
>   
> -			if (!lstat(gl.gl_pathv[j], &s) && !S_ISLNK(s.st_mode))
> +			if (!err && !S_ISLNK(s.st_mode))
>   				foreachdir(gl.gl_pathv[j], cb);
>   	}
>   	cb(dir);
diff mbox series

Patch

diff --git a/libfstools/overlay.c b/libfstools/overlay.c
index 1d69f5a..a41364c 100644
--- a/libfstools/overlay.c
+++ b/libfstools/overlay.c
@@ -88,15 +88,25 @@  foreachdir(const char *dir, int (*cb)(const char*))
 
 	sprintf(globdir, "%s/*", dir);
 
+	/* Include GLOB_MARK as callbacks expect a trailing slash */
 	if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
 		for (j = 0; j < gl.gl_pathc; j++) {
 			char *dir = gl.gl_pathv[j];
 			int len = strlen(gl.gl_pathv[j]);
+			int err;
 
-			if (len > 1 && dir[len - 1] == '/')
+			/* Quick way of skipping files */
+			if (dir[len - 1] != '/')
+				continue;
+
+			/* lstat needs path without a trailing slash */
+			if (len > 1)
 				dir[len - 1] = '\0';
+			err = lstat(gl.gl_pathv[j], &s);
+			if (len > 1)
+				dir[len - 1] = '/';
 
-			if (!lstat(gl.gl_pathv[j], &s) && !S_ISLNK(s.st_mode))
+			if (!err && !S_ISLNK(s.st_mode))
 				foreachdir(gl.gl_pathv[j], cb);
 	}
 	cb(dir);