[LON-CAPA-cvs] cvs: loncom / lond

www lon-capa-cvs@mail.lon-capa.org
Thu, 18 May 2006 17:55:50 -0000


www		Thu May 18 13:55:50 2006 EDT

  Modified files:              
    /loncom	lond 
  Log:
  Again, saving my work on zipping.
  
  Do a shared lock before even testing for the existence of a .gz file
  
  
Index: loncom/lond
diff -u loncom/lond:1.328 loncom/lond:1.329
--- loncom/lond:1.328	Wed May 17 22:32:06 2006
+++ loncom/lond	Thu May 18 13:55:49 2006
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.328 2006/05/18 02:32:06 albertel Exp $
+# $Id: lond,v 1.329 2006/05/18 17:55:49 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -60,7 +60,7 @@
 my $lastlog='';
 my $lond_max_wait_time = 13;
 
-my $VERSION='$Revision: 1.328 $'; #' stupid emacs
+my $VERSION='$Revision: 1.329 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1063,34 +1063,22 @@
                }
 # Apparently just no lock file yet. Make one
                open($sym,">>$file_prefix.db.lock");
-           } 
+           }
+# Do a shared lock
+           if (!&flock_sym(LOCK_SH)) { return undef; } 
+# If this is compressed, we will actually need an exclusive lock
+	   if (-e "$file_prefix.db.gz") {
+	       if (!&flock_sym(LOCK_EX)) { return undef; }
+	   }
         } elsif ($how eq &GDBM_WRCREAT()) {
 # We are writing
            open($sym,">>$file_prefix.db.lock");
 # Writing needs exclusive lock
-           $lock_type=LOCK_EX;
+           if (!&flock_sym(LOCK_EX)) { return undef; }
         } else {
            &logthis("Unknown method $how for $file_prefix");
            die();
         }
-# If this is compressed, we will also need an exclusive lock
-       if (-e "$file_prefix.db.gz") { $lock_type=LOCK_EX; }
-# Okay, try to obtain the lock we want
-       my $failed=0;
-       eval {
-           local $SIG{__DIE__}='DEFAULT';
-           local $SIG{ALRM}=sub {
-               $failed=1;
-               die("failed lock");
-           };
-           alarm($lond_max_wait_time);
-           flock($sym,$lock_type);
-           alarm(0);
-       };
-       if ($failed) {
-           $! = 100; # throwing error # 100
-           return undef;
-       }
 # The file is ours!
 # If it is archived, un-archive it now
        if (-e "$file_prefix.db.gz") {
@@ -1105,6 +1093,27 @@
        return &_do_hash_tie($file_prefix,$namespace,$how,$loghead,$what);
     }
 
+    sub flock_sym {
+        my ($lock_type)=@_;
+	my $failed=0;
+	eval {
+	    local $SIG{__DIE__}='DEFAULT';
+	    local $SIG{ALRM}=sub {
+		$failed=1;
+		die("failed lock");
+	    };
+	    alarm($lond_max_wait_time);
+	    flock($sym,$lock_type);
+	    alarm(0);
+	};
+	if ($failed) {
+	    $! = 100; # throwing error # 100
+	    return undef;
+	} else {
+	    return 1;
+	}
+    }
+
     sub _locking_hash_untie {
 	my ($hashref) = @_;
 	my $result = untie(%$hashref);