mailing list of musl libc
 help / color / mirror / code / Atom feed
382169f66b8b81ed19726a96755d5d578baba9e1 blob 3741 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
#include <ftw.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <pthread.h>

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(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
{
	size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
	struct stat st;
	struct history new;
	int type;
	int r, r2;
	int dfd;
	int err;
	struct FTW lev;

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

	if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev)
		return 0;
	
	new.chain = h;
	new.dev = st.st_dev;
	new.ino = st.st_ino;
	new.level = h ? h->level+1 : 0;
	new.base = j+1;
	
	lev.level = new.level;
	if (h) {
		lev.base = h->base;
	} else {
		size_t k;
		for (k=j; k && path[k]=='/'; k--);
		for (; k && path[k-1]!='/'; k--);
		lev.base = k;
	}

	if (type == FTW_D || type == FTW_DP) {
		dfd = open(path, O_RDONLY);
		err = errno;
		if (dfd < 0 && err == EACCES) type = FTW_DNR;
		if (!fd_limit) close(dfd);
	}

	r = 0;
	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
		if ((flags & FTW_ACTIONRETVAL)) {
			if (r == FTW_STOP) return FTW_STOP;
			if (r == FTW_SKIP_SUBTREE) return 0;
			/* other values are saved for when returning */
		} else
			return r;
	}

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

	if ((type == FTW_D || type == FTW_DP) && fd_limit) {
		if (dfd < 0) {
			errno = err;
			return -1;
		}
		DIR *d = fdopendir(dfd);
		if (d) {
			struct dirent *de;
			if (flags & FTW_CHDIR) {
				if (!fchdir(dfd)) {
					err = errno;
					closedir(d);
					errno = err;
					return -1;
				}
			}
			while ((de = readdir(d))) {
				if (de->d_name[0] == '.'
				 && (!de->d_name[1]
				  || (de->d_name[1]=='.'
				   && !de->d_name[2]))) continue;
				if (strlen(de->d_name) >= PATH_MAX-l) {
					closedir(d);
					errno = ENAMETOOLONG;
					return -1;
				}
				path[j]='/';
				strcpy(path+j+1, de->d_name);
				if ((r2=do_nftw(path, fn, fd_limit-1, flags, &new))) {
					if ((flags & FTW_ACTIONRETVAL)
					    && r2 == FTW_SKIP_SIBLINGS)
						break;
					closedir(d);
					return r2;
				}
			}
			closedir(d);
		} else {
			close(dfd);
			return -1;
		}
	}

	path[l] = 0;
	if (flags & FTW_DEPTH) {
		r = fn(path, &st, type, &lev);
		/* ignore FTW_SKIP_SUBTREE (too late), the caller is broken */
		if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SUBTREE)
			return 0;
	}
	return r;
}

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

	if (fd_limit <= 0) return 0;

	l = strlen(path);
	if (l > PATH_MAX) {
		errno = ENAMETOOLONG;
		return -1;
	}
	memcpy(pathbuf, path, l+1);
	
	if (flags & FTW_CHDIR) {
		orig_dfd = open(".", O_CLOEXEC | O_PATH);
		if (orig_dfd < 0)
			return -1;
	}

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
	pthread_setcancelstate(cs, 0);
	if (flags & FTW_CHDIR) {
		if (!fchdir(orig_dfd))
			r = -1;
		int err = errno;
		close(orig_dfd);
		errno = err;
	}
	return r;
}

weak_alias(nftw, nftw64);
debug log:

solving 382169f66b8b ...
found 382169f66b8b in https://inbox.vuxu.org/musl/20220123155955.16484-3-ismael@iodev.co.uk/
found 7569a657e54e in https://inbox.vuxu.org/musl/20220123155955.16484-2-ismael@iodev.co.uk/
found 5b233b2b8e77 in https://inbox.vuxu.org/musl/20220123154517.15262-1-ismael@iodev.co.uk/
found 8dcff7fefd2a in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 8dcff7fefd2a3d120b0e0bab7647d3a782aee9c9	src/misc/nftw.c

applying [1/3] https://inbox.vuxu.org/musl/20220123154517.15262-1-ismael@iodev.co.uk/
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 8dcff7fefd2a..5b233b2b8e77 100644


applying [2/3] https://inbox.vuxu.org/musl/20220123155955.16484-2-ismael@iodev.co.uk/
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 5b233b2b8e77..7569a657e54e 100644


applying [3/3] https://inbox.vuxu.org/musl/20220123155955.16484-3-ismael@iodev.co.uk/
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 7569a657e54e..382169f66b8b 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.
Checking patch src/misc/nftw.c...
Applied patch src/misc/nftw.c cleanly.

index at:
100644 382169f66b8b81ed19726a96755d5d578baba9e1	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).