Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFTP Append doesn't work [?] #390

Closed
IgnatBeresnev opened this issue Dec 27, 2017 · 5 comments
Closed

SFTP Append doesn't work [?] #390

IgnatBeresnev opened this issue Dec 27, 2017 · 5 comments

Comments

@IgnatBeresnev
Copy link

IgnatBeresnev commented Dec 27, 2017

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

@IgnatBeresnev
Copy link
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)
);
``

@hierynomus
Copy link
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?

@hierynomus
Copy link
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.

@IgnatBeresnev
Copy link
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

@hierynomus
Copy link
Owner

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

CCLiu added a commit to CCLiu/sshj that referenced this issue Jan 11, 2018
* Check whether filename is a child of the current file (Fixes hierynomus#341)

* Fixed codacy

* Updated README release notes

* Removed oraclejdk7 as that is no longer supported on trusty, added openjdk

* Added gradle caching to travis config

* Removed use of DataTypeConverter as that is no longer in default JDK9

* Removed build of broken openJDK7 in favour of using animal-sniffer to detect java 1.6 compatibility

* Improved test stability

* Correctly determine KeyType for ECDSA public key (Fixes hierynomus#356)

* fixed build

* Fixed Java9 build?

* Disambiguated signature initialization

* Removed deprecated method

* Organised imports

* Added 'out/' to gitignore

* Added support for new-style fingerprints (hierynomus#365)

* Added support for new-style fingerprints

* Fixed codacy warnings

* Fix decoding signature bytes (Fixes hierynomus#355, hierynomus#354) (hierynomus#361)

* Fix for signature verify in DSA

* Cleaned up signature verification

* Fixed import

* Ignored erroneous pmd warnings

* Updated JavaDoc

* Extracted ASN.1/DER encoding to method (hierynomus#368)

* Update net.i2p.crypto:eddsa to 0.2.0 (hierynomus#372)

* Update net.i2p.crypto:eddsa to 0.2.0

* Update net.i2p.crypto.eddsa to 0.2.0

* Update net.i2p.crypto.eddsa to 0.2.0

* Update net.i2p.crypto.eddsa to 0.2.0

* Log security provider registration failures (hierynomus#374)

* Migrate remaining block ciphers

* Updated README for v0.23.0 release

* Using new release plugin

* Updated build plugins

* Fix escaping in WildcardHostMatcher (hierynomus#382)

* Escape '[' and ']' in WildcardHostMatcher

* Anchoring regex to match entire string (Fixes hierynomus#381)

* Updated builds to include CodeCov

* - Experimenting with travis

* - fix ip for online testing

* - account for different working dir

* - yaml-yaml

* - double before_install

* - still -d

* - try common format

* - Fixed server keys
- Use sshj branding

* - grr, ip

* - minor improvements

* - eh?

* - switch username back

* - orly?

* - desperation

* - One more time

* Upgraded gradle to cope with java9

* Separated out integration tests

* Fixed length bug in putString (Fixes hierynomus#187)

* Removed docker from travis yml as it is included in gradle build now

* Added integration test to travis

* Update AndroidConfig (hierynomus#389)

* Add EdDSA signature for AndroidConfig.

* Initialize KeyExchange- and FileKeyProviderFactories with registered "bouncyCastle" (in fact, SpongyCastle is registered).

See hierynomus#308 for discussion.

* Added integration test for append scenario (Fixes hierynomus#390)

* Fixed headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants