~ft/9pro

4a9f03f341b69bcad13c5ea24c855980c5e3c8f0 — Sigrid Haflínudóttir 3 months ago fa3dedf
9pex: fix dir reading
1 files changed, 20 insertions(+), 8 deletions(-)

M 9pex.c
M 9pex.c => 9pex.c +20 -8
@@ 685,6 685,7 @@ readf(C9ctx *c, C9tag tag, Fid *f, uint64_t offset, uint32_t size, char **err)
		}
	}

	res = 0;
	num = 0;
	for (i = 0; i < nelem(c9st); i++) {
		dirpos[i] = telldir(f->dir); /* so we can rewind in case another stat doesn't fit */


@@ 692,7 693,8 @@ readf(C9ctx *c, C9tag tag, Fid *f, uint64_t offset, uint32_t size, char **err)
		errno = 0;
		if ((e = readdir(f->dir)) == NULL && errno != 0) {
			*err = strerror(errno);
			return -1;
			res = -1;
			goto done;
		}
		if (e == NULL) /* eof */
			break;


@@ 701,22 703,32 @@ readf(C9ctx *c, C9tag tag, Fid *f, uint64_t offset, uint32_t size, char **err)

		if (fstatat(f->fd, e->d_name, &st, 0) != 0) {
			*err = strerror(errno);
			if (fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) /* broken symlink, try to stat the link itself */
				return -1;
			if (fstatat(f->fd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) { /* broken symlink, try to stat the link itself */
				res = -1;
				goto done;
			}
		}
		if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0)
			return -1;
		if (stat2c9stat(e->d_name, &st, &c9st[i], err) != 0) {
			res = -1;
			goto done;
		}
		c9st[i].name = strdup(c9st[i].name);
		c9stp[num++] = &c9st[i];
	}

	i = num;
	if (s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0)
		return -1;
	if (s9do(s9readdir(c, tag, c9stp, &num, &f->diroffset, size), err) != 0) {
		res = -1;
		goto done;
	}
	trace("<- Rread tag=%d count=%"PRIu64" data=...\n", tag, f->diroffset - offset);
	if (i != num)
		seekdir(f->dir, dirpos[num]);

	return 0;
done:
	for (i = 0; i < num; i++)
		free(c9st[i].name);
	return res;
}

static void