[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / ecrire / inc / nfslock.php
index 0b6de6a..0389d24 100644 (file)
@@ -1,11 +1,21 @@
 <?php
-/* 
- *
+
+/**
+ * Gestion des verrous NFS
  *
- * function: nfslock
- * original author: Chuck Cranor <chuck@maria.wustl.edu>
- * rewritten by: Alexis Rosen <alexis@panix.com>
- * rewritten by: Cedric Morin <cedric@yterium.com> for php&SPIP
+ * @package SPIP\Core\NFS
+ **/
+
+if (!defined('_ECRIRE_INC_VERSION')) {
+       return;
+}
+
+include_spip('inc/acces');
+define('_DEFAULT_LOCKTIME', 60);
+define('_NAME_LOCK', 'spip_nfs_lock');
+
+/**
+ * Crée un verrou pour NFS
  *
  * (Excerpts from Chuck's notes:
  *   this becomes complex, due to our dear friend, the NFS mounted mail spool.
@@ -43,9 +53,9 @@
  *
  * args: path = path to directory of lock file (/net/u/1/a/alexis/.mailspool)
  *       namelock = file name of lock file (alexis.lock)
- *      max_age = age of lockfile, in seconds, after which the lock is stale.
- *             stale locks are always broken. Defaults to DEFAULT_LOCKTIME
- *             if zero. Panix mail locks go stale at 300 seconds, the default.
+ *   max_age = age of lockfile, in seconds, after which the lock is stale.
+ *    stale locks are always broken. Defaults to DEFAULT_LOCKTIME
+ *    if zero. Panix mail locks go stale at 300 seconds, the default.
  *       notify = 1 if we should tell stdout that we're sleeping on a lock
  *
  * Returns the time that the lock was created on the other system. This is
  *
  * Change comments drastically.
  *
+ * @author Chuck Cranor <chuck@maria.wustl.edu> (original author)
+ * @author Alexis Rosen <alexis@panix.com> (rewritter)
+ * @author Cedric Morin <cedric@yterium.com> (rewritter for php&SPIP)
+ *
+ * @param string $fichier Chemin du fichier
+ * @param int $max_age Age maximum du verrou
+ * @return int|bool Timestamp du verrou, false si erreur
  */
+function spip_nfslock($fichier, $max_age = 0) {
+       $tries = 0;
 
-if (!defined('_ECRIRE_INC_VERSION')) return;
-
-include_spip('inc/acces');
-define('_DEFAULT_LOCKTIME',60);
-define('_NAME_LOCK','spip_nfs_lock');
+       if (!$max_age) {
+               $max_age = _DEFAULT_LOCKTIME;
+       }
+       $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8);
 
-// http://doc.spip.org/@spip_nfslock
-function spip_nfslock($fichier,$max_age=0) {
-       $tries = 0;
-               
-       if (!$max_age) $max_age = _DEFAULT_LOCKTIME;
-       $lock_file = _DIR_TMP . _NAME_LOCK . "-" . substr(md5($fichier),0,8);
 
-  
        /*
         * 1. create a tmp file with a psuedo random file name. we also make
         *    tpath which is a buffer to store the full pathname of the tmp file.
         */
 
        $id = creer_uniqid();
-       $tpath = _DIR_TMP."slock.$id";
+       $tpath = _DIR_TMP . "slock.$id";
        $tmpfd = @fopen($tpath, 'w'); // hum, le 'x' necessite php4,3,2 ...
-       if (!$tmpfd) {  /* open failed */
+       if (!$tmpfd) {  /* open failed */
                @fclose($tmpfd);
                spip_unlink($tpath);
+
                return false; //NFSL_SYSF
        }
-  
+
        /*
         * 2. make fullpath, a buffer for the full pathname of the lock file.
         *    then start looping trying to lock it
@@ -126,11 +138,13 @@ function spip_nfslock($fichier,$max_age=0) {
                if (link($tpath, $lock_file) == 1) {
                        spip_unlink($tpath); /* got it! */
                        @fclose($tmpfd);
-                       if (($our_tmp = lstat($lock_file))==false) {    /* stat failed... shouldn't happen */
+                       if (($our_tmp = lstat($lock_file)) == false) {  /* stat failed... shouldn't happen */
                                spip_unlink($lock_file);
+
                                return false; // (NFSL_SYSF);
                        }
-                       return($our_tmp['ctime']);
+
+                       return ($our_tmp['ctime']);
                }
 
                /*
@@ -141,11 +155,12 @@ function spip_nfslock($fichier,$max_age=0) {
                 */
 
                $old_stat = lstat($lock_file);
-               if (@fputs($tmpfd, "zz", 2)!=2 || !$our_tmp=fstat($tmpfd))
-                       break; /* something bogus is going on */
+               if (@fputs($tmpfd, 'zz', 2) != 2 || !$our_tmp = fstat($tmpfd)) {
+                       break;
+               } /* something bogus is going on */
 
 
-               if ($old_stat!=false && (($old_stat['ctime'] + $max_age) < $our_tmp['ctime'])) {
+               if ($old_stat != false && (($old_stat['ctime'] + $max_age) < $our_tmp['ctime'])) {
                        spip_unlink($lock_file); /* break the stale lock */
                        $tries++;
                        /* It is CRITICAL that we sleep after breaking
@@ -153,7 +168,7 @@ function spip_nfslock($fichier,$max_age=0) {
                         * another process and unlink it's newly-
                         * created file.
                         */
-                       sleep(1+rand(0,4));
+                       sleep(1 + rand(0, 4));
                        continue;
                }
 
@@ -161,9 +176,9 @@ function spip_nfslock($fichier,$max_age=0) {
                 * 5. try again
                 */
 
-    $tries++;
-               sleep(1+rand(0,4));
-  }
+               $tries++;
+               sleep(1 + rand(0, 4));
+       }
 
        /*
         * 6. give up, failure.
@@ -171,14 +186,12 @@ function spip_nfslock($fichier,$max_age=0) {
 
        spip_unlink($tpath);
        @fclose($tmpfd);
+
        return false; //(NFSL_LOCKED);
 }
 
-/*
- * function: nfsunlock
- * author: Alexis Rosen <alexis@panix.com>
- *
- * Unlock an nfslock()ed file.
+/**
+ * Unlock an nfslock()ed file
  *
  * This can get tricky because the lock may have expired (perhaps even
  * during a process that should be "atomic"). We have to make sure we don't
@@ -189,9 +202,9 @@ function spip_nfslock($fichier,$max_age=0) {
  * args: path = path to directory of lock file (/net/u/1/a/alexis/.mailspool)
  *       namelock = file name of lock file (alexis.lock)
  *       max_age = age of lockfile, in seconds, after which the lock is stale.
- *             stale locks are always broken. Defaults to DEFAULT_LOCKTIME
- *             if zero. Panix mail locks go stale at 300 seconds, the default.
- *      birth = time the lock was created (as returned by nfslock()).
+ *    stale locks are always broken. Defaults to DEFAULT_LOCKTIME
+ *    if zero. Panix mail locks go stale at 300 seconds, the default.
+ *   birth = time the lock was created (as returned by nfslock()).
  *
  * Returns NFSL_OK if successful, NFSL_LOST if the lock has been lost
  * legitimately (because more than max_age has passed since the lock was
@@ -208,37 +221,47 @@ function spip_nfslock($fichier,$max_age=0) {
  * In practice, a return code of NFSL_LOST or NFSL_STOLEN will virtually never
  * happen unless someone is violating the locking protocol.
  *
+ * @author Alexis Rosen <alexis@panix.com>
+ * @see spip_nfslock()
+ *
+ * @param string $fichier Chemin du fichier
+ * @param bool $birth Timestamp de l'heure de création du verrou
+ * @param int $max_age Age maximum du verrou
+ * @param bool $test Mode de test
+ * return bool true si déverrouillé, false sinon
  */
-
-// http://doc.spip.org/@spip_nfsunlock
-function spip_nfsunlock($fichier, $birth, $max_age=0, $test = false) {
+function spip_nfsunlock($fichier, $birth, $max_age = 0, $test = false) {
        $id = creer_uniqid();
-       if (!$max_age) $max_age = _DEFAULT_LOCKTIME;
+       if (!$max_age) {
+               $max_age = _DEFAULT_LOCKTIME;
+       }
 
        /*
         * 1. Build a temp file and stat that to get an idea of what the server
         *    thinks the current time is (our_tmp.st_ctime)..
         */
 
-       $tpath = _DIR_TMP."stime.$id";
-  $tmpfd = @fopen($tpath,'w');
-  if ((!$tmpfd)
-    OR (@fputs($tmpfd, "zz", 2) != 2)
-               OR !($our_tmp = fstat($tmpfd))) {
+       $tpath = _DIR_TMP . "stime.$id";
+       $tmpfd = @fopen($tpath, 'w');
+       if ((!$tmpfd)
+               or (@fputs($tmpfd, 'zz', 2) != 2)
+               or !($our_tmp = fstat($tmpfd))
+       ) {
                /* The open failed, or we can't write the file, or we can't stat it */
                @fclose($tmpfd);
                spip_unlink($tpath);
+
                return false; //(NFSL_SYSF);
        }
 
-       @fclose($tmpfd);                /* We don't need this once we have our_tmp.st_ctime. */
+       @fclose($tmpfd);    /* We don't need this once we have our_tmp.st_ctime. */
        spip_unlink($tpath);
 
        /*
         * 2. make fullpath, a buffer for the full pathname of the lock file
         */
 
-       $lock_file = _DIR_TMP . _NAME_LOCK . "-" . substr(md5($fichier),0,8);
+       $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8);
 
        /*
         * 3. If the ctime hasn't been modified, unlink the file and return. If the
@@ -247,16 +270,21 @@ function spip_nfsunlock($fichier, $birth, $max_age=0, $test = false) {
         *    tries to relock the file.
         */
 
-  if ( ($old_stat=@lstat($lock_file))  /* stat succeeds so file is there */
-         && ($old_stat['ctime'] == $birth)) {  /* hasn't been modified since birth */
-         if (!$test)
-       spip_unlink($lock_file);                        /* so the lock is ours to remove */
-    if ($our_tmp['ctime'] >= $birth + $max_age){       /* the lock has expired */
-       if (!$test) return false; //(NFSL_LOST);
-      sleep(1+(random(0,4)));          /* so sleep a bit */
-    }
+       if (($old_stat = @lstat($lock_file))  /* stat succeeds so file is there */
+               && ($old_stat['ctime'] == $birth)
+       ) {  /* hasn't been modified since birth */
+               if (!$test) {
+                       spip_unlink($lock_file);
+               }      /* so the lock is ours to remove */
+               if ($our_tmp['ctime'] >= $birth + $max_age) {  /* the lock has expired */
+                       if (!$test) {
+                               return false;
+                       } //(NFSL_LOST);
+                       sleep(1 + (random(0, 4)));    /* so sleep a bit */
+               }
+
                return true;//(NFSL_OK);                        /* success */
-  }
+       }
 
        /*
         * 4. Either ctime has been modified, or the entire lock file is missing.
@@ -265,21 +293,17 @@ function spip_nfsunlock($fichier, $birth, $max_age=0, $test = false) {
         *    someone else has grabbed the file, so return NFSL_LOST.
         */
 
-       if ($our_tmp['ctime'] < $birth + $max_age)      /* lock was stolen */
-               return false; //(NFSL_STOLEN);
+       if ($our_tmp['ctime'] < $birth + $max_age) { /* lock was stolen */
+               return false;
+       } //(NFSL_STOLEN);
 
        return false; //(NFSL_LOST);    /* The lock must have expired first. */
 }
 
 
-/*
- * function: nfslock_test
- * author: Alexis Rosen <alexis@panix.com>
- *
+/**
  * Test a lock to see if it's still valid.
  *
- * See the comments in nfsunlock() about lost and stolen locks.
- *
  * Args, return codes, and behavior are identical to nfsunlock except
  * that nfslock_test doesn't remove the lock. NFSL_OK means the lock is
  * good, NFLS_LOST and NFSL_STOLEN means it's bad, and NFSL_SYSF means
@@ -287,11 +311,15 @@ function spip_nfsunlock($fichier, $birth, $max_age=0, $test = false) {
  *
  * The source for this routine is almost identical to nfsunlock(), but it's
  * coded separately to make things as clear as possible.
+ *
+ * @author Alexis Rosen <alexis@panix.com>
+ * @see spip_nfsunlock() about lost and stolen locks.
+ *
+ * @param string $fichier Chemin du fichier
+ * @param bool $birth Timestamp de l'heure de création du verrou
+ * @param int $max_age Age maximum du verrou
+ * return bool true si déverrouillé, false sinon
  */
-
-// http://doc.spip.org/@spip_nfslock_test
-function spip_nfslock_test($fichier, $birth, $max_age=0) {
+function spip_nfslock_test($fichier, $birth, $max_age = 0) {
        return spip_nfsunlock($fichier, $birth, $max_age, true);
 }
-
-?>