Skip to content

Commit

Permalink
Enable renaming with flags (#652)
Browse files Browse the repository at this point in the history
* Enable renaming with flags

The SFTP protocol allows to rename files by specifying
extra flags:

- OVERWRITE
- ATOMIC
- NATIVE

The flags are exposed through a new RenameFlags enum and
can be passed as parameters to the rename() method in
SFTPClient/SFTPEngine.

Relates to #563

* Update RenameFlags.java

* Update RenameFlags.java

* Align license header with all other files

* Make RenameFlags parameter in line with OpenMode(s)

Co-authored-by: Jeroen van Erp <[email protected]>
  • Loading branch information
lucamilanesio and hierynomus authored Sep 27, 2021
1 parent 03dd1aa commit 53d241e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/main/java/net/schmizz/sshj/sftp/RenameFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.sftp;

public enum RenameFlags {
OVERWRITE(1),
ATOMIC(2),
NATIVE(4);

private final long flag;

RenameFlags(long flag) {
this.flag = flag;
}

public long longValue() {
return flag;
}
}
8 changes: 6 additions & 2 deletions src/main/java/net/schmizz/sshj/sftp/SFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ public FileAttributes statExistence(String path)
}
}

public void rename(String oldpath, String newpath)
public void rename(String oldpath, String newpath) throws IOException {
rename(oldpath, newpath, EnumSet.noneOf(RenameFlags.class));
}

public void rename(String oldpath, String newpath, Set<RenameFlags> renameFlags)
throws IOException {
engine.rename(oldpath, newpath);
engine.rename(oldpath, newpath, renameFlags);
}

public void rm(String filename)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,18 @@ public FileAttributes lstat(String path)
return stat(PacketType.LSTAT, path);
}

public void rename(String oldPath, String newPath)
public void rename(String oldPath, String newPath, Set<RenameFlags> flags)
throws IOException {
if (operativeVersion < 1)
throw new SFTPException("RENAME is not supported in SFTPv" + operativeVersion);

long renameFlagMask = 0L;
for (RenameFlags flag : flags) {
renameFlagMask = renameFlagMask | flag.longValue();
}

doRequest(
newRequest(PacketType.RENAME).putString(oldPath, sub.getRemoteCharset()).putString(newPath, sub.getRemoteCharset())
newRequest(PacketType.RENAME).putString(oldPath, sub.getRemoteCharset()).putString(newPath, sub.getRemoteCharset()).putUInt32(renameFlagMask)
).ensureStatusPacketIsOK();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public FileAttributes statExistence(String path)
}

@Override
public void rename(String oldpath, String newpath)
public void rename(String oldpath, String newpath, Set<RenameFlags> renameFlags)
throws IOException {
super.rename(cwdify(oldpath), cwdify(newpath));
super.rename(cwdify(oldpath), cwdify(newpath), renameFlags);
}

@Override
Expand Down

0 comments on commit 53d241e

Please sign in to comment.