public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 7/7] whereis: remove duplicates from search results
Date: Sat, 16 Mar 2013 23:40:19 +0000	[thread overview]
Message-ID: <1363477219-29245-8-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1363477219-29245-1-git-send-email-kerolasa@iki.fi>

Now when the whereis will not have search directory duplicates, and it
finds real path for symlinked files in paths it is time to remove search
result duplicates.

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=835211
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/whereis.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c
index 5136eff..0e333e1 100644
--- a/misc-utils/whereis.c
+++ b/misc-utils/whereis.c
@@ -173,6 +173,28 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
+static int is_same_file(char *file, struct is_here *filelist)
+{
+	struct stat statbuf;
+	struct is_here *prev = NULL;
+
+	if (stat(file, &statbuf))
+		return 1;
+	while (filelist) {
+		if (filelist->st_ino == statbuf.st_ino &&
+		    filelist->st_dev == statbuf.st_dev)
+			return 1;
+		prev = filelist;
+		filelist = filelist->next;
+	}
+	assert(prev);
+	filelist = xcalloc(1, sizeof(struct is_here));
+	filelist->st_ino = statbuf.st_ino;
+	filelist->st_dev = statbuf.st_dev;
+	prev->next = filelist;
+	return 0;
+}
+
 static int itsit(char *cp, char *dp)
 {
 	int i = strlen(dp);
@@ -201,7 +223,7 @@ static int itsit(char *cp, char *dp)
 	return 0;
 }
 
-static void findin(char *dir, char *cp, int *nr_found, char **wait)
+static void findin(char *dir, char *cp, int *nr_found, char **wait, struct is_here *filelist)
 {
 	DIR *dirp;
 	struct dirent *dp;
@@ -213,6 +235,8 @@ static void findin(char *dir, char *cp, int *nr_found, char **wait)
 	while ((dp = readdir(dirp)) != NULL)
 		if (itsit(cp, dp->d_name)) {
 			xasprintf(&full_path, "%s/%s", dir, dp->d_name);
+			if (is_same_file(full_path, filelist))
+				continue;
 			rl = canonicalize_path(full_path);
 			free(full_path);
 			if (*(nr_found) == 0)
@@ -376,16 +400,19 @@ static void lookup(char *argv, struct is_here *dirlist, int flags)
 {
 	int nr_found;
 	char *wait = NULL;
+	struct is_here *filelist;
 
 	nr_found = uflag == 1 ? 0 : 1;
+	filelist = xcalloc(1, sizeof(struct is_here));
 
 	for (; dirlist; dirlist = dirlist->next) {
 		if (check_type(dirlist->type, flags))
 			continue;
 		if (dirlist->path != NULL)
-			findin(dirlist->path, argv, &nr_found, &wait);
+			findin(dirlist->path, argv, &nr_found, &wait, filelist);
 	}
 	free(wait);
+	free_dirs(filelist, ALL_DIRS);
 	if (1 < nr_found)
 		putchar('\n');
 	return;
-- 
1.8.2


  parent reply	other threads:[~2013-03-16 23:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-16 23:40 [PATCH 0/7] pull: whereis changes Sami Kerola
2013-03-16 23:40 ` [PATCH 1/7] whereis: rewrite most of the command Sami Kerola
2013-03-16 23:40 ` [PATCH 2/7] whereis: add search scope listing option Sami Kerola
2013-03-16 23:40 ` [PATCH 3/7] whereis: align with howto-usage-function.txt Sami Kerola
2013-03-16 23:40 ` [PATCH 4/7] whereis: support MANPATH environment variable Sami Kerola
2013-03-16 23:40 ` [PATCH 5/7] docs: generic whereis.1 clean up Sami Kerola
2013-03-16 23:40 ` [PATCH 6/7] whereis: canonicalize files before printing Sami Kerola
2013-03-20 12:56   ` Karel Zak
2013-03-20 14:03     ` Sami Kerola
2013-03-16 23:40 ` Sami Kerola [this message]
2013-03-20 12:59   ` [PATCH 7/7] whereis: remove duplicates from search results Karel Zak
2013-03-17  9:23 ` [PATCH 0/7] pull: whereis changes Sami Kerola
2013-03-20 13:02 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1363477219-29245-8-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox