richard: server/perl-kolab/lib/Kolab/LDAP/Backend syncrepl.pm, 1.4, 1.5

cvs at kolab.org cvs at kolab.org
Sun Oct 19 12:25:07 CEST 2008


Author: richard

Update of /kolabrepository/server/perl-kolab/lib/Kolab/LDAP/Backend
In directory doto:/tmp/cvs-serv14248/lib/Kolab/LDAP/Backend

Modified Files:
	syncrepl.pm 
Log Message:
keep syncrepl cookie on disk between sessions (kolab/issue3152)



Index: syncrepl.pm
===================================================================
RCS file: /kolabrepository/server/perl-kolab/lib/Kolab/LDAP/Backend/syncrepl.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- syncrepl.pm	16 Oct 2008 18:58:23 -0000	1.4
+++ syncrepl.pm	19 Oct 2008 10:25:05 -0000	1.5
@@ -29,8 +29,7 @@
 use Net::LDAP::Control;
 use Net::LDAP::Control::SyncRequest;
 use Net::LDAP::Entry;
-use vars qw($ldap $cookie $disconnected);
-my $cookie = '';
+use vars qw($ldap $disconnected);
 my $disconnected = 1;
 
 require Exporter;
@@ -50,13 +49,41 @@
     
 );
 
-our $VERSION = '0.2';
+our $VERSION = '0.3';
 
 sub mode { 
   LDAP_SYNC_REFRESH_ONLY;
   #LDAP_SYNC_REFRESH_AND_PERSIST;
 }
 
+# calling without args means: get,
+# giving an argument means: set
+sub cookie {
+  my($cookie) = @_;
+  if(defined($cookie)) {
+    if(!open(COOKIE_FILE, '>', $Kolab::config{'syncrepl_cookie_file'})) {
+        Kolab::log("SYNCREPL', 'Cannot open file `".$Kolab::config{'syncrepl_cookie_file'}.
+        "' for writing", KOLAB_DEBUG);
+        &abort;
+    }
+    Kolab::log("SYNCREPL', 'Writing cookie to file: ".$cookie, KOLAB_DEBUG);
+    print COOKIE_FILE $cookie;
+    close(COOKIE_FILE);
+    return $cookie;
+  } else {
+    if(!open(COOKIE_FILE, '+<', $Kolab::config{'syncrepl_cookie_file'})) {
+        Kolab::log("SYNCREPL', 'Cannot open file `".$Kolab::config{'syncrepl_cookie_file'}.
+        "' for reading", KOLAB_DEBUG);
+        &abort;
+    }
+    read COOKIE_FILE, $cookie, 1024, 0;
+    close COOKIE_FILE;
+    #an empty file means no cookie:
+    $cookie = undef if !$cookie;
+    return $cookie;
+  }
+}
+
 sub startup { 1; }
 
 sub shutdown
@@ -107,8 +134,8 @@
 
     while($ldap and not $disconnected) {
       my $ctrl = Net::LDAP::Control::SyncRequest->new(
-        mode       => Kolab::LDAP::Backend::syncrepl::mode,
-        cookie     => $cookie,
+        mode       => Kolab::LDAP::Backend::syncrepl::mode(),
+        cookie     => Kolab::LDAP::Backend::syncrepl::cookie(),
         reloadHint => 0);
       Kolab::log('SYNCREPL', 'Control created: mode='.$ctrl->mode().
       	'; cookie='.$ctrl->cookie().
@@ -160,8 +187,7 @@
       return;
     }
     if(!$sync_controls[0]->entryUUID) {
-      Kolab::log('SYNCREPL', 'Got empty entryUUID',
-        KOLAB_DEBUG);
+      Kolab::log('SYNCREPL', 'Got empty entryUUID', KOLAB_DEBUG);
       return;
     }
     Kolab::log('SYNCREPL', 'Search Entry has Sync State Control: '.
@@ -170,8 +196,9 @@
       '; cookie='.(defined($sync_controls[0]->cookie()) ? $sync_controls[0]->cookie() : 'UNDEF')
 	, KOLAB_DEBUG);
     if(defined($sync_controls[0]->cookie)) {
-      $cookie = $sync_controls[0]->cookie;
-      Kolab::log('SYNCREPL',"New cookie: $cookie", KOLAB_DEBUG);
+      Kolab::LDAP::Backend::syncrepl::cookie($sync_controls[0]->cookie);
+      Kolab::log('SYNCREPL',"New cookie: ".Kolab::LDAP::Backend::syncrepl::cookie(),
+        KOLAB_DEBUG);
     }
     Kolab::log('SYNCREPL', "Entry (".$param2->changetype."): ".$param2->dn(), KOLAB_DEBUG);
   } elsif($param2 && $param2->isa("Net::LDAP::Reference")) {
@@ -180,16 +207,19 @@
   #if it not first control?
   } elsif($controls[0] and $controls[0]->isa('Net::LDAP::Control::SyncDone')) {
     Kolab::log('SYNCREPL', 'Received Sync Done Control: '.
-      	'cookie='.(defined($controls[0]->cookie()) ? $controls[0]->cookie() : 'UNDEF').
-      	'; refreshDeletes='.$controls[0]->refreshDeletes()
-	, KOLAB_DEBUG);
-	  #we have a new cookie
-	  if(defined($controls[0]->cookie()) and not $controls[0]->cookie() eq '' and not $controls[0]->cookie() eq $cookie) {
-		  $cookie = $controls[0]->cookie();
-		  Kolab::log('SYNCREPL', "New cookie: $cookie", KOLAB_DEBUG);
+      'cookie='.(defined($controls[0]->cookie()) ? $controls[0]->cookie() : 'UNDEF').
+      '; refreshDeletes='.$controls[0]->refreshDeletes(), KOLAB_DEBUG);
+    #we have a new cookie
+    if(defined($controls[0]->cookie())
+        and not $controls[0]->cookie() eq '' 
+        and not $controls[0]->cookie() eq Kolab::LDAP::Backend::syncrepl::cookie()) {
+      Kolab::LDAP::Backend::syncrepl::cookie($controls[0]->cookie());
+      Kolab::log('SYNCREPL', "New cookie: ".
+        Kolab::LDAP::Backend::syncrepl::cookie(), KOLAB_DEBUG);
       Kolab::log('SYNCREPL', "Calling Kolab::LDAP::sync", KOLAB_DEBUG);
       Kolab::LDAP::sync;
-      system($Kolab::config{'kolabconf_script'}) == 0 || Kolab::log('SD', "Failed to run kolabconf: $?", KOLAB_ERROR);
+      system($Kolab::config{'kolabconf_script'}) == 0
+        || Kolab::log('SD', "Failed to run kolabconf: $?", KOLAB_ERROR);
       Kolab::log('SYNCREPL', "Finished Kolab::LDAP::sync sleeping 1s", KOLAB_DEBUG);
       sleep 1; # we get too many bogus change notifications!
 	  } 
@@ -197,21 +227,26 @@
     Kolab::log('SYNCREPL', 'Received Intermediate Message', KOLAB_DEBUG);
     my $attrs = $param2->{asn};
     if($attrs->{newcookie}) {
-      $cookie = $attrs->{newcookie};
-      Kolab::log('SYNCREPL', "New cookie: $cookie", KOLAB_DEBUG);
+      Kolab::LDAP::Backend::syncrepl::cookie($attrs->{newcookie});
+      Kolab::log('SYNCREPL', "New cookie: ".
+        Kolab::LDAP::Backend::syncrepl::cookie(), KOLAB_DEBUG);
     } elsif(my $refreshInfos = ($attrs->{refreshDelete} || $attrs->{refreshPresent})) {
-      $cookie = $refreshInfos->{cookie} if defined($refreshInfos->{cookie});
+      Kolab::LDAP::Backend::syncrepl::cookie($refreshInfos->{cookie})
+        if defined($refreshInfos->{cookie});
       Kolab::log('SYNCREPL', 
         (defined($refreshInfos->{cookie}) ? 'New ' : 'Empty ').
         "cookie from ".
         ($attrs->{refreshDelete} ? 'refreshDelete' : 'refreshPresent').
-        " (refreshDone=".$refreshInfos->{refreshDone}."): $cookie", KOLAB_DEBUG);
+        " (refreshDone=".$refreshInfos->{refreshDone}."): ".
+        Kolab::LDAP::Backend::syncrepl::cookie(), KOLAB_DEBUG);
     } elsif(my $syncIdSetInfos = $attrs->{syncIdSet}) {
-      $cookie = $syncIdSetInfos->{cookie} if defined($syncIdSetInfos->{cookie});
+      Kolab::LDAP::Backend::syncrepl::cookie($syncIdSetInfos->{cookie})
+        if defined($syncIdSetInfos->{cookie});
       Kolab::log('SYNCREPL', 
         (defined($syncIdSetInfos->{cookie}) ? 'Empty ' : 'New ').
         "cookie from syncIdSet".
-        " (refreshDeletes=".$syncIdSetInfos->{refreshDeletes}."): $cookie", KOLAB_DEBUG);
+        " (refreshDeletes=".$syncIdSetInfos->{refreshDeletes}."): ".
+        Kolab::LDAP::Backend::syncrepl::cookie(), KOLAB_DEBUG);
       foreach my $syncUUID ($syncIdSetInfos->{syncUUIDs}) {
         Kolab::log('SYNCREPL', 'entryUUID='.
           unpack("H*",$syncUUID), KOLAB_DEBUG);
@@ -227,7 +262,7 @@
         return;
     } elsif ($mesg->code == 4096) {
         Kolab::log('SYNCREPL', 'Refresh required', KOLAB_DEBUG);
-        $cookie = '';
+        Kolab::LDAP::Backend::syncrepl::cookie('');
     } else {
         Kolab::log('SYNCREPL', "searchCallback: mesg->code = `" . $mesg->code . "', mesg->msg = `" . $mesg->error . "'", KOLAB_DEBUG);
         &abort;
@@ -286,4 +321,4 @@
 
 
 
-=cut
\ No newline at end of file
+=cut





More information about the commits mailing list