UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-01-17 09:12 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Best way to nuke a directory?

Hi all,

My program creates a cache folder in /tmp when it runs, and the cache contents are not valid outside the context of a particular instance of my program.  Therefore, when my program starts up, I'd like to have it do the equivalent of an "rm -rf" on this folder to ensure that no leftover files from a previous (killed/crashed) instance of my program are used.

One way to do that would be to simply call system("rm -rf /tmp/myprograms_cache_dir"), but that seems kind of hacky.  Is there a better way to do that?  rmdir(), unlink(), and remove() all don't seem quite brutal enough, in that they won't delete the directory if it has any files or folders in it.

Thanks,
Jeremy

Offline

#2 2008-01-17 11:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: Best way to nuke a directory?

Yeah, if you need a recursive delete, you probably have to just roll your own using
opendir()/readdir() (or scandir(), or similar), stat(), then unlink() or recurse...  Or, if
your system has ftw()/nftw(), you can use that to walk the directory tree for you, and
have it unlink() or rmdir() as appropriate (you'll need FTW_DEPTH to process dirs
after files, or make multiple runs through, until you kill all the subdirs)...

Offline

#3 2008-01-17 11:55 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,231

Re: Best way to nuke a directory?

Unfortunately there's no recursive rmdir(2). No idea why not.

If the files aren't valid after the program exits you can also unlink them immediately after creating them, and leaving the fd open. Though the question is why you're using files at all, if you don't want to store the data.

Edit: ftw() looks interesting... Rob, you should write a small list of obscure, but useful (mostly) standard functions like that. ;-)

Offline

#4 2008-01-18 12:52 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Best way to nuke a directory?

Thanks for the responses guys.... since there isn't a good drop-in replacement short of writing (and testing) a manual recursive walk, I think I'll just stick with the system() hack for now.

As for why I'm using files... the real reason is that this program is responsible for populating a directory with files that can then be read by a web server process whenever someone points their web browser at the host.  The contents of this directory need to always reflect my program's internal state.

-Jeremy

Offline

#5 2008-01-18 02:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,829
Website

Re: Best way to nuke a directory?

Personally, I'd never use system()...  If I had to, I'd use fork()+exec*() instead (without
the underlying shell that system() forces on you)...  If your app is setuid/setgid, it's
risky to use for security reasons (the shell can possibly be subverted to run arbitrary
commands via certain environment variables)...  But, even if not, I don't care for it, if
for no other reason than the added bloat of the usually unnecessary shell underneath
the real command you want to run...

But, it really doesn't sound like it should be that hard to implement via nftw()...  Just
from the man page example, I whipped this up, which seems to work perfectly:

#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int do_delete (const char *fpath, const struct stat *sb,
                        int tflag, struct FTW *ftwbuf)
{
    switch (tflag) {
        case FTW_D:
        case FTW_DNR:
        case FTW_DP:
            rmdir (fpath);
            break;
        default:
            unlink (fpath);
            break;
    }

    return (0);
}

int main (int argc, char *argv[])
{
    if (argc < 2) {
        fprintf (stderr, "Usage: %s <dir_to_delete>\n", argv[0]);
        exit (-1);
    }

    exit (nftw (argv[1], do_delete, 20, FTW_DEPTH));
}

Technically, the do_delete() could probably be simplified to just always call remove(),
since that should do rmdir() or unlink() as appropriate, anyway...  Oh, and you probably
should really have it return the return value of remove()/rmdir()/unlink() to stop nftw()
if any removal fails...  (In fact, given the API and standard C calling semantics, you could
actually get away with simply passing remove() to nftw() as the callback function, and
have things work properly, I'm sure...  The extra args will be ignored and cause no harm...
The only thing is, you'll have to listen to your compiler complain about function pointer type
mismatch, unless you do some hellishly ugly function pointer casting, which would be
worse than just writing the appropriately typed cover function... ;-))

(And, BTW i3839, I just discovered the function myself, as well!  I was looking in
the man page for readdir(), and saw in the "SEE ALSO" section it mentioned ftw(3),
so I had to take a look...  It does indeed to seem to be a very cool function...)

Offline

#6 2009-03-19 02:12 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Best way to nuke a directory?

Just for posterity's sake, the algorithm I finally came up with for this can be found as the DeleteDirectory() method in my Directory class, here:

https://public.msli.com/lcs/muscle/musc … ectory.cpp

Offline

Board footer

Powered by FluxBB