On Wed, Dec 27, 2000 at 11:17:47PM +1300, Dan Langille wrote:
> On 27 Dec 2000, at 12:11, Peter Pentchev wrote:
> 
> > I would do that (and have done it in several projects) using opendir()
> > and readdir().  Open the directory, read entry by entry, when you find
> > a file you want, process it and unlink() it.  Get to the end of the dir,
> > sleep, repeat.
> 
> Thanks for that.
> 
> Do you have code I can use as a starting position?

Try the attached file, it works for me.

Btw, anybody reading this discussion - I tried the attached script with
#!/usr/bin/perl -wT, and Perl died on the unlink() - "unsafe dependency".
What gives?

> 
> > DJB's Maildir concept is based on having two directories - a temporary
> > one where files are created and then atomically move/rename'd to
> > the real one.  This works best when the tempdir and the real dir are
> > located on the same filesystem, and you can use the rename() syscall.
> 
> At present the files are created through procmail like this:
> 
> |/usr/bin/perl $HOME/process_cvs_mail.pl > ~/msgs/$FILE
> 
> I guess I could add a rename.

Something like..
| /usr/bin/perl $HOME/process.pl > ~/msgs/$FILE.tmp && \
  mv ~/msgs/$FILE.tmp ~/msgs/$FILE.cvs

..or alternatively, use safecat (which I shall commit real-soon-now), and..
| /usr/bin/perl process.pl | /usr/local/bin/safecat ~/msgs/tmpdir/ ~/msgs/

safecat takes two arguments - a temp dir and the real dir - reads stdin,
and stores it there.

G'luck,
Peter

-- 
What would this sentence be like if pi were 3?

#!/usr/bin/perl -w
# $Id: procdir.pl,v 1.1 2000/12/27 10:48:30 roam Exp $

use strict;

sub OnePass {
        my $dir = (shift || "");
        my ($fname, @files);

        die("OnePass() requires a dir argument\n") if ($dir eq "");
        opendir(D, $dir) or die("Opening $dir: $!\n");
        @files = readdir(D);
        closedir(D);
        foreach $fname (@files) {
                next if (($fname eq ".") || ($fname eq ".."));
                # more filename vailidity checks go here
                next unless $fname =~ /\.cvs$/;

                # ok, we want this file
                print "Processing $dir/$fname\n";

                # done with it..
                unlink("$dir/$fname") or warn("Removing $dir/$fname: $!\n");
                # this is evil - if we could process it, but could not
                # remove it, we might end up processing it again at the next
                # iteration :(
        }
}

sub ProcessDir {
        my $dir = (shift || "");

        die("ProcessDir() requires a dir argument\n") if ($dir eq "");
        for (;;) {
                OnePass($dir);
                # this could be done with select(), with a signal handler,
                # many different ways..  polling and sleep() is easy
                sleep(2);
        }
}

MAIN:{
        # obtain directory name in some way
        my $d = "/tmp";
        ProcessDir($d);
        # er heh.. this should never return :)
        die("ProcessDir() returned?.. $!\n");
}


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to