Search in sources :

Example 1 with ShellNotRunningException

use of com.amaze.filemanager.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class Operations method mkfile.

public static void mkfile(@NonNull final HybridFile file, final Context context, final boolean rootMode, @NonNull final ErrorCallBack errorCallBack) {
    new AsyncTask<Void, Void, Void>() {

        private DataUtils dataUtils = DataUtils.getInstance();

        @Override
        protected Void doInBackground(Void... params) {
            // check whether filename is valid or not
            if (!Operations.isFileNameValid(file.getName(context))) {
                errorCallBack.invalidName(file);
                return null;
            }
            if (file.exists()) {
                errorCallBack.exists(file);
                return null;
            }
            if (file.isSftp()) {
                OutputStream out = file.getOutputStream(context);
                try {
                    out.close();
                    errorCallBack.done(file, true);
                    return null;
                } catch (IOException e) {
                    errorCallBack.done(file, false);
                    return null;
                }
            }
            if (file.isSmb()) {
                try {
                    file.getSmbFile(2000).createNewFile();
                } catch (SmbException e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                    return null;
                }
                errorCallBack.done(file, file.exists());
                return null;
            } else if (file.isDropBoxFile()) {
                CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isBoxFile()) {
                CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isOneDriveFile()) {
                CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageOneDrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isGoogleDriveFile()) {
                CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
                try {
                    byte[] tempBytes = new byte[0];
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
                    cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
                    errorCallBack.done(file, true);
                } catch (Exception e) {
                    e.printStackTrace();
                    errorCallBack.done(file, false);
                }
            } else if (file.isOtgFile()) {
                // first check whether new file already exists
                DocumentFile fileToCreate = OTGUtil.getDocumentFile(file.getPath(), context, false);
                if (fileToCreate != null)
                    errorCallBack.exists(file);
                DocumentFile parentDirectory = OTGUtil.getDocumentFile(file.getParent(), context, false);
                if (parentDirectory.isDirectory()) {
                    parentDirectory.createFile(file.getName(context).substring(file.getName().lastIndexOf(".")), file.getName(context));
                    errorCallBack.done(file, true);
                } else
                    errorCallBack.done(file, false);
                return null;
            } else {
                if (file.isLocal() || file.isRoot()) {
                    int mode = checkFolder(new File(file.getParent()), context);
                    if (mode == 2) {
                        errorCallBack.launchSAF(file);
                        return null;
                    }
                    if (mode == 1 || mode == 0)
                        try {
                            FileUtil.mkfile(file.getFile(), context);
                        } catch (IOException e) {
                        }
                    if (!file.exists() && rootMode) {
                        file.setMode(OpenMode.ROOT);
                        if (file.exists())
                            errorCallBack.exists(file);
                        try {
                            RootUtils.mkFile(file.getPath());
                        } catch (ShellNotRunningException e) {
                            e.printStackTrace();
                        }
                        errorCallBack.done(file, file.exists());
                        return null;
                    }
                    errorCallBack.done(file, file.exists());
                    return null;
                }
                errorCallBack.done(file, file.exists());
            }
            return null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) IOException(java.io.IOException) SmbException(jcifs.smb.SmbException) SmbException(jcifs.smb.SmbException) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) DataUtils(com.amaze.filemanager.utils.DataUtils) File(java.io.File) SmbFile(jcifs.smb.SmbFile) DocumentFile(android.support.v4.provider.DocumentFile)

Example 2 with ShellNotRunningException

use of com.amaze.filemanager.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class RootHelper method runShellCommand.

/**
 * Runs the command and stores output in a list. The listener is set on the handler
 * thread {@link MainActivity#handlerThread} thus any code run in callback must be thread safe.
 * Command is run from the root context (u:r:SuperSU0)
 *
 * @param cmd the command
 * @return a list of results. Null only if the command passed is a blocking call or no output is
 * there for the command passed
 */
public static ArrayList<String> runShellCommand(String cmd) throws ShellNotRunningException {
    if (MainActivity.shellInteractive == null || !MainActivity.shellInteractive.isRunning())
        throw new ShellNotRunningException();
    final ArrayList<String> result = new ArrayList<>();
    // callback being called on a background handler thread
    MainActivity.shellInteractive.addCommand(cmd, 0, (commandCode, exitCode, output) -> {
        for (String line : output) {
            result.add(line);
        }
    });
    MainActivity.shellInteractive.waitForIdle();
    return result;
}
Also used : ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) ArrayList(java.util.ArrayList)

Example 3 with ShellNotRunningException

use of com.amaze.filemanager.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class RootHelper method isDirectory.

/**
 * Whether toTest file is directory or not
 *
 * @param toTest
 * @param root
 * @param count
 * @return TODO: Avoid parsing ls
 */
public static boolean isDirectory(String toTest, boolean root, int count) throws ShellNotRunningException {
    File f = new File(toTest);
    String name = f.getName();
    String p = f.getParent();
    if (p != null && p.length() > 0) {
        ArrayList<String> ls = runShellCommand("ls -l " + p);
        for (String s : ls) {
            if (contains(s.split(" "), name)) {
                try {
                    HybridFileParcelable path = FileUtils.parseName(s);
                    if (path.getPermission().trim().startsWith("d"))
                        return true;
                    else if (path.getPermission().trim().startsWith("l")) {
                        if (count > 5)
                            return f.isDirectory();
                        else
                            return isDirectory(path.getLink().trim(), root, ++count);
                    } else
                        return f.isDirectory();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    return f.isDirectory();
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException)

Example 4 with ShellNotRunningException

use of com.amaze.filemanager.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class RootHelper method getFiles.

/**
 * Get files using shell, supposing the path is not a SMB/OTG/Custom (*.apk/images)
 *
 * @param path
 * @param root            whether root is available or not
 * @param showHidden      to show hidden files
 * @param getModeCallBack callback to set the type of file
 * @return TODO: Avoid parsing ls
 */
public static void getFiles(String path, boolean root, boolean showHidden, GetModeCallBack getModeCallBack, OnFileFound fileCallback) {
    OpenMode mode = OpenMode.FILE;
    ArrayList<HybridFileParcelable> files = new ArrayList<>();
    if (root && !path.startsWith("/storage") && !path.startsWith("/sdcard")) {
        try {
            // we're rooted and we're trying to load file with superuser
            // we're at the root directories, superuser is required!
            ArrayList<String> ls;
            String cpath = getCommandLineString(path);
            // ls = Shell.SU.run("ls -l " + cpath);
            ls = runShellCommand("ls -l " + (showHidden ? "-a " : "") + "\"" + cpath + "\"");
            if (ls != null) {
                for (int i = 0; i < ls.size(); i++) {
                    String file = ls.get(i);
                    if (!file.contains("Permission denied")) {
                        HybridFileParcelable array = FileUtils.parseName(file);
                        if (array != null) {
                            array.setMode(OpenMode.ROOT);
                            array.setName(array.getPath());
                            array.setPath(path + "/" + array.getPath());
                            if (array.getLink().trim().length() > 0) {
                                boolean isdirectory = isDirectory(array.getLink(), root, 0);
                                array.setDirectory(isdirectory);
                            } else
                                array.setDirectory(isDirectory(array));
                            files.add(array);
                            fileCallback.onFileFound(array);
                        }
                    }
                }
                mode = OpenMode.ROOT;
            }
            if (getModeCallBack != null)
                getModeCallBack.getMode(mode);
        } catch (ShellNotRunningException e) {
            e.printStackTrace();
        }
    }
    if (FileUtils.canListFiles(new File(path))) {
        // we're taking a chance to load files using basic java filesystem
        getFilesList(path, showHidden, fileCallback);
        mode = OpenMode.FILE;
    } else {
        // we couldn't load files using native java filesystem callbacks
        // maybe the access is not allowed due to android system restrictions, we'll see later
        mode = OpenMode.FILE;
    }
    if (getModeCallBack != null)
        getModeCallBack.getMode(mode);
}
Also used : ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) ArrayList(java.util.ArrayList) DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) OpenMode(com.amaze.filemanager.utils.OpenMode)

Example 5 with ShellNotRunningException

use of com.amaze.filemanager.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.

the class WriteFileAbstraction method doInBackground.

@Override
protected Integer doInBackground(Void... voids) {
    try {
        OutputStream outputStream;
        switch(fileAbstraction.scheme) {
            case EditableFileAbstraction.SCHEME_CONTENT:
                if (fileAbstraction.uri == null)
                    throw new NullPointerException("Something went really wrong!");
                try {
                    outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
                } catch (RuntimeException e) {
                    throw new StreamNotFoundException(e);
                }
                break;
            case EditableFileAbstraction.SCHEME_FILE:
                final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
                if (hybridFileParcelable == null)
                    throw new NullPointerException("Something went really wrong!");
                Context context = this.context.get();
                if (context == null) {
                    cancel(true);
                    return null;
                }
                outputStream = FileUtil.getOutputStream(hybridFileParcelable.getFile(), context);
                if (isRootExplorer && outputStream == null) {
                    // try loading stream associated using root
                    try {
                        if (cachedFile != null && cachedFile.exists()) {
                            outputStream = new FileOutputStream(cachedFile);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        outputStream = null;
                    }
                }
                break;
            default:
                throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
        }
        if (outputStream == null)
            throw new StreamNotFoundException();
        outputStream.write(dataToSave.getBytes());
        outputStream.close();
        if (cachedFile != null && cachedFile.exists()) {
            // cat cache content to original file and delete cache file
            RootUtils.cat(cachedFile.getPath(), fileAbstraction.hybridFileParcelable.getPath());
            cachedFile.delete();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return EXCEPTION_IO;
    } catch (StreamNotFoundException e) {
        e.printStackTrace();
        return EXCEPTION_STREAM_NOT_FOUND;
    } catch (ShellNotRunningException e) {
        e.printStackTrace();
        return EXCEPTION_SHELL_NOT_RUNNING;
    }
    return NORMAL;
}
Also used : HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) Context(android.content.Context) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) StreamNotFoundException(com.amaze.filemanager.exceptions.StreamNotFoundException) IOException(java.io.IOException)

Aggregations

ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)14 File (java.io.File)9 DocumentFile (android.support.v4.provider.DocumentFile)7 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)6 IOException (java.io.IOException)5 SmbException (jcifs.smb.SmbException)5 SmbFile (jcifs.smb.SmbFile)5 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)4 DataUtils (com.amaze.filemanager.utils.DataUtils)4 MalformedURLException (java.net.MalformedURLException)4 StreamNotFoundException (com.amaze.filemanager.exceptions.StreamNotFoundException)2 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)2 Context (android.content.Context)1 SpannableString (android.text.SpannableString)1 ArrayAdapter (android.widget.ArrayAdapter)1 CheckBox (android.widget.CheckBox)1