"Fossies" - the Fresh Open Source Software Archive

Member "htmlrecode-1.3.1/makediff.php" (21 Jul 2009, 11384 Bytes) of package /linux/www/old/htmlrecode-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "makediff.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 # This is Bisqwit's generic makediff.php, activated from depfun.mak.
    4 # The same program is used in many different projects to create
    5 # a diff file version history (patches).
    6 #
    7 # makediff.php version 3.0.6
    8 
    9 # Copyright (C) 2000,2002 Bisqwit (http://bisqwit.iki.fi/)
   10 
   11 # Syntax:
   12 
   13 # argv[1]: Newest archive if any
   14 # argv[2]: Archive directory if any
   15 # argv[3]: Disable /WWW/src/arch linking if set
   16 
   17 function ShellFix($s)
   18 {
   19   return "'".str_replace("'", "'\''", $s)."'";
   20 }
   21 
   22 if(isset($REMOTE_ADDR))
   23 {
   24   header('Content-type: text/plain');
   25   readfile($PHP_SELF);
   26   exit;
   27 }
   28 
   29 error_reporting(E_ALL & ~E_NOTICE);
   30 
   31 ob_implicit_flush(true);
   32 
   33 if(strlen($argv[2]))
   34 {
   35   chdir($argv[2]);
   36   echo "\tcd ", $argv[2], "\n";
   37 }
   38 
   39 function calcversion($versionstring)
   40 {
   41   $k = '.'.str_replace('.', '..', $versionstring).'.';
   42   $k = ereg_replace('([^0-9])([0-9][0-9a-z][0-9a-z][0-9a-z][^0-9a-z])', '\1-\2', $k);
   43   $k = ereg_replace('([^0-9])([0-9][0-9a-z][0-9a-z][^0-9a-z])', '\1--\2', $k);
   44   $k = ereg_replace('([^0-9])([0-9][0-9a-z][^0-9a-z])', '\1---\2', $k);
   45   $k = ereg_replace('([^0-9])([0-9][^0-9a-z])', '\1----\2', $k);
   46   $k = str_replace('.', '', $k);
   47   $k = str_pad($k, 6*5, '-');
   48   # Reverse:
   49   #$k = strtr($k, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
   50   #               '9876543210ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba');
   51   return $k;
   52 }
   53 
   54 
   55 $openfiles = Array();
   56 $archtmpdir = 'archives.tmp';
   57 $subtmpdir = 'subarch.tmp';
   58 $istmp=0;
   59 function GoTmp()
   60 {
   61   global $istmp, $archtmpdir;
   62   if($istmp)return;
   63   $istmp=1;
   64   if(@mkdir($archtmpdir, 0700))print "\tmkdir $archtmpdir\n";
   65   print "\tcd $archtmpdir\n";
   66   chdir($archtmpdir);
   67 }
   68 function IsInTmp() { global $istmp; return $istmp; }
   69 function UnGoTmp()
   70 {
   71   global $istmp, $archtmpdir;
   72   if(!$istmp)return;
   73   $istmp=0;
   74   print "\tcd ..\n";
   75   chdir('..');
   76 }
   77 function Open($files, $keeplist=array())
   78 {
   79   global $openfiles;
   80   global $archtmpdir, $subtmpdir;
   81 
   82   /* Open any of the given files */
   83   foreach($files as $fn)
   84   {
   85     // If any of the files is already open, return true.
   86     $puh = $openfiles[$fn];
   87     if($puh) return $puh['dir'];
   88   }
   89 
   90   #echo count($openfiles), " files open now...\n";
   91   if(count($openfiles) >= 2)
   92   {
   93     $oldest=''; $oldesttime=999999999;
   94     foreach($openfiles as $fn => $puh)
   95     {
   96       $keep = 0;
   97       foreach($keeplist as $keepfn) if($fn == $keepfn) { $keep=1; break; }
   98       if($keep) continue;
   99       if($puh['time'] < $oldesttime) { $oldesttime = $puh['time']; $oldest = $fn; }
  100     }
  101     if($oldest)
  102     {
  103       Close($oldest);
  104     }
  105   }
  106   
  107   $pick = '';
  108   foreach($files as $fn)
  109     if(ereg('\.tar\.gz$', $fn))
  110     {
  111       $pick = $fn;
  112       break;
  113     }
  114 
  115   if(!$pick)
  116   {
  117     reset($files);
  118     list($dummy, $pick) = each($files);
  119   }
  120   
  121   GoTmp();
  122   
  123   @mkdir($subtmpdir, 0700);
  124   chdir($subtmpdir);
  125   
  126   if(ereg('\.tar\.gz$', $pick))
  127   {
  128     print "\ttar xfz ../".shellfix($pick)."\n";
  129     exec('tar xfz ../../'.shellfix($pick));
  130   }
  131   else
  132   {
  133     print "\tbzip2 -d < ../".shellfix($pick)."| tar xf -\n";
  134     exec('bzip2 -d < ../../'.shellfix($pick).'| tar xf -');
  135   }
  136   $thisdir = exec('echo *');
  137   exec('mv * ../');
  138   chdir('..');
  139 
  140   global $timeind;
  141   $openfiles[$pick] = array('dir' => $thisdir, 'time' => ++$timeind);
  142   
  143   return $thisdir;
  144 }
  145 function Close($fn)
  146 {
  147   global $openfiles;
  148   global $archtmpdir;
  149   
  150   $puh = $openfiles[$fn];
  151   if(!$puh) return;
  152   
  153   $prefix = IsInTmp() ? '' : $archtmpdir.'/';
  154   
  155   $cmd = 'rm -rf '.shellfix($prefix.$puh['dir']);
  156   print "\t$cmd\n";
  157   exec($cmd);
  158   unset($openfiles[$fn]);
  159 }
  160 function CloseAll()
  161 {
  162   global $openfiles;
  163   global $archtmpdir;
  164   
  165   UnGoTmp();
  166   
  167   $openfiles = Array();
  168   $cmd = 'rm -rf '.shellfix($archtmpdir);
  169   print "\t$cmd\n";
  170   exec($cmd);
  171 }
  172 function MakeDiff($dir1, $dir2, $patchname)
  173 {
  174   GoTmp();
  175   #print "## Building $patchname from $dir1 and $dir2\n";
  176   #passthru('ls -al');
  177   
  178   /*
  179     Before doing the diff, we should do the following:
  180       - Remove all symlinks
  181       - Remove all duplicate hardlinks
  182   */
  183   
  184   /* Gather up inode numbers. */
  185   
  186   $data1 = FindInodes($dir1);
  187   $data2 = FindInodes($dir2);
  188   
  189   $era1 = Array();
  190   $era2 = Array();
  191   
  192   foreach($data1['if'] as $ino => $fils)
  193   {
  194     if(count($fils) > 1)
  195     {
  196       $bestcommoncount = 0;
  197       $bestcommon = $fn;
  198       $group2 = array();
  199       foreach($fils as $fn)
  200       {
  201         $ino2 = $data2['fi'][$fn];
  202         if(!isset($ino2))continue;
  203         
  204         $fils2 = $data2['if'][$ino2];
  205         
  206         $common = array_intersect($fils, $fils2);
  207         if(count($common) > $bestcommoncount)
  208         {
  209           $bestcommoncount = count($common);
  210           $bestcommon = $fn;
  211           $group2 = $fils2;
  212         }
  213       }
  214       $common = array_intersect($fils, $group2);
  215       
  216       // Leave one file so that diff works
  217       reset($common);
  218       list($dummy, $fn) = each($common);
  219       unset($common[$dummy]);
  220       
  221       foreach($common as $fn)
  222       {
  223         $era1[] = $fn;
  224         $era2[] = $fn;
  225       }
  226     }
  227   }
  228   
  229   if(count($era1))
  230   {
  231     chdir($dir1); print "\tcd $dir1\n";
  232     $cmd = 'rm -f';
  233     foreach($era1 as $fn)$cmd .= ' '.shellfix($fn);
  234     print "\t$cmd\n";
  235     exec($cmd);
  236     chdir('..'); print "\tcd ..\n";
  237   }
  238   if(count($era2))
  239   {
  240     chdir($dir2); print "\tcd $dir2\n";
  241     $cmd = 'rm -f';
  242     foreach($era1 as $fn)$cmd .= ' '.shellfix($fn);
  243     print "\t$cmd\n";
  244     exec($cmd);
  245     chdir('..'); print "\tcd ..\n";
  246   }
  247   
  248   $cmd = 'LC_ALL=C LANG=C diff -NaHudr '.shellfix($dir1) . ' ' . shellfix($dir2).' > '.shellfix($patchname);
  249   print "\t$cmd\n";
  250   exec($cmd);
  251 }
  252 function FindInodes($directory)
  253 {
  254   for($try = 0; $try < 10; $try++)
  255   {
  256     $fp = @opendir($directory);
  257     if($fp) break;
  258     print "OPENDIR $directory failed (cwd=".getcwd()."), retrying\n";
  259     sleep(1);
  260   }
  261   if($try == 10)
  262   {
  263     print "OPENDIR $directory failed (cwd=".getcwd().")!\n";
  264     exit;
  265   }
  266   
  267   $inofil = array();
  268   $filino = array();
  269   
  270   while(($fn = readdir($fp)))
  271   {
  272     if($fn=='.' || $fn=='..')continue;
  273     
  274     if($directory != '.')
  275       $fn = $directory.'/'.$fn;
  276     
  277     $st = stat($fn);
  278     $ino = $st[0].':'.$st[1];
  279     
  280     $inofil[$ino][] = $fn;
  281     $filino[$fn] = $ino;
  282     
  283     if(is_dir($fn))
  284     {
  285       $sub = FindInodes($fn);
  286       $filino = $filino + $sub['fi'];
  287       
  288       foreach($sub['if'] as $ino => $fil)
  289       {
  290         $tgt = &$inofil[$ino];
  291         if(is_array($tgt))
  292           $tgt = array_merge($tgt, $fil);
  293         else
  294           $tgt = $fil;
  295       }
  296       unset($tgt);
  297     }
  298   }
  299   closedir($fp);
  300   return array('if' => $inofil, 'fi' => $filino);
  301 }
  302 function MakePatch($progname, $v1, $v2, $paks1, $paks2)
  303 {
  304   // print "Make patch for $progname $v1 - $v2\n";
  305   // Available packages for prog1: print_r($paks1);
  306   // Available packages for prog2: print_r($paks2);
  307   
  308   $v1 = ereg_replace('(-----)*$', '', $v1);
  309   $v2 = ereg_replace('(-----)*$', '', $v2);
  310   
  311   $v1string = ereg_replace('\.$', '', preg_replace('|(.....)|e', '(str_replace("-","","$1"))."."', $v1));
  312   $v2string = ereg_replace('\.$', '', preg_replace('|(.....)|e', '(str_replace("-","","$1"))."."', $v2));
  313   
  314   $files1 = Array();
  315   foreach($paks1 as $ext)
  316     $files1[] = $progname . '-' . $v1string . '.' . $ext;
  317 
  318   $files2 = Array();
  319   foreach($paks2 as $ext)
  320     $files2[] = $progname . '-' . $v2string . '.' . $ext;
  321   
  322   $keeplist = array_merge($files1, $files2);
  323   $dir1 = Open($files1, $keeplist);
  324   $dir2 = Open($files2, $keeplist);
  325   
  326   $patchname = "patch-$progname-$v1string-$v2string";
  327   
  328   MakeDiff($dir1, $dir2, $patchname);
  329   
  330   GoTmp();
  331   
  332   $cmd = "gzip -9 ".shellfix($patchname);
  333   print "\t$cmd\n";
  334   exec($cmd);
  335   
  336   $cmd = "gzip -d < ".shellfix($patchname). ".gz | bzip2 -9 > ".shellfix($patchname).".bz2";
  337   print "\t$cmd\n";
  338   exec($cmd);
  339   
  340   $cmd = "mv -f ".shellfix($patchname).".{gz,bz2} ../";
  341   print "\t$cmd\n";
  342   exec($cmd);
  343   
  344   UnGoTmp();
  345 
  346   $cmd = "touch -r".shellfix($files2[0])." ".shellfix($patchname).".{gz,bz2}";
  347   print "\t$cmd\n";
  348   exec($cmd);
  349 
  350   $cmd = "chown --reference ".shellfix($files2[0])." ".shellfix($patchname).".{gz,bz2}";
  351   print "\t$cmd\n";
  352   exec($cmd);
  353 
  354   global $argv;
  355   if(!$argv[3])
  356   {
  357     $cmd = 'ln -f '.shellfix($patchname).'.{gz,bz2} /WWW/src/arch/';
  358     print "\t$cmd\n";
  359     exec($cmd);
  360   }
  361 }
  362 
  363 $progs = array();
  364 $fp = opendir('.');
  365 $f = array();
  366 while(($fn = readdir($fp)))
  367 {
  368   if(ereg('\.sh\.(gz|bz2)$', $fn))continue;
  369   if(ereg('^patch-.*-[0-9].*-[0-9].*\...*', $fn))
  370   {
  371     preg_match(
  372       '/^patch-(.*(?:-opt)?)-([0-9][0-9.a-z-]*)-([0-9][0-9.a-z-]*)\.([a-z0-9]+)$/', $fn, $tab);
  373     // tab[0] = fn
  374     // tab[1] = progname
  375     // tab[2] = old version
  376     // tab[3] = new version
  377     // tab[4] = compression type (gz, bz2)
  378 
  379     $progname = $tab[1];
  380     $version1 = calcversion($tab[2]);
  381     $version2 = calcversion($tab[3]);
  382     $archtype = $tab[4];
  383 
  384     # print "patch prog {$tab[1]} vers1 {$tab[2]} vers2 {$tab[3]} comp {$tab[4]}\n";
  385 
  386     $progs[$progname]['p'][$version1][$version2][$archtype] = $archtype;
  387   }
  388   else
  389   {
  390     preg_match('/(.*(?:-opt)?)-((?!-opt)[0-9][0-9.a-z-]*)\.(tar\.[a-z0-9]+|zip|rar)$/', $fn, $tab);
  391     // tab[0] = fn
  392     // tab[1] = progname
  393     // tab[2] = version
  394     // tab[3] = archive type (tar.gz, tar.bz2, zip, rar)
  395     
  396     #print "$fn:\n";
  397     #print_r($tab);
  398     
  399     $progname = $tab[1];
  400     $version  = calcversion($tab[2]);
  401     $archtype = $tab[3];
  402     
  403     if($archtype != 'zip' && $archtype != 'rar')
  404     {
  405       # print "prog {$tab[1]} vers {$tab[2]} comp {$tab[3]}\n";
  406       
  407       $progs[$progname]['v'][$version][$archtype] = $archtype;
  408     }
  409   }
  410 }
  411 closedir($fp);
  412 
  413 $argv[1] = preg_replace('@^[^-]*(?:-opt)?-([0-9])@', '\1', $argv[1]);
  414 $wantversion = strlen($argv[1]) ? calcversion($argv[1]) : '';
  415 
  416 foreach($progs as $progname => $data)
  417 {
  418   $versions = $data['v'];
  419   ksort($versions);
  420   
  421   $wantpatches = Array();
  422   
  423   $verstabs = Array();
  424   for($c=1; $c<=6; $c++)
  425   {
  426     foreach($versions as $version => $paks)
  427     {
  428       $k = substr($version, 0, $c*5);
  429       $verstabs[$c][$k] = $k;
  430     }
  431     asort($verstabs[$c]);
  432   }
  433 
  434   $lastversio = '';
  435   foreach($versions as $version => $paks)
  436   {
  437     if($lastversio)
  438     {
  439       $did = Array($lastversio => 1);
  440       
  441       $wantpatches[$lastversio][$version] = 1;
  442       
  443       for($c=1; $c<=5; ++$c)
  444       {
  445         $prev = '';
  446         $k = substr($version, 0, $c*5);
  447         
  448         $test = str_pad($k, 5*6, '-');
  449         if($test != $version
  450         && isset($versions[$test])) continue;
  451         foreach($verstabs[$c] as $k2)
  452         {
  453           if($k2 == $k)break;
  454           $prev = $k2;
  455         }
  456         
  457         if($prev)
  458         {
  459           $prev = str_pad($prev, 5*6, '-');
  460           if(isset($versions[$prev]) && !$did[$prev])
  461           {
  462             $did[$prev] = 1;
  463             
  464             // Extra
  465             $wantpatches[$prev][$version] = 1;
  466           }
  467         }
  468       }
  469     }
  470     $lastversio = $version;
  471   }
  472   
  473   foreach($wantpatches as $version1 => $v2tab)
  474   {
  475     foreach($v2tab as $version2 => $dummy)
  476     {
  477       if(strlen($wantversion))
  478       {
  479         if($version2 != $wantversion)
  480         {
  481           continue;
  482         }
  483       }
  484       else if(isset($progs[$progname]['p'][$version1][$version2]))
  485       {
  486         continue;
  487       }
  488       
  489       MakePatch($progname, $version1, $version2,
  490                 $versions[$version1],
  491                 $versions[$version2]);
  492     }
  493   }
  494 }
  495 print_r($thisdir);
  496 CloseAll();
  497 UnGoTmp();