| Method from com.sshtools.j2ssh.SftpClient Detail: |
public void addEventListener(ChannelEventListener eventListener) {
sftp.addEventListener(eventListener);
}
|
public void cd(String dir) throws IOException {
try {
String actual;
if (dir.equals("")) {
actual = sftp.getDefaultDirectory();
} else {
actual = resolveRemotePath(dir);
actual = sftp.getAbsolutePath(actual);
}
FileAttributes attr = sftp.getAttributes(actual);
if (!attr.isDirectory()) {
throw new IOException(dir + " is not a directory");
}
cwd = actual;
} catch (IOException ex) {
throw new FileNotFoundException(dir + " could not be found");
}
}
|
public void chgrp(int gid,
String path) throws IOException {
String actual = resolveRemotePath(path);
FileAttributes attrs = sftp.getAttributes(actual);
attrs.setGID(new UnsignedInteger32(gid));
sftp.setAttributes(actual, attrs);
}
|
public void chmod(int permissions,
String path) throws IOException {
String actual = resolveRemotePath(path);
sftp.changePermissions(actual, permissions);
}
Changes the access permissions or modes of the specified file or
directory.
Modes determine who can read, change or execute a file.
Absolute modes are octal numbers specifying the complete list of
attributes for the files; you specify attributes by OR'ing together
these bits.
0400 Individual read
0200 Individual write
0100 Individual execute (or list directory)
0040 Group read
0020 Group write
0010 Group execute
0004 Other read
0002 Other write
0001 Other execute
|
public void chown(int uid,
String path) throws IOException {
String actual = resolveRemotePath(path);
FileAttributes attrs = sftp.getAttributes(actual);
attrs.setUID(new UnsignedInteger32(uid));
sftp.setAttributes(actual, attrs);
}
|
public DirectoryOperation copyLocalDirectory(String localdir,
String remotedir,
boolean recurse,
boolean sync,
boolean commit,
FileTransferProgress progress) throws IOException {
DirectoryOperation op = new DirectoryOperation();
// Record the previous
String pwd = pwd();
String lpwd = lpwd();
File local = resolveLocalPath(localdir);
remotedir = resolveRemotePath(remotedir);
remotedir += (remotedir.endsWith("/") ? "" : "/");
remotedir += local.getName();
remotedir += (remotedir.endsWith("/") ? "" : "/");
// Setup the remote directory if were committing
if (commit) {
try {
FileAttributes attrs = stat(remotedir);
} catch (IOException ex) {
mkdir(remotedir);
}
}
// List the local files and verify against the remote server
File[] ls = local.listFiles();
if (ls != null) {
for (int i = 0; i < ls.length; i++) {
if (ls[i].isDirectory() && !ls[i].getName().equals(".") &&
!ls[i].getName().equals("..")) {
if (recurse) {
File f = new File(local, ls[i].getName());
op.addDirectoryOperation(copyLocalDirectory(
f.getAbsolutePath(), remotedir, recurse, sync,
commit, progress), f);
}
} else if (ls[i].isFile()) {
try {
FileAttributes attrs = stat(remotedir +
ls[i].getName());
if ((ls[i].length() == attrs.getSize().longValue()) &&
((ls[i].lastModified() / 1000) == attrs.getModifiedTime()
.longValue())) {
op.addUnchangedFile(ls[i]);
} else {
op.addUpdatedFile(ls[i]);
}
} catch (IOException ex1) {
op.addNewFile(ls[i]);
}
if (commit) {
put(ls[i].getAbsolutePath(),
remotedir + ls[i].getName(), progress);
FileAttributes attrs = stat(remotedir +
ls[i].getName());
attrs.setTimes(new UnsignedInteger32(
ls[i].lastModified() / 1000),
new UnsignedInteger32(ls[i].lastModified() / 1000));
sftp.setAttributes(remotedir + ls[i].getName(), attrs);
}
}
}
}
if (sync) {
// List the contents of the new local directory and remove any
// files/directories that were not updated
try {
List files = ls(remotedir);
SftpFile file;
File f;
for (Iterator it = files.iterator(); it.hasNext();) {
file = (SftpFile) it.next();
// Create a local file object to test for its existence
f = new File(local, file.getFilename());
if (!op.containsFile(file) &&
!file.getFilename().equals(".") &&
!file.getFilename().equals("..")) {
op.addDeletedFile(file);
if (commit) {
if (file.isDirectory()) {
// Recurse through the directory, deleting stuff
recurseMarkForDeletion(file, op);
if (commit) {
rm(file.getAbsolutePath(), true, true);
}
} else if (file.isFile()) {
rm(file.getAbsolutePath());
}
}
}
}
} catch (IOException ex2) {
// Ignorew since if it does not exist we cant delete it
}
}
// Return the operation details
return op;
}
|
public DirectoryOperation copyRemoteDirectory(String remotedir,
String localdir,
boolean recurse,
boolean sync,
boolean commit,
FileTransferProgress progress) throws IOException {
// Create an operation object to hold the information
DirectoryOperation op = new DirectoryOperation();
// Record the previous working directoies
String pwd = pwd();
String lpwd = lpwd();
cd(remotedir);
// Setup the local cwd
String base = remotedir;
int idx = base.lastIndexOf('/");
if (idx != -1) {
base = base.substring(idx + 1);
}
File local = new File(localdir, base);
// File local = new File(localdir, remotedir);
if (!local.isAbsolute()) {
local = new File(lpwd(), localdir);
}
if (!local.exists() && commit) {
local.mkdir();
}
List files = ls();
SftpFile file;
File f;
for (Iterator it = files.iterator(); it.hasNext();) {
file = (SftpFile) it.next();
if (file.isDirectory() && !file.getFilename().equals(".") &&
!file.getFilename().equals("..")) {
if (recurse) {
f = new File(local, file.getFilename());
op.addDirectoryOperation(copyRemoteDirectory(
file.getFilename(), local.getAbsolutePath(),
recurse, sync, commit, progress), f);
}
} else if (file.isFile()) {
f = new File(local, file.getFilename());
if (f.exists() &&
(f.length() == file.getAttributes().getSize().longValue()) &&
((f.lastModified() / 1000) == file.getAttributes()
.getModifiedTime()
.longValue())) {
if (commit) {
op.addUnchangedFile(f);
} else {
op.addUnchangedFile(file);
}
continue;
}
if (f.exists()) {
if (commit) {
op.addUpdatedFile(f);
} else {
op.addUpdatedFile(file);
}
} else {
if (commit) {
op.addNewFile(f);
} else {
op.addNewFile(file);
}
}
if (commit) {
FileAttributes attrs = get(file.getFilename(),
f.getAbsolutePath(), progress);
f.setLastModified(attrs.getModifiedTime().longValue() * 1000);
}
}
}
if (sync) {
// List the contents of the new local directory and remove any
// files/directories that were not updated
File[] contents = local.listFiles();
if (contents != null) {
for (int i = 0; i < contents.length; i++) {
if (!op.containsFile(contents[i])) {
op.addDeletedFile(contents[i]);
if (contents[i].isDirectory() &&
!contents[i].getName().equals(".") &&
!contents[i].getName().equals("..")) {
recurseMarkForDeletion(contents[i], op);
if (commit) {
IOUtil.recurseDeleteDirectory(contents[i]);
}
} else if (commit) {
contents[i].delete();
}
}
}
}
}
cd(pwd);
return op;
}
|
public FileAttributes get(String path) throws IOException {
return get(path, (FileTransferProgress) null);
}
|
public FileAttributes get(String path,
FileTransferProgress progress) throws IOException, TransferCancelledException {
String localfile;
if (path.lastIndexOf("/") > -1) {
localfile = path.substring(path.lastIndexOf("/") + 1);
} else {
localfile = path;
}
return get(path, localfile, progress);
}
|
public FileAttributes get(String remote,
String local) throws IOException {
return get(remote, local, null);
}
|
public FileAttributes get(String remote,
OutputStream local) throws IOException {
return get(remote, local, null);
}
|
public FileAttributes get(String remote,
String local,
FileTransferProgress progress) throws IOException, TransferCancelledException {
File localPath = resolveLocalPath(local);
if (!localPath.exists()) {
localPath.getParentFile().mkdirs();
localPath.createNewFile();
}
FileOutputStream out = new FileOutputStream(localPath);
return get(remote, out, progress);
}
Download the remote file to the local computer. If the paths provided
are not absolute the current working directory is used.
|
public FileAttributes get(String remote,
OutputStream local,
FileTransferProgress progress) throws IOException, TransferCancelledException {
String remotePath = resolveRemotePath(remote);
FileAttributes attrs = stat(remotePath);
if (progress != null) {
progress.started(attrs.getSize().longValue(), remotePath);
}
SftpFileInputStream in = new SftpFileInputStream(sftp.openFile(
remotePath, SftpSubsystemClient.OPEN_READ));
transferFile(in, local, progress);
if (progress != null) {
progress.completed();
}
return attrs;
}
Download the remote file writing it to the specified
OutputStream. The OutputStream is closed by this mehtod
even if the operation fails.
|
public String getAbsolutePath(String path) throws IOException {
String actual = resolveRemotePath(path);
return sftp.getAbsolutePath(path);
}
|
public boolean isClosed() {
return sftp.isClosed();
}
Returns the state of the SFTP client. The client is closed if the
underlying session channel is closed. Invoking the quit
method of this object will close the underlying session channel.
|
public void lcd(String path) throws IOException {
File actual;
if (!isLocalAbsolutePath(path)) {
actual = new File(lcwd, path);
} else {
actual = new File(path);
}
if (!actual.isDirectory()) {
throw new IOException(path + " is not a directory");
}
lcwd = actual.getCanonicalPath();
}
|
public String lpwd() {
return lcwd;
}
|
public List ls() throws IOException {
return ls(cwd);
}
List the contents of the current remote working directory.
Returns a list of SftpFile instances for the current
working directory.
|
public List ls(String path) throws IOException {
String actual = resolveRemotePath(path);
FileAttributes attrs = sftp.getAttributes(actual);
if (!attrs.isDirectory()) {
throw new IOException(path + " is not a directory");
}
SftpFile file = sftp.openDirectory(actual);
Vector children = new Vector();
while (sftp.listChildren(file, children) > -1) {
;
}
file.close();
return children;
}
List the contents remote directory.
Returns a list of SftpFile instances for the remote
directory.
|
public void mkdir(String dir) throws IOException {
String actual = resolveRemotePath(dir);
try {
FileAttributes attrs = stat(actual);
if (!attrs.isDirectory()) {
throw new IOException("File already exists named " + dir);
}
} catch (IOException ex) {
sftp.makeDirectory(actual);
chmod(default_permissions ^ umask, actual);
}
}
Creates a new directory on the remote server. This method will throw an
exception if the directory already exists. To create directories and
disregard any errors use the mkdirs method.
|
public void mkdirs(String dir) {
StringTokenizer tokens = new StringTokenizer(dir, "/");
String path = dir.startsWith("/") ? "/" : "";
while (tokens.hasMoreElements()) {
path += (String) tokens.nextElement();
try {
stat(path);
} catch (IOException ex) {
try {
mkdir(path);
} catch (IOException ex2) {
}
}
path += "/";
}
}
Create a directory or set of directories. This method will not fail even
if the directories exist. It is advisable to test whether the directory
exists before attempting an operation by using the stat
method to return the directories attributes.
|
public void put(String local) throws IOException {
put(local, (FileTransferProgress) null);
}
|
public void put(String local,
FileTransferProgress progress) throws IOException, TransferCancelledException {
File f = new File(local);
put(local, f.getName(), progress);
}
|
public void put(String local,
String remote) throws IOException {
put(local, remote, null);
}
|
public void put(InputStream in,
String remote) throws IOException {
put(in, remote, null);
}
|
public void put(String local,
String remote,
FileTransferProgress progress) throws IOException, TransferCancelledException {
File localPath = resolveLocalPath(local);
FileInputStream in = new FileInputStream(localPath);
try {
FileAttributes attrs = stat(remote);
if (attrs.isDirectory()) {
File f = new File(local);
remote += ((remote.endsWith("/") ? "" : "/") + f.getName());
}
} catch (IOException ex) {
}
put(in, remote, progress);
}
Upload a file to the remote computer. If the paths provided are not
absolute the current working directory is used.
|
public void put(InputStream in,
String remote,
FileTransferProgress progress) throws IOException, TransferCancelledException {
String remotePath = resolveRemotePath(remote);
SftpFileOutputStream out;
FileAttributes attrs;
boolean newfile = false;
try {
attrs = stat(remotePath);
out = new SftpFileOutputStream(sftp.openFile(remotePath,
SftpSubsystemClient.OPEN_CREATE |
SftpSubsystemClient.OPEN_TRUNCATE |
SftpSubsystemClient.OPEN_WRITE));
} catch (IOException ex) {
attrs = new FileAttributes();
newfile = true;
attrs.setPermissions(new UnsignedInteger32(default_permissions ^
umask));
out = new SftpFileOutputStream(sftp.openFile(remotePath,
SftpSubsystemClient.OPEN_CREATE |
SftpSubsystemClient.OPEN_WRITE, attrs));
}
if (progress != null) {
progress.started(in.available(), remotePath);
}
transferFile(in, out, progress);
if (progress != null) {
progress.completed();
}
// Set the permissions here since at creation they dont always work
if (newfile) {
chmod(default_permissions ^ umask, remotePath);
}
}
Upload a file to the remote computer reading from the specified
InputStream. The InputStream is closed, even if the operation
fails.
|
public String pwd() {
return cwd;
}
|
public void quit() throws IOException {
sftp.close();
}
|
public void rename(String oldpath,
String newpath) throws IOException {
String from = resolveRemotePath(oldpath);
String to = resolveRemotePath(newpath);
sftp.renameFile(from, to);
}
|
public void rm(String path) throws IOException {
String actual = resolveRemotePath(path);
FileAttributes attrs = sftp.getAttributes(actual);
if (attrs.isDirectory()) {
sftp.removeDirectory(actual);
} else {
sftp.removeFile(actual);
}
}
|
public void rm(String path,
boolean force,
boolean recurse) throws IOException {
String actual = resolveRemotePath(path);
FileAttributes attrs = sftp.getAttributes(actual);
SftpFile file;
if (attrs.isDirectory()) {
List list = ls(path);
if (!force && (list.size() > 0)) {
throw new IOException(
"You cannot delete non-empty directory, use force=true to overide");
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
file = (SftpFile) it.next();
if (file.isDirectory() && !file.getFilename().equals(".") &&
!file.getFilename().equals("..")) {
if (recurse) {
rm(file.getAbsolutePath(), force, recurse);
} else {
throw new IOException(
"Directory has contents, cannot delete without recurse=true");
}
} else if (file.isFile()) {
sftp.removeFile(file.getAbsolutePath());
}
}
}
sftp.removeDirectory(actual);
} else {
sftp.removeFile(actual);
}
}
|
public FileAttributes stat(String path) throws IOException {
String actual = resolveRemotePath(path);
return sftp.getAttributes(actual);
}
|
public void symlink(String path,
String link) throws IOException {
String actualPath = resolveRemotePath(path);
String actualLink = resolveRemotePath(link);
sftp.createSymbolicLink(actualPath, actualLink);
}
|
public int umask(int umask) {
int old = umask;
this.umask = umask;
return old;
}
Sets the umask used by this client. |
public void umask(String umask) throws IOException {
try {
this.umask = Integer.parseInt(umask, 8);
} catch (NumberFormatException ex) {
throw new IOException(
"umask must be 4 digit octal number e.g. 0022");
}
}
|