MailFolder

 view release on metacpan or  search on metacpan

Mail/Folder/Maildir.pm  view on Meta::CPAN

Calls the superclass C<append_message> method.

Writes a temporary copy of the message in C<$mref> to the
folder C<tmp> directory, then moves that temporary copy into the
folder C<cur> directory.

It will delete the C<From_> line in the header if one is present.

=cut

sub append_message {
  my $self = shift;
  my $mref = shift;

  my $folder = $self->foldername;
  my $msg_num = $self->last_message;

  my $dup_mref = $mref->dup;

  return 0 unless $self->SUPER::append_message($dup_mref);

  $msg_num++;
  $dup_mref->delete('From ');
  
  my $tmpfile = $self->_get_tmp_file()
    or croak "timed out trying to create a file in $folder/tmp";
  my $fh = new IO::File "$folder/tmp/$tmpfile", O_CREAT|O_WRONLY, 0600
    or croak "can't create $folder/tmp/$tmpfile: $!";
  $fh->autoflush(1);
  _coerce_header($dup_mref);
  $dup_mref->print($fh) or croak "failed writing $folder/tmp/$tmpfile: $!";
  fsync($fh) or croak "failed fsyncing $folder/tmp/$tmpfile: $!";
  $fh->close or croak "failed closing $folder/tmp/$tmpfile: $!";

  link("$folder/tmp/$tmpfile", "$folder/cur/$tmpfile")
    or croak "can't link $folder/tmp/$tmpfile to $folder/cur/$tmpfile for append method: $!";
  unlink("$folder/tmp/$tmpfile")
    or croak "can't unlink $folder/tmp/$tmpfile for append method: $!";

  $self->remember_message($msg_num);
  $self->cache_header($msg_num, $dup_mref->head);
  $self->{MAILDIR_MsgFiles}{$tmpfile} = $msg_num; # file to msgnum mapping
  $self->{Messages}{$msg_num}{Filename} = "cur/$tmpfile";

  return 1;
}

=head2 update_message($msg_number, $mref)

Calls the superclass C<update_message> method.

Writes a temporary copy of the message in C<$mref> to the
folder C<tmp> directory, then moves that temporary copy into the
folder C<cur> directory, replacing the message pointed to by
C<$msg_number>.

It will delete the C<From_> line in the header if one is present.

=cut

sub update_message {
  my $self = shift;
  my $key = shift;
  my $mref = shift;

  my $folder = $self->foldername;
  my $dup_mref = $mref->dup;

  return 0 unless $self->SUPER::update_message($key, $dup_mref);

  $dup_mref->delete('From ');

  my $tmpfile = $self->_get_tmp_file()
    or croak "timed out trying to create a tmpfile";
  my $fh = new IO::File $tmpfile, O_CREAT|O_WRONLY, 0600
    or croak "can't create $tmpfile: $!";
  $fh->autoflush(1);
  _coerce_header($dup_mref);
  $dup_mref->print($fh) or croak "failed writing $tmpfile: $!";
  fsync($fh) or croak "failed fsyncing $tmpfile: $!";
  $fh->close or croak "failed closing $tmpfile: $!";

  rename($tmpfile, "$folder/$self->{Messages}{$key}{Filename}") or
    croak "can't rename $tmpfile to $folder/$self->{Messages}{$key}{Filename}: $!";

  return 1;
}

=head2 is_valid_folder_format($foldername)

Returns C<1> if the folder is a directory and contains C<tmp>, C<cur>,
and C<new> subdirectories otherwise returns C<0>.

=cut

sub is_valid_folder_format {
  my $foldername = shift;

  return 0 unless (-d $foldername &&
		   -d "$foldername/tmp" &&
		   -d "$foldername/cur" &&
		   -d "$foldername/new");
  return 1;
}

=head2 create($foldername)

Creates a new folder named C<$foldername>.  Returns C<0> if the folder
already exists, otherwise returns C<1>.

=cut

sub create {
  my $self = shift;
  my $foldername = shift;

  return 0 if (-e $foldername);

  mkdir($foldername, 0700) or croak "can't create $foldername: $!";
  mkdir("$foldername/cur", 0700);
  mkdir("$foldername/new", 0700);



( run in 0.594 second using v1.01-cache-2.11-cpan-39bf76dae61 )