@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | **/ |
| 8 | 8 | |
| 9 | 9 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 10 | - return; |
|
| 10 | + return; |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | include_spip('inc/acces'); |
@@ -101,93 +101,93 @@ discard block |
||
| 101 | 101 | * @return int|bool Timestamp du verrou, false si erreur |
| 102 | 102 | */ |
| 103 | 103 | function spip_nfslock($fichier, $max_age = 0) { |
| 104 | - $tries = 0; |
|
| 104 | + $tries = 0; |
|
| 105 | 105 | |
| 106 | - if (!$max_age) { |
|
| 107 | - $max_age = _DEFAULT_LOCKTIME; |
|
| 108 | - } |
|
| 109 | - $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 106 | + if (!$max_age) { |
|
| 107 | + $max_age = _DEFAULT_LOCKTIME; |
|
| 108 | + } |
|
| 109 | + $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /* |
|
| 112 | + /* |
|
| 113 | 113 | * 1. create a tmp file with a psuedo random file name. we also make |
| 114 | 114 | * tpath which is a buffer to store the full pathname of the tmp file. |
| 115 | 115 | */ |
| 116 | 116 | |
| 117 | - $id = creer_uniqid(); |
|
| 118 | - $tpath = _DIR_TMP . "slock.$id"; |
|
| 119 | - $tmpfd = @fopen($tpath, 'w'); // hum, le 'x' necessite php4,3,2 ... |
|
| 120 | - if (!$tmpfd) { /* open failed */ |
|
| 121 | - @fclose($tmpfd); |
|
| 122 | - spip_unlink($tpath); |
|
| 117 | + $id = creer_uniqid(); |
|
| 118 | + $tpath = _DIR_TMP . "slock.$id"; |
|
| 119 | + $tmpfd = @fopen($tpath, 'w'); // hum, le 'x' necessite php4,3,2 ... |
|
| 120 | + if (!$tmpfd) { /* open failed */ |
|
| 121 | + @fclose($tmpfd); |
|
| 122 | + spip_unlink($tpath); |
|
| 123 | 123 | |
| 124 | - return false; //NFSL_SYSF |
|
| 125 | - } |
|
| 124 | + return false; //NFSL_SYSF |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /* |
|
| 127 | + /* |
|
| 128 | 128 | * 2. make fullpath, a buffer for the full pathname of the lock file. |
| 129 | 129 | * then start looping trying to lock it |
| 130 | 130 | */ |
| 131 | 131 | |
| 132 | - while ($tries < 10) { |
|
| 133 | - /* |
|
| 132 | + while ($tries < 10) { |
|
| 133 | + /* |
|
| 134 | 134 | * 3. link tmp file to lock file. if it goes, we win and we clean |
| 135 | 135 | * up and return the st_ctime of the lock file. |
| 136 | 136 | */ |
| 137 | 137 | |
| 138 | - if (link($tpath, $lock_file) == 1) { |
|
| 139 | - spip_unlink($tpath); /* got it! */ |
|
| 140 | - @fclose($tmpfd); |
|
| 141 | - if (($our_tmp = lstat($lock_file)) == false) { /* stat failed... shouldn't happen */ |
|
| 142 | - spip_unlink($lock_file); |
|
| 138 | + if (link($tpath, $lock_file) == 1) { |
|
| 139 | + spip_unlink($tpath); /* got it! */ |
|
| 140 | + @fclose($tmpfd); |
|
| 141 | + if (($our_tmp = lstat($lock_file)) == false) { /* stat failed... shouldn't happen */ |
|
| 142 | + spip_unlink($lock_file); |
|
| 143 | 143 | |
| 144 | - return false; // (NFSL_SYSF); |
|
| 145 | - } |
|
| 144 | + return false; // (NFSL_SYSF); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return ($our_tmp['ctime']); |
|
| 148 | - } |
|
| 147 | + return ($our_tmp['ctime']); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /* |
|
| 150 | + /* |
|
| 151 | 151 | * 4. the lock failed. check for a stale lock file, being mindful |
| 152 | 152 | * of NFS and the fact the time is set from the NFS server. we |
| 153 | 153 | * do a write on the tmp file to update its time to the server's |
| 154 | 154 | * idea of "now." |
| 155 | 155 | */ |
| 156 | 156 | |
| 157 | - $old_stat = lstat($lock_file); |
|
| 158 | - if (@fputs($tmpfd, 'zz', 2) != 2 || !$our_tmp = fstat($tmpfd)) { |
|
| 159 | - break; |
|
| 160 | - } /* something bogus is going on */ |
|
| 157 | + $old_stat = lstat($lock_file); |
|
| 158 | + if (@fputs($tmpfd, 'zz', 2) != 2 || !$our_tmp = fstat($tmpfd)) { |
|
| 159 | + break; |
|
| 160 | + } /* something bogus is going on */ |
|
| 161 | 161 | |
| 162 | 162 | |
| 163 | - if ($old_stat != false && (($old_stat['ctime'] + $max_age) < $our_tmp['ctime'])) { |
|
| 164 | - spip_unlink($lock_file); /* break the stale lock */ |
|
| 165 | - $tries++; |
|
| 166 | - /* It is CRITICAL that we sleep after breaking |
|
| 163 | + if ($old_stat != false && (($old_stat['ctime'] + $max_age) < $our_tmp['ctime'])) { |
|
| 164 | + spip_unlink($lock_file); /* break the stale lock */ |
|
| 165 | + $tries++; |
|
| 166 | + /* It is CRITICAL that we sleep after breaking |
|
| 167 | 167 | * the lock. Otherwise, we could race with |
| 168 | 168 | * another process and unlink it's newly- |
| 169 | 169 | * created file. |
| 170 | 170 | */ |
| 171 | - sleep(1 + rand(0, 4)); |
|
| 172 | - continue; |
|
| 173 | - } |
|
| 171 | + sleep(1 + rand(0, 4)); |
|
| 172 | + continue; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - /* |
|
| 175 | + /* |
|
| 176 | 176 | * 5. try again |
| 177 | 177 | */ |
| 178 | 178 | |
| 179 | - $tries++; |
|
| 180 | - sleep(1 + rand(0, 4)); |
|
| 181 | - } |
|
| 179 | + $tries++; |
|
| 180 | + sleep(1 + rand(0, 4)); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /* |
|
| 183 | + /* |
|
| 184 | 184 | * 6. give up, failure. |
| 185 | 185 | */ |
| 186 | 186 | |
| 187 | - spip_unlink($tpath); |
|
| 188 | - @fclose($tmpfd); |
|
| 187 | + spip_unlink($tpath); |
|
| 188 | + @fclose($tmpfd); |
|
| 189 | 189 | |
| 190 | - return false; //(NFSL_LOCKED); |
|
| 190 | + return false; //(NFSL_LOCKED); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /** |
@@ -231,75 +231,75 @@ discard block |
||
| 231 | 231 | * return bool true si déverrouillé, false sinon |
| 232 | 232 | */ |
| 233 | 233 | function spip_nfsunlock($fichier, $birth, $max_age = 0, $test = false) { |
| 234 | - $id = creer_uniqid(); |
|
| 235 | - if (!$max_age) { |
|
| 236 | - $max_age = _DEFAULT_LOCKTIME; |
|
| 237 | - } |
|
| 234 | + $id = creer_uniqid(); |
|
| 235 | + if (!$max_age) { |
|
| 236 | + $max_age = _DEFAULT_LOCKTIME; |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /* |
|
| 239 | + /* |
|
| 240 | 240 | * 1. Build a temp file and stat that to get an idea of what the server |
| 241 | 241 | * thinks the current time is (our_tmp.st_ctime).. |
| 242 | 242 | */ |
| 243 | 243 | |
| 244 | - $tpath = _DIR_TMP . "stime.$id"; |
|
| 245 | - $tmpfd = @fopen($tpath, 'w'); |
|
| 246 | - if ( |
|
| 247 | - (!$tmpfd) |
|
| 248 | - or (@fputs($tmpfd, 'zz', 2) != 2) |
|
| 249 | - or !($our_tmp = fstat($tmpfd)) |
|
| 250 | - ) { |
|
| 251 | - /* The open failed, or we can't write the file, or we can't stat it */ |
|
| 252 | - @fclose($tmpfd); |
|
| 253 | - spip_unlink($tpath); |
|
| 244 | + $tpath = _DIR_TMP . "stime.$id"; |
|
| 245 | + $tmpfd = @fopen($tpath, 'w'); |
|
| 246 | + if ( |
|
| 247 | + (!$tmpfd) |
|
| 248 | + or (@fputs($tmpfd, 'zz', 2) != 2) |
|
| 249 | + or !($our_tmp = fstat($tmpfd)) |
|
| 250 | + ) { |
|
| 251 | + /* The open failed, or we can't write the file, or we can't stat it */ |
|
| 252 | + @fclose($tmpfd); |
|
| 253 | + spip_unlink($tpath); |
|
| 254 | 254 | |
| 255 | - return false; //(NFSL_SYSF); |
|
| 256 | - } |
|
| 255 | + return false; //(NFSL_SYSF); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - @fclose($tmpfd); /* We don't need this once we have our_tmp.st_ctime. */ |
|
| 259 | - spip_unlink($tpath); |
|
| 258 | + @fclose($tmpfd); /* We don't need this once we have our_tmp.st_ctime. */ |
|
| 259 | + spip_unlink($tpath); |
|
| 260 | 260 | |
| 261 | - /* |
|
| 261 | + /* |
|
| 262 | 262 | * 2. make fullpath, a buffer for the full pathname of the lock file |
| 263 | 263 | */ |
| 264 | 264 | |
| 265 | - $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 265 | + $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 266 | 266 | |
| 267 | - /* |
|
| 267 | + /* |
|
| 268 | 268 | * 3. If the ctime hasn't been modified, unlink the file and return. If the |
| 269 | 269 | * lock has expired, sleep the usual random interval before returning. |
| 270 | 270 | * If we didn't sleep, there could be a race if the caller immediately |
| 271 | 271 | * tries to relock the file. |
| 272 | 272 | */ |
| 273 | 273 | |
| 274 | - if ( |
|
| 275 | - ($old_stat = @lstat($lock_file)) /* stat succeeds so file is there */ |
|
| 276 | - && ($old_stat['ctime'] == $birth) |
|
| 277 | - ) { /* hasn't been modified since birth */ |
|
| 278 | - if (!$test) { |
|
| 279 | - spip_unlink($lock_file); |
|
| 280 | - } /* so the lock is ours to remove */ |
|
| 281 | - if ($our_tmp['ctime'] >= $birth + $max_age) { /* the lock has expired */ |
|
| 282 | - if (!$test) { |
|
| 283 | - return false; |
|
| 284 | - } //(NFSL_LOST); |
|
| 285 | - sleep(1 + (rand(0, 4))); /* so sleep a bit */ |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - return true;//(NFSL_OK); /* success */ |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /* |
|
| 274 | + if ( |
|
| 275 | + ($old_stat = @lstat($lock_file)) /* stat succeeds so file is there */ |
|
| 276 | + && ($old_stat['ctime'] == $birth) |
|
| 277 | + ) { /* hasn't been modified since birth */ |
|
| 278 | + if (!$test) { |
|
| 279 | + spip_unlink($lock_file); |
|
| 280 | + } /* so the lock is ours to remove */ |
|
| 281 | + if ($our_tmp['ctime'] >= $birth + $max_age) { /* the lock has expired */ |
|
| 282 | + if (!$test) { |
|
| 283 | + return false; |
|
| 284 | + } //(NFSL_LOST); |
|
| 285 | + sleep(1 + (rand(0, 4))); /* so sleep a bit */ |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + return true;//(NFSL_OK); /* success */ |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /* |
|
| 292 | 292 | * 4. Either ctime has been modified, or the entire lock file is missing. |
| 293 | 293 | * If the lock should still be ours, based on the ctime of the temp |
| 294 | 294 | * file, return with NFSL_STOLEN. If not, then our lock is expired and |
| 295 | 295 | * someone else has grabbed the file, so return NFSL_LOST. |
| 296 | 296 | */ |
| 297 | 297 | |
| 298 | - if ($our_tmp['ctime'] < $birth + $max_age) { /* lock was stolen */ |
|
| 299 | - return false; |
|
| 300 | - } //(NFSL_STOLEN); |
|
| 298 | + if ($our_tmp['ctime'] < $birth + $max_age) { /* lock was stolen */ |
|
| 299 | + return false; |
|
| 300 | + } //(NFSL_STOLEN); |
|
| 301 | 301 | |
| 302 | - return false; //(NFSL_LOST); /* The lock must have expired first. */ |
|
| 302 | + return false; //(NFSL_LOST); /* The lock must have expired first. */ |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | |
@@ -323,5 +323,5 @@ discard block |
||
| 323 | 323 | * return bool true si déverrouillé, false sinon |
| 324 | 324 | */ |
| 325 | 325 | function spip_nfslock_test($fichier, $birth, $max_age = 0) { |
| 326 | - return spip_nfsunlock($fichier, $birth, $max_age, true); |
|
| 326 | + return spip_nfsunlock($fichier, $birth, $max_age, true); |
|
| 327 | 327 | } |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | if (!$max_age) { |
| 107 | 107 | $max_age = _DEFAULT_LOCKTIME; |
| 108 | 108 | } |
| 109 | - $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 109 | + $lock_file = _DIR_TMP._NAME_LOCK.'-'.substr(md5($fichier), 0, 8); |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | 112 | /* |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | |
| 117 | 117 | $id = creer_uniqid(); |
| 118 | - $tpath = _DIR_TMP . "slock.$id"; |
|
| 118 | + $tpath = _DIR_TMP."slock.$id"; |
|
| 119 | 119 | $tmpfd = @fopen($tpath, 'w'); // hum, le 'x' necessite php4,3,2 ... |
| 120 | 120 | if (!$tmpfd) { /* open failed */ |
| 121 | 121 | @fclose($tmpfd); |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * thinks the current time is (our_tmp.st_ctime).. |
| 242 | 242 | */ |
| 243 | 243 | |
| 244 | - $tpath = _DIR_TMP . "stime.$id"; |
|
| 244 | + $tpath = _DIR_TMP."stime.$id"; |
|
| 245 | 245 | $tmpfd = @fopen($tpath, 'w'); |
| 246 | 246 | if ( |
| 247 | 247 | (!$tmpfd) |
@@ -255,14 +255,14 @@ discard block |
||
| 255 | 255 | return false; //(NFSL_SYSF); |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - @fclose($tmpfd); /* We don't need this once we have our_tmp.st_ctime. */ |
|
| 258 | + @fclose($tmpfd); /* We don't need this once we have our_tmp.st_ctime. */ |
|
| 259 | 259 | spip_unlink($tpath); |
| 260 | 260 | |
| 261 | 261 | /* |
| 262 | 262 | * 2. make fullpath, a buffer for the full pathname of the lock file |
| 263 | 263 | */ |
| 264 | 264 | |
| 265 | - $lock_file = _DIR_TMP . _NAME_LOCK . '-' . substr(md5($fichier), 0, 8); |
|
| 265 | + $lock_file = _DIR_TMP._NAME_LOCK.'-'.substr(md5($fichier), 0, 8); |
|
| 266 | 266 | |
| 267 | 267 | /* |
| 268 | 268 | * 3. If the ctime hasn't been modified, unlink the file and return. If the |
@@ -282,10 +282,10 @@ discard block |
||
| 282 | 282 | if (!$test) { |
| 283 | 283 | return false; |
| 284 | 284 | } //(NFSL_LOST); |
| 285 | - sleep(1 + (rand(0, 4))); /* so sleep a bit */ |
|
| 285 | + sleep(1 + (rand(0, 4))); /* so sleep a bit */ |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - return true;//(NFSL_OK); /* success */ |
|
| 288 | + return true; //(NFSL_OK); /* success */ |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /* |