Skip to content

SFTP writes with 'append' mode may insert instead #690

@smitsjelle

Description

@smitsjelle

Version

2.14.0

Bug description

I'm using Spring Integration to write files to an SFTP server. I noticed that on one specific server I get odd behavior. Instead of adding the contents to the existing file, it overrides the content. It seems to be inserting the data from position 0 until it is done. If there was already more data in the file, that data remains.

For example, if I have a test.txt on my server with content abcdefghi and I send 123456 with setting APPEND, the resulting file content is 123456ghi instead of abcdefghi123456.

In order to do the append, Spring Integrations' SFTPSession#append(..) method uses AbstractSftpClient#write(String path, int bufferSize, Collection mode) to do appends. When I change Spring's method to use the SftpClient#write(Handle handle, long fileOffset, byte[] src) instead, where I pass the current length of the file as its offset (retrieved by calling sftpClient.stat(destination).getSize()), the append works as expected.

It feels like the write method of AbstractSftpClient doesn't handle the append correctly if the supplied OpenModes are Write, Append, Create. Is this correct, or is there something wrong with the parameters that are passed?

Actual behavior

The data gets inserted from position 0 instead of getting added after the last position in the document. I.e.: when the inserted data is less than the length of the existing document, it gets overriden until the new data ends, and remains the same after that.

Expected behavior

When the OpenMode 'Append' is provided, data is always added at the end of the file.

Relevant log output

Other information

The 'getVersion' command for the server I'm encountering the issue for returns version 3. In testcases where I cannot reproduce the issue, the version is 6. Unfortunately, I don't have any further details on the configuration of the SFTP server I'm encountering the issue on.

Metadata

Metadata

Assignees

Labels

not sshdIssue caused by third-party code; not a problem of Apache MINA sshd

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions