mailing list of musl libc
 help / color / mirror / code / Atom feed
6057d8935a3b19d9112d973e701f6da4f50f91da blob 3182 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
#include <ftw.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>
#include "libc.h"

struct context
{
	char *path;
	int (*fn)(const char *, const struct stat *, int, struct FTW *);
	int fd_limit;
	int flags;

	size_t l;
	struct stat st;
	struct FTW lev;
};

struct history
{
	struct history *chain;
	dev_t dev;
	ino_t ino;
	int level;
	int base;
};

#undef dirfd
#define dirfd(d) (*(int *)d)

static int do_nftw(struct context *c, struct history *h)
{
	struct history new;
	int type;
	int r;

	if ((c->flags & FTW_PHYS) ? lstat(c->path, &c->st) : stat(c->path, &c->st) < 0) {
		if (!(c->flags & FTW_PHYS) && errno==ENOENT && !lstat(c->path, &c->st))
			type = FTW_SLN;
		else if (errno != EACCES) return -1;
		else type = FTW_NS;
	} else if (S_ISDIR(c->st.st_mode)) {
		if (access(c->path, R_OK) < 0) type = FTW_DNR;
		else if (c->flags & FTW_DEPTH) type = FTW_DP;
		else type = FTW_D;
	} else if (S_ISLNK(c->st.st_mode)) {
		if (c->flags & FTW_PHYS) type = FTW_SL;
		else type = FTW_SLN;
	} else {
		type = FTW_F;
	}

	if ((c->flags & FTW_MOUNT) && c->st.st_dev != h->dev)
		return 0;

	new.chain = h;
	new.dev = c->st.st_dev;
	new.ino = c->st.st_ino;
	new.level = h->level+1;
	new.base = c->l+1;
	
	if (!(c->flags & FTW_DEPTH)) {
		c->lev.level = new.level;
		c->lev.base = h->base;
		if ((r=c->fn(c->path, &c->st, type, &c->lev)))
			return r;
	}

	for (; h; h = h->chain)
		if (h->dev == c->st.st_dev && h->ino == c->st.st_ino)
			return 0;

	if ((type == FTW_D || type == FTW_DP) && c->fd_limit) {
		DIR *d = opendir(c->path);
		if (d) {
			struct dirent *de;
			while ((de = readdir(d))) {
				size_t dl;
				if (de->d_name[0] == '.'
				 && (!de->d_name[1]
				  || (de->d_name[1]=='.'
				   && !de->d_name[2]))) continue;
				if ((dl=strlen(de->d_name)) > PATH_MAX-new.base) {
					errno = ENAMETOOLONG;
					closedir(d);
					return -1;
				}
				c->l = new.base+dl;
				c->path[new.base-1]='/';
				memcpy(c->path+new.base, de->d_name, dl+1);
				--c->fd_limit;
				r=do_nftw(c, &new);
				++c->fd_limit;
				if (r) {
					closedir(d);
					return r;
				}
			}
			closedir(d);
		} else if (errno != EACCES) {
			return -1;
		}
	}
	c->path[new.base-1]=0;

	if (c->flags & FTW_DEPTH) {
		c->lev.level = new.level;
		c->lev.base = h->base;
		if ((r=c->fn(c->path, &c->st, type, &c->lev)))
			return r;
	}

	return 0;
}

int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
{
	struct context c;
	struct history h;
	int r, cs;
	size_t l;
	char pathbuf[PATH_MAX+1];

	if (fd_limit <= 0) return 0;

	l = strlen(path);
	while (l && path[l-1]=='/')
		--l;
	if (l > PATH_MAX) {
		errno = ENAMETOOLONG;
		return -1;
	}
	memcpy(pathbuf, path, l);
	pathbuf[l] = 0;
	
	h.chain = NULL;
	h.dev = (dev_t)-1;
	h.ino = (ino_t)-1;
	h.level = -1;
	h.base = l;
	while (h.base && pathbuf[h.base-1]!='/')
		--h.base;

	c.path = pathbuf;
	c.fn = fn;
	c.fd_limit = fd_limit;
	c.flags = flags;
	c.l = l;

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
	r = do_nftw(&c, &h);
	pthread_setcancelstate(cs, 0);
	return r;
}

LFS64(nftw);
debug log:

solving 6057d89 ...
found 6057d89 in https://inbox.vuxu.org/musl/1468316500.14738.1@mail.zhasha.com/
found 7df7226 in https://inbox.vuxu.org/musl/1468316500.14738.1@mail.zhasha.com/
found efb2b89 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 efb2b89524747397b0ccf3e5bbfb5438fd9ecd87	src/misc/nftw.c

applying [1/2] https://inbox.vuxu.org/musl/1468316500.14738.1@mail.zhasha.com/
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index efb2b89..7df7226 100644


applying [2/2] https://inbox.vuxu.org/musl/1468316500.14738.1@mail.zhasha.com/
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 7df7226..6057d89 100644

Checking patch src/misc/nftw.c...
Applied patch src/misc/nftw.c cleanly.
Checking patch src/misc/nftw.c...
Applied patch src/misc/nftw.c cleanly.

index at:
100644 6057d8935a3b19d9112d973e701f6da4f50f91da	src/misc/nftw.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).