Skip to content

Commit

Permalink
Make the dependency on stdbuf optional for everything but notify
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Jun 13, 2016
1 parent 01b6150 commit 2a101e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Exception/DependencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Copyright (c) 2016 Robin Appelman <[email protected]>
* This file is licensed under the Licensed under the MIT license:
* http://opensource.org/licenses/MIT
*/

namespace Icewind\SMB\Exception;

class DependencyException extends Exception {
}
9 changes: 8 additions & 1 deletion src/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Icewind\SMB;

use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\FileInUseException;
use Icewind\SMB\Exception\InvalidTypeException;
use Icewind\SMB\Exception\NotFoundException;
Expand Down Expand Up @@ -53,7 +54,8 @@ public function __construct($server, $name) {

protected function getConnection() {
$workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
$command = sprintf('stdbuf -o0 %s %s --authentication-file=%s %s',
$command = sprintf('%s%s %s --authentication-file=%s %s',
$this->system->hasStdBuf() ? 'stdbuf -o0 ' : '',
$this->system->getSmbclientPath(),
$workgroupArgument,
System::getFD(3),
Expand Down Expand Up @@ -353,8 +355,13 @@ public function setMode($path, $mode) {
* @param string $path
* @param callable $callback callable which will be called for each received change
* @return mixed
* @throws ConnectionException
* @throws DependencyException
*/
public function notify($path, callable $callback) {
if (!$this->system->hasStdBuf()) { //stdbuf is required to disable smbclient's output buffering
throw new DependencyException('stdbuf is required for usage of the notify command');
}
$connection = $this->getConnection(); // use a fresh connection since the notify command blocks the process
$command = 'notify ' . $this->escapePath($path);
$connection->write($command . PHP_EOL);
Expand Down
12 changes: 12 additions & 0 deletions src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class System {

private $net;

private $stdbuf;

public static function getFD($num) {
$folders = array(
'/proc/self/fd',
Expand All @@ -40,4 +42,14 @@ public function getNetPath() {
}
return $this->net;
}

public function hasStdBuf() {
if (!$this->stdbuf) {
$result = null;
$output = array();
exec('which stdbuf 2>&1', $output, $result);
$this->stdbuf = $result === 0;
}
return $this->stdbuf;
}
}

0 comments on commit 2a101e7

Please sign in to comment.