protected function write_sql($sth) { $flock = new FileLock('db/lock'); if ($flock->lock($this->filename)) { $sth->execute(); $flock->unlock($this->filename); } else { throw new Exception('ファイルに書き込めませんでした。'); } }
public function testExists() { $fs = $this->getMockBuilder('League\\Flysystem\\Filesystem', array('read', 'has', 'delete'))->disableOriginalConstructor()->getMock(); $fs->expects($this->once())->method('delete')->with('/tmp/test3.lock')->will($this->returnValue(true)); $fs->method('has')->will($this->returnValueMap(array(array('/tmp/test1.locl', false), array('/tmp/test2.lock', true), array('/tmp/test3.lock', true)))); $fs->method('read')->will($this->returnValueMap(array(array('/tmp/test2.lock', (string) (round(microtime(true) * 1000) + 10000)), array('/tmp/test3.lock', '10000')))); $fl = new FileLock($fs, '/tmp'); $this->assertFalse($fl->exists('test1')); $this->assertTrue($fl->exists('test2')); $this->assertFalse($fl->exists('test3')); }
/** * Sets a new base path for the locks. * * @param String $directory * @throws RuntimeException if provided directory is either not existant * or is not a directory or is not writable. */ public static function setDirectory($directory) { if (!file_exists($directory) || !is_dir($directory) || !is_writable($directory)) { throw new RuntimeException('Passed directory is not an actual directory or is not writable.'); } self::$directory = rtrim($directory, '/') . '/'; }
class FileLock { public $fp; public $lock_file; public function lock() { $this->lock_file = "/tmp/" . __CLASS__ . ".php"; if (!file_exists($this->lock_file)) { $this->fp = fopen($this->lock_file, "wa"); } else { echo "File Locked\n"; exit; } } public function unlock() { if (is_resource($this->fp)) { fclose($this->fp); } unlink($this->lock_file); } public function doTest() { $this->lock(); sleep(60); $this->unlock(); } } $obj = new FileLock(); $obj->doTest();
/** * Private constructor to ensure the singleton pattern is used correctly. */ private function __construct() { FileLock::setDirectory($GLOBALS['TMP_PATH']); $this->lock = new FileLock('studip-cronjob'); }
require_once 'lib/migrations/db_migration.php'; require_once 'lib/migrations/db_schema_version.php'; require_once 'lib/migrations/migrator.php'; page_open(array("sess" => "Seminar_Session", "auth" => "Seminar_Auth", "perm" => "Seminar_Perm", "user" => "Seminar_User")); $auth->login_if(!$perm->have_perm("root")); $perm->check("root"); if (empty($_SESSION['_language'])) { $_SESSION['_language'] = get_accepted_languages(); } $_language_path = init_i18n($_SESSION['_language']); include 'lib/include/html_head.inc.php'; $path = $GLOBALS['STUDIP_BASE_PATH'] . '/db/migrations'; $verbose = true; $target = NULL; FileLock::setDirectory($GLOBALS['TMP_PATH']); $lock = new FileLock('web-migrate'); if ($lock->isLocked() && Request::int('release_lock')) { $lock->release(); } if (Request::int('target')) { $target = (int) Request::int('target'); } $version = new DBSchemaVersion('studip'); $migrator = new Migrator($path, $version, $verbose); if (Request::submitted('start')) { ob_start(); set_time_limit(0); $lock->lock(array('timestamp' => time(), 'user_id' => $GLOBALS['user']->id)); $migrator->migrate_to($target); $lock->release(); $announcements = ob_get_clean();
/** * récupère les infos du document à partir d'un fichier DocBook * @param string $fichier nom du fichier * @return boolean <code>true</code> lorsque tout s'est bien passé */ function setContentFromDocbook($fichier) { require_once PATH_INC_BACKEND_SERVICE . "DocbookParse.lib.php"; $errors =& PEAR_ErrorStack::singleton('OpenWeb_Backend_DocbookParse'); $errors->pushCallback(array(&$this, '_repackageErrorStack')); $this->infos = docbookGetArticleInfoFromFile($fichier); $this->infos->verifyRepertoire(); $errors->popCallback(); if ($this->errors->hasErrors()) { return false; } $errors =& PEAR_ErrorStack::singleton('OpenWeb_Backend_DocumentType'); $errors->pushCallback(array(&$this, '_repackageErrorStack')); $this->type = new DocumentType($this->infos->type); $errors->popCallback(); if ($this->errors->hasErrors()) { return false; } /* lorsque l'article est tout nouveau, on vérifie en base si le nom du répertoire est unique */ $newdoc = false; if (!is_int($this->id)) { $newdoc = true; $sql = 'SELECT count(doc_id) as cnt FROM doc_document WHERE lower(doc_repertoire) = ' . $this->db->quote(strtolower($this->infos->repertoire)); $rep = $this->db->getRow($sql); if (DB::isError($rep)) { $this->errors->push(17); /*impossible de lire depuis la base de données*/ return false; } if (intval($rep['cnt']) > 0) { $this->errors->push(17); /*"donnez un autre nom de répertoire : ".$this->infos->repertoire." est déjà utilisé";*/ return false; } } if (in_array($this->type->repertoire . $this->infos->repertoire, $GLOBALS['OW_FORBIDDEN_DIR'])) { $this->errors->push(17); /*"nom de répertoire interdit pas l'admin*/ return false; } $destdir = PATH_SITE_ROOT . $this->getDocumentPath(); // vérification des propriétes $this->_validateProperties(); if ($this->errors->hasErrors()) { return false; } // création du répertoire de l'article dans temp umask(022); if (file_exists($destdir)) { if (!is_dir($destdir)) { $this->errors->push(17); } /* @todo error code */ } else { mkdir($destdir, 0755); } $annexes = $destdir . "/annexes"; if (file_exists($annexes)) { if (!is_dir($annexes)) { $this->errors->push(17); } /* @todo error code */ } else { mkdir($annexes, 0755); } $dbkfile = $this->getDocumentFileName('docbook'); /* @todo traiter les erreurs ici */ $fl = new FileLock(); ignore_user_abort(true); $fp = $fl->Open($dbkfile); fwrite($fp, file_get_contents($fichier)); $fl->Close($fp); ignore_user_abort(false); chmod($dbkfile, 0644); if ($this->errors->hasErrors()) { return false; } // enregistrement en base des différentes infos $this->_saveDb(); if ($newdoc) { $etat = $this->ref->getStatusInfos('OW_STATUS_TEMP'); $this->changeEtat($etat['id']); } if ($this->type->isintro) { $this->ref->dumpClassements(); } // conversion du fichier dans les autres formats $this->generation(); return !$this->errors->hasErrors(); }
<?php require_once 'FileLock.php'; // // To obtain a "lock" on the file... // // 1) Create a FileLock object and pass in the name of the file you wish to access. // The FileLock class will then create a file with that name suffixed with '.lock'. // (So, no existing file in the file system should end in .lock.) // $lock = new FileLock('user.db'); // // If you want to only read the file, then call requestReadLock() just before you need to // read from the file. // //$lock->requestReadLock(); // // If you want to write to the file, then call requestWriteLock() just before you need // to write to the file. Please note that a write lock implies that there are no readers and // that there are no other writers. Only request a write lock if it is truly needed! // //$lock->requestWriteLock(); // // When done reading/writing, you should manually release the lock so that other processes // can access the file. NOTE: Release it after flushing any and all buffers and/or closing // the file. // $lock->releaseLock(); // // If you don't release the lock, the FileLock's destructor will free it once the object // is destroyed by the PHP engine.