Reputation: 182
I am in the process of converting a piece of code from FTP to SFTP. In FTP, this code used to check if the given file is open or not by trying to rename the file. In library for FTP (org.apache.commons.net) this is a method which try to rename the file and return if the given file is changed or not. But in SFTP (I am using JSch library), the rename
method returns null. My moto is to check whether the given file is open or not? Any ideas?
Below is the code in FTP:
boolean renameSuccess = userSharedDrive.rename(prefixedOriginalFileName, prefixedTempRenamedFile);
Upvotes: 0
Views: 1986
Reputation: 26
Why are you doing the check? If you are doing a check on a file to make sure that you do not pick up a file that is in the process of being changed, then I'd recommend that you stat the file to get its modification time (SftpATTRS.getMTime) and (SftpATTRS.getSize) size. Then wait a short period of time and stat again to make sure the modification time and size have not changed.
Upvotes: 1
Reputation: 202222
The JSch ChannelSftp.rename
does not return anything, it has void
"return" type (maybe that's what you mean by null).
It throws an exception if the rename fails. So if it does not throw, the rename succeeded.
Another method you can try is to open the file for writing in an append mode, not writing anything to it. The opening should fail (throw an exception), if the file is opened by another process/client.
sftp.put(filepath, ChannelSftp.APPEND).close()
This way, you do not modify (rename) the file at all.
Upvotes: 1