aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-08-26 21:25:42 -0500
committerRob Landley <rob@landley.net>2019-08-26 21:25:42 -0500
commitc26870dab3462c6176936384b090df6b9ba46dee (patch)
treed7672c441d36d0b655b4a352dc4300107265b685
parent0b51fcccf097f70cfd033fee1c983b4dc8d214b7 (diff)
downloadtoybox-c26870dab3462c6176936384b090df6b9ba46dee.tar.gz
Try to make ls failure more graceful. Print ? ? ? entries instead of error msg.
Tweak DIRTREE_STATLESS so it returns zero stat for any error (I'm testing that dev, ino, and blksize are all zero), and fill in file type from readdir()
-rw-r--r--lib/dirtree.c8
-rw-r--r--toys/posix/ls.c53
2 files changed, 39 insertions, 22 deletions
diff --git a/lib/dirtree.c b/lib/dirtree.c
index e44df66c..df38b257 100644
--- a/lib/dirtree.c
+++ b/lib/dirtree.c
@@ -27,7 +27,7 @@ int dirtree_notdotdot(struct dirtree *catch)
struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
{
- struct dirtree *dt = NULL;
+ struct dirtree *dt = 0;
struct stat st;
int len = 0, linklen = 0, statless = 0;
@@ -36,7 +36,7 @@ struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
int fd = parent ? parent->dirfd : AT_FDCWD;
if (fstatat(fd, name, &st,AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) {
- if ((flags&DIRTREE_STATLESS) && errno == ENOENT) statless++;
+ if (flags&DIRTREE_STATLESS) statless++;
else goto error;
}
if (S_ISLNK(st.st_mode)) {
@@ -122,7 +122,7 @@ struct dirtree *dirtree_handle_callback(struct dirtree *new,
// If this had children, it was callback's job to free them already.
if (!(flags & DIRTREE_SAVE)) {
free(new);
- new = NULL;
+ new = 0;
}
return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
@@ -157,6 +157,8 @@ int dirtree_recurse(struct dirtree *node,
while ((entry = readdir(dir))) {
if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
+ if (!new->st.st_blksize && !new->st.st_mode)
+ new->st.st_mode = entry->d_type<<12;
new = dirtree_handle_callback(new, callback);
if (new == DIRTREE_ABORTVAL) break;
if (new) {
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index a05141ea..fbd2405c 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -283,6 +283,15 @@ static int color_from_mode(mode_t mode)
return color;
}
+static void zprint(int zap, char *pat, int len, unsigned long arg)
+{
+ char tmp[32];
+
+ sprintf(tmp, "%%*%s", zap ? "s" : pat);
+ if (zap && pat[strlen(pat)-1]==' ') strcat(tmp, " ");
+ printf(tmp, len, zap ? (unsigned long)"?" : arg);
+}
+
// Display a list of dirtree entries, according to current format
// Output types -1, -l, -C, or stream
@@ -321,7 +330,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Read directory contents. We dup() the fd because this will close it.
// This reads/saves contents to display later, except for in "ls -1f" mode.
} else dirtree_recurse(indir, filter, dup(dirfd),
- DIRTREE_SYMFOLLOW*!!(flags&FLAG_L));
+ DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*!!(flags&FLAG_L));
// Copy linked list to array and sort it. Directories go in array because
// we visit them in sorted order too. (The nested loops let us measure and
@@ -392,13 +401,16 @@ static void listfiles(int dirfd, struct dirtree *indir)
// Loop through again to produce output.
width = 0;
for (ul = 0; ul<dtlen; ul++) {
- int ii;
+ int ii, zap;
unsigned curcol, color = 0;
unsigned long next = next_column(ul, dtlen, columns, &curcol);
struct stat *st = &(sort[next]->st);
mode_t mode = st->st_mode;
char et = endtype(st), *ss;
+ // If we couldn't stat, output ? for most fields
+ zap = !st->st_blksize && !st->st_dev && !st->st_ino;
+
// Skip directories at the top of the tree when -d isn't set
if (S_ISDIR(mode) && !indir->parent && !(flags & FLAG_d)) continue;
TT.nl_title=1;
@@ -421,25 +433,26 @@ static void listfiles(int dirfd, struct dirtree *indir)
}
width += *len;
- if (flags & FLAG_i) printf("%*lu ", totals[1], (unsigned long)st->st_ino);
+ if (flags & FLAG_i) zprint(zap, "lu ", totals[1], st->st_ino);
if (flags & FLAG_s) {
print_with_h(tmp, st->st_blocks, 512);
- printf("%*s ", totals[6], tmp);
+ zprint(zap, "s ", totals[6], (unsigned long)tmp);
}
if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
- struct tm *tm;
// (long) is to coerce the st types into something we know we can print.
mode_to_string(mode, tmp);
- printf("%s% *ld", tmp, totals[2]+1, (long)st->st_nlink);
+ if (zap) memset(tmp+1, '?', 9);
+ printf("%s", tmp);
+ zprint(zap, "ld", totals[2]+1, st->st_nlink);
// print user
if (!(flags&FLAG_g)) {
putchar(' ');
ii = -totals[3];
- if (flags&FLAG_n) printf("%*u", ii, (unsigned)st->st_uid);
+ if (zap || (flags&FLAG_n)) zprint(zap, "lu", ii, st->st_uid);
else draw_trim_esc(getusername(st->st_uid), ii, abs(ii), TT.escmore,
crunch_qb);
}
@@ -448,21 +461,24 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (!(flags&FLAG_o)) {
putchar(' ');
ii = -totals[4];
- if (flags&FLAG_n) printf("%*u", ii, (unsigned)st->st_gid);
+ if (zap || (flags&FLAG_n)) zprint(zap, "lu", ii, st->st_gid);
else draw_trim_esc(getgroupname(st->st_gid), ii, abs(ii), TT.escmore,
crunch_qb);
}
+ }
+ if (flags & FLAG_Z)
+ printf(" %-*s", -(int)totals[7], (char *)sort[next]->extra);
- if (flags & FLAG_Z)
- printf(" %-*s", -(int)totals[7], (char *)sort[next]->extra);
+ if (flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) {
+ struct tm *tm;
// print major/minor, or size
- if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode))
+ if (!zap && (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)))
printf("% *d,% 4d", totals[5]-4, dev_major(st->st_rdev),
dev_minor(st->st_rdev));
else {
print_with_h(tmp, st->st_size, 1);
- printf("%*s", totals[5]+1, tmp);
+ zprint(zap, "s", totals[5]+1, (unsigned long)tmp);
}
// print time, always in --time-style=long-iso
@@ -474,9 +490,8 @@ static void listfiles(int dirfd, struct dirtree *indir)
s += sprintf(s, ":%02d.%09d ", tm->tm_sec, (int)st->st_mtim.tv_nsec);
strftime(s, sizeof(tmp)-(s-tmp), "%z", tm);
}
- printf(" %s ", tmp);
- } else if (flags & FLAG_Z)
- printf("%-*s ", (int)totals[7], (char *)sort[next]->extra);
+ zprint(zap, "s ", 17+(TT.l>1)*13, (unsigned long)tmp);
+ }
if (flags & FLAG_color) {
color = color_from_mode(st->st_mode);
@@ -489,7 +504,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
printf(" -> ");
- if (flags & FLAG_color) {
+ if (!zap && (flags & FLAG_color)) {
struct stat st2;
if (fstatat(dirfd, sort[next]->symlink, &st2, 0)) color = 256+31;
@@ -498,8 +513,8 @@ static void listfiles(int dirfd, struct dirtree *indir)
if (color) printf("\033[%d;%dm", color>>8, color&255);
}
- printf("%s", sort[next]->symlink);
- if (color) printf("\033[0m");
+ zprint(zap, "s", 0, (unsigned long)sort[next]->symlink);
+ if (!zap && color) printf("\033[0m");
}
if (et) xputc(et);
@@ -566,7 +581,7 @@ void ls_main(void)
int sym = !(toys.optflags&(FLAG_l|FLAG_d|FLAG_F))
|| (toys.optflags&(FLAG_L|FLAG_H));
- dt = dirtree_add_node(0, *s, DIRTREE_SYMFOLLOW*sym);
+ dt = dirtree_add_node(0, *s, DIRTREE_STATLESS|DIRTREE_SYMFOLLOW*sym);
// note: double_list->prev temporarirly goes in dirtree->parent
if (dt) dlist_add_nomalloc((void *)&TT.files->child, (void *)dt);