bugfix related to setting attributes on sftp upload

This commit is contained in:
Shikhar Bhushan
2010-05-22 00:57:50 +01:00
parent 0c28deab88
commit b54a4abfce
4 changed files with 14 additions and 12 deletions

View File

@@ -25,6 +25,8 @@ import java.util.Set;
public final class FileAttributes { public final class FileAttributes {
public static final FileAttributes EMPTY = new FileAttributes();
public static enum Flag { public static enum Flag {
SIZE(0x00000001), SIZE(0x00000001),
@@ -58,7 +60,7 @@ public final class FileAttributes {
private final long mtime; private final long mtime;
private final Map<String, String> ext = new HashMap<String, String>(); private final Map<String, String> ext = new HashMap<String, String>();
public FileAttributes() { private FileAttributes() {
size = atime = mtime = uid = gid = mask = 0; size = atime = mtime = uid = gid = mask = 0;
mode = new FileMode(0); mode = new FileMode(0);
} }

View File

@@ -73,7 +73,7 @@ public class SFTPClient {
public RemoteFile open(String filename, Set<OpenMode> mode) public RemoteFile open(String filename, Set<OpenMode> mode)
throws IOException { throws IOException {
return open(filename, mode, new FileAttributes()); return open(filename, mode, FileAttributes.EMPTY);
} }
public RemoteFile open(String filename) public RemoteFile open(String filename)

View File

@@ -122,7 +122,7 @@ public class SFTPEngine
public RemoteFile open(String filename, Set<OpenMode> modes) public RemoteFile open(String filename, Set<OpenMode> modes)
throws IOException { throws IOException {
return open(filename, modes, new FileAttributes()); return open(filename, modes, FileAttributes.EMPTY);
} }
public RemoteFile open(String filename) public RemoteFile open(String filename)
@@ -155,14 +155,12 @@ public class SFTPEngine
public void makeDir(String path, FileAttributes attrs) public void makeDir(String path, FileAttributes attrs)
throws IOException { throws IOException {
doRequest( doRequest(newRequest(PacketType.MKDIR).putString(path).putFileAttributes(attrs)).ensureStatusPacketIsOK();
newRequest(PacketType.MKDIR).putString(path).putFileAttributes(attrs)
).ensureStatusPacketIsOK();
} }
public void makeDir(String path) public void makeDir(String path)
throws IOException { throws IOException {
makeDir(path, new FileAttributes()); makeDir(path, FileAttributes.EMPTY);
} }
public void symlink(String linkpath, String targetpath) public void symlink(String linkpath, String targetpath)

View File

@@ -109,7 +109,7 @@ public class SFTPFileTransfer
private void downloadDir(final RemoteResourceInfo remote, final File local) private void downloadDir(final RemoteResourceInfo remote, final File local)
throws IOException { throws IOException {
final File adjusted = FileTransferUtil.getTargetDirectory(local, remote.getName()); final File adjusted = FileTransferUtil.getTargetDirectory(local, remote.getName());
setAttributes(remote, adjusted); copyAttributes(remote, adjusted);
final RemoteDirectory rd = sftp.openDir(remote.getPath()); final RemoteDirectory rd = sftp.openDir(remote.getPath());
try { try {
for (RemoteResourceInfo rri : rd.scan(getDownloadFilter())) for (RemoteResourceInfo rri : rd.scan(getDownloadFilter()))
@@ -122,7 +122,7 @@ public class SFTPFileTransfer
private void downloadFile(final RemoteResourceInfo remote, final File local) private void downloadFile(final RemoteResourceInfo remote, final File local)
throws IOException { throws IOException {
final File adjusted = FileTransferUtil.getTargetFile(local, remote.getName()); final File adjusted = FileTransferUtil.getTargetFile(local, remote.getName());
setAttributes(remote, adjusted); copyAttributes(remote, adjusted);
final RemoteFile rf = sftp.open(remote.getPath()); final RemoteFile rf = sftp.open(remote.getPath());
try { try {
final FileOutputStream fos = new FileOutputStream(adjusted); final FileOutputStream fos = new FileOutputStream(adjusted);
@@ -137,7 +137,7 @@ public class SFTPFileTransfer
} }
} }
private void setAttributes(final RemoteResourceInfo remote, final File local) private void copyAttributes(final RemoteResourceInfo remote, final File local)
throws IOException { throws IOException {
final FileAttributes attrs = remote.getAttributes(); final FileAttributes attrs = remote.getAttributes();
getModeSetter().setPermissions(local, attrs.getMode().getPermissionsMask()); getModeSetter().setPermissions(local, attrs.getMode().getPermissionsMask());
@@ -172,8 +172,10 @@ public class SFTPFileTransfer
private void uploadFile(File local, String remote) private void uploadFile(File local, String remote)
throws IOException { throws IOException {
final String adjusted = prepareFile(local, remote); final String adjusted = prepareFile(local, remote);
final RemoteFile rf = sftp.open(adjusted, EnumSet.of(OpenMode.WRITE, OpenMode.CREAT, OpenMode.TRUNC), final RemoteFile rf = sftp.open(adjusted, EnumSet.of(OpenMode.WRITE,
getAttributes(local)); OpenMode.CREAT,
OpenMode.TRUNC));
rf.setAttributes(getAttributes(local));
try { try {
final FileInputStream fis = new FileInputStream(local); final FileInputStream fis = new FileInputStream(local);
try { try {