Work around issue where 'allinfo' keeps the file open

This commit is contained in:
Robin Appelman 2015-02-10 17:37:37 +01:00
commit ededbfbaa3
5 changed files with 57 additions and 6 deletions

View file

@ -24,4 +24,5 @@ class ErrorCodes {
const DirectoryNotEmpty = 'NT_STATUS_DIRECTORY_NOT_EMPTY'; const DirectoryNotEmpty = 'NT_STATUS_DIRECTORY_NOT_EMPTY';
const FileIsADirectory = 'NT_STATUS_FILE_IS_A_DIRECTORY'; const FileIsADirectory = 'NT_STATUS_FILE_IS_A_DIRECTORY';
const NotADirectory = 'NT_STATUS_NOT_A_DIRECTORY'; const NotADirectory = 'NT_STATUS_NOT_A_DIRECTORY';
const SharingViolation = 'NT_STATUS_SHARING_VIOLATION';
} }

View file

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/
namespace Icewind\SMB\Exception;
class FileInUseException extends InvalidRequestException {}

View file

@ -10,6 +10,7 @@ namespace Icewind\SMB;
use Icewind\SMB\Exception\AccessDeniedException; use Icewind\SMB\Exception\AccessDeniedException;
use Icewind\SMB\Exception\AlreadyExistsException; use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\Exception; use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\FileInUseException;
use Icewind\SMB\Exception\InvalidTypeException; use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotEmptyException; use Icewind\SMB\Exception\NotEmptyException;
use Icewind\SMB\Exception\NotFoundException; use Icewind\SMB\Exception\NotFoundException;
@ -55,6 +56,8 @@ class Parser {
case ErrorCodes::FileIsADirectory: case ErrorCodes::FileIsADirectory:
case ErrorCodes::NotADirectory: case ErrorCodes::NotADirectory:
throw new InvalidTypeException($path); throw new InvalidTypeException($path);
case ErrorCodes::SharingViolation:
throw new FileInUseException($path);
default: default:
$message = 'Unknown error (' . $error . ')'; $message = 'Unknown error (' . $error . ')';
if ($path) { if ($path) {

View file

@ -10,6 +10,16 @@ namespace Icewind\SMB;
use Icewind\SMB\Exception\ConnectionException; use Icewind\SMB\Exception\ConnectionException;
class RawConnection { class RawConnection {
/**
* @var string
*/
private $command;
/**
* @var string[]
*/
private $env;
/** /**
* @var resource[] $pipes * @var resource[] $pipes
* *
@ -24,6 +34,12 @@ class RawConnection {
private $process; private $process;
public function __construct($command, $env = array()) { public function __construct($command, $env = array()) {
$this->command = $command;
$this->env = $env;
$this->connect();
}
private function connect() {
$descriptorSpec = array( $descriptorSpec = array(
0 => array('pipe', 'r'), // child reads from stdin 0 => array('pipe', 'r'), // child reads from stdin
1 => array('pipe', 'w'), // child writes to stdout 1 => array('pipe', 'w'), // child writes to stdout
@ -33,13 +49,13 @@ class RawConnection {
5 => array('pipe', 'w') // child writes to fd#5 5 => array('pipe', 'w') // child writes to fd#5
); );
setlocale(LC_ALL, Server::LOCALE); setlocale(LC_ALL, Server::LOCALE);
$env = array_merge($env, array( $env = array_merge($this->env, array(
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE, 'LC_ALL' => Server::LOCALE,
'LANG' => Server::LOCALE, 'LANG' => Server::LOCALE,
'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output 'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output
)); ));
$this->process = proc_open($command, $descriptorSpec, $this->pipes, '/', $env); $this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env);
if (!$this->isValid()) { if (!$this->isValid()) {
throw new ConnectionException(); throw new ConnectionException();
} }
@ -138,6 +154,11 @@ class RawConnection {
proc_close($this->process); proc_close($this->process);
} }
public function reconnect() {
$this->close();
$this->connect();
}
public function __destruct() { public function __destruct() {
$this->close(); $this->close();
} }

View file

@ -11,6 +11,7 @@ use Icewind\SMB\Exception\AccessDeniedException;
use Icewind\SMB\Exception\AlreadyExistsException; use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\ConnectionException; use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\Exception; use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\FileInUseException;
use Icewind\SMB\Exception\InvalidTypeException; use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotEmptyException; use Icewind\SMB\Exception\NotEmptyException;
use Icewind\SMB\Exception\NotFoundException; use Icewind\SMB\Exception\NotFoundException;
@ -70,6 +71,14 @@ class Share implements IShare {
} }
} }
protected function reconnect() {
$this->connection->reconnect();
$this->connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
if (!$this->connection->isValid()) {
throw new ConnectionException();
}
}
/** /**
* Get the name of the share * Get the name of the share
* *
@ -150,12 +159,13 @@ class Share implements IShare {
* Delete a file on the share * Delete a file on the share
* *
* @param string $path * @param string $path
* @param bool $secondTry
* @return bool * @return bool
* * @throws InvalidTypeException
* @throws \Icewind\SMB\Exception\NotFoundException * @throws NotFoundException
* @throws \Icewind\SMB\Exception\InvalidTypeException * @throws \Exception
*/ */
public function del($path) { public function del($path, $secondTry = false) {
//del return a file not found error when trying to delete a folder //del return a file not found error when trying to delete a folder
//we catch it so we can check if $path doesn't exist or is of invalid type //we catch it so we can check if $path doesn't exist or is of invalid type
try { try {
@ -170,6 +180,12 @@ class Share implements IShare {
throw new InvalidTypeException($path); throw new InvalidTypeException($path);
} }
throw $e; throw $e;
} catch (FileInUseException $e) {
if ($secondTry) {
throw $e;
}
$this->reconnect();
return $this->del($path, true);
} }
} }