From 85853f9ec29be9a2ba92737de204da1469f72dd8 Mon Sep 17 00:00:00 2001
From: Klaas Freitag <freitag@owncloud.com>
Date: Fri, 10 Feb 2012 11:30:38 +0100
Subject: [PATCH] - Added the ability to change a files mtime via webdavs
 propset. - Added some minor debug help to fscache

---
 lib/connector/sabre/node.php | 30 +++++++++++++++++++++++-------
 lib/filecache.php            | 11 +++++++----
 lib/filestorage/local.php    |  7 +++++++
 lib/filesystem.php           |  3 +++
 lib/filesystemview.php       |  3 +++
 5 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/lib/connector/sabre/node.php b/lib/connector/sabre/node.php
index ace572a1ee..b8b675c120 100644
--- a/lib/connector/sabre/node.php
+++ b/lib/connector/sabre/node.php
@@ -92,6 +92,19 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr
 
 	}
 
+	/** 
+	 *  sets the last modification time of the file (mtime) to the value given 
+	 *  in the second parameter or to now if the second param is empty.
+	 *  Even if the modification time is set to a custom value the access time is set to now.
+	 */
+	public function setLastModifiedTime($mtime) {
+		OC_Filesystem::setFileMtime($this->path, $mtime);
+	}
+
+	public function endsWith( $str, $sub ) {
+		return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
+	}
+
 	/**
 	 * Updates properties on this node,
 	 *
@@ -110,13 +123,16 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr
 				}
 			}
 			else {
-				if(!array_key_exists( $propertyName, $existing )){
-					$query = OC_DB::prepare( 'INSERT INTO *PREFIX*properties (userid,propertypath,propertyname,propertyvalue) VALUES(?,?,?,?)' );
-					$query->execute( array( OC_User::getUser(), $this->path, $propertyName,$propertyValue ));
-				}
-				else{
-					$query = OC_DB::prepare( 'UPDATE *PREFIX*properties SET propertyvalue = ? WHERE userid = ? AND propertypath = ? AND propertyname = ?' );
-					$query->execute( array( $propertyValue,OC_User::getUser(), $this->path, $propertyName ));
+				if(  $this->endsWith( $propertyName, "modificationTime")) {
+					$this->setLastModifiedTime($propertyValue);
+				} else {
+					if(!array_key_exists( $propertyName, $existing )){
+						$query = OC_DB::prepare( 'INSERT INTO *PREFIX*properties (userid,propertypath,propertyname,propertyvalue) VALUES(?,?,?,?)' );
+						$query->execute( array( OC_User::getUser(), $this->path, $propertyName,$propertyValue ));
+					} else {
+						$query = OC_DB::prepare( 'UPDATE *PREFIX*properties SET propertyvalue = ? WHERE userid = ? AND propertypath = ? AND propertyname = ?' );
+						$query->execute( array( $propertyValue,OC_User::getUser(), $this->path, $propertyName ));
+					}
 				}
 			}
 
diff --git a/lib/filecache.php b/lib/filecache.php
index cb516223f6..921d4a2790 100644
--- a/lib/filecache.php
+++ b/lib/filecache.php
@@ -62,7 +62,7 @@ class OC_FileCache{
 		if(is_array($result)){
 			return $result;
 		}else{
-			OC_Log::write('file not found in cache ('.$path.')','core',OC_Log::DEBUG);
+			OC_Log::write('get(): file not found in cache ('.$path.')','core',OC_Log::DEBUG);
 			return false;
 		}
 	}
@@ -125,7 +125,9 @@ class OC_FileCache{
 			$queryParts[]='mimepart=?';
 		}
 		$arguments[]=$id;
-		$query=OC_DB::prepare('UPDATE *PREFIX*fscache SET '.implode(' , ',$queryParts).' WHERE id=?');
+		
+		$sql = 'UPDATE *PREFIX*fscache SET '.implode(' , ',$queryParts).' WHERE id=?';
+		$query=OC_DB::prepare($sql);
 		$query->execute($arguments);
 	}
 
@@ -231,7 +233,7 @@ class OC_FileCache{
 		if(is_array($result)){
 			return $result;
 		}else{
-			OC_Log::write('file not found in cache ('.$path.')','core',OC_Log::DEBUG);
+			OC_Log::write('getFolderContent(): file not found in cache ('.$path.')','core',OC_Log::DEBUG);
 			return false;
 		}
 	}
@@ -264,7 +266,7 @@ class OC_FileCache{
 		if(is_array($result)){
 			return $result['id'];
 		}else{
-			OC_Log::write('file not found in cache ('.$path.')','core',OC_Log::DEBUG);
+			OC_Log::write('getFieldId(): file not found in cache ('.$path.')','core',OC_Log::DEBUG);
 			return -1;
 		}
 	}
@@ -293,6 +295,7 @@ class OC_FileCache{
 		}else{
 			$view=new OC_FilesystemView(($root=='/')?'':$root);
 		}
+		
 		$path=$params['path'];
 		$fullPath=$view->getRoot().$path;
 		$mimetype=$view->getMimeType($path);
diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php
index 2c1f650cb9..292d2a84e7 100644
--- a/lib/filestorage/local.php
+++ b/lib/filestorage/local.php
@@ -65,6 +65,13 @@ class OC_Filestorage_Local extends OC_Filestorage{
 	public function filemtime($path){
 		return filemtime($this->datadir.$path);
 	}
+	
+	public function setFileMtime($path, $mtime){
+                   // sets the modification time of the file to the given value. If mtime is nil the current time is set.
+                  // note that the access time of the file always changes to the current time.
+                  return touch($this->datadir.$path, $mtime);
+         }
+
 	public function file_get_contents($path){
 		return file_get_contents($this->datadir.$path);
 	}
diff --git a/lib/filesystem.php b/lib/filesystem.php
index a18072ecbc..75997c244f 100644
--- a/lib/filesystem.php
+++ b/lib/filesystem.php
@@ -345,6 +345,9 @@ class OC_Filesystem{
 	static public function filemtime($path){
 		return self::$defaultInstance->filemtime($path);
 	}
+	static public function setFileMtime($path, $mtime){
+		return self::$defaultInstance->setFileMtime($path, $mtime);
+	}
 	static public function file_get_contents($path){
 		return self::$defaultInstance->file_get_contents($path);
 	}
diff --git a/lib/filesystemview.php b/lib/filesystemview.php
index 4586507a81..0f1c546f4c 100644
--- a/lib/filesystemview.php
+++ b/lib/filesystemview.php
@@ -156,6 +156,9 @@ class OC_FilesystemView {
 	public function filemtime($path){
 		return $this->basicOperation('filemtime',$path);
 	}
+	public function setFileMtime($path, $mtime){
+		return $this->basicOperation('setFileMtime',$path, array('write'), $mtime);
+	}
 	public function file_get_contents($path){
 		return $this->basicOperation('file_get_contents',$path,array('read'));
 	}
-- 
GitLab