Acme::Archive::Mbox - Mbox as an archive format.


Acme-Archive-Mbox documentation Contained in the Acme-Archive-Mbox distribution.

Index


Code Index:

NAME

Top

Acme::Archive::Mbox - Mbox as an archive format.

VERSION

Top

Version 0.01

SYNOPSIS

Top

Uses Mbox as an archive format, like tar or zip but silly. Creates an mbox with one message per file or directory. File contents are stored as an attachment, metadata goes in mail headers.

    use Acme::Archive::Mbox;

    my $archive = Acme::Archive::Mbox->new();
    $archive->add_file('filename');
    $archive->add_data('file/name', $contents);
    $archive->write('foo.mbox');

    ...

    $archive->read('foo.mbox');
    $archive->extract();

FUNCTIONS

Top

new ()

Create an Acme::Archive::Mbox object.

add_data ($name, $contents, %attr)

Add a file given a filename and contents. (File need not exist on disk)

add_file ($name, [$archive_name])

Add a file given a filename. File will be read from disk, leading slashes will be stripped. Will accept an optional alternative filename to be used in the archive.

get_files ()

Returns a list of AAM::File objects.

write (filename)

Write archive to a file

read (filename)

Read archive from a file.

AUTHOR

Top

Ian Kilgore, <iank at cpan.org>

BUGS

Top

Undefined behavior in spades. Anyone using this probably deserves it.
Fails to overwrite or truncate when creating archives
As Acme::Archive::Mbox does not store directories, directory mode and ownership will not be preserved.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Acme::Archive::Mbox




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-Archive-Mbox

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Acme-Archive-Mbox

* CPAN Ratings

http://cpanratings.perl.org/d/Acme-Archive-Mbox

* Search CPAN

http://search.cpan.org/dist/Acme-Archive-Mbox/

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Acme-Archive-Mbox documentation Contained in the Acme-Archive-Mbox distribution.
package Acme::Archive::Mbox;

use warnings;
use strict;

use Acme::Archive::Mbox::File;
use File::Slurp;
use Mail::Box::Manager;

our $VERSION = '0.01';


sub new {
    my $class = shift;
    my $self = { files => [] };
    return bless $self,$class;
}

sub add_data {
    my $self = shift;
    my $name = shift;
    my $contents = shift;
    my %attr = @_;

    my $file = Acme::Archive::Mbox::File->new($name, $contents, %attr);
    push @{$self->{files}}, $file if $file;

    return $file;
}

sub add_file {
    my $self = shift;
    my $name = shift;
    my $altname = shift || $name;
    my %attr;

    my $contents = read_file($name, err_mode => 'carp', binmode => ':raw');
    return unless $contents;

    my (undef, undef, $mode, undef, $uid, $gid, undef, undef, undef, $mtime) = stat $name;
    $attr{mode} = $mode & 0777;
    $attr{uid} = $uid;
    $attr{gid} = $gid;
    $attr{mtime} = $mtime;

    my $file = Acme::Archive::Mbox::File->new($altname, $contents, %attr);
    push @{$self->{files}}, $file if $file;

    return $file;
}

sub get_files {
    my $self = shift;
    return @{$self->{files}};
}

sub write {
    my $self = shift;
    my $mboxname = shift;
    
    my $mgr = Mail::Box::Manager->new;
    my $folder = $mgr->open($mboxname, type => 'mbox', create => 1, access => 'rw') or die "Could not create $mboxname";

    for my $file (@{$self->{files}}) {
        my $attach = Mail::Message::Body->new(  mime_type => 'application/octet-stream',
                                                data => $file->contents,
                                             );

        my $message = Mail::Message->build( From          => '"Acme::Archive::Mbox" <AAM@example.com>',
                                            To            => '"Anyone, really" <anyone@example.com>',
                                            Subject       => $file->name,
                                            'X-AAM-uid'   => $file->uid,
                                            'X-AAM-gid'   => $file->gid,
                                            'X-AAM-mode'  => $file->mode,
                                            'X-AAM-mtime' => $file->mtime,

                                            data => 'attached',
                                            attach => $attach, );
        $folder->addMessage($message);
    }
    $folder->write();
    $mgr->close($folder);
}

sub read {
    my $self = shift;
    my $mboxname = shift;

    my $mgr = Mail::Box::Manager->new;
    my $folder = $mgr->open($mboxname, type => 'mbox') or die "Could not open $mboxname";
    my @messages = $folder->messages;
    for my $message (@messages) {
        my %attr;
        my $name = $message->get('Subject');
        for (qw/uid gid mode mtime/) {
            $attr{$_} = $message->get("X-AAM-$_");
        }
        my $contents = ($message->parts())[1]->decoded();

        $self->add_data($name, $contents, %attr);
    }
    $mgr->close($folder);
}

1; # End of Acme::Archive::Mbox