function begin($exclusive = true)
{
if ($this->txFp !== false) {
throw new LogicException('a transaction is already active');
}
$this->txExclusive = $exclusive;
if (($this->txFp = @fopen($this->file, 'r+')) === false) {
if (($this->txFp = @fopen($this->file, 'r')) !== false) {
$this->readOnly = true;
}
}
if ($this->txFp === false) {
if (file_exists($this->file)) {
if (is_dir($this->file)) {
throw new RuntimeException($this->file . ' is a directory');
}
throw new RuntimeException($this->file . ' is not readable');
}
if ($this->skeleton_file) {
copy($this->skeleton_file, $this->file);
if ($this->createmode !== false) {
chmod($this->file, $this->createmode);
}
if (($this->txFp = fopen($this->file, 'r+')) === false) {
throw new RuntimeException('fopen(' . var_export($this->file, 1) . ', "r+") failed');
}
} else {
if ($this->mkdirs) {
$dir = dirname($this->file);
if (!file_exists($dir)) {
$mode = $this->createmode | 0111;
mkdir($dir, $mode, true);
chmod($dir, $mode);
}
}
if (($this->txFp = fopen($this->file, 'x+')) === false) {
throw new RuntimeException('fopen(' . var_export($this->file, 1) . ', "x+") failed');
} elseif ($this->createmode !== false) {
chmod($this->file, $this->createmode);
}
}
}
if ($this->txExclusive && !flock($this->txFp, LOCK_EX)) {
fclose($this->txFp);
throw new RuntimeException('flock(<txFp>, LOCK_EX) failed');
}
}