mailing list of musl libc
 help / color / mirror / code / Atom feed
blob 8883b9f802d6f425bf1fe2c533b322b9d260bb77 2410 bytes (raw)
name: src/dirent/scandir.c 	 # note: path name is non-authoritative(*)

 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
 
#include <dirent.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <stddef.h>

struct cleanup_params {
	DIR *d;
	struct dirent **names;
	size_t cnt;
};

static void cleanup(void *p)
{
	struct cleanup_params *c = p;
	/* Cancellation is disabled if c->d is not NULL, so closedir()
	 * won't act upon a cancellation request here. */
	if (c->d) closedir(c->d);
	if (c->names) {
		size_t cnt = c->cnt;
		while (cnt-->0) free(c->names[cnt]);
		free(c->names);
	}
}

int scandir(const char *path, struct dirent ***res,
	int (*sel)(const struct dirent *),
	int (*cmp)(const struct dirent **, const struct dirent **))
{
	/* opendir() might act upon a cancellation request. */
	struct cleanup_params c = {.d = opendir(path)};
	struct dirent *de, **tmp;
	size_t len=0;
	int old_errno = errno, err;

	if (!c.d) return -1;

	/* sel(), closedir(), or cmp() might act upon a cancellation request. */
	pthread_cleanup_push(cleanup, &c);

	while ((errno=0), (de = readdir(c.d))) {
		if (sel) {
			/* sel() must not observe that errno was set to 0. */
			errno = old_errno;
			if (!sel(de)) continue;
		}
		if (c.cnt >= len) {
			len = 2*len+1;
			if (len > SIZE_MAX/sizeof *c.names) break;
			tmp = realloc(c.names, len * sizeof *c.names);
			if (!tmp) break;
			c.names = tmp;
		}
		c.names[c.cnt] = malloc(de->d_reclen);
		if (!c.names[c.cnt]) break;
		memcpy(c.names[c.cnt++], de, de->d_reclen);
	}
	/* closedir() might set errno via __aio_close(). It might also "fail"
	 * (return -1 and set errno). But even then, the file descriptor is
	 * closed and memory is freed, so there is no reason to report the
	 * "failure" of closedir() as a failure of scandir(). */
	err = errno;

	/* If closedir() acts upon a cancellation request, it is retried by
	 * cleanup() with cancellation disabled. closedir() won't act upon a
	 * cancellation request in masked cancellation mode, meaning errno does
	 * not need to be inspected for ECANCELED. */
	closedir(c.d);
	/* cleanup() shouldn't call closedir() from here on, because the
	 * directory stream is already closed. */
	c.d = 0;

	if (!err) {
		/* cmp() and caller must not observe that errno was set to 0. */
		errno = old_errno;
		if (cmp) qsort(c.names, c.cnt, sizeof *c.names, (int (*)(const void *, const void *))cmp);
		*res = c.names;
	}

	pthread_cleanup_pop(err);

	return err ? (errno = err), -1 : c.cnt;
}

debug log:

solving 8883b9f8 ...
found 8883b9f8 in https://inbox.vuxu.org/musl/20250710185131.186-3-mailto.luca.kellermann@gmail.com/
found cf668535 in https://inbox.vuxu.org/musl/20250710185131.186-2-mailto.luca.kellermann@gmail.com/ ||
	https://inbox.vuxu.org/musl/20250721002322.703-2-mailto.luca.kellermann@gmail.com/
found eead7e50 in https://inbox.vuxu.org/musl/20250710185131.186-1-mailto.luca.kellermann@gmail.com/ ||
	https://inbox.vuxu.org/musl/20250721002322.703-1-mailto.luca.kellermann@gmail.com/
found 7456b9b8 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 7456b9b8bcec48f28500b3bc354736ca2d89f3a0	src/dirent/scandir.c

applying [1/3] https://inbox.vuxu.org/musl/20250710185131.186-1-mailto.luca.kellermann@gmail.com/
diff --git a/src/dirent/scandir.c b/src/dirent/scandir.c
index 7456b9b8..eead7e50 100644

Checking patch src/dirent/scandir.c...
Applied patch src/dirent/scandir.c cleanly.

skipping https://inbox.vuxu.org/musl/20250721002322.703-1-mailto.luca.kellermann@gmail.com/ for eead7e50
index at:
100644 eead7e508ae9c68a067977293aabb52d23eabe93	src/dirent/scandir.c

applying [2/3] https://inbox.vuxu.org/musl/20250710185131.186-2-mailto.luca.kellermann@gmail.com/
diff --git a/src/dirent/scandir.c b/src/dirent/scandir.c
index eead7e50..cf668535 100644

Checking patch src/dirent/scandir.c...
Applied patch src/dirent/scandir.c cleanly.

skipping https://inbox.vuxu.org/musl/20250721002322.703-2-mailto.luca.kellermann@gmail.com/ for cf668535
index at:
100644 cf668535523e9049b2f1d61361b761856f27c9cf	src/dirent/scandir.c

applying [3/3] https://inbox.vuxu.org/musl/20250710185131.186-3-mailto.luca.kellermann@gmail.com/
diff --git a/src/dirent/scandir.c b/src/dirent/scandir.c
index cf668535..8883b9f8 100644

Checking patch src/dirent/scandir.c...
Applied patch src/dirent/scandir.c cleanly.

index at:
100644 8883b9f802d6f425bf1fe2c533b322b9d260bb77	src/dirent/scandir.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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).