Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function touch($mtime = null) {
if (is_null($mtime)) {
$mtime = time();
}
$this->fileInfo['mtime'] = $mtime;
$this->fileInfo['mtime'] = $mtime; // XXX This isn't normalized like it is at the View::touch() level
}
} else {
throw new NotPermittedException();
Expand Down
37 changes: 31 additions & 6 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,29 +515,31 @@ public function filemtime($path) {
* @param int|string $mtime
*/
public function touch($path, $mtime = null): bool {
if (!is_null($mtime) && !is_numeric($mtime)) {
$mtime = strtotime($mtime);
}
$mtime = $this->normalizeMTime($mtime);

$hooks = ['touch'];

if (!$this->file_exists($path)) {
$hooks[] = 'create';
$hooks[] = 'write';
}

try {
$result = $this->basicOperation('touch', $path, $hooks, $mtime);
} catch (\Exception $e) {
$this->logger->info('Error while setting modified time', ['app' => 'core', 'exception' => $e]);
$this->logger->info(
'Error while setting modified time',
[ 'app' => 'core', 'exception' => $e, 'path' => $path, 'mtime' => $mtime ]
);
$result = false;
}

if (!$result) {
// If create file fails because of permissions on external storage like SMB folders,
// check file exists and return false if not.
if (!$this->file_exists($path)) {
return false;
}
if (is_null($mtime)) {
if ($mtime === null) {
$mtime = time();
}
//if native touch fails, we emulate it by changing the mtime in the cache
Expand All @@ -546,6 +548,29 @@ public function touch($path, $mtime = null): bool {
return true;
}

private function normalizeMTime($mtime): int|null {
if ($mtime === null) {
return null;
}

if (!is_numeric($mtime)) {
$timestamp = strtotime($mtime);
if ($timestamp === false || $timestamp < 0) {
// Treat unparseable or negative timestamps as "now"
return null;
}
return $timestamp;
}

// Prevent implicit conversion warnings if a float
$intMtime = (int)$mtime;
if ($intMtime < 0) {
// Treat negative mtime as "now"
return null;
}
return $intMtime;
}

/**
* @param string $path
* @return string|false
Expand Down
Loading