简体   繁体   English

同步文件

[英]Synchronizing Files

I have a project in which a file has to be synchronized across computers. 我有一个项目,其中文件必须在计算机之间同步。

My problem is that my program gives an error that it runs out of execution time of 30 sec. 我的问题是我的程序给出了一个错误,指出它的执行时间已超过30秒。

Now, i have made a program in php for this; 现在,我为此编写了一个php程序; what it does is divides old file into blocks and makes md5 hashes of each block and compares it with modified file by dynamically making hashes of given length at any offset.(from starting till the end of modified file) And this way it finds the blocks which need not be transferred. 它的作用是将旧文件分成多个块,并通过在每个偏移量处动态生成给定长度的哈希值(从修改文件的开始到结尾),将每个块的md5哈希值与修改后的文件进行比较。不需要转移。

Any one out there has any experience,advice,links or code your more than welcome.Thnx 那里的任何人都有任何经验,建议,链接或代码,非常值得欢迎。

ps i have the luxury to work in php, java or c++. 附言:我有工作在php,java或c ++中的奢望。

the code i'm giving is for testing purpose, it takes 2 files from same location(one modified file and the other the original) makes hashes of blocks from old file and compares it with hashes from new file at every other offset. 我提供的代码用于测试目的,它从相同位置获取2个文件(一个修改后的文件,另一个从原始位置获取)对旧文件进行块哈希处理,并将其与每隔一个偏移量的新文件进行哈希比较。 hope this helps: 希望这可以帮助:

<html>
<body>
<?php  
   $k=0;
   $old_file = file_get_contents('11.jpg');
   $new_file = file_get_contents('12.jpg'); 
   $block_length = 2048;
   $j = 0;
   $md5_hashes_old = array();
   $md5_hashes_new = array();
   $diff_blocks = array(); 
   $first_char=array();
   $k = 0;
   while(1){
     if($j >strlen($old_file))
     break;
     $block = substr($old_file,$j,$block_length);
     $md5_hashes_old[$k] = md5($block);
     $first_char[$k]=$block[0];
     $j = $j+$block_length;
     $k++;
  } 
   $j = 0;
   $k = 0;
   $no_of_blocks = sizeof($md5_hashes_old);
   echo $no_of_blocks;
   $matched_blocks = array();
   $matched = 0;
   $fc=0;
   echo $md5_hashes_old[1].'</br>';
  for($i=0;$i<$no_of_blocks;$i++){
      $j =0;
      while(1){
    $block = substr($new_file,$j,$block_length);
    $md5_hash = md5($block);
    if($md5_hashes_old[$i] == $md5_hash){
        $match_block = array();
        $match_block['block_no'] = $i;
        $match_block['index'] = $j;
        array_push($matched_blocks,$match_block);
        break;
    }   
    else
        $j++;

    if($j > strlen($new_file))
        break;
    echo 'old='.$md5_hashes_old[$i].' i='.$i.' new='.$md5_hash.'</br>';
}       
}       
print_r($matched_blocks);   
?> 

</body>
</html>

Increasing the time out is your first port of call. 增加超时时间是您的第一通电话。

I assume you are only doing the md5 comparison when you have a more recent modified date and the file length is different. 我假设您只有在修改日期最近且文件长度不同时才进行md5比较。

If you were using C++ you could use file system watchers to be notified when files are modified and then use that to trigger your process or to trigger the creation of the hash. 如果您使用的是C ++,则可以使用文件系统监视程序在文件被修改时收到通知,然后使用它来触发您的进程或触发哈希的创建。

Another trick would be to cache files for making a binary diff: 另一个技巧是缓存文件以进行二进制比较:

http://dev.chromium.org/developers/design-documents/software-updates-courgette http://dev.chromium.org/developers/design-documents/software-updates-courgette

You always can apply the dirty trick: 您总是可以应用肮脏的把戏:

<?php  set_time_limit(9999);  ?>

But I'm agree with @aioobe, sounds like a reinvented rsync . 但是我同意@aioobe,听起来像是重新发明了rsync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM