From: Karel Zak Date: Fri, 14 Sep 2018 14:27:26 +0000 (+0200) Subject: lsblk: encapsulate stat() usage X-Git-Tag: v2.33-rc1~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=652cb1cd26a9fd62653e112b04a9512f5f42578c;p=thirdparty%2Futil-linux.git lsblk: encapsulate stat() usage Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 2a6044c0e5..4c8cb7d1f4 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -822,16 +822,19 @@ static char *get_vfs_attribute(struct blkdev_cxt *cxt, int id) return sizestr; } +static struct stat *device_get_stat(struct blkdev_cxt *cxt) +{ + if (!cxt->st.st_rdev) + stat(cxt->filename, &cxt->st); + + return &cxt->st; +} static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libscols_line *ln) { - int sort = 0, st_rc = 0; + int sort = 0; char *str = NULL; - if (!cxt->st.st_rdev && (id == COL_OWNER || id == COL_GROUP || - id == COL_MODE)) - st_rc = stat(cxt->filename, &cxt->st); - if (lsblk->sort_id == id) sort = 1; @@ -852,26 +855,27 @@ static void set_scols_data(struct blkdev_cxt *cxt, int col, int id, struct libsc break; case COL_OWNER: { - struct passwd *pw = st_rc ? NULL : getpwuid(cxt->st.st_uid); + struct stat *st = device_get_stat(cxt); + struct passwd *pw = st ? NULL : getpwuid(st->st_uid); if (pw) str = xstrdup(pw->pw_name); break; } case COL_GROUP: { - struct group *gr = st_rc ? NULL : getgrgid(cxt->st.st_gid); + struct stat *st = device_get_stat(cxt); + struct group *gr = st ? NULL : getgrgid(st->st_gid); if (gr) str = xstrdup(gr->gr_name); break; } case COL_MODE: { - char md[11]; + struct stat *st = device_get_stat(cxt); + char md[11] = { '\0' }; - if (!st_rc) { - xstrmode(cxt->st.st_mode, md); - str = xstrdup(md); - } + if (st) + str = xstrdup(xstrmode(st->st_mode, md)); break; } case COL_MAJMIN: