3333 * along with this program. If not, see <http://www.gnu.org/licenses/>
3434 *
3535 */
36+
3637namespace OC \Files \Storage \Wrapper ;
3738
3839use OC \Encryption \Exceptions \ModuleDoesNotExistsException ;
4142use OC \Files \Cache \CacheEntry ;
4243use OC \Files \Filesystem ;
4344use OC \Files \Mount \Manager ;
45+ use OC \Files \ObjectStore \ObjectStoreStorage ;
4446use OC \Files \Storage \LocalTempFileTrait ;
4547use OC \Memcache \ArrayCache ;
4648use OCP \Encryption \Exceptions \GenericEncryptionException ;
@@ -139,28 +141,36 @@ public function filesize($path) {
139141 $ size = $ this ->unencryptedSize [$ fullPath ];
140142 // update file cache
141143 if ($ info instanceof ICacheEntry) {
142- $ info = $ info ->getData ();
143144 $ info ['encrypted ' ] = $ info ['encryptedVersion ' ];
144145 } else {
145146 if (!is_array ($ info )) {
146147 $ info = [];
147148 }
148149 $ info ['encrypted ' ] = true ;
150+ $ info = new CacheEntry ($ info );
149151 }
150152
151- $ info ['size ' ] = $ size ;
152- $ this ->getCache ()->put ($ path , $ info );
153+ if ($ size !== $ info ->getUnencryptedSize ()) {
154+ $ this ->getCache ()->update ($ info ->getId (), [
155+ 'unencrypted_size ' => $ size
156+ ]);
157+ }
153158
154159 return $ size ;
155160 }
156161
157162 if (isset ($ info ['fileid ' ]) && $ info ['encrypted ' ]) {
158- return $ this ->verifyUnencryptedSize ($ path , $ info[ ' size ' ] );
163+ return $ this ->verifyUnencryptedSize ($ path , $ info-> getUnencryptedSize () );
159164 }
160165
161166 return $ this ->storage ->filesize ($ path );
162167 }
163168
169+ /**
170+ * @param string $path
171+ * @param array $data
172+ * @return array
173+ */
164174 private function modifyMetaData (string $ path , array $ data ): array {
165175 $ fullPath = $ this ->getFullPath ($ path );
166176 $ info = $ this ->getCache ()->get ($ path );
@@ -170,7 +180,7 @@ private function modifyMetaData(string $path, array $data): array {
170180 $ data ['size ' ] = $ this ->unencryptedSize [$ fullPath ];
171181 } else {
172182 if (isset ($ info ['fileid ' ]) && $ info ['encrypted ' ]) {
173- $ data ['size ' ] = $ this ->verifyUnencryptedSize ($ path , $ info[ ' size ' ] );
183+ $ data ['size ' ] = $ this ->verifyUnencryptedSize ($ path , $ info-> getUnencryptedSize () );
174184 $ data ['encrypted ' ] = true ;
175185 }
176186 }
@@ -478,7 +488,7 @@ public function fopen($path, $mode) {
478488 *
479489 * @return int unencrypted size
480490 */
481- protected function verifyUnencryptedSize ($ path , $ unencryptedSize ) {
491+ protected function verifyUnencryptedSize (string $ path , int $ unencryptedSize ): int {
482492 $ size = $ this ->storage ->filesize ($ path );
483493 $ result = $ unencryptedSize ;
484494
@@ -510,7 +520,7 @@ protected function verifyUnencryptedSize($path, $unencryptedSize) {
510520 *
511521 * @return int calculated unencrypted size
512522 */
513- protected function fixUnencryptedSize ($ path , $ size , $ unencryptedSize ) {
523+ protected function fixUnencryptedSize (string $ path , int $ size , int $ unencryptedSize ): int {
514524 $ headerSize = $ this ->getHeaderSize ($ path );
515525 $ header = $ this ->getHeader ($ path );
516526 $ encryptionModule = $ this ->getEncryptionModule ($ path );
@@ -581,7 +591,9 @@ protected function fixUnencryptedSize($path, $size, $unencryptedSize) {
581591 $ cache = $ this ->storage ->getCache ();
582592 if ($ cache ) {
583593 $ entry = $ cache ->get ($ path );
584- $ cache ->update ($ entry ['fileid ' ], ['size ' => $ newUnencryptedSize ]);
594+ $ cache ->update ($ entry ['fileid ' ], [
595+ 'unencrypted_size ' => $ newUnencryptedSize
596+ ]);
585597 }
586598
587599 return $ newUnencryptedSize ;
@@ -621,7 +633,12 @@ private function fread_block($handle, int $blockSize): string {
621633 * @param bool $preserveMtime
622634 * @return bool
623635 */
624- public function moveFromStorage (Storage \IStorage $ sourceStorage , $ sourceInternalPath , $ targetInternalPath , $ preserveMtime = true ) {
636+ public function moveFromStorage (
637+ Storage \IStorage $ sourceStorage ,
638+ $ sourceInternalPath ,
639+ $ targetInternalPath ,
640+ $ preserveMtime = true
641+ ) {
625642 if ($ sourceStorage === $ this ) {
626643 return $ this ->rename ($ sourceInternalPath , $ targetInternalPath );
627644 }
@@ -656,7 +673,13 @@ public function moveFromStorage(Storage\IStorage $sourceStorage, $sourceInternal
656673 * @param bool $isRename
657674 * @return bool
658675 */
659- public function copyFromStorage (Storage \IStorage $ sourceStorage , $ sourceInternalPath , $ targetInternalPath , $ preserveMtime = false , $ isRename = false ) {
676+ public function copyFromStorage (
677+ Storage \IStorage $ sourceStorage ,
678+ $ sourceInternalPath ,
679+ $ targetInternalPath ,
680+ $ preserveMtime = false ,
681+ $ isRename = false
682+ ) {
660683
661684 // TODO clean this up once the underlying moveFromStorage in OC\Files\Storage\Wrapper\Common is fixed:
662685 // - call $this->storage->copyFromStorage() instead of $this->copyBetweenStorage
@@ -676,7 +699,13 @@ public function copyFromStorage(Storage\IStorage $sourceStorage, $sourceInternal
676699 * @param bool $isRename
677700 * @param bool $keepEncryptionVersion
678701 */
679- private function updateEncryptedVersion (Storage \IStorage $ sourceStorage , $ sourceInternalPath , $ targetInternalPath , $ isRename , $ keepEncryptionVersion ) {
702+ private function updateEncryptedVersion (
703+ Storage \IStorage $ sourceStorage ,
704+ $ sourceInternalPath ,
705+ $ targetInternalPath ,
706+ $ isRename ,
707+ $ keepEncryptionVersion
708+ ) {
680709 $ isEncrypted = $ this ->encryptionManager ->isEnabled () && $ this ->shouldEncrypt ($ targetInternalPath );
681710 $ cacheInformation = [
682711 'encrypted ' => $ isEncrypted ,
@@ -725,7 +754,13 @@ private function updateEncryptedVersion(Storage\IStorage $sourceStorage, $source
725754 * @return bool
726755 * @throws \Exception
727756 */
728- private function copyBetweenStorage (Storage \IStorage $ sourceStorage , $ sourceInternalPath , $ targetInternalPath , $ preserveMtime , $ isRename ) {
757+ private function copyBetweenStorage (
758+ Storage \IStorage $ sourceStorage ,
759+ $ sourceInternalPath ,
760+ $ targetInternalPath ,
761+ $ preserveMtime ,
762+ $ isRename
763+ ) {
729764
730765 // for versions we have nothing to do, because versions should always use the
731766 // key from the original file. Just create a 1:1 copy and done
@@ -743,7 +778,7 @@ private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInte
743778 if (isset ($ info ['encrypted ' ]) && $ info ['encrypted ' ] === true ) {
744779 $ this ->updateUnencryptedSize (
745780 $ this ->getFullPath ($ targetInternalPath ),
746- $ info[ ' size ' ]
781+ $ info-> getUnencryptedSize ()
747782 );
748783 }
749784 $ this ->updateEncryptedVersion ($ sourceStorage , $ sourceInternalPath , $ targetInternalPath , $ isRename , true );
@@ -808,13 +843,6 @@ private function copyBetweenStorage(Storage\IStorage $sourceStorage, $sourceInte
808843 return (bool )$ result ;
809844 }
810845
811- /**
812- * get the path to a local version of the file.
813- * The local version of the file can be temporary and doesn't have to be persistent across requests
814- *
815- * @param string $path
816- * @return string
817- */
818846 public function getLocalFile ($ path ) {
819847 if ($ this ->encryptionManager ->isEnabled ()) {
820848 $ cachedFile = $ this ->getCachedFile ($ path );
@@ -825,42 +853,25 @@ public function getLocalFile($path) {
825853 return $ this ->storage ->getLocalFile ($ path );
826854 }
827855
828- /**
829- * Returns the wrapped storage's value for isLocal()
830- *
831- * @return bool wrapped storage's isLocal() value
832- */
833856 public function isLocal () {
834857 if ($ this ->encryptionManager ->isEnabled ()) {
835858 return false ;
836859 }
837860 return $ this ->storage ->isLocal ();
838861 }
839862
840- /**
841- * see https://www.php.net/manual/en/function.stat.php
842- * only the following keys are required in the result: size and mtime
843- *
844- * @param string $path
845- * @return array
846- */
847863 public function stat ($ path ) {
848864 $ stat = $ this ->storage ->stat ($ path );
865+ if (!$ stat ) {
866+ return false ;
867+ }
849868 $ fileSize = $ this ->filesize ($ path );
850869 $ stat ['size ' ] = $ fileSize ;
851870 $ stat [7 ] = $ fileSize ;
852871 $ stat ['hasHeader ' ] = $ this ->getHeaderSize ($ path ) > 0 ;
853872 return $ stat ;
854873 }
855874
856- /**
857- * see https://www.php.net/manual/en/function.hash.php
858- *
859- * @param string $type
860- * @param string $path
861- * @param bool $raw
862- * @return string
863- */
864875 public function hash ($ type , $ path , $ raw = false ) {
865876 $ fh = $ this ->fopen ($ path , 'rb ' );
866877 $ ctx = hash_init ($ type );
@@ -1068,6 +1079,13 @@ public function writeStream(string $path, $stream, int $size = null): int {
10681079 [$ count , $ result ] = \OC_Helper::streamCopy ($ stream , $ target );
10691080 fclose ($ stream );
10701081 fclose ($ target );
1082+
1083+ // object store, stores the size after write and doesn't update this during scan
1084+ // manually store the unencrypted size
1085+ if ($ result && $ this ->getWrapperStorage ()->instanceOfStorage (ObjectStoreStorage::class)) {
1086+ $ this ->getCache ()->put ($ path , ['unencrypted_size ' => $ count ]);
1087+ }
1088+
10711089 return $ count ;
10721090 }
10731091}
0 commit comments