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);
}
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;
}
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();
}
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);
}
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;
}
Aggregations