Skip to content

Cannot create empty file on SFTP #17027

Closed

Description

@PVince81

Steps to reproduce

  1. Mount SFTP storage to "/sftp"
  2. Setup sync client
  3. Wait for sync
  4. Create a local empty file "/sftp/empty" (I used touch)
  5. Wait for sync

Expected result

Upload success

Actual result

{"reqId":"xfcr9w7zcMtDujLalJUL","remoteAddr":"127.0.0.1","app":"webdav","message":"Exception: {\"Message\":\"HTTP\\\/1.1 500 Could not rename part file to final file\",\"Code\":0,\"Trace\":\"#0 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/lib\\\/private\\\/connector\\\/sabre\\\/directory.php(111): OC\\\\Connector\\\\Sabre\\\\File->put(Resource id #241)\\n#1 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/3rdparty\\\/sabre\\\/dav\\\/lib\\\/DAV\\\/Server.php(1053): OC\\\\Connector\\\\Sabre\\\\Directory->createFile('empty2', Resource id #241)\\n#2 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/3rdparty\\\/sabre\\\/dav\\\/lib\\\/DAV\\\/CorePlugin.php(513): Sabre\\\\DAV\\\\Server->createFile('sftp\\\/empty2', Resource id #241, NULL)\\n#3 [internal function]: Sabre\\\\DAV\\\\CorePlugin->httpPut(Object(Sabre\\\\HTTP\\\\Request), Object(Sabre\\\\HTTP\\\\Response))\\n#4 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/3rdparty\\\/sabre\\\/event\\\/lib\\\/EventEmitterTrait.php(105): call_user_func_array(Array, Array)\\n#5 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/3rdparty\\\/sabre\\\/dav\\\/lib\\\/DAV\\\/Server.php(469): Sabre\\\\Event\\\\EventEmitter->emit('method:PUT', Array)\\n#6 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/3rdparty\\\/sabre\\\/dav\\\/lib\\\/DAV\\\/Server.php(254): Sabre\\\\DAV\\\\Server->invokeMethod(Object(Sabre\\\\HTTP\\\\Request), Object(Sabre\\\\HTTP\\\\Response))\\n#7 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/apps\\\/files\\\/appinfo\\\/remote.php(83): Sabre\\\\DAV\\\\Server->exec()\\n#8 \\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/remote.php(84): require_once('\\\/srv\\\/www\\\/htdocs...')\\n#9 {main}\",\"File\":\"\\\/srv\\\/www\\\/htdocs\\\/owncloud\\\/lib\\\/private\\\/connector\\\/sabre\\\/file.php\",\"Line\":211}","level":4,"time":"2015-06-18T16:06:41+00:00","method":"PUT","url":"\/owncloud\/remote.php\/webdav\/sftp\/empty2"}

Versions

Observed on ownCloud 8.1 git master (7a0917e)

