Skip to content

Commit 266dfbc

Browse files
authored
Merge pull request #58970 from nextcloud/fix/sftp-mtime
fix: manually set modified time to SFTP files after editing
2 parents 820e091 + 0cd74df commit 266dfbc

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

apps/files_external/lib/Lib/Storage/SFTP.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function fopen(string $path, string $mode) {
333333
$fh = fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
334334
if ($fh) {
335335
$fh = CallbackWrapper::wrap($fh, null, null, function () use ($path): void {
336-
$this->knownMTimes->set($path, time());
336+
$this->touch($path, time());
337337
});
338338
}
339339
return $fh;
@@ -357,18 +357,17 @@ public function fopen(string $path, string $mode) {
357357
}
358358

359359
public function touch(string $path, ?int $mtime = null): bool {
360-
try {
360+
361+
$result = $this->getConnection()->touch($this->absPath($path), $mtime, $mtime);
362+
363+
if ($result) {
364+
$this->getConnection()->clearStatCache();
361365
if (!is_null($mtime)) {
362-
return false;
363-
}
364-
if (!$this->file_exists($path)) {
365-
return $this->getConnection()->put($this->absPath($path), '');
366-
} else {
367-
return false;
366+
$this->knownMTimes->set($path, $mtime);
368367
}
369-
} catch (\Exception $e) {
370-
return false;
371368
}
369+
370+
return $result;
372371
}
373372

374373
/**
@@ -429,6 +428,9 @@ public function constructUrl(string $path): string {
429428
public function file_put_contents(string $path, mixed $data): int|float|false {
430429
/** @psalm-suppress InternalMethod */
431430
$result = $this->getConnection()->put($this->absPath($path), $data);
431+
432+
$this->touch($path, time());
433+
432434
if ($result) {
433435
return strlen($data);
434436
} else {
@@ -448,6 +450,9 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
448450
/** @psalm-suppress InternalMethod */
449451
$result = $this->getConnection()->put($this->absPath($path), $stream);
450452
fclose($stream);
453+
454+
$this->touch($path, time());
455+
451456
if ($result) {
452457
if ($size === null) {
453458
throw new \Exception('Failed to get written size from sftp storage wrapper');

apps/files_external/lib/Lib/Storage/SFTPWriteStream.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SFTPWriteStream implements File {
3232

3333
private $buffer = '';
3434

35+
private string $path;
36+
3537
public static function register($protocol = 'sftpwrite') {
3638
if (in_array($protocol, stream_get_wrappers(), true)) {
3739
return false;
@@ -71,6 +73,8 @@ public function stream_open($path, $mode, $options, &$opened_path) {
7173
}
7274

7375
$remote_file = $this->sftp->_realpath($path);
76+
77+
$this->path = $remote_file;
7478
if ($remote_file === false) {
7579
return false;
7680
}
@@ -160,6 +164,8 @@ public function stream_close() {
160164
if (!$this->sftp->_close_handle($this->handle)) {
161165
return false;
162166
}
167+
$this->sftp->touch($this->path, time(), time());
168+
163169
return true;
164170
}
165171
}

build/psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,6 @@
13591359
<InternalMethod>
13601360
<code><![CDATA[getLocalFile]]></code>
13611361
<code><![CDATA[new View('/' . $userId . '/files_external')]]></code>
1362-
<code><![CDATA[put]]></code>
13631362
</InternalMethod>
13641363
</file>
13651364
<file src="apps/files_external/lib/Lib/Storage/SMB.php">

0 commit comments

Comments
 (0)