Wanted to see if there was any room for improvement...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Tried just having branches at the initial stage checking the arguments, then have the directories entries fetched and unlinked without any branching. But I'm wondering if fetching and then unlinking per directory entry is a bottleneck.
Would it be faster if I fetched 1024 directory entries, put them in an array and then do a for loop to unlink each?
The unlink function can only take 1 directory entry at a time.
Otherwise you might save a bit of time by using unlinkat(2) so that the kernel doesn't have reparse the path every time (the bottleneck is probably the actual deletion though, so I doubt you'll be able to observe much difference).
Quote:
Would it be faster if I fetched 1024 directory entries, put them in an array and then do a for loop to unlink each?
Pretty sure the entries are fetched in bunches already, so that would only help if you dropped down to the underlying system calls, and even then you've only saved the function call overhead.
I have been trying to rewrite the program using unlinkat but I cannot seem to set it up. The unlinkat requires a file descriptor intead of a FILE pointer. So I need to use the open system call, but then the readdir function call only uses a DIR pointer.
Actually, I guess you could get the same effect by a chdir(2) and then just use relative paths to unlink.
Quote:
The unlinkat requires a file descriptor intead of a FILE pointer. So I need to use the open system call, but then the readdir function call only uses a DIR pointer.
Probably the DIR struct has a file descriptor somewhere inside (although it's not really portable to access it). Is there an equivalent of fileno(3) for DIR*? EDIT: yes, dirfd(3).
Last edited by ntubski; 08-29-2014 at 11:47 AM.
Reason: dirfd
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.