use of com.jcraft.jsch.SftpException in project n2a by frothga.
the class SshFileSystemProvider method checkAccess.
public void checkAccess(Path path, AccessMode... modes) throws IOException {
SshPath A = (SshPath) path;
String name = A.toAbsolutePath().toString();
try {
SftpATTRS attributes = A.getSftp().stat(name);
int permissions = attributes.getPermissions();
for (AccessMode mode : modes) {
switch(mode) {
case READ:
if ((permissions & 0444) == 0)
throw new AccessDeniedException("READ " + A);
break;
case WRITE:
if ((permissions & 0222) == 0)
throw new AccessDeniedException("WRITE " + A);
break;
case EXECUTE:
if ((permissions & 0111) == 0)
throw new AccessDeniedException("EXECUTE " + A);
break;
default:
throw new UnsupportedOperationException();
}
}
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
throw new NoSuchFileException(name);
throw new IOException(e);
}
}
use of com.jcraft.jsch.SftpException in project n2a by frothga.
the class SshFileSystemProvider method delete.
public void delete(Path path) throws IOException {
String name = path.toAbsolutePath().toString();
try {
WrapperSftp sftp = ((SshPath) path).getSftp();
// doesn't follow links
SftpATTRS attributes = sftp.lstat(name);
if (attributes.isDir())
sftp.rmdir(name);
else
sftp.rm(name);
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
throw new NoSuchFileException(name);
throw new IOException(e);
}
}
use of com.jcraft.jsch.SftpException in project ats-framework by Axway.
the class SftpClient method performUploadFile.
@Override
protected void performUploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
checkConnected("file upload");
FileInputStream fis = null;
try {
String remoteFileAbsPath = null;
remoteDir = remoteDir.replace("\\", "/");
remoteFile = remoteFile.replace("\\", "/");
if (remoteDir.endsWith("/") && remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir.substring(0, remoteDir.length() - 2) + remoteFile;
} else if (!remoteDir.endsWith("/") && !remoteFile.endsWith("/")) {
remoteFileAbsPath = remoteDir + "/" + remoteFile;
} else {
remoteFileAbsPath = remoteDir + remoteFile;
}
// upload the file
File file = new File(localFile);
fis = new FileInputStream(file);
if (synchronizationSftpTransferListener != null) {
this.channel.put(fis, remoteFileAbsPath, synchronizationSftpTransferListener);
} else if (isDebugMode() && debugProgressMonitor != null) {
debugProgressMonitor.setTransferMetadata(localFile, remoteFileAbsPath, file.length());
this.channel.put(fis, remoteFileAbsPath, debugProgressMonitor);
} else {
this.channel.put(fis, remoteFileAbsPath);
}
} catch (SftpException e) {
log.error("Unable to upload file!", e);
throw new FileTransferException(e);
} catch (FileNotFoundException e) {
log.error("Unable to find the file that needs to be uploaded!", e);
throw new FileTransferException(e);
} finally {
// close the file input stream
IoUtils.closeStream(fis, "Unable to close the file stream after successful upload!");
}
if (remoteDir != null && !remoteDir.endsWith("/")) {
remoteDir += "/";
}
log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + this.hostname);
}
use of com.jcraft.jsch.SftpException in project acceptance-test-harness by jenkinsci.
the class GitRepo method transferToDockerContainer.
/**
* Zip bare repository, copy to Docker container using sftp, then unzip.
* The repo is now accessible over "ssh://git@ip:port/home/git/gitRepo.git"
*
* @param host IP of Docker container
* @param port SSH port of Docker container
*/
public void transferToDockerContainer(String host, int port) {
try {
Path zipPath = Files.createTempFile("git", "zip");
File zippedRepo = zipPath.toFile();
String zippedFilename = zipPath.getFileName().toString();
ZipUtil.pack(new File(dir.getPath()), zippedRepo);
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
JSch jSch = new JSch();
jSch.addIdentity(privateKey.getAbsolutePath());
Session session = jSch.getSession("git", host, port);
session.setConfig(props);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd("/home/git");
channel.put(new FileInputStream(zippedRepo), zippedFilename);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("unzip " + zippedFilename + " -d " + REPO_NAME);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
System.out.println(++index + " : " + line);
}
channelExec.disconnect();
channel.disconnect();
session.disconnect();
Files.delete(zipPath);
} catch (IOException | JSchException | SftpException e) {
throw new AssertionError("Can't transfer git repository to docker container", e);
}
}
use of com.jcraft.jsch.SftpException in project keepass2android by PhilippC.
the class SftpStorage method uploadFile.
@Override
public void uploadFile(String path, byte[] data, boolean writeTransactional) throws Exception {
ChannelSftp c = init(path);
try {
InputStream in = new ByteArrayInputStream(data);
String targetPath = extractSessionPath(path);
if (writeTransactional) {
// upload to temporary location:
String tmpPath = targetPath + ".tmp";
c.put(in, tmpPath);
// remove previous file:
try {
c.rm(targetPath);
} catch (SftpException e) {
// ignore. Can happen if file didn't exist before
}
// rename tmp to target path:
c.rename(tmpPath, targetPath);
} else {
c.put(in, targetPath);
}
tryDisconnect(c);
} catch (Exception e) {
tryDisconnect(c);
throw e;
}
}
Aggregations