From 44c43973238ca1c8d7ac6e243932fb5b32731155 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 8 Apr 2020 10:36:03 -0700 Subject: ls: fix -h with block counts. The filter() function modifies st_blocks so it's always 1KiB rather than 512B blocks, but the human-readable output was still assuming 512B. This meant that `ls -sh` was showing figures half the size of `ls -s`, and that the "total" line with -h was also off by a factor of 2. No new test, because I don't know how to write one that would work on all file systems. Bug: http://b/153383721 --- toys/posix/ls.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'toys/posix/ls.c') diff --git a/toys/posix/ls.c b/toys/posix/ls.c index dda82d40..01a64c2d 100644 --- a/toys/posix/ls.c +++ b/toys/posix/ls.c @@ -151,7 +151,7 @@ static void entrylen(struct dirtree *dt, unsigned *len) } else len[5] = print_with_h(tmp, st->st_size, 1); } - len[6] = FLAG(s) ? print_with_h(tmp, st->st_blocks, 512) : 0; + len[6] = FLAG(s) ? print_with_h(tmp, st->st_blocks, 1024) : 0; len[7] = FLAG(Z) ? strwidth((char *)dt->extra) : 0; } @@ -200,7 +200,7 @@ static int filter(struct dirtree *new) if (FLAG(u)) new->st.st_mtime = new->st.st_atime; if (FLAG(c)) new->st.st_mtime = new->st.st_ctime; - new->st.st_blocks >>= 1; + new->st.st_blocks >>= 1; // Use 1KiB blocks rather than 512B blocks. if (FLAG(a)||FLAG(f)) return DIRTREE_SAVE; if (!FLAG(A) && new->name[0]=='.') return 0; @@ -325,7 +325,7 @@ static void listfiles(int dirfd, struct dirtree *indir) totpad = totals[1]+!!totals[1]+totals[6]+!!totals[6]+totals[7]+!!totals[7]; if ((FLAG(h)||FLAG(l)||FLAG(o)||FLAG(n)||FLAG(g)||FLAG(s)) && indir->parent) { - print_with_h(tmp, blocks, 512); + print_with_h(tmp, blocks, 1024); xprintf("total %s\n", tmp); } } @@ -401,7 +401,7 @@ static void listfiles(int dirfd, struct dirtree *indir) if (FLAG(i)) zprint(zap, "lu ", totals[1], st->st_ino); if (FLAG(s)) { - print_with_h(tmp, st->st_blocks, 512); + print_with_h(tmp, st->st_blocks, 1024); zprint(zap, "s ", totals[6], (unsigned long)tmp); } -- cgit v1.2.3