Changeset 500


Ignore:
Timestamp:
May 3, 2010, 9:29:29 PM (10 years ago)
Author:
zecrazytux
Message:

Added a lock and enhanced mailmap writing mechanism

Location:
bearmail/lib/BearMail
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • bearmail/lib/BearMail/Backend/Files.pm

    r498 r500  
    1919use Carp;
    2020use Exporter 'import';
     21use Fcntl ':flock';
    2122@EXPORT_OK = qw(new commit apply get_domains get_users get_user set_domain set_address add_domain add_address del_domain del_address get_postmasters get_postmaster_domains);
    2223
     
    4142my $version = "0.3";
    4243my %records;
     44my %new_records;
     45my @records_to_delete;
    4346my %by_domain;
    4447my @domains;
     
    110113sub add_domain() {
    111114  my ($class, $domain, $postmaster, $password) = @_;
     115
     116  if(exists($by_domain{$domain})) {
     117    carp "Domain $domain already exist !";
     118    return 0;
     119  }
     120
    112121  if($postmaster eq "postmaster@".$domain) {
    113122    add_address($class, $postmaster, $password, "local");
     
    131140    return 0;
    132141  } else {
    133     $records{"$address"} = {
     142    $new_records{"$address"} = {
    134143              address => $address,
    135144              password => $password,
     
    142151  my ($class, $domain) = @_;
    143152  if(scalar(@{$by_domain{$domain}}) le 1) {
    144     delete($records{"postmaster\@$domain"});
     153    push(@records_to_delete, "postmaster\@$domain");
    145154  } else {
    146155    carp "There are remaining email addresses for this domain !\n";
     
    151160sub del_address() {
    152161  my ($class, $address) = @_;
    153   delete($records{"$address"});
     162  push(@records_to_delete, $address);
    154163}
    155164
     
    192201
    193202  open(MAILMAP, "<$mailmap") or croak "$mailmap: $!";
     203  flock(MAILMAP, LOCK_NB);
     204
    194205
    195206  while(<MAILMAP>) {
     
    214225  }
    215226
    216   close MAILMAP;
     227  close(MAILMAP);
     228  flock(MAILMAP, LOCK_UN);
    217229  _sort_mailmap();
    218230}
    219231
    220 sub _write_mailmap {   #FIXME: Add a lock !
     232sub _write_mailmap {
    221233  return if !%records;
    222234
    223235  open(MAILMAP, ">$mailmap") or croak "$mailmap: $!";
     236  flock(MAILMAP, LOCK_EX + LOCK_NB)
     237    or croak "Can't get exclusive lock on $mailmap: $!";
     238
     239  _read_mailmap();
     240  %records = (%records, %new_records);
     241  delete($records{$_}) foreach(@records_to_delete);
     242  _sort_mailmap();
    224243
    225244  foreach(keys %records) {
     
    228247  }
    229248
    230   close MAILMAP;
     249  flock(MAILMAP, LOCK_UN);
     250  close(MAILMAP);
    231251}
    232252
  • bearmail/lib/BearMail/Dispatch.pm

    r497 r500  
    3535        }
    3636    },
    37     debug => 1,
     37    #debug => 1,
    3838    table       => [
    3939            ':app'     => {},
  • bearmail/lib/BearMail/Web.pm

    r354 r500  
    2424use BearMail::Backend;
    2525use Data::Dumper;
    26 
    27 
    2826
    2927sub setup {
  • bearmail/lib/BearMail/Web/Address/Add.pm

    r453 r500  
    3737    }
    3838
    39     if(grep(/$domain/, $backend->get_domains())) {
     39    if(grep(/^$domain$/, $backend->get_domains())) {
    4040      $backend->add_address($email, $password, $target)
    4141        or error($self, "BAD_CONFIGURATION");
  • bearmail/lib/BearMail/Web/Login.pm

    r432 r500  
    7171    $self->session->param('user', $user);
    7272    $self->session->param('level', $level);
     73    $self->session->flush();
    7374
    7475    # Redirect to the original page the user intended to go, or some fitting
Note: See TracChangeset for help on using the changeset viewer.