Commit 2a1ef032 authored by Masami Hiramatsu's avatar Masami Hiramatsu Committed by Arnaldo Carvalho de Melo

perf tools: Fix rm_rf() to handle non-regular files correctly

Fix rm_rf() to handle non-regular files correctly. This fix includes two
changes;

 - Fix to use lstat(3) instead of stat(3) since if the target
   file is a symbolic link, rm_rf() should unlink the symbolic
   link itself, not the file which pointed by the symlink.
 - Fix to unlink non-regular files (except for directory),
   including symlink.

Even though the first one fixes to stat symlink itself, without second
fix, it still failed because the symlink is not a regular file.
Signed-off-by: default avatarMasami Hiramatsu <mhiramat@kernel.org>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Hemant Kumar <hemant@linux.vnet.ibm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160608092911.3116.90929.stgit@devboxSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 826424cc
...@@ -97,20 +97,17 @@ int rm_rf(char *path) ...@@ -97,20 +97,17 @@ int rm_rf(char *path)
scnprintf(namebuf, sizeof(namebuf), "%s/%s", scnprintf(namebuf, sizeof(namebuf), "%s/%s",
path, d->d_name); path, d->d_name);
ret = stat(namebuf, &statbuf); /* We have to check symbolic link itself */
ret = lstat(namebuf, &statbuf);
if (ret < 0) { if (ret < 0) {
pr_debug("stat failed: %s\n", namebuf); pr_debug("stat failed: %s\n", namebuf);
break; break;
} }
if (S_ISREG(statbuf.st_mode)) if (S_ISDIR(statbuf.st_mode))
ret = unlink(namebuf);
else if (S_ISDIR(statbuf.st_mode))
ret = rm_rf(namebuf); ret = rm_rf(namebuf);
else { else
pr_debug("unknown file: %s\n", namebuf); ret = unlink(namebuf);
ret = -1;
}
} }
closedir(dir); closedir(dir);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment