use md5sum on ini files to determine if they need re-writing
authorAlan <alan@roojs.com>
Tue, 18 Jan 2022 07:57:41 +0000 (15:57 +0800)
committerAlan <alan@roojs.com>
Tue, 18 Jan 2022 07:57:41 +0000 (15:57 +0800)
HTML/FlexyFramework.php
HTML/FlexyFramework/Generator.php
HTML/FlexyFramework2.php
HTML/FlexyFramework2/Generator.php

index 1f7dbf6..b9297b7 100755 (executable)
@@ -504,7 +504,13 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
     function _configDataObjectsCache()
     {
         // cli works under different users... it may cause problems..
+        
         $this->debug(__METHOD__);
+        
+        if ($this->database === false) {
+            return;
+        }
+        
         if (function_exists('posix_getpwuid')) {
             $uinfo = posix_getpwuid( posix_getuid () ); 
             $user = $uinfo['name'];
@@ -515,7 +521,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
 
         $iniCache = ini_get('session.save_path') .'/' . 
-               'dbcfg-' . $user . '/'. str_replace('/', '_', $this->project) ;
+               'dbcfg-' . $user . '/'. str_replace('/', '_', $this->project);
         
         
         if ($this->appNameShort) {
@@ -524,9 +530,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         if ($this->version) {
             $iniCache .= '.' . $this->version;
         }
-        if ($this->database === false) {
-            return;
-        }
+       
         
         $dburl = parse_url($this->database);
         if (!empty($dburl['path'])) {
@@ -544,10 +548,8 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
             $this->dataObjectsOriginalIni = $this->DB_DataObject[$dbini];
             ///print_r($this->DB_DataObject);exit;
         }
-        // 
-        
-        
-        
+          
+         
         $this->DB_DataObject[$dbini] =   $iniCache;
         // we now have the configuration file name..
         
@@ -584,13 +586,15 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         
         $iniCache = $this->DB_DataObject[$dbini];
         $this->debug('generateDataobjectsCache:' .dirname($iniCache).'/*.ini');
-        if ($force && file_exists($iniCache)) {
-            
+        
+        $replace = array();
+        
+        if (file_exists($iniCache)) {
             $files = glob(dirname($iniCache).'/*.ini');
             foreach($files as $f) {
-                unlink($f);
+                $replace[$f] = md5(file_get_contents($f)); // hash it..
+               
             }
-            clearstatcache();
         }
         $this->debug('generateDataobjectsCache: DONE ini delete');
         
@@ -633,7 +637,7 @@ RewriteRule ^(.+)$ /web.hpasite/index.local.php [L,NC,E=URL:$1]
         $generator->start();
         $this->debug('generateDataobjectsCache: done generator');
 
-        HTML_FlexyFramework_Generator::writeCache($iniCacheTmp, $iniCache); 
+        HTML_FlexyFramework_Generator::writeCache($iniCacheTmp, $iniCache, $replace); 
         // reset the cache to the correct lcoation.
         $this->DB_DataObject[$dbini] = $iniCache;
         
index bda57c2..2f8280b 100644 (file)
@@ -168,9 +168,10 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
     }
     /**
      * Generate the cached *.ini and links.ini files (merged for all components)
-     * 
+     * @param string $iniCacheTmp a temporary file
+     * @param string $iniCache the real target.
      */
-    static function writeCache($iniCacheTmp, $iniCache)
+    static function writeCache($iniCacheTmp, $iniCache, $replace = array())
     {
         
         $fp = fopen($iniCache.".lock", "a+");
@@ -182,14 +183,22 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
         //var_dump($iniCacheTmp);
        // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
         // only unpdate if nothing went wrong.
+        clearstatcace();
         if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
-            if (file_exists($iniCache)) {
-                unlink($iniCache);
+            // is the replace file exist?
+            if (!isset($replace[$iniCache]) || $replace[$iniCache] != md5_file($iniCacheTmp)) {
+            
+            
+                if (file_exists($iniCache)) {
+                    unlink($iniCache);
+                }
+                $ff->debug("Writing merged ini file : $iniCache\n");
+                rename($iniCacheTmp, $iniCache);
+            } else {
+                touch($iniCache);
             }
-            $ff->debug("Writing merged ini file : $iniCache\n");
-            rename($iniCacheTmp, $iniCache);
         }
-        
+        /*
         // readers..??? not needed??? (historical)
         if (file_exists($iniCacheTmp.'.reader') &&  filesize($iniCacheTmp.'.reader')) {
             if (file_exists($iniCache.'.reader')) {
@@ -198,7 +207,7 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
             $ff->debug("Writing merged reader file : $iniCache.reader\n");
             rename($iniCacheTmp.'.reader', $iniCache.'.reader');
         }
-        
+        */
         
         // merge and set links.. test for generated links file..
         
@@ -259,13 +268,19 @@ class HTML_FlexyFramework_Generator extends DB_DataObject_Generator
         }
         if (count($out)) {
             $ff->debug("Writing merged Links file : $iniLinksCache \n");
-
-           
-            file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
-            if (file_exists($iniLinksCache)) {                
-                unlink($iniLinksCache);
+            $out_str = implode("\n", $out);
+            // is target file different?
+            if (!isset($replace[$iniLinksCache]) || $replace[$iniLinksCache] != md5($out)) {
+          
+                 file_put_contents($iniCacheTmp. '.links.ini', $out_str);
+                 if (file_exists($iniLinksCache)) {                
+                     unlink($iniLinksCache);
+                 }
+                 rename($iniCacheTmp. '.links.ini', $iniLinksCache);
+            } else {
+                touch($iniLinksCache);
+                
             }
-            rename($iniCacheTmp. '.links.ini', $iniLinksCache);
         }
         
         flock($fp, LOCK_UN);
index 3676733..5b67c95 100644 (file)
@@ -524,14 +524,19 @@ class HTML_FlexyFramework2 {
        
         $iniCache = $this->PDO_DataObject['schema_location'];
         
-        if ($force && file_exists($iniCache)) {
+        
+        $replace = array();
+        
+        if (file_exists($iniCache)) {
             $files = glob(dirname($iniCache).'/*.ini');
             foreach($files as $f) {
-                unlink($f);
-            } 
-            clearstatcache();
+                $replace[$f] = md5(file_get_contents($f)); // hash it..
+               
+            }
         }
         
+      
+        
         $iniCacheTmp = $iniCache . '.tmp' .md5(rand());  // random to stop two processes using the same file.
         // has it expired..
         $force = ($force ? $force : !file_exists($iniCache)) || !$this->dataObjectsCacheExpires;
@@ -572,7 +577,7 @@ class HTML_FlexyFramework2 {
         $generator->start();
         
         $this->debug('generateDataobjectsCache', 'dataObjectsCache  writing');
-        HTML_FlexyFramework2_Generator::writeCache($iniCacheTmp, $iniCache); 
+        HTML_FlexyFramework2_Generator::writeCache($iniCacheTmp, $iniCache, $replace); 
         // reset the cache to the correct lcoation.
         PDO_DataObject::config('schema_location',  $iniCache);
         $this->PDO_DataObject['schema_location'] = $iniCache;
index b13967e..f864f45 100644 (file)
@@ -179,7 +179,7 @@ class HTML_FlexyFramework2_Generator extends PDO_DataObject_Generator
      * Generate the cached *.ini and links.ini files (merged for all components)
      * 
      */
-    static function writeCache($iniCacheTmp, $iniCache)
+    static function writeCache($iniCacheTmp, $iniCache, $replace = array())
     {
         
         $fp = fopen($iniCache.".lock", "a+");
@@ -192,13 +192,20 @@ class HTML_FlexyFramework2_Generator extends PDO_DataObject_Generator
        // echo '<PRE>';echo file_get_contents($iniCacheTmp);exit;
         // only unpdate if nothing went wrong.
         if (file_exists($iniCacheTmp) && filesize($iniCacheTmp)) {
-            if (file_exists($iniCache)) {
-                unlink($iniCache);
+            // is the replace file exist?
+            if (!isset($replace[$iniCache]) || $replace[$iniCache] != md5_file($iniCacheTmp)) {
+            
+            
+                if (file_exists($iniCache)) {
+                    unlink($iniCache);
+                }
+                $ff->debug("Writing merged ini file : $iniCache\n");
+                rename($iniCacheTmp, $iniCache);
+            } else {
+                touch($iniCache);
             }
-            $ff->debug("Writing merged ini file : $iniCache\n");
-            rename($iniCacheTmp, $iniCache);
         }
-        
+        /* 
         // readers..??? not needed??? (historical)
         if (file_exists($iniCacheTmp.'.reader') &&  filesize($iniCacheTmp.'.reader')) {
             if (file_exists($iniCache.'.reader')) {
@@ -207,7 +214,7 @@ class HTML_FlexyFramework2_Generator extends PDO_DataObject_Generator
             $ff->debug("Writing merged reader file : $iniCache.reader\n");
             rename($iniCacheTmp.'.reader', $iniCache.'.reader');
         }
-        
+        */
         
         // merge and set links.. test for generated links file..
         
@@ -268,14 +275,20 @@ class HTML_FlexyFramework2_Generator extends PDO_DataObject_Generator
         }
         if (count($out)) {
             $ff->debug("Writing merged Links file : $iniLinksCache \n");
-
-           
-            file_put_contents($iniCacheTmp. '.links.ini', implode("\n", $out));
-            if (file_exists($iniLinksCache)) {                
-                unlink($iniLinksCache);
+            $out_str = implode("\n", $out);
+            // is target file different?
+            if (!isset($replace[$iniLinksCache]) || $replace[$iniLinksCache] != md5($out)) {
+          
+                 file_put_contents($iniCacheTmp. '.links.ini', $out_str);
+                 if (file_exists($iniLinksCache)) {                
+                     unlink($iniLinksCache);
+                 }
+                 rename($iniCacheTmp. '.links.ini', $iniLinksCache);
+            } else {
+                touch($iniLinksCache);
+                
             }
-            rename($iniCacheTmp. '.links.ini', $iniLinksCache);
-        }
+        } // we ignore that we might need to delete old links.ini
         
         flock($fp, LOCK_UN);
         fclose($fp);