LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2014, 12:50 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
Wanted to see if there was any room for improvement...


This is a program in C to remove files in a directory in Linux. Many times you get the "Too many arguments" error when even using

find . -exec rm -rf {} \;
or
ls -U1 | xargs -0 rm -rf

So I wrote a simple C program but I wanted to see if I could do anything to make it faster other than compiling with "-Ox" option.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <dirent.h>

#define BUFSIZE 128

void usage(int *argc, char **argv) {
        fprintf(stderr, "\"Argument list too long\" Incinerator\n");
        fprintf(stderr, "\n*** Run binary outside the target directory ***\n");
        fprintf(stderr, "\nUsage: %s <dir>\n", argv[0]);
        exit(1);
}

int main(int argc, char **argv) {

    DIR *dirp;
    struct dirent *dp;
        char dir[BUFSIZE];

        if((argv[1] == NULL) || (argc > 2) || ((strcmp(argv[1]), ".") == 0))
                usage(&argc, argv);

        dirp = opendir(argv[1]);
        while ((dp = readdir(dirp)) != NULL) {
                snprintf(dir, BUFSIZE, "%s/%s", argv[1], dp->d_name);
                fprintf(stderr, "%s\n", dir);
                unlink(dir);
        }

        return 0;
}
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.

-Tristan
 
Old 08-28-2014, 02:16 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't see why cannot do this from a shell, eg:
Code:
find . -maxdepth 1 -print0 | xargs -0 rm -rf
But, if you want to improve your program, here's a suggestion: allow multiple command-line parameters:

Code:
static void ProcessOneArg (const char *dirname)
{
    DIRP *dirp;
    ...
}

...
int main (int argc, char **argv)
{
    int i;

    if (argc==1) {
        usage();
    } else {
        for (i=1; i<argc; ++i) {
            ProcessOneArg (argv[i]);
        }
    }
    return 0;
}
 
Old 08-28-2014, 02:28 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
What about
Code:
find . -delete
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.
 
1 members found this post helpful.
Old 08-29-2014, 10:21 AM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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.

-Tristan
 
Old 08-29-2014, 11:45 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
web server improvement byran cheung Linux - General 3 11-20-2013 05:35 AM
hi libnetfilter_queue performance improvement kikilinux Programming 1 07-25-2013 05:23 PM
Suggestions for improvement jknwhz Linux - Newbie 1 04-09-2013 08:06 AM
KDE 4.4.3 is a huge improvement. akschu Slackware 3 05-26-2010 01:05 PM
Password checking has room for improvement kenhtanaka Linux - Security 7 03-01-2008 12:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration