Skip to content

SFTP Append doesn't work [?] #390

Closed
Closed

Description

@IgnatBeresnev

I've been trying to append text to the end of the file with OpenMode.APPEND, but no luck so far. It seems to ignore this flag and just uses the offset. If I set the offsets (both file & off) to 0, then it'll rewrite the file. How do I go about appending text to the end of the file WITHOUT offsets and ONLY with OpenMode.APPEND, if it's possible?

Here's how I'm trying to do it

String fileName = "test_write_continuously.txt";
String filePath = HOME_FOLDER + "/" + fileName;

String initialText = "This is initial text of the file. ";
byte[] bytes = initialText.getBytes();

RemoteFile writeInitial = sftpClient.open(filePath, new HashSet<>(Arrays.asList(OpenMode.WRITE, OpenMode.CREAT)));
writeInitial.write(0, bytes, 0, bytes.length);
writeInitial.close();

String appendText = "This is the continuation text that should be appended after initial. ";
byte[] appendTextBytes = appendText.getBytes();

RemoteFile writeAppend = sftpClient.open(filePath, new HashSet<>(Arrays.asList(OpenMode.WRITE, OpenMode.APPEND)));
writeAppend.write(0, appendTextBytes, 0, appendTextBytes.length);
writeAppend.close();

RemoteFile readContent = sftpClient.open(filePath, new HashSet<>(Arrays.asList(OpenMode.READ)));
byte[] readBytes = new byte[bytes.length + appendTextBytes.length];
readContent.read(0, readBytes, 0, readBytes.length);
String readString = new String(readBytes);
readContent.close();

assertEquals(initialText + appendText, readString);

What I want to achieve: continuously write to the end of the file, pretty much real-time logging on a remote server through ssh, and I need the data to be consistent and in order. You think it's possible to achieve? So far I've been having file rewrites

Activity

  1. IgnatBeresnev commented on Dec 27, 2017

    @IgnatBeresnev
    Author

    Maybe the problem is that we shouldn't put file offset if APPEND is specified?

    return requester.request(newRequest(PacketType.WRITE)
            .putUInt64(fileOffset)
            .putUInt32(len)
            .putRawBytes(data, off, len)
    );
    ``
    
  2. hierynomus commented on Dec 28, 2017

    @hierynomus
    Owner

    According to the specifications, the fileOffset field is required and cannot be omitted. When a file is opened with SSH_FXF_APPEND, the sftp server should ignore the offset flag and append to the file.

    Which SFTP server are you using?

  3. hierynomus commented on Dec 28, 2017

    @hierynomus
    Owner

    I've added an integration test in 0f67fa2 which shows that it should work with your exact scenario. This is as far as I can see really a bug in your SFTP server.

  4. IgnatBeresnev commented on Jan 9, 2018

    @IgnatBeresnev
    Author

    You seem to be right. The problem is reproduced only on the server that uses OpenSSH 5.3. However, another server with OpenSSH 7.4 is passing tests just fine.

    Thanks. Didn't even think that could be a problem

  5. hierynomus commented on Jan 9, 2018

    @hierynomus
    Owner

    Thanks for confirming! Good to know that OpenSSH had a bug ;).

  6. added a commit that references this issue on Jan 11, 2018
    b265cfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions