Search in sources :

Example 1 with SFTPException

use of net.schmizz.sshj.sftp.SFTPException in project AmazeFileManager by TeamAmaze.

the class HybridFile method getUsableSpace.

/**
 * Gets usable i.e. free space of a device
 * @return
 */
public long getUsableSpace() {
    long size = 0L;
    switch(mode) {
        case SMB:
            try {
                size = (new SmbFile(path).getDiskFreeSpace());
            } catch (MalformedURLException e) {
                size = 0L;
                e.printStackTrace();
            } catch (SmbException e) {
                size = 0L;
                e.printStackTrace();
            }
            break;
        case FILE:
        case ROOT:
            size = new File(path).getUsableSpace();
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
            size = spaceAllocation.getTotal() - spaceAllocation.getUsed();
            break;
        case SFTP:
            size = SshClientUtils.execute(new SFtpClientTemplate(path) {

                @Override
                public Long execute(@NonNull SFTPClient client) throws IOException {
                    try {
                        Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
                        return response.diskFreeSpace();
                    } catch (SFTPException e) {
                        Log.e(TAG, "Error querying server", e);
                        return 0L;
                    } catch (Buffer.BufferException e) {
                        Log.e(TAG, "Error parsing reply", e);
                        return 0L;
                    }
                }
            });
            break;
        case OTG:
            // TODO: Get free space from OTG when {@link DocumentFile} API adds support
            break;
    }
    return size;
}
Also used : Buffer(net.schmizz.sshj.common.Buffer) MalformedURLException(java.net.MalformedURLException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Statvfs(com.amaze.filemanager.filesystem.ssh.Statvfs) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) NonNull(android.support.annotation.NonNull) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 2 with SFTPException

use of net.schmizz.sshj.sftp.SFTPException in project AmazeFileManager by TeamAmaze.

the class HybridFile method getTotal.

/**
 * Gets total size of the disk
 * @param context
 * @return
 */
public long getTotal(Context context) {
    long size = 0l;
    switch(mode) {
        case SMB:
            // TODO: Find total storage space of SMB when JCIFS adds support
            try {
                size = new SmbFile(path).getDiskFreeSpace();
            } catch (SmbException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            break;
        case FILE:
        case ROOT:
            size = new File(path).getTotalSpace();
            break;
        case DROPBOX:
        case BOX:
        case ONEDRIVE:
        case GDRIVE:
            SpaceAllocation spaceAllocation = dataUtils.getAccount(mode).getAllocation();
            size = spaceAllocation.getTotal();
            break;
        case SFTP:
            size = SshClientUtils.execute(new SFtpClientTemplate(path) {

                @Override
                public Long execute(@NonNull SFTPClient client) throws IOException {
                    try {
                        Statvfs.Response response = new Statvfs.Response(path, client.getSFTPEngine().request(Statvfs.request(client, SshClientUtils.extractRemotePathFrom(path))).retrieve());
                        return response.diskSize();
                    } catch (SFTPException e) {
                        Log.e(TAG, "Error querying server", e);
                        return 0L;
                    } catch (Buffer.BufferException e) {
                        Log.e(TAG, "Error parsing reply", e);
                        return 0L;
                    }
                }
            });
            break;
        case OTG:
            // TODO: Find total storage space of OTG when {@link DocumentFile} API adds support
            DocumentFile documentFile = OTGUtil.getDocumentFile(path, context, false);
            documentFile.length();
            break;
    }
    return size;
}
Also used : Buffer(net.schmizz.sshj.common.Buffer) MalformedURLException(java.net.MalformedURLException) DocumentFile(android.support.v4.provider.DocumentFile) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) Statvfs(com.amaze.filemanager.filesystem.ssh.Statvfs) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) SpaceAllocation(com.cloudrail.si.types.SpaceAllocation) NonNull(android.support.annotation.NonNull) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 3 with SFTPException

use of net.schmizz.sshj.sftp.SFTPException in project AmazeFileManager by TeamAmaze.

the class HybridFile method exists.

public boolean exists() {
    boolean exists = false;
    if (isSftp()) {
        exists = SshClientUtils.execute(new SFtpClientTemplate(path) {

            @Override
            public Boolean execute(SFTPClient client) throws IOException {
                try {
                    return client.stat(SshClientUtils.extractRemotePathFrom(path)) != null;
                } catch (SFTPException notFound) {
                    return false;
                }
            }
        });
    } else if (isSmb()) {
        try {
            SmbFile smbFile = getSmbFile(2000);
            exists = smbFile != null && smbFile.exists();
        } catch (SmbException e) {
            exists = false;
        }
    } else if (isDropBoxFile()) {
        CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
        exists = cloudStorageDropbox.exists(CloudUtil.stripPath(OpenMode.DROPBOX, path));
    } else if (isBoxFile()) {
        CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
        exists = cloudStorageBox.exists(CloudUtil.stripPath(OpenMode.BOX, path));
    } else if (isGoogleDriveFile()) {
        CloudStorage cloudStorageGoogleDrive = dataUtils.getAccount(OpenMode.GDRIVE);
        exists = cloudStorageGoogleDrive.exists(CloudUtil.stripPath(OpenMode.GDRIVE, path));
    } else if (isOneDriveFile()) {
        CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
        exists = cloudStorageOneDrive.exists(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
    } else if (isLocal()) {
        exists = new File(path).exists();
    } else if (isRoot()) {
        try {
            return RootHelper.fileExists(path);
        } catch (ShellNotRunningException e) {
            e.printStackTrace();
            return false;
        }
    }
    return exists;
}
Also used : SmbException(jcifs.smb.SmbException) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteFile(net.schmizz.sshj.sftp.RemoteFile) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile)

Aggregations

DocumentFile (android.support.v4.provider.DocumentFile)3 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)3 File (java.io.File)3 SmbException (jcifs.smb.SmbException)3 SmbFile (jcifs.smb.SmbFile)3 RemoteFile (net.schmizz.sshj.sftp.RemoteFile)3 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)3 SFTPException (net.schmizz.sshj.sftp.SFTPException)3 NonNull (android.support.annotation.NonNull)2 Statvfs (com.amaze.filemanager.filesystem.ssh.Statvfs)2 SpaceAllocation (com.cloudrail.si.types.SpaceAllocation)2 MalformedURLException (java.net.MalformedURLException)2 Buffer (net.schmizz.sshj.common.Buffer)2 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)1 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)1