Skip to content

Instantly share code, notes, and snippets.

Created September 29, 2010 12:52

Revisions

  1. @invalid-email-address Anonymous created this gist Sep 29, 2010.
    127 changes: 127 additions & 0 deletions gistfile1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    for mod in dav_fs.conf dav_fs.load dav.load dav_lock.load; do
    (cd /etc/apache2/mods-enabled; ln -s ../mods-available/${mod})
    done
    cat<<EOF>/etc/apache2/conf.d/recollection.dav
    Alias /working/ "/software/data/factory/working/"
    <Directory "/software/data/factory/working/">
    DAV On
    PerlFixupHandler +ApachePerl::AutoMKCOL
    PerlCleanupHandler +ApachePerl::AutoDeleteEmpty
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
    EOF
    mkdir /var/www/working
    /etc/init.d/apache2 restart

    ################################################################################
    # /etc/apache2/ApachePerl/AutoMKCOL.pm
    ###########################################################
    #
    # This is a PerlFixupHandler that will take any PUT
    # requests, determine the path required, and if it doesn't
    # exist, creates the whole path. If there is a problem
    # creating the path, an error is generated.
    #
    ###########################################################
    package ApachePerl::AutoMKCOL;

    use strict;
    use warnings;

    use Apache2::RequestRec ();
    use Apache2::ServerRec ();
    use Apache2::Log ();

    use File::Path;
    use File::Basename;

    # Compile constants
    use Apache2::Const -compile => qw(DECLINED);

    sub handler {
    my $r = shift;

    # Create directories if processing a put request.
    if ($r->method() eq "PUT")
    {
    # The full file system path to the file requested is a concat of the request filename and path_info.
    my $fullpath = $r->filename() . $r->path_info();
    my $dirname = dirname($fullpath);

    # If the directory doesn't exist, create it
    if (!(-d $dirname))
    {
    $r->log->info("Creating directory structure for PUT request: '" . $dirname . "'.");
    my @dirlist = mkpath ($dirname);

    # If at least one directory wasn't created, there was a problem
    die "Failed to create directory structure: '" . $dirname . "'." unless $#dirlist > -1;
    }
    }

    # Allow next handler to run
    return Apache2::Const::DECLINED;
    }
    1;



    ################################################################################
    #/etc/apache2/ApachePerl/AutoDeleteEmpty.pm
    ###########################################################
    #
    # This is a PerlCleanupHandler that will, after any DELETE
    # requests, traverse the path for the file deleted
    # backwards and delete any empty directories
    #
    ###########################################################
    package ApachePerl::AutoDeleteEmpty;

    use strict;
    use warnings;

    use Apache2::RequestRec ();
    use Apache2::ServerRec ();
    use Apache2::Log ();

    use File::Path;
    use File::Basename;

    # Compile constants
    use Apache2::Const -compile => qw(DECLINED);

    sub handler {
    my $r = shift;

    # Create directories if processing a put request.
    if ($r->method() eq "DELETE")
    {
    # The full file system path to the file requested is a concat of the request filename and path_info.
    my $fullpath = $r->filename() . $r->path_info();
    my $dirname = dirname($fullpath);
    opendir(my $dh, $dirname) || warn "can't opendir $dirname: $!";
    my @files = grep { /^[^\.]+/ && -f "$dirname/$_" } readdir($dh);
    closedir($dh);
    if($#files < 0){ rmdir $dirname || warn "cannot remove $dirname $!!"; }
    my @dirparts=split('/',$dirname);

    while(pop(@dirparts)){
    $dirname=join('/',@dirparts);
    opendir(my $dh, $dirname) || warn "can't opendir $dirname: $!";
    my @files = grep { /^[^\.]+/ } readdir($dh);
    closedir($dh);
    if($#files < 0){
    rmdir $dirname || warn "cannot remove $dirname $!!";
    }else{
    # no point in continuing if this directory is not empty
    return Apache2::Const::DECLINED;
    }
    }
    }
    # Allow next handler to run
    return Apache2::Const::DECLINED;
    }
    1;