From 2063745c6b1968da55b28a0a47e0958115e84777 Mon Sep 17 00:00:00 2001 From: Alan Date: Tue, 18 Jan 2022 15:57:41 +0800 Subject: [PATCH] use md5sum on ini files to determine if they need re-writing --- HTML/FlexyFramework.php | 30 ++++++++++++--------- HTML/FlexyFramework/Generator.php | 43 ++++++++++++++++++++---------- HTML/FlexyFramework2.php | 15 +++++++---- HTML/FlexyFramework2/Generator.php | 41 ++++++++++++++++++---------- 4 files changed, 83 insertions(+), 46 deletions(-) diff --git a/HTML/FlexyFramework.php b/HTML/FlexyFramework.php index 1f7dbf6d..b9297b7a 100755 --- a/HTML/FlexyFramework.php +++ b/HTML/FlexyFramework.php @@ -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; diff --git a/HTML/FlexyFramework/Generator.php b/HTML/FlexyFramework/Generator.php index bda57c25..2f8280b1 100644 --- a/HTML/FlexyFramework/Generator.php +++ b/HTML/FlexyFramework/Generator.php @@ -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 '
';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);
diff --git a/HTML/FlexyFramework2.php b/HTML/FlexyFramework2.php
index 3676733b..5b67c951 100644
--- a/HTML/FlexyFramework2.php
+++ b/HTML/FlexyFramework2.php
@@ -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;
diff --git a/HTML/FlexyFramework2/Generator.php b/HTML/FlexyFramework2/Generator.php
index b13967eb..f864f451 100644
--- a/HTML/FlexyFramework2/Generator.php
+++ b/HTML/FlexyFramework2/Generator.php
@@ -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 '
';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);
-- 
2.39.2