Empty files (zero byte files) cannot be copied via Nextcloud web interface: // Sabre\DAV\Exception: Error while copying file to target location (copied bytes: 0, expected filesize: -1 )
The bug relates to Nextcloud server accessed via the Nextcloud web interface. Copying empty files (zero bytes) causes an exception and the file is not copied or moved.
Sabre\DAV\Exception: Error while copying file to target location (copied bytes: 0, expected filesize: -1 )
Steps to reproduce
- Open Nextcloud web interface
- Select an empty file, i.e. a file with size of zero bytes or select a folder in which such a file is located
- Copy the selection to another place
- Wait for "
could not be copied"
Expected behaviour
Even files with a size of zero bytes should be copied.
Actual behaviour
As soon as the file or folder selection marked for copy contains one or more zero byte files (empty files), the copy process stops as soon as the empty file is processed. This behaviour does not occur when having the empty files on a client and using a Nextcloud client for synchronizing the files. In this case, even zero byte files are properly synchronized with the server. The problem is limited to the case where the copy operation is invoked via the web interface. However, moving an empty file via the Nextcloud web interface works. It is only the copy that causes problems.
Server configuration
Nextcloud version: 16.0.4 Operating system and version: Ubuntu 18.04.3 Apache or nginx version: Apache/2.4.29 (Ubuntu) PHP version: PHP 7.2.19-0ubuntu0.18.04.2
The output of your Nextcloud log in Admin > Logging:
Fatal webdav Sabre\DAV\Exception: Error while copying file to target location (copied bytes: 0, expected filesize: -1 )
{"reqId":"8AFy1iaAcUI95PDmI1SZ","level":3,"time":"2019-09-02T11:17:56+00:00","remoteAddr":"<ipaddress-proxyserver>","user":"<nextcloud-user>","app":"no app in context","method":"COPY","url":"/remote.php/dav/files/<nextcloud-user>/<large-folder-to-copy>","message":{"Exception":"Sabre\\DAV\\Exception","Message":"Error while copying file to target location (copied bytes: 0, expected filesize: -1 )","Code":0,"Trace":[{"file":"/var/www/html/nextcloud/apps/dav/lib/Connector/Sabre/Directory.php","line":156,"function":"put","class":"OCA\\DAV\\Connector\\Sabre\\File","type":"->","args":[null]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Tree.php","line":316,"function":"createFile","class":"OCA\\DAV\\Connector\\Sabre\\Directory","type":"->","args":["28 Urlaubstage pro Jahr.txt",null]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Tree.php","line":326,"function":"copyNode","class":"Sabre\\DAV\\Tree","type":"->","args":[{"__class__":"OCA\\DAV\\Connector\\Sabre\\File"},{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"}]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Tree.php","line":326,"function":"copyNode","class":"Sabre\\DAV\\Tree","type":"->","args":[{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"},{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"}]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Tree.php","line":326,"function":"copyNode","class":"Sabre\\DAV\\Tree","type":"->","args":[{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"},{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"}]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Tree.php","line":130,"function":"copyNode","class":"Sabre\\DAV\\Tree","type":"->","args":[{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"},{"__class__":"OCA\\DAV\\Connector\\Sabre\\Directory"},"<large-folder-to-copy>"]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/CorePlugin.php","line":684,"function":"copy","class":"Sabre\\DAV\\Tree","type":"->","args":["files/<nextcloud-user>/<large-folder-to-copy>","files/<nextcloud-user>/<nextcloud-user>/Archiv/<large-folder-to-copy>"]},{"function":"httpCopy","class":"Sabre\\DAV\\CorePlugin","type":"->","args":[{"absoluteUrl":"http://<hostaddress-proxyserver>/remote.php/dav/files/<nextcloud-user>/<large-folder-to-copy>","__class__":"Sabre\\HTTP\\Request"},{"__class__":"Sabre\\HTTP\\Response"}]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/event/lib/EventEmitterTrait.php","line":105,"function":"call_user_func_array","args":[[{"__class__":"Sabre\\DAV\\CorePlugin"},"httpCopy"],[{"absoluteUrl":"http://<hostaddress-proxyserver>/remote.php/dav/files/<nextcloud-user>/<large-folder-to-copy>","__class__":"Sabre\\HTTP\\Request"},{"__class__":"Sabre\\HTTP\\Response"}]]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Server.php","line":479,"function":"emit","class":"Sabre\\Event\\EventEmitter","type":"->","args":["method:COPY",[{"absoluteUrl":"http://<hostaddress-proxyserver>/remote.php/dav/files/<nextcloud-user>/<large-folder-to-copy>","__class__":"Sabre\\HTTP\\Request"},{"__class__":"Sabre\\HTTP\\Response"}]]},{"file":"/var/www/html/nextcloud/3rdparty/sabre/dav/lib/DAV/Server.php","line":254,"function":"invokeMethod","class":"Sabre\\DAV\\Server","type":"->","args":[{"absoluteUrl":"http://<hostaddress-proxyserver>/remote.php/dav/files/<nextcloud-user>/<large-folder-to-copy>","__class__":"Sabre\\HTTP\\Request"},{"__class__":"Sabre\\HTTP\\Response"}]},{"file":"/var/www/html/nextcloud/apps/dav/lib/Server.php","line":316,"function":"exec","class":"Sabre\\DAV\\Server","type":"->","args":[]},{"file":"/var/www/html/nextcloud/apps/dav/appinfo/v2/remote.php","line":35,"function":"exec","class":"OCA\\DAV\\Server","type":"->","args":[]},{"file":"/var/www/html/nextcloud/remote.php","line":163,"args":["/var/www/html/nextcloud/apps/dav/appinfo/v2/remote.php"],"function":"require_once"}],"File":"/var/www/html/nextcloud/apps/dav/lib/Connector/Sabre/File.php","Line":218,"CustomMessage":"--"},"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:68.0) Gecko/20100101 Firefox/68.0","version":"16.0.4.1","id":"5d6cfa64b1588"}
The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):
<?php
$CONFIG = array (
'instanceid' => '<hidden>',
'passwordsalt' => '<hidden>',
'secret' => '<hidden>',
'trusted_domains' =>
array (
0 => '<hidden>',
1 => '<hidden>',
2 => '<hidden>',
3 => '<hidden>',
4 => '<hidden>',
),
'trusted_proxies' =>
array (
0 => '<hidden>',
),
'datadirectory' => '<hidden>',
'dbtype' => 'mysql',
'version' => '16.0.4.1',
'overwrite.cli.url' => '<hidden>',
'overwriteprotocol' => 'https',
'dbname' => '<hidden>',
'dbhost' => '<hidden>',
'dbport' => '<hidden>',
'dbtableprefix' => '<hidden>',
'dbuser' => '<hidden>',
'dbpassword' => '<hidden>',
'installed' => true,
'mail_smtpmode' => 'sendmail',
'mail_smtpsecure' => 'tls',
'mail_from_address' => '<hidden>',
'mail_domain' => '<hidden>',
'mail_smtpauthtype' => 'LOGIN',
'mail_smtphost' => '<hidden>',
'mail_smtpauth' => 1,
'mail_smtpname' => '<hidden>',
'mail_smtppassword' => '<hidden>',
'memcache.local' => '\\OC\\Memcache\\APCu',
'filelocking.enabled' => true,
'memcache.locking' => '\\OC\\Memcache\\Redis',
'redis' =>
array (
'host' => 'localhost',
'port' => 6379,
'timeout' => 0.0,
),
'memcache.distributed' => '\\OC\\Memcache\\Memcached',
'memcached_servers' =>
array (
0 =>
array (
0 => 'localhost',
1 => 11211,
),
),
'maintenance' => false,
'ldapIgnoreNamingRules' => false,
'ldapProviderFactory' => 'OCA\\User_LDAP\\LDAPProviderFactory',
'theme' => '',
'loglevel' => 2,
'mysql.utf8mb4' => true,
'data-fingerprint' => '<hidden>',
'app_install_overwrite' =>
array (
0 => 'calendar',
1 => 'mail',
2 => 'spreed',
3 => 'bookmarks_fulltextsearch',
),
'mail_sendmailmode' => 'pipe',
);
No output in Apache error log at /var/log/apache2/nextcloud_error.log
I'm able to reproduce this issue with https://try.nextcloud.com for copying empty files.
- Create a empty file
- Upload it through web interface or client
- Try to copy the empty file
cc @nextcloud/server-triage
I realized that MOVING empty files via the Nextcloud web interface works. However, copying still does not work.
@rullzer @nickvergessen @blizzz 😬
Bug exists also in 16.0.3 FWIW
FWIW, this is how I patched the bug locally (v16.0.3). There likely is a better way to fix it, but at least the following is working for me:
/nextcloud/apps/dav/lib/Connector/Sabre/File.php 119a120,131
// Special handling for zero size files $zero_size = false; try { $metadata = stream_get_meta_data($data); if ($metadata['wrapper_type'] === 'plainfile') { if (filesize($metadata['uri']) === 0) { $zero_size = true; } } } catch (Exception $e) { }
209a222,226
// Special handling for zero size files if ($zero_size && $count === 0) { $result = true; }
A refinement to the snippet to be inserted at the beginning of the put() method. The wrapper_type test isn't necessary, and actually results in the workaround not working for other wrapper types than plainfile. The following simplified version handles all wrapper types compatible with the filesize() function, catching and ignoring cases where it is not.
// Special handling for zero size files
$zero_size = false;
try {
$metadata = stream_get_meta_data($data);
if (filesize($metadata['uri']) === 0) {
$zero_size = true;
}
} catch (Exception $e) { }
It still may not be (or arguably isn't) the best way / place to fix this issue, but at least it provides a workaround...
Mind to open a pull request with your changes?
Sorry for the delayed response.
I would prefer not to make any pull request, as I suspect the fix is most likely needed more ubiquitously in the WebDAV/Sabre code and all I provided was a workaround.
Hopefully those responsible for the code in question will review the issue, and my workaround, and provide a more proper and longstanding fix to the issue.
Cheers.
Thanks for digging @patrick-stickler-csc-fi :+1: You were very close ;)
Index: apps/dav/lib/Connector/Sabre/File.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- apps/dav/lib/Connector/Sabre/File.php (date 1574129867000)
+++ apps/dav/lib/Connector/Sabre/File.php (date 1574200238068)
@@ -211,10 +211,10 @@
if ($result === false) {
$expected = -1;
- if (isset($_SERVER['CONTENT_LENGTH'])) {
- $expected = $_SERVER['CONTENT_LENGTH'];
+ if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] !== '') {
+ $expected = (int)$_SERVER['CONTENT_LENGTH'];
}
- if ($expected !== "0") {
+ if ($expected !== 0) {
throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
}
}
For some reason we don't send a content-length header with the copy request but the above code only prevents the exception for content-length = "0". But this only fixing the case that $result is false which is weird because writing a file with 0 bytes looks fine to me.
https://github.com/nextcloud/server/blob/13fd89575d554f33813c22212469f228815a2377/apps/dav/lib/Connector/Sabre/File.php#L191-L192
$result is false because count is not bigger than 0. Unfortunately we cannot use >= 0 here (yet)
https://github.com/nextcloud/server/blob/1cc8a2f5d251b5f2ab594f9c96eaff77ce6697f3/lib/private/Files/Storage/Local.php#L488
file_put_contents will return a integer or false (on error). Thus (int)false is 0. So actually we don't know if a write was successful or not. Probably a exception would be better at this place.
Related change regarding the error handling which now already throws a GenericFileException instead of returning 0: https://github.com/nextcloud/server/pull/21628/commits/ad7798f9c9066b1eee6ad91526ffab34186f4a7b