Activity

  1. self-assigned this
    on Jun 18, 2015
  2. added this to the milestone on Jun 18, 2015
  3. PVince81 commented on Jun 18, 2015

    @PVince81
    ContributorAuthor

    It looks like the part file is not being created on the SFTP storage, so the final rename cannot work.

    It works fine for non-empty files.

  4. PVince81 commented on Jun 18, 2015

    @PVince81
    ContributorAuthor

    Also broken on stable8.

    I suspect that the phpseclib doesn't create the file remotely until the first block is sent.
    We might want to work this around by calling file_put_contents without content instead of fopen.

    Strangely it works in the web UI, I'll try and compare the code paths.

  5. PVince81 commented on Jun 22, 2015

    @PVince81
    ContributorAuthor

    CC @bantu in case you have an idea regarding phpseclib

  6. bantu commented on Jun 22, 2015

    @bantu

    @PVince81 I can look into it, if you want. Can you get me a complete trace?

  7. PVince81 commented on Jun 24, 2015

    @PVince81
    ContributorAuthor

    Ok, the reason why it works in the web UI is because newfile.php does a "touch()" instead of "file_put_contents()".

    Here is a stack from when creating an empty file:

    0  Net_SFTP_Stream->_stream_open() /srv/www/htdocs/owncloud/3rdparty/phpseclib/phpseclib/phpseclib/Net/SFTP/Stream.php:295
    1  call_user_func_array:{/srv/www/htdocs/owncloud/3rdparty/phpseclib/phpseclib/phpseclib/Net/SFTP/Stream.php:798}() /srv/www/htdocs/owncloud/3rdparty/phpseclib/phpseclib/phpseclib/Net/SFTP/Stream.php:798
    2  Net_SFTP_Stream->__call() /srv/www/htdocs/owncloud/3rdparty/phpseclib/phpseclib/phpseclib/Net/SFTP/Stream.php:798
    3  Net_SFTP_Stream->stream_open() /srv/www/htdocs/owncloud/apps/files_external/lib/sftp.php:354
    4  fopen()         /srv/www/htdocs/owncloud/apps/files_external/lib/sftp.php:354
    5  OC\Files\Storage\SFTP->fopen() /srv/www/htdocs/owncloud/apps/files_external/lib/sftp.php:354
    6  OC\Files\Storage\Wrapper\Encryption->fopen() /srv/www/htdocs/owncloud/lib/private/files/storage/wrapper/encryption.php:418
    7  OC\Files\Storage\Wrapper\Wrapper->fopen() /srv/www/htdocs/owncloud/lib/private/files/storage/wrapper/wrapper.php:285
    8  OC\Connector\Sabre\File->put() /srv/www/htdocs/owncloud/lib/private/connector/sabre/file.php:128
    9  OC\Connector\Sabre\Directory->createFile() /srv/www/htdocs/owncloud/lib/private/connector/sabre/directory.php:112
    10 Sabre\DAV\Server->createFile() /srv/www/htdocs/owncloud/3rdparty/sabre/dav/lib/DAV/Server.php:1053
    11 Sabre\DAV\CorePlugin->httpPut() /srv/www/htdocs/owncloud/3rdparty/sabre/dav/lib/DAV/CorePlugin.php:513
    12 call_user_func_array:{/srv/www/htdocs/owncloud/3rdparty/sabre/event/lib/EventEmitterTrait.php:105}() /srv/www/htdocs/owncloud/3rdparty/sabre/event/lib/EventEmitterTrait.php:105
    13 Sabre\Event\EventEmitter->emit() /srv/www/htdocs/owncloud/3rdparty/sabre/event/lib/EventEmitterTrait.php:105
    14 Sabre\DAV\Server->invokeMethod() /srv/www/htdocs/owncloud/3rdparty/sabre/dav/lib/DAV/Server.php:469
    15 Sabre\DAV\Server->exec() /srv/www/htdocs/owncloud/3rdparty/sabre/dav/lib/DAV/Server.php:254
    16 require_once()  /srv/www/htdocs/owncloud/apps/files/appinfo/remote.php:83
    17 {main}          /srv/www/htdocs/owncloud/remote.php:84
    

    If you look at the code, there is no actual instruction sent to the SFTP server to create an empty file when calling fopen() in write mode: https://github.com/owncloud/3rdparty/blob/master/phpseclib/phpseclib/phpseclib/Net/SFTP/Stream.php#L270

    Note that it does not enter the block with truncate() because $this->size === false is true, so it goes to the block above.

  8. bantu commented on Jun 24, 2015

    @bantu

    @PVince81 An fopen() followed by an fclose() should be sufficient to create a file, IMO. I've constructed a test case at phpseclib/phpseclib#713

  9. PVince81 commented on Jun 25, 2015

    @PVince81
    ContributorAuthor

    Hmm, are you saying that maybe the upload code isn't closing the file properly ?

  10. bantu commented on Jun 25, 2015

    @bantu

    @PVince81 No. This clearly seems to be a bug in phpseclib. The test case above reproduces the behaviour. An fopen followed by an fclose does not result in the file being created.

  11. PVince81 commented on Jun 25, 2015

    @PVince81
    ContributorAuthor

    Ok thanks for the info.
    So we'll need to put a workaround in the meantime.

  12. bantu commented on Jun 25, 2015

    @bantu

    @PVince81 phpseclib/phpseclib#713 Contains a possible fix now, if you want to test and confirm.

  13. PVince81 commented on Jun 25, 2015

    @PVince81
    ContributorAuthor

    I won't have time to fix this for 8.1. Still busy with many other issues.
    Removing assignment for now.

  14. removed their assignment
    on Jun 25, 2015
  15. ghost added this to the milestone on Jun 25, 2015
  16. ghost removed this from the milestone on Jun 25, 2015
  17. PVince81 commented on Jul 7, 2015

    @PVince81
    ContributorAuthor

    Ok let's upgrade phpseclib then for 8.2.
    8.1.1 might need a workaround.

  18. PVince81 commented on Jul 7, 2015

    @PVince81
    ContributorAuthor

    @bantu phpseclib has a tag 0.3.10 which I could upgrade to, but the PR says 2.0. Do I need to wait for version 2.0 to be released ?

  19. bantu commented on Jul 7, 2015

    @bantu

    @PVince81 There have been many fixes in phpseclib recently. I think phpseclib should make a release soon you can then upgrade to. Possibly 0.3.11.

  20. modified the milestones: , on Jul 16, 2015
  21. RobinMcCorkell commented on Sep 13, 2015

    @RobinMcCorkell
    Contributor

    phpseclib is now version 2.0 in master, can this be closed?

  22. karlitschek commented on Sep 14, 2015

    @karlitschek
    Contributor

    @Xenopathic have you tested if the original issue is solved?

  23. PVince81 commented on Sep 22, 2015

    @PVince81
    ContributorAuthor

    Tested on master, works now. Closing.

  24. locked as resolved and limited conversation to collaborators on Aug 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions