4 ms·
How would FindFile() work then? As far as I can see, either you have some separate call to initialize the find handle (just like FindFirstFile does), or you pa
by magicalhippo 8y ago
How would FindFile() work then?
As far as I can see, either you have some separate call to initialize the find handle (just like FindFirstFile does), or you pass the directory/filename mask redundantly over and over again, introducing the potential for suddenly calling it with an already initialized find handle but with a different directory.
- blattimwind 8y agoThe classic UNIX approach would of course be making FindFile non-reentrant and not thread-safe either, then later adding a FindFile_r which takes an void* * where FindFile_r will store some intermediate state that you need to free yourself except when an error occured, because when you free it then you get a double free. (However, not all UNIX systems have FindFile_r, and some of those instead have FindFile2 with slightly different semantics).
- jstimpfle 8y agoYou are snarky, but I don't think it's warranted. Any examples of non-reentrant or thread-unsafe system calls in POSIX? There are a few library calls that have a cousin with an _r suffix in POSIX, but these are not system interfacing calls. Just don't use them if they don't match your requirements. Most of them aren't even non-reentrant or thread-unsafe, but simply lack an additional parameter for some use cases (the use cases mostly come when you program in object-oriented style). The one offender (not a system call, either) that I know from the top of my head is strtok() which is of course terrible, but why would you use it?
- Someone 8y ago”There are a few library calls that have a cousin with an _r suffix in POSIX, but these are not system interfacing calls.” Neither are most Windows functions. FindFirstFile, for example, is defined in kernel32.dll, and calls through to ntdll.dll, and eventually makes system calls such as NtOpenDirectoryObject and NtQueryDirectoryObject. What IMO matters is the usability of the stable API. It doesn’t matter whether that is the kernel interface or not.
- jstimpfle 8y agoYes. The distinction I made was meant to be between calls that end up calling into the system and ones that do not (like strtok()). I agree with what you say and my statement was exactly that the stable APIs into the system offered by POSX are mostly unproblematic.
- manwe150 8y agoProbably true, but there is in fact the apropos readdir function, which does have a readdir_r variant to address thread safety issues (now apparently deprecated due to other unrelated design issues). So it seems the joke’s on us. Getting API design right is _hard_.
- deleted 8y ago[deleted]
- humblebee 8y agoI don't know, I was just asking for clarification to the question, because you did not provide any information on the distinction of the two methods in the question "why do I need to find the first file with ...". Your reply here could of been a great reply to the original question.
- magicalhippo 8y agoI misunderstood the poster I was replying to. To me it sounded like he wanted a singular call to be able to iterate over all the files in the directory. I then assumed you proposed something similar with your FindFile call, and I didn't and still don't see how to do that in a clean way, hence asking how you would imagine it working. In a reply the OP clarified that it was the fact that FindFirstFile does two things (initializing the handle, and getting the first result if any), rather than cleanly separating the handle initialization from getting the results. This is different, and a point I can agree with.
- spc476 8y agoIn POSIX, there are two sets of functions related to directories. The first set, `opendir()` (open a directory for reading), `readdir()` (return next filename upon each call) and `closedir()` (close the directory). `readdir()` does not guarantee an ordering of filenames and any filtering must be done by the application. Then there's `wordexp()` and `wordfree()`. The first takes a file glob (file pattern?) and returns an array of filenames matching the pattern. Any memory allocated by `wordexp()` is freed by calling `wordfree()`. Both have their uses.