mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-09 08:25:10 +03:00
... now this should be solid
This commit is contained in:
@@ -154,28 +154,30 @@ public class SFTPFileTransfer
|
|||||||
private void upload(File local, String remote)
|
private void upload(File local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
log.info("Uploading [{}] to [{}]", local, remote);
|
log.info("Uploading [{}] to [{}]", local, remote);
|
||||||
|
final String adjustedPath;
|
||||||
if (local.isDirectory())
|
if (local.isDirectory())
|
||||||
uploadDir(local, remote);
|
adjustedPath = uploadDir(local, remote);
|
||||||
else if (local.isFile())
|
else if (local.isFile())
|
||||||
uploadFile(local, remote);
|
adjustedPath = uploadFile(local, remote);
|
||||||
else
|
else
|
||||||
throw new IOException(local + " is not a file or directory");
|
throw new IOException(local + " is not a file or directory");
|
||||||
|
sftp.setAttributes(adjustedPath, getAttributes(local));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uploadDir(File local, String remote)
|
private String uploadDir(File local, String remote)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
final String adjusted = prepareDir(local, remote);
|
final String adjusted = prepareDir(local, remote);
|
||||||
for (File f : local.listFiles(getUploadFilter()))
|
for (File f : local.listFiles(getUploadFilter()))
|
||||||
upload(f, adjusted);
|
upload(f, adjusted);
|
||||||
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uploadFile(File local, String remote)
|
private String 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,
|
final RemoteFile rf = sftp.open(adjusted, EnumSet.of(OpenMode.WRITE,
|
||||||
OpenMode.CREAT,
|
OpenMode.CREAT,
|
||||||
OpenMode.TRUNC));
|
OpenMode.TRUNC));
|
||||||
rf.setAttributes(getAttributes(local));
|
|
||||||
try {
|
try {
|
||||||
final FileInputStream fis = new FileInputStream(local);
|
final FileInputStream fis = new FileInputStream(local);
|
||||||
try {
|
try {
|
||||||
@@ -187,6 +189,7 @@ public class SFTPFileTransfer
|
|||||||
} finally {
|
} finally {
|
||||||
rf.close();
|
rf.close();
|
||||||
}
|
}
|
||||||
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String prepareDir(File local, String remote)
|
private String prepareDir(File local, String remote)
|
||||||
@@ -197,7 +200,7 @@ public class SFTPFileTransfer
|
|||||||
} catch (SFTPException e) {
|
} catch (SFTPException e) {
|
||||||
if (e.getStatusCode() == StatusCode.NO_SUCH_FILE) {
|
if (e.getStatusCode() == StatusCode.NO_SUCH_FILE) {
|
||||||
log.debug("probeDir: {} does not exist, creating", remote);
|
log.debug("probeDir: {} does not exist, creating", remote);
|
||||||
sftp.makeDir(remote, getAttributes(local));
|
sftp.makeDir(remote);
|
||||||
return remote;
|
return remote;
|
||||||
} else
|
} else
|
||||||
throw e;
|
throw e;
|
||||||
@@ -206,11 +209,6 @@ public class SFTPFileTransfer
|
|||||||
if (attrs.getMode().getType() == FileMode.Type.DIRECTORY)
|
if (attrs.getMode().getType() == FileMode.Type.DIRECTORY)
|
||||||
if (pathHelper.getComponents(remote).getName().equals(local.getName())) {
|
if (pathHelper.getComponents(remote).getName().equals(local.getName())) {
|
||||||
log.debug("probeDir: {} already exists", remote);
|
log.debug("probeDir: {} already exists", remote);
|
||||||
final FileAttributes localAttrs = getAttributes(local);
|
|
||||||
if (attrs.getMode().getMask() != localAttrs.getMode().getMask()
|
|
||||||
|| (getModeGetter().preservesTimes()
|
|
||||||
&& (attrs.getAtime() != attrs.getAtime() || attrs.getMtime() != localAttrs.getMtime())))
|
|
||||||
sftp.setAttributes(remote, localAttrs);
|
|
||||||
return remote;
|
return remote;
|
||||||
} else {
|
} else {
|
||||||
log.debug("probeDir: {} already exists, path adjusted for {}", remote, local.getName());
|
log.debug("probeDir: {} already exists, path adjusted for {}", remote, local.getName());
|
||||||
|
|||||||
Reference in New Issue
Block a user