From 54b110b3abc77ad472fac9651d88dcdccb800cb7 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sat, 25 Apr 2020 11:08:40 +0200 Subject: [PATCH] Fix #256: sar: Don't display duplicate entries in /etc/mtab Signed-off-by: Sebastien GODARD --- rd_stats.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/rd_stats.c b/rd_stats.c index 8879bc6..45b1be7 100644 --- a/rd_stats.c +++ b/rd_stats.c @@ -2503,7 +2503,7 @@ __nr_t read_filesystem(struct stats_filesystem *st_filesystem, __nr_t nr_alloc) { FILE *fp; char line[512], fs_name[MAX_FS_LEN], mountp[256], type[128]; - int skip = 0, skip_next = 0; + int skip = 0, skip_next = 0, fs; char *pos = 0, *pos2 = 0; __nr_t fs_read = 0; struct stats_filesystem *st_filesystem_i; @@ -2546,12 +2546,13 @@ __nr_t read_filesystem(struct stats_filesystem *st_filesystem, __nr_t nr_alloc) sscanf(line, "%127s", fs_name); /* * And now read the corresponding mount point. - * Read fs name and mount point in two distinct operations. + * Read fs name and mount point in two distinct operations, + * using '@pos + 1' position value for the mount point. * Indeed, if fs name length is greater than 127 chars, - * previous scanf() will read only the first 127 chars, and - * mount point name will be read using the remaining chars - * from the fs name. This will result in a bogus name - * and following statvfs() function will always fail. + * previous scanf() would read only the first 127 chars, and + * mount point name would be read using the remaining chars + * from the fs name. This would result in a bogus name + * and following statvfs() function would always fail. */ sscanf(pos + 1, "%255s", mountp); @@ -2565,6 +2566,18 @@ __nr_t read_filesystem(struct stats_filesystem *st_filesystem, __nr_t nr_alloc) if ((__statvfs(mountp, &buf) < 0) || (!buf.f_blocks)) continue; + /* Check if it's a duplicate entry */ + fs = fs_read - 1; + while (fs >= 0) { + st_filesystem_i = st_filesystem + fs; + if (!strcmp(st_filesystem_i->fs_name, fs_name)) + break; + fs--; + } + if (fs >= 0) + /* Duplicate entry found! Ignore current entry */ + continue; + if (fs_read + 1 > nr_alloc) { fs_read = -1; break; -- 2.40.0