Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fts_read and fts_children do not return any file or directory #10

Open
fuck-shithub opened this issue Nov 22, 2022 · 1 comment
Open

Comments

@fuck-shithub
Copy link

Hello, I am developing a server program for Alpine Linux, and I want to traverse file hierarchies, so I look up functions to do just that, and I find nftw which apparently sucks (stupid callback) so I saw the fts family of functions from BSD. Turns out musl does not support it but this package should, so I write a function using fts and it doesn't work at all and I don't know why.

Here is the relevant function I wrote. The output is just . Am I doing something wrong or is this fts implementation broken on Alpine Linux?
I rewrote it in nftw and that works, but I don't like nftw's design and that it requires global variables.

/*
 * Free after use with xmlFree
 */
xmlChar *
generate_tracks_xml_fts(void)
{
	xmlNodePtr root_node;
	xmlChar *xmlbuf;
	FTS *fts;
	FTSENT *parent;
	FTSENT *child;

	doc = xmlNewDoc(BAD_CAST "1.0");
	root_node = xmlNewNode(NULL, BAD_CAST "releases");
	xmlDocSetRootElement(doc, root_node);

	char *path_argv[] = { (char*) MUSIC_PATH, NULL };
	fts = fts_open(path_argv, FTS_NOCHDIR | FTS_XDEV, NULL);
	if (fts == NULL) {
		if (errno != ENOENT) {
			fprintf(stderr, "Failed to fts_open %s", MUSIC_PATH);
		}
		return 0;
	}

	while ((parent = fts_read(fts)) != NULL) {
		child = fts_children(fts, 0);

		if (errno != 0) {
			perror("fts_children");
		}

		while ((child != NULL)
			&& (child->fts_link != NULL)) {
			child = child->fts_link;
			switch (child->fts_info) {
				case FTS_F:
					xmlNewTextChild(root_node, NULL, BAD_CAST "track", BAD_CAST child->fts_name);
					puts(child->fts_name);
					break;
			}
		}
	}
	fts_close(fts);

	xmlDocDumpFormatMemory(doc, &xmlbuf, NULL, 1);
	return xmlbuf;
}
@wdlkmpx
Copy link

wdlkmpx commented May 22, 2023

The way to know whether your code is broken is to link against glibc, if your code works with glibc then this implementation probably needs fixing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants