Created
September 8, 2012 12:31
-
-
Save ironpinguin/3674432 to your computer and use it in GitHub Desktop.
Simple use of php_rsync (librsync implementation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple client for librsync handshake. | |
*/ | |
$opts = "s:f:n:p:"; | |
$longOpts = array("signature:", "file:", "newfile:", "patch:"); | |
class rsyncClient | |
{ | |
private $_signature; | |
private $_file; | |
private $_newFile; | |
private $_patch; | |
public function __construct($options) | |
{ | |
foreach($options as $option => $value) | |
{ | |
switch($option) | |
{ | |
case 's': | |
case 'signature': | |
$this->_signature = $value; | |
break; | |
case 'f': | |
case 'file': | |
$this->_file = $value; | |
break; | |
case 'n': | |
case 'newfile': | |
$this->_newFile = $value; | |
break; | |
case 'p': | |
case 'patch': | |
$this->_patch = $value; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
public function process() | |
{ | |
if (empty($this->_patch)) { | |
$this->_generateSignature(); | |
} else { | |
$this->_patchFile(); | |
} | |
} | |
private function _generateSignature() | |
{ | |
try { | |
$ret = rsync_generate_signature($this->_file, $this->_signature); | |
} catch (\Exception $e) { | |
self::logging("Exception: ".$e->getMessage()."!!"); | |
exit; | |
} | |
if ($ret != RSYNC_DONE) | |
{ | |
self::logging("Signature generating failed with message: ".rsync_error($ret)); | |
exit; | |
} | |
self:logging("Signature file ".$this->_signature." generated."); | |
} | |
private function _patchFile() | |
{ | |
try { | |
$ret = rsync_patch_file($this->_file, $this->_patch, $this->_newFile); | |
} catch(\Exception $e) { | |
self::logging("Exception: ".$e->getMessage()."!!"); | |
exit; | |
} | |
if ($ret != RSYNC_DONE) | |
{ | |
self::logging("Patching file failed with message: ".rsync_error($ret)); | |
exit; | |
} | |
self:logging("Patched file ".$this->_newFile." generated."); | |
} | |
public static function logging($message) | |
{ | |
echo $message."\n"; | |
} | |
} | |
$options = getopt($opts, $longOpts); | |
$client = new rsyncClient($options); | |
$client->process(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple server for librsync handshake. | |
* | |
*/ | |
$opts = "f:s:p:"; | |
$optsLong = array("file:","signature:", "patch:"); | |
class rsyncServer | |
{ | |
private $_file; | |
private $_signature; | |
private $_patch; | |
public function __construct($options) | |
{ | |
foreach($options as $option => $value) | |
{ | |
switch($option) | |
{ | |
case 'f': | |
case 'file': | |
$this->_file = $value; | |
break; | |
case 's': | |
case 'signature': | |
$this->_signature = $value; | |
break; | |
case 'p': | |
case 'patch': | |
$this->_patch = $value; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
public function process() | |
{ | |
try { | |
$ret = rsync_generate_delta($this->_signature, $this->_file, $this->_patch); | |
} catch (\Exception $e) { | |
self::logging("Exception: ".$e->getMessage()."!!"); | |
exit; | |
} | |
if ($ret != RSYNC_DONE) | |
{ | |
self::logging("Generating patch file failed with message: ".rsync_error($ret)); | |
exit; | |
} | |
self:logging("Patch file ".$this->_patch." generated."); | |
} | |
public static function logging($message) | |
{ | |
echo $message."\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment