Updated Caching Framework to be in sync with FLOW3 revision 2953
authorIngo Renner <ingo.renner@typo3.org>
Mon, 3 Aug 2009 09:09:29 +0000 (09:09 +0000)
committerIngo Renner <ingo.renner@typo3.org>
Mon, 3 Aug 2009 09:09:29 +0000 (09:09 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@5753 709f56b5-9817-0410-a4d7-c38de5d9e867

t3lib/cache/backend/class.t3lib_cache_backend_filebackend.php
t3lib/cache/last_synched_revision
t3lib/class.t3lib_div.php

index a53a560..94f2e1d 100644 (file)
@@ -47,15 +47,24 @@ class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBacken
        protected $root = '/';
 
        /**
+        * Maximum allowed file path length in the current environment.
+        * Will be set in initializeObject()
+        *
+        * @var integer
+        */
+       protected $maximumPathLength = null;
+
+       /**
         * Constructs this backend
         *
         * @param mixed Configuration options - depends on the actual backend
         */
        public function __construct(array $options = array()) {
                parent::__construct($options);
-       if (TYPO3_OS === 'WIN') {
-               $this->root = '';
-       }
+               if (TYPO3_OS === 'WIN') {
+                       $this->root = '';
+               }
+
                if (empty($this->cacheDirectory)) {
                        $cacheDirectory = 'typo3temp/cache/';
                        try {
@@ -64,6 +73,10 @@ class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBacken
 
                        }
                }
+
+               if (is_null($this->maximumPathLength)) {
+                       $this->maximumPathLength = t3lib_div::getMaximumPathLength();
+               }
        }
 
        /**
@@ -140,7 +153,7 @@ class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBacken
         * @param array Tags to associate with this cache entry
         * @param integer Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
         * @return void
-        * @throws t3lib_cache_Exception if the directory does not exist or is not writable, or if no cache frontend has been set.
+        * @throws t3lib_cache_Exception if the directory does not exist or is not writable or exceeds the maximum allowed path length, or if no cache frontend has been set.
         * @throws t3lib_cache_exception_InvalidData if the data to bes stored is not a string.
         * @author Robert Lemke <robert@typo3.org>
         */
@@ -182,17 +195,24 @@ class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBacken
                $this->remove($entryIdentifier);
 
                $data = $expirytime->format(self::EXPIRYTIME_FORMAT) . $data;
-               $temporaryFilename = uniqid() . '.temp';
-               $result = file_put_contents($cacheEntryPath . $temporaryFilename, $data);
+               $cacheEntryPathAndFilename = $cacheEntryPath . uniqid() . '.temp';
+               if (strlen($cacheEntryPathAndFilename) > $this->maximumPathLength) {
+                       throw new t3lib_cache_Exception(
+                               'The length of the temporary cache file path "' . $cacheEntryPathAndFilename . '" is ' . strlen($cacheEntryPathAndFilename) . ' characters long and exceeds the maximum path length of ' . $this->maximumPathLength . '. Please consider setting the temporaryDirectoryBase option to a shorter path. ',
+                               1248710426
+                       );
+               }
+
+               $result = file_put_contents($cacheEntryPathAndFilename, $data);
                if ($result === FALSE) {
                        throw new t3lib_cache_Exception(
-                               'The temporary cache file "' . $temporaryFilename . '" could not be written.',
+                               'The temporary cache file "' . $cacheEntryPathAndFilename . '" could not be written.',
                                1204026251
                        );
                }
 
                for ($i = 0; $i < 5; $i++) {
-                       $result = rename($cacheEntryPath . $temporaryFilename, $cacheEntryPath . $entryIdentifier);
+                       $result = rename($cacheEntryPathAndFilename, $cacheEntryPath . $entryIdentifier);
                        if ($result === TRUE) {
                                break;
                        }
@@ -206,27 +226,42 @@ class t3lib_cache_backend_FileBackend extends t3lib_cache_backend_AbstractBacken
                }
 
                foreach ($tags as $tag) {
-                       $tagPath = $this->cacheDirectory . 'tags/' . $tag . '/';
+                       $this->setTag($entryIdentifier, $tag);
+               }
+       }
 
+       /**
+        * Creates a tag that is associated with the given cache identifier
+        *
+        * @param string $entryIdentifier An identifier for this specific cache entry
+        * @param string Tag to associate with this cache entry
+        * @return void
+        * @throws t3lib_cache_Exception if the tag path is not writable or exceeds the maximum allowed path length
+        * @author Bastian Waidelich <bastian@typo3.org>
+        * @author Ingo Renner <ingo@typo3.org>
+        */
+       protected function setTag($entryIdentifier, $tag) {
+               $tagPath = $this->cacheDirectory . 'tags/' . $tag . '/';
+
+               if (!is_writable($tagPath)) {
+                       t3lib_div::mkdir_deep($this->root, $tagPath);
                        if (!is_writable($tagPath)) {
-                               t3lib_div::mkdir_deep(
-                                       $this->root,
-                                       $tagPath
+                               throw new t3lib_cache_Exception(
+                                       'The tag directory "' . $tagPath . '" could not be created.',
+                                       1238242144
                                );
-                               if (!is_writable($tagPath)) {
-                                       throw new t3lib_cache_Exception(
-                                               'The tag directory "' . $tagPath . '" could not be created.',
-                                               1238242144
-                                       );
-                               }
                        }
+               }
 
-                       touch($tagPath
-                               . $this->cache->getIdentifier()
-                               . self::SEPARATOR
-                               . $entryIdentifier
+               $tagPathAndFilename = $tagPath . $this->cache->getIdentifier()
+                       . self::SEPARATOR . $entryIdentifier;
+               if (strlen($tagPathAndFilename) > $this->maximumPathLength) {
+                       throw new t3lib_cache_Exception(
+                               'The length of the tag path "' . $tagPathAndFilename . '" is ' . strlen($tagPathAndFilename) . ' characters long and exceeds the maximum path length of ' . $maximumPathLength . '. Please consider setting the temporaryDirectoryBase option to a shorter path. ',
+                               1248710426
                        );
                }
+               touch($tagPathAndFilename);
        }
 
        /**
index 9913077..0537279 100644 (file)
@@ -1 +1 @@
-2616
\ No newline at end of file
+2953
\ No newline at end of file
index e5c04a2..bea4dec 100644 (file)
@@ -3268,6 +3268,31 @@ final class t3lib_div {
                return $bytes;
        }
 
+       /**
+        * Retrieves the maximum path length that is valid in the current environment.
+        *
+        * @return integer The maximum available path length
+        * @author Ingo Renner <ingo@typo3.org>
+        */
+       public static function getMaximumPathLength() {
+               $maximumPathLength = 0;
+
+               if (version_compare(PHP_VERSION, '5.3.0', '<')) {
+                               // rough assumptions
+                       if (TYPO3_OS == 'WIN') {
+                                       // WIN is usually 255, Vista 260, although NTFS can hold about 2k
+                               $maximumPathLength = 255;
+                       } else {
+                               $maximumPathLength = 2048;
+                       }
+               } else {
+                               // precise information is available since PHP 5.3
+                       $maximumPathLength = PHP_MAXPATHLEN;
+               }
+
+               return $maximumPathLength;
+       }
+