2 * Copyright (C) 2012 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.cyanogenmod.filemanager.util;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.media.MediaScannerConnection;
24 import android.net.Uri;
26 import com.cyanogenmod.filemanager.commands.AsyncResultListener;
27 import com.cyanogenmod.filemanager.commands.ChangeOwnerExecutable;
28 import com.cyanogenmod.filemanager.commands.ChangePermissionsExecutable;
29 import com.cyanogenmod.filemanager.commands.ChecksumExecutable;
30 import com.cyanogenmod.filemanager.commands.CompressExecutable;
31 import com.cyanogenmod.filemanager.commands.ConcurrentAsyncResultListener;
32 import com.cyanogenmod.filemanager.commands.CopyExecutable;
33 import com.cyanogenmod.filemanager.commands.CreateDirExecutable;
34 import com.cyanogenmod.filemanager.commands.CreateFileExecutable;
35 import com.cyanogenmod.filemanager.commands.DeleteDirExecutable;
36 import com.cyanogenmod.filemanager.commands.DeleteFileExecutable;
37 import com.cyanogenmod.filemanager.commands.DiskUsageExecutable;
38 import com.cyanogenmod.filemanager.commands.EchoExecutable;
39 import com.cyanogenmod.filemanager.commands.ExecExecutable;
40 import com.cyanogenmod.filemanager.commands.Executable;
41 import com.cyanogenmod.filemanager.commands.FindExecutable;
42 import com.cyanogenmod.filemanager.commands.FolderUsageExecutable;
43 import com.cyanogenmod.filemanager.commands.GroupsExecutable;
44 import com.cyanogenmod.filemanager.commands.IdentityExecutable;
45 import com.cyanogenmod.filemanager.commands.LinkExecutable;
46 import com.cyanogenmod.filemanager.commands.ListExecutable;
47 import com.cyanogenmod.filemanager.commands.MountExecutable;
48 import com.cyanogenmod.filemanager.commands.MountPointInfoExecutable;
49 import com.cyanogenmod.filemanager.commands.MoveExecutable;
50 import com.cyanogenmod.filemanager.commands.ParentDirExecutable;
51 import com.cyanogenmod.filemanager.commands.ProcessIdExecutable;
52 import com.cyanogenmod.filemanager.commands.QuickFolderSearchExecutable;
53 import com.cyanogenmod.filemanager.commands.ReadExecutable;
54 import com.cyanogenmod.filemanager.commands.ResolveLinkExecutable;
55 import com.cyanogenmod.filemanager.commands.SIGNAL;
56 import com.cyanogenmod.filemanager.commands.SendSignalExecutable;
57 import com.cyanogenmod.filemanager.commands.SyncResultExecutable;
58 import com.cyanogenmod.filemanager.commands.UncompressExecutable;
59 import com.cyanogenmod.filemanager.commands.WritableExecutable;
60 import com.cyanogenmod.filemanager.commands.WriteExecutable;
61 import com.cyanogenmod.filemanager.commands.shell.InvalidCommandDefinitionException;
62 import com.cyanogenmod.filemanager.console.AuthenticationFailedException;
63 import com.cyanogenmod.filemanager.console.CancelledOperationException;
64 import com.cyanogenmod.filemanager.console.CommandNotFoundException;
65 import com.cyanogenmod.filemanager.console.Console;
66 import com.cyanogenmod.filemanager.console.ConsoleAllocException;
67 import com.cyanogenmod.filemanager.console.ConsoleBuilder;
68 import com.cyanogenmod.filemanager.console.ExecutionException;
69 import com.cyanogenmod.filemanager.console.InsufficientPermissionsException;
70 import com.cyanogenmod.filemanager.console.NoSuchFileOrDirectory;
71 import com.cyanogenmod.filemanager.console.OperationTimeoutException;
72 import com.cyanogenmod.filemanager.console.ReadOnlyFilesystemException;
73 import com.cyanogenmod.filemanager.console.VirtualMountPointConsole;
74 import com.cyanogenmod.filemanager.console.secure.SecureConsole;
75 import com.cyanogenmod.filemanager.model.Directory;
76 import com.cyanogenmod.filemanager.model.DiskUsage;
77 import com.cyanogenmod.filemanager.model.FileSystemObject;
78 import com.cyanogenmod.filemanager.model.FolderUsage;
79 import com.cyanogenmod.filemanager.model.Group;
80 import com.cyanogenmod.filemanager.model.Identity;
81 import com.cyanogenmod.filemanager.model.MountPoint;
82 import com.cyanogenmod.filemanager.model.ParentDirectory;
83 import com.cyanogenmod.filemanager.model.Permissions;
84 import com.cyanogenmod.filemanager.model.Query;
85 import com.cyanogenmod.filemanager.model.SearchResult;
86 import com.cyanogenmod.filemanager.model.User;
87 import com.cyanogenmod.filemanager.preferences.CompressionMode;
88 import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
91 import java.io.FileNotFoundException;
92 import java.io.IOException;
93 import java.util.ArrayList;
94 import java.util.List;
95 import java.util.Stack;
99 * A helper class with useful methods for deal with commands.
101 public final class CommandHelper {
104 * A wrapper class for asynchronous operations that need restore the filesystem
105 * after the operation.
107 private static class UnmountAsyncResultListener implements AsyncResultListener {
110 AsyncResultListener mRef;
111 boolean mUnmount = false;
113 MountPoint mMountPoint;
116 * Constructor of <code>UnmountAsyncResultListener</code>
118 public UnmountAsyncResultListener() {
126 public void onAsyncStart() {
127 if (this.mRef != null) {
128 this.mRef.onAsyncStart();
136 public void onAsyncEnd(boolean cancelled) {
137 if (this.mRef != null) {
138 this.mRef.onAsyncEnd(cancelled);
146 public void onAsyncExitCode(int exitCode) {
147 if (this.mRef != null) {
148 this.mRef.onAsyncExitCode(exitCode);
151 // Now if that the process has finished check if the operation
152 // requires to unmount the filesystem
153 if (this.mUnmount && this.mConsole != null &&
154 this.mMountPoint != null && this.mCtx != null) {
155 // Run in background because the console is still executing
157 Thread t = new Thread() {
161 MountExecutable unmountExecutable =
162 UnmountAsyncResultListener.this.mConsole.
163 getExecutableFactory().newCreator().
164 createMountExecutable(
165 UnmountAsyncResultListener.this.mMountPoint,
167 UnmountAsyncResultListener.this.mConsole.execute(
168 unmountExecutable, mCtx);
169 } catch (Exception e) {
170 // Capture the exception but not show to the user
171 ExceptionUtil.translateException(
172 UnmountAsyncResultListener.this.mCtx, e, true, false);
184 public void onPartialResult(Object result) {
185 if (this.mRef != null) {
186 this.mRef.onPartialResult(result);
194 public void onException(Exception cause) {
195 if (this.mRef != null) {
196 this.mRef.onException(cause);
202 * Constructor of <code>CommandHelper</code>.
204 private CommandHelper() {
209 * Method that changes the owner of a file system object.
211 * @param context The current context (needed if console == null)
212 * @param src The file system object to change its permissions
213 * @param user The new user owner of the file system object
214 * @param group The new group owner of the file system object
215 * @param console The console in which execute the program. <code>null</code>
216 * to attach to the default console
217 * @return boolean The operation result
218 * @throws FileNotFoundException If the initial directory not exists
219 * @throws IOException If initial directory couldn't be checked
220 * @throws InvalidCommandDefinitionException If the command has an invalid definition
221 * @throws NoSuchFileOrDirectory If the file or directory was not found
222 * @throws ConsoleAllocException If the console can't be allocated
223 * @throws InsufficientPermissionsException If an operation requires elevated permissions
224 * @throws CommandNotFoundException If the command was not found
225 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
226 * @throws ExecutionException If the operation returns a invalid exit code
227 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
228 * @throws CancelledOperationException If the operation was cancelled
229 * @see ChangeOwnerExecutable
231 public static boolean changeOwner(
232 Context context, String src, User user, Group group, Console console)
233 throws FileNotFoundException, IOException, ConsoleAllocException,
234 NoSuchFileOrDirectory, InsufficientPermissionsException,
235 CommandNotFoundException, OperationTimeoutException, ExecutionException,
236 InvalidCommandDefinitionException, ReadOnlyFilesystemException,
237 CancelledOperationException {
238 Console c = ensureConsole(context, console);
239 ChangeOwnerExecutable executable =
240 c.getExecutableFactory().
241 newCreator().createChangeOwnerExecutable(src, user, group);
242 writableExecute(context, executable, c);
243 return executable.getResult().booleanValue();
247 * Method that changes the permissions of a file system object.
249 * @param context The current context (needed if console == null)
250 * @param src The file system object to change its permissions
251 * @param permissions The new permissions of the file system object
252 * @param console The console in which execute the program. <code>null</code>
253 * to attach to the default console
254 * @return boolean The operation result
255 * @throws FileNotFoundException If the initial directory not exists
256 * @throws IOException If initial directory couldn't be checked
257 * @throws InvalidCommandDefinitionException If the command has an invalid definition
258 * @throws NoSuchFileOrDirectory If the file or directory was not found
259 * @throws ConsoleAllocException If the console can't be allocated
260 * @throws InsufficientPermissionsException If an operation requires elevated permissions
261 * @throws CommandNotFoundException If the command was not found
262 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
263 * @throws ExecutionException If the operation returns a invalid exit code
264 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
265 * @throws CancelledOperationException If the operation was cancelled
266 * @see ChangePermissionsExecutable
268 public static boolean changePermissions(
269 Context context, String src, Permissions permissions, Console console)
270 throws FileNotFoundException, IOException, ConsoleAllocException,
271 NoSuchFileOrDirectory, InsufficientPermissionsException,
272 CommandNotFoundException, OperationTimeoutException,
273 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
274 CancelledOperationException {
275 Console c = ensureConsole(context, console);
276 ChangePermissionsExecutable executable =
277 c.getExecutableFactory().newCreator().
278 createChangePermissionsExecutable(src, permissions);
279 writableExecute(context, executable, c);
280 return executable.getResult().booleanValue();
284 * Method that creates a directory.
286 * @param context The current context (needed if console == null)
287 * @param directory The directory to create
288 * @param console The console in which execute the program. <code>null</code>
289 * to attach to the default console
290 * @return boolean The operation result
291 * @throws FileNotFoundException If the initial directory not exists
292 * @throws IOException If initial directory couldn't be checked
293 * @throws InvalidCommandDefinitionException If the command has an invalid definition
294 * @throws NoSuchFileOrDirectory If the file or directory was not found
295 * @throws ConsoleAllocException If the console can't be allocated
296 * @throws InsufficientPermissionsException If an operation requires elevated permissions
297 * @throws CommandNotFoundException If the command was not found
298 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
299 * @throws ExecutionException If the operation returns a invalid exit code
300 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
301 * @throws CancelledOperationException If the operation was cancelled
302 * @see CreateDirExecutable
304 public static boolean createDirectory(Context context, String directory, Console console)
305 throws FileNotFoundException, IOException, ConsoleAllocException,
306 NoSuchFileOrDirectory, InsufficientPermissionsException,
307 CommandNotFoundException, OperationTimeoutException,
308 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
309 CancelledOperationException {
310 Console c = ensureConsoleForFile(context, console, directory);
311 CreateDirExecutable executable =
312 c.getExecutableFactory().newCreator().createCreateDirectoryExecutable(directory);
313 writableExecute(context, executable, c);
316 MediaScannerConnection.scanFile(context, new String[]{
317 MediaHelper.normalizeMediaPath(directory)}, null, null);
319 return executable.getResult().booleanValue();
323 * Method that creates a file.
325 * @param context The current context (needed if console == null)
326 * @param file The file to create
327 * @param console The console in which execute the program. <code>null</code>
328 * to attach to the default console
329 * @return boolean The operation result
330 * @throws FileNotFoundException If the initial directory not exists
331 * @throws IOException If initial directory couldn't be checked
332 * @throws InvalidCommandDefinitionException If the command has an invalid definition
333 * @throws NoSuchFileOrDirectory If the file or directory was not found
334 * @throws ConsoleAllocException If the console can't be allocated
335 * @throws InsufficientPermissionsException If an operation requires elevated permissions
336 * @throws CommandNotFoundException If the command was not found
337 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
338 * @throws ExecutionException If the operation returns a invalid exit code
339 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
340 * @throws CancelledOperationException If the operation was cancelled
341 * @see CreateFileExecutable
343 public static boolean createFile(Context context, String file, Console console)
344 throws FileNotFoundException, IOException, ConsoleAllocException,
345 NoSuchFileOrDirectory, InsufficientPermissionsException,
346 CommandNotFoundException, OperationTimeoutException,
347 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
348 CancelledOperationException {
349 Console c = ensureConsoleForFile(context, console, file);
350 CreateFileExecutable executable =
351 c.getExecutableFactory().newCreator().createCreateFileExecutable(file);
352 writableExecute(context, executable, c);
355 MediaScannerConnection.scanFile(context, new String[]{
356 MediaHelper.normalizeMediaPath(file)}, null, null);
358 return executable.getResult().booleanValue();
362 * Method that deletes a directory.
364 * @param context The current context (needed if console == null)
365 * @param directory The directory to delete
366 * @param console The console in which execute the program. <code>null</code>
367 * to attach to the default console
368 * @return boolean The operation result
369 * @throws FileNotFoundException If the initial directory not exists
370 * @throws IOException If initial directory couldn't be checked
371 * @throws InvalidCommandDefinitionException If the command has an invalid definition
372 * @throws NoSuchFileOrDirectory If the file or directory was not found
373 * @throws ConsoleAllocException If the console can't be allocated
374 * @throws InsufficientPermissionsException If an operation requires elevated permissions
375 * @throws CommandNotFoundException If the command was not found
376 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
377 * @throws ExecutionException If the operation returns a invalid exit code
378 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
379 * @throws CancelledOperationException If the operation was cancelled
380 * @see DeleteDirExecutable
382 public static boolean deleteDirectory(Context context, String directory, Console console)
383 throws FileNotFoundException, IOException, ConsoleAllocException,
384 NoSuchFileOrDirectory, InsufficientPermissionsException,
385 CommandNotFoundException, OperationTimeoutException,
386 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
387 CancelledOperationException {
388 Console c = ensureConsoleForFile(context, console, directory);
389 DeleteDirExecutable executable =
390 c.getExecutableFactory().newCreator().createDeleteDirExecutable(directory);
391 writableExecute(context, executable, c);
394 MediaScannerConnection.scanFile(context, new String[]{
395 MediaHelper.normalizeMediaPath(directory)}, null, null);
397 return executable.getResult().booleanValue();
401 * Method that deletes a file.
403 * @param context The current context (needed if console == null)
404 * @param file The file to delete
405 * @param console The console in which execute the program. <code>null</code>
406 * to attach to the default console
407 * @return boolean The operation result
408 * @throws FileNotFoundException If the initial directory not exists
409 * @throws IOException If initial directory couldn't be checked
410 * @throws InvalidCommandDefinitionException If the command has an invalid definition
411 * @throws NoSuchFileOrDirectory If the file or directory was not found
412 * @throws ConsoleAllocException If the console can't be allocated
413 * @throws InsufficientPermissionsException If an operation requires elevated permissions
414 * @throws CommandNotFoundException If the command was not found
415 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
416 * @throws ExecutionException If the operation returns a invalid exit code
417 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
418 * @throws CancelledOperationException If the operation was cancelled
419 * @see DeleteFileExecutable
421 public static boolean deleteFile(Context context, String file, Console console)
422 throws FileNotFoundException, IOException, ConsoleAllocException,
423 NoSuchFileOrDirectory, InsufficientPermissionsException,
424 CommandNotFoundException, OperationTimeoutException,
425 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
426 CancelledOperationException {
427 Console c = ensureConsoleForFile(context, console, file);
428 DeleteFileExecutable executable =
429 c.getExecutableFactory().newCreator().createDeleteFileExecutable(file);
430 writableExecute(context, executable, c);
432 // Remove from mediascanner
433 MediaScannerConnection.scanFile(context, new String[]{
434 MediaHelper.normalizeMediaPath(file)
437 return executable.getResult().booleanValue();
441 * Method that resolves a symlink to its real file system object.
443 * @param context The current context (needed if console == null)
444 * @param symlink The link to be resolved
445 * @param console The console in which execute the program. <code>null</code>
446 * to attach to the default console
447 * @return String The resolved link
448 * @throws FileNotFoundException If the initial directory not exists
449 * @throws IOException If initial directory couldn't be checked
450 * @throws InvalidCommandDefinitionException If the command has an invalid definition
451 * @throws NoSuchFileOrDirectory If the file or directory was not found
452 * @throws ConsoleAllocException If the console can't be allocated
453 * @throws InsufficientPermissionsException If an operation requires elevated permissions
454 * @throws CommandNotFoundException If the command was not found
455 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
456 * @throws ExecutionException If the operation returns a invalid exit code
457 * @throws CancelledOperationException If the operation was cancelled
458 * @see ResolveLinkExecutable
460 public static FileSystemObject resolveSymlink(Context context, String symlink, Console console)
461 throws FileNotFoundException, IOException, ConsoleAllocException,
462 NoSuchFileOrDirectory, InsufficientPermissionsException,
463 CommandNotFoundException, OperationTimeoutException,
464 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
465 Console c = ensureConsole(context, console);
466 ResolveLinkExecutable executable =
467 c.getExecutableFactory().newCreator().createResolveLinkExecutable(symlink);
468 execute(context, executable, c);
469 return executable.getResult();
473 * Method that retrieves the information of a file system object.
475 * @param context The current context (needed if console == null)
476 * @param src The file system object
477 * @param console The console in which execute the program. <code>null</code>
478 * to attach to the default console
479 * @return FileSystemObject The file system object reference
480 * @throws FileNotFoundException If the initial directory not exists
481 * @throws IOException If initial directory couldn't be checked
482 * @throws InvalidCommandDefinitionException If the command has an invalid definition
483 * @throws NoSuchFileOrDirectory If the file or directory was not found
484 * @throws ConsoleAllocException If the console can't be allocated
485 * @throws InsufficientPermissionsException If an operation requires elevated permissions
486 * @throws CommandNotFoundException If the command was not found
487 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
488 * @throws ExecutionException If the operation returns a invalid exit code
489 * @throws CancelledOperationException If the operation was cancelled
490 * @see ListExecutable
492 public static FileSystemObject getFileInfo(Context context, String src, Console console)
493 throws FileNotFoundException, IOException, ConsoleAllocException,
494 NoSuchFileOrDirectory, InsufficientPermissionsException,
495 CommandNotFoundException, OperationTimeoutException,
496 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
497 return getFileInfo(context, src, true, console);
501 * Method that retrieves the information of a file system object.
503 * @param context The current context (needed if console == null)
504 * @param src The file system object
505 * @param followSymlinks It should be follow the symlinks
506 * @param console The console in which execute the program. <code>null</code>
507 * to attach to the default console
508 * @return FileSystemObject The file system object reference
509 * @throws FileNotFoundException If the initial directory not exists
510 * @throws IOException If initial directory couldn't be checked
511 * @throws InvalidCommandDefinitionException If the command has an invalid definition
512 * @throws NoSuchFileOrDirectory If the file or directory was not found
513 * @throws ConsoleAllocException If the console can't be allocated
514 * @throws InsufficientPermissionsException If an operation requires elevated permissions
515 * @throws CommandNotFoundException If the command was not found
516 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
517 * @throws ExecutionException If the operation returns a invalid exit code
518 * @throws CancelledOperationException If the operation was cancelled
519 * @see ListExecutable
521 public static FileSystemObject getFileInfo(
522 Context context, String src, boolean followSymlinks, Console console)
523 throws FileNotFoundException, IOException, ConsoleAllocException,
524 NoSuchFileOrDirectory, InsufficientPermissionsException,
525 CommandNotFoundException, OperationTimeoutException,
526 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
527 Console c = ensureConsoleForFile(context, console, src);
528 ListExecutable executable =
529 c.getExecutableFactory().
530 newCreator().createFileInfoExecutable(src, followSymlinks);
531 execute(context, executable, c);
532 List<FileSystemObject> files = executable.getResult();
533 if (files != null && files.size() > 0) {
534 // Resolve symlinks prior to return the object
535 if (followSymlinks) {
536 FileHelper.resolveSymlinks(context, files);
544 * Method that retrieves the information of the groups of the current user.
546 * @param context The current context (needed if console == null)
547 * @param console The console in which execute the program. <code>null</code>
548 * to attach to the default console
549 * @return List<Group> The groups of the current user
550 * @throws FileNotFoundException If the initial directory not exists
551 * @throws IOException If initial directory couldn't be checked
552 * @throws InvalidCommandDefinitionException If the command has an invalid definition
553 * @throws NoSuchFileOrDirectory If the file or directory was not found
554 * @throws ConsoleAllocException If the console can't be allocated
555 * @throws InsufficientPermissionsException If an operation requires elevated permissions
556 * @throws CommandNotFoundException If the command was not found
557 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
558 * @throws ExecutionException If the operation returns a invalid exit code
559 * @throws CancelledOperationException If the operation was cancelled
560 * @see GroupsExecutable
562 public static List<Group> getGroups(Context context, Console console)
563 throws FileNotFoundException, IOException, ConsoleAllocException,
564 NoSuchFileOrDirectory, InsufficientPermissionsException,
565 CommandNotFoundException, OperationTimeoutException,
566 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
567 Console c = ensureConsole(context, console);
568 GroupsExecutable executable =
569 c.getExecutableFactory().newCreator().createGroupsExecutable();
570 execute(context, executable, c);
571 return executable.getResult();
575 * Method that retrieves the identity of the current user.
577 * @param context The current context (needed if console == null)
578 * @param console The console in which execute the program. <code>null</code>
579 * to attach to the default console
580 * @return Identity The identity of the current user
581 * @throws FileNotFoundException If the initial directory not exists
582 * @throws IOException If initial directory couldn't be checked
583 * @throws InvalidCommandDefinitionException If the command has an invalid definition
584 * @throws NoSuchFileOrDirectory If the file or directory was not found
585 * @throws ConsoleAllocException If the console can't be allocated
586 * @throws InsufficientPermissionsException If an operation requires elevated permissions
587 * @throws CommandNotFoundException If the command was not found
588 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
589 * @throws ExecutionException If the operation returns a invalid exit code
590 * @throws CancelledOperationException If the operation was cancelled
591 * @see IdentityExecutable
593 public static Identity getIdentity(Context context, Console console)
594 throws FileNotFoundException, IOException, ConsoleAllocException,
595 NoSuchFileOrDirectory, InsufficientPermissionsException,
596 CommandNotFoundException, OperationTimeoutException,
597 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
598 Console c = ensureConsole(context, console);
599 IdentityExecutable executable =
600 c.getExecutableFactory().newCreator().createIdentityExecutable();
601 execute(context, executable, c);
602 return executable.getResult();
606 * Method that creates a symlink of an other file system object.
608 * @param context The current context (needed if console == null)
609 * @param src The absolute path to the source fso
610 * @param link The absolute path to the link fso
611 * @param console The console in which execute the program. <code>null</code>
612 * to attach to the default console
613 * @return boolean The operation result
614 * @throws FileNotFoundException If the initial directory not exists
615 * @throws IOException If initial directory couldn't be checked
616 * @throws InvalidCommandDefinitionException If the command has an invalid definition
617 * @throws NoSuchFileOrDirectory If the file or directory was not found
618 * @throws ConsoleAllocException If the console can't be allocated
619 * @throws InsufficientPermissionsException If an operation requires elevated permissions
620 * @throws CommandNotFoundException If the command was not found
621 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
622 * @throws ExecutionException If the operation returns a invalid exit code
623 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
624 * @throws CancelledOperationException If the operation was cancelled
625 * @see LinkExecutable
627 public static boolean createLink(Context context, String src, String link, Console console)
628 throws FileNotFoundException, IOException, ConsoleAllocException,
629 NoSuchFileOrDirectory, InsufficientPermissionsException,
630 CommandNotFoundException, OperationTimeoutException,
631 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
632 CancelledOperationException {
633 Console c = ensureConsole(context, console);
634 LinkExecutable executable =
635 c.getExecutableFactory().newCreator().createLinkExecutable(src, link);
636 writableExecute(context, executable, c);
637 return executable.getResult().booleanValue();
641 * Method that retrieves the parent directory of a file system object.
643 * @param context The current context (needed if console == null)
644 * @param src The file system object
645 * @param console The console in which execute the program. <code>null</code>
646 * to attach to the default console
647 * @return String The current directory
648 * @throws FileNotFoundException If the initial directory not exists
649 * @throws IOException If initial directory couldn't be checked
650 * @throws InvalidCommandDefinitionException If the command has an invalid definition
651 * @throws NoSuchFileOrDirectory If the file or directory was not found
652 * @throws ConsoleAllocException If the console can't be allocated
653 * @throws InsufficientPermissionsException If an operation requires elevated permissions
654 * @throws CommandNotFoundException If the command was not found
655 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
656 * @throws ExecutionException If the operation returns a invalid exit code
657 * @throws CancelledOperationException If the operation was cancelled
658 * @see ParentDirExecutable
660 public static String getParentDir(Context context, String src, Console console)
661 throws FileNotFoundException, IOException, ConsoleAllocException,
662 NoSuchFileOrDirectory, InsufficientPermissionsException,
663 CommandNotFoundException, OperationTimeoutException,
664 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
665 Console c = ensureConsoleForFile(context, console, src);
666 ParentDirExecutable executable =
667 c.getExecutableFactory().newCreator().createParentDirExecutable(src);
668 execute(context, executable, c);
669 return executable.getResult();
673 * Method that retrieves the value of a variable.
675 * @param context The current context (needed if console == null)
676 * @param msg The message to echo. This message can have one or multiple variables
677 * and text. xe: "This is $VAR_1 the value of $VAR2" or simple "$PATH"
678 * @param console The console in which execute the program. <code>null</code>
679 * to attach to the default console
680 * @return String The value of the variable
681 * @throws FileNotFoundException If the initial directory not exists
682 * @throws IOException If initial directory couldn't be checked
683 * @throws InvalidCommandDefinitionException If the command has an invalid definition
684 * @throws NoSuchFileOrDirectory If the file or directory was not found
685 * @throws ConsoleAllocException If the console can't be allocated
686 * @throws InsufficientPermissionsException If an operation requires elevated permissions
687 * @throws CommandNotFoundException If the command was not found
688 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
689 * @throws ExecutionException If the operation returns a invalid exit code
690 * @throws CancelledOperationException If the operation was cancelled
691 * @see EchoExecutable
693 public static String getVariable(Context context, String msg, Console console)
694 throws FileNotFoundException, IOException, ConsoleAllocException,
695 NoSuchFileOrDirectory, InsufficientPermissionsException,
696 CommandNotFoundException, OperationTimeoutException,
697 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
698 Console c = ensureConsole(context, console);
699 EchoExecutable executable =
700 c.getExecutableFactory().newCreator().createEchoExecutable(msg);
701 execute(context, executable, c);
702 return executable.getResult();
706 * Method that lists a directory.
708 * @param context The current context (needed if console == null)
709 * @param directory The path of the directory to list
710 * @param console The console in which execute the program. <code>null</code>
711 * to attach to the default console
712 * @return List<FileSystemObject> The list of files of the directory
713 * @throws FileNotFoundException If the initial directory not exists
714 * @throws IOException If initial directory couldn't be checked
715 * @throws InvalidCommandDefinitionException If the command has an invalid definition
716 * @throws NoSuchFileOrDirectory If the file or directory was not found
717 * @throws ConsoleAllocException If the console can't be allocated
718 * @throws InsufficientPermissionsException If an operation requires elevated permissions
719 * @throws CommandNotFoundException If the command was not found
720 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
721 * @throws ExecutionException If the operation returns a invalid exit code
722 * @throws CancelledOperationException If the operation was cancelled
723 * @see ListExecutable
725 public static List<FileSystemObject> listFiles(
726 Context context, String directory, Console console)
727 throws FileNotFoundException, IOException, ConsoleAllocException,
728 NoSuchFileOrDirectory, InsufficientPermissionsException,
729 CommandNotFoundException, OperationTimeoutException,
730 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException,
731 CancelledOperationException {
732 Console c = ensureConsoleForFile(context, console, directory);
733 ListExecutable executable =
734 c.getExecutableFactory().newCreator().
735 createListExecutable(directory);
736 execute(context, executable, c);
737 List<FileSystemObject> result = executable.getResult();
738 FileHelper.resolveSymlinks(context, result);
740 // And now we need to verify if the directory is the
741 if (VirtualMountPointConsole.isVirtualStorageDir(directory)) {
742 result.addAll(VirtualMountPointConsole.getVirtualMountableDirectories());
751 * @param srcPath null or the source in which the file is being moved from
752 * @param destPath the root of the path being created
754 private static void recursiveScan(@NonNull final Context context,
755 @Nullable String srcPath,
756 @NonNull String destPath) {
757 ArrayList<String> paths = new ArrayList<>();
758 Stack<FileSystemObject> pathsToScan = new Stack<>();
760 FileSystemObject fso = getFileInfo(context, destPath, null);
764 pathsToScan.push(fso);
765 while (!pathsToScan.isEmpty()) {
766 fso = pathsToScan.pop();
767 if (srcPath != null) {
768 String src = fso.getFullPath().replace(destPath, srcPath);
769 // Add the path to be deleted
770 paths.add(MediaHelper.normalizeMediaPath(src));
772 // Add the path to be added
773 paths.add(MediaHelper.normalizeMediaPath(fso.getFullPath()));
774 if (fso instanceof Directory) {
776 List<FileSystemObject> files =
777 CommandHelper.listFiles(context, fso.getFullPath(), null);
782 for (FileSystemObject file : files) {
783 if (file instanceof ParentDirectory) {
786 pathsToScan.push(file);
789 | ConsoleAllocException
790 | NoSuchFileOrDirectory
791 | InsufficientPermissionsException
792 | CommandNotFoundException
793 | OperationTimeoutException
795 | InvalidCommandDefinitionException e) {
796 // Just skip this directory
800 if (paths.size() > 100) {
801 MediaScannerConnection.scanFile(context, paths.toArray(new String[paths.size()]), null, null);
805 MediaScannerConnection.scanFile(context, paths.toArray(new String[paths.size()]), null, null);
807 | ConsoleAllocException
808 | NoSuchFileOrDirectory
809 | InsufficientPermissionsException
810 | CommandNotFoundException
811 | OperationTimeoutException
813 | InvalidCommandDefinitionException e) {
814 // Just stop scanning
819 * Method that moves a file system object.
821 * @param context The current context (needed if console == null)
822 * @param src The file system object to move
823 * @param dst The destination file system object
824 * @param console The console in which execute the program. <code>null</code>
825 * to attach to the default console
826 * @return boolean The operation result
827 * @throws FileNotFoundException If the initial directory not exists
828 * @throws IOException If initial directory couldn't be checked
829 * @throws InvalidCommandDefinitionException If the command has an invalid definition
830 * @throws NoSuchFileOrDirectory If the file or directory was not found
831 * @throws ConsoleAllocException If the console can't be allocated
832 * @throws InsufficientPermissionsException If an operation requires elevated permissions
833 * @throws CommandNotFoundException If the command was not found
834 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
835 * @throws ExecutionException If the operation returns a invalid exit code
836 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
837 * @throws CancelledOperationException If the operation was cancelled
838 * @see MoveExecutable
840 public static boolean move(Context context, String src, String dst, Console console)
841 throws FileNotFoundException, IOException, ConsoleAllocException,
842 NoSuchFileOrDirectory, InsufficientPermissionsException,
843 CommandNotFoundException, OperationTimeoutException,
844 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
845 CancelledOperationException {
847 Console cSrc = ensureConsoleForFile(context, console, src);
848 Console cDst = ensureConsoleForFile(context, console, dst);
851 if (cSrc.equals(cDst) && !FileHelper.isSamePath(src, dst)) {
852 // Is safe to use the same console
853 MoveExecutable executable =
854 cSrc.getExecutableFactory().newCreator().createMoveExecutable(src, dst);
855 writableExecute(context, executable, cSrc);
856 ret = executable.getResult().booleanValue();
858 // We need to create a temporary file in the external filesystem to make it
859 // available to virtual consoles
861 // 1.- Move to a temporary file with the source console (destination
862 // is a safe location)
863 File tmp = FileHelper.createTempFilename(context, true);
865 MoveExecutable moveExecutable =
866 cSrc.getExecutableFactory().newCreator().createMoveExecutable(
867 src, tmp.getAbsolutePath());
868 writableExecute(context, moveExecutable, cSrc);
869 if (!moveExecutable.getResult().booleanValue()) {
874 // 2.- Move the temporary file to the final filesystem with the destination console
877 cDst.getExecutableFactory().newCreator().createMoveExecutable(
878 tmp.getAbsolutePath(), dst);
879 writableExecute(context, moveExecutable, cDst);
880 if (!moveExecutable.getResult().booleanValue()) {
886 FileHelper.deleteFileOrFolder(tmp);
890 // Do media scan (don't scan the file if is virtual file)
892 File parent = new File(dst).getParentFile();
893 if ((parent != null && !VirtualMountPointConsole.isVirtualStorageResource(parent
894 .getAbsolutePath()))) {
895 recursiveScan(context, src, dst);
903 * Method that copies a file system object.
905 * @param context The current context (needed if console == null)
906 * @param src The file system object to copy
907 * @param dst The destination file system object
908 * @param console The console in which execute the program. <code>null</code>
909 * to attach to the default console
910 * @return boolean The operation result
911 * @throws FileNotFoundException If the initial directory not exists
912 * @throws IOException If initial directory couldn't be checked
913 * @throws InvalidCommandDefinitionException If the command has an invalid definition
914 * @throws NoSuchFileOrDirectory If the file or directory was not found
915 * @throws ConsoleAllocException If the console can't be allocated
916 * @throws InsufficientPermissionsException If an operation requires elevated permissions
917 * @throws CommandNotFoundException If the command was not found
918 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
919 * @throws ExecutionException If the operation returns a invalid exit code
920 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
921 * @throws CancelledOperationException If the operation was cancelled
922 * @see CopyExecutable
924 public static boolean copy(Context context, String src, String dst, Console console)
925 throws FileNotFoundException, IOException, ConsoleAllocException,
926 NoSuchFileOrDirectory, InsufficientPermissionsException,
927 CommandNotFoundException, OperationTimeoutException,
928 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
929 CancelledOperationException {
931 Console cSrc = ensureConsoleForFile(context, console, src);
932 Console cDst = ensureConsoleForFile(context, console, dst);
934 if (cSrc.equals(cDst) && !FileHelper.isSamePath(src, dst)) {
935 // Is safe to use the same console
936 CopyExecutable executable =
937 cSrc.getExecutableFactory().newCreator().createCopyExecutable(src, dst);
938 writableExecute(context, executable, cSrc);
939 ret = executable.getResult().booleanValue();
941 // We need to create a temporary file in the external filesystem to make it
942 // available to virtual consoles
944 // 1.- Copy to a temporary file with the source console (destination
945 // is a safe location)
946 File tmp = FileHelper.createTempFilename(context, true);
948 CopyExecutable copyExecutable =
949 cSrc.getExecutableFactory().newCreator().createCopyExecutable(
950 src, tmp.getAbsolutePath());
951 writableExecute(context, copyExecutable, cSrc);
952 if (!copyExecutable.getResult().booleanValue()) {
956 // 2.- Move the temporary file to the final filesystem with the destination console
958 MoveExecutable moveExecutable =
959 cDst.getExecutableFactory().newCreator().createMoveExecutable(
960 tmp.getAbsolutePath(), dst);
961 writableExecute(context, moveExecutable, cDst);
962 if (!moveExecutable.getResult().booleanValue()) {
968 FileHelper.deleteFileOrFolder(tmp);
972 // Do media scan (don't scan the file if is virtual file)
974 if (!VirtualMountPointConsole.isVirtualStorageResource(dst)) {
975 recursiveScan(context, null, dst);
983 * Method that executes a command.
985 * @param context The current context (needed if console == null)
986 * @param cmd The command to execute
987 * @param asyncResultListener The partial result listener
988 * @param console The console in which execute the program.
989 * <code>null</code> to attach to the default console
990 * @return ExecExecutable The command executed in background
991 * @throws FileNotFoundException If the initial directory not exists
992 * @throws IOException If initial directory couldn't be checked
993 * @throws InvalidCommandDefinitionException If the command has an invalid definition
994 * @throws NoSuchFileOrDirectory If the file or directory was not found
995 * @throws ConsoleAllocException If the console can't be allocated
996 * @throws InsufficientPermissionsException If an operation requires elevated permissions
997 * @throws CommandNotFoundException If the command was not found
998 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
999 * @throws ExecutionException If the operation returns a invalid exit code
1000 * @throws CancelledOperationException If the operation was cancelled
1001 * @see ExecExecutable
1003 public static ExecExecutable exec(
1004 Context context, String cmd, AsyncResultListener asyncResultListener, Console console)
1005 throws FileNotFoundException, IOException, ConsoleAllocException,
1006 NoSuchFileOrDirectory, InsufficientPermissionsException,
1007 CommandNotFoundException, OperationTimeoutException,
1008 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1009 Console c = ensureConsole(context, console);
1010 ExecExecutable executable =
1011 c.getExecutableFactory().newCreator().
1012 createExecExecutable(cmd, asyncResultListener);
1013 execute(context, executable, c);
1018 * Method that does a search in a directory tree seeking for some terms.
1020 * @param context The current context (needed if console == null)
1021 * @param directory The "absolute" directory where start the search
1022 * @param search The terms to be searched
1023 * @param asyncResultListener The partial result listener
1024 * @param console The console in which execute the program.
1025 * <code>null</code> to attach to the default console
1026 * @return FindExecutable The command executed in background
1027 * @throws FileNotFoundException If the initial directory not exists
1028 * @throws IOException If initial directory couldn't be checked
1029 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1030 * @throws NoSuchFileOrDirectory If the file or directory was not found
1031 * @throws ConsoleAllocException If the console can't be allocated
1032 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1033 * @throws CommandNotFoundException If the command was not found
1034 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1035 * @throws ExecutionException If the operation returns a invalid exit code
1036 * @throws CancelledOperationException If the operation was cancelled
1038 * @see FindExecutable
1040 public static FindExecutable findFiles(
1041 Context context, String directory, Query search,
1042 ConcurrentAsyncResultListener asyncResultListener, Console console)
1043 throws FileNotFoundException, IOException, ConsoleAllocException,
1044 NoSuchFileOrDirectory, InsufficientPermissionsException,
1045 CommandNotFoundException, OperationTimeoutException,
1046 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1047 List<Console> consoles = new ArrayList<Console>();
1048 List<FindExecutable> executables = new ArrayList<FindExecutable>();
1049 Console c = ensureConsoleForFile(context, console, directory);
1052 // Obtain all the rest of console that will participate in the search, that aren't the
1054 List<Console> vcs = VirtualMountPointConsole.getVirtualConsoleForSearchPath(directory);
1055 for (int i = vcs.size() - 1; i >= 0; i--) {
1056 Console vc = vcs.get(i);
1061 consoles.addAll(vcs);
1063 // Register all the executables
1064 for (Console cc : consoles) {
1066 cc.getExecutableFactory().newCreator().
1067 createFindExecutable(directory, search, asyncResultListener));
1070 // Launch every executable
1071 int count = executables.size();
1072 for (int i = 0; i < count; i++) {
1073 execute(context, executables.get(i), consoles.get(i));
1076 // Return the first of the executables
1077 return executables.get(0);
1081 * Method that compute the disk usage of a folder.
1083 * @param context The current context (needed if console == null)
1084 * @param directory The "absolute" directory where start the search
1085 * @param asyncResultListener The partial result listener
1086 * @param console The console in which execute the program.
1087 * <code>null</code> to attach to the default console
1088 * @return FolderUsageExecutable The command executed in background
1089 * @throws FileNotFoundException If the initial directory not exists
1090 * @throws IOException If initial directory couldn't be checked
1091 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1092 * @throws NoSuchFileOrDirectory If the file or directory was not found
1093 * @throws ConsoleAllocException If the console can't be allocated
1094 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1095 * @throws CommandNotFoundException If the command was not found
1096 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1097 * @throws ExecutionException If the operation returns a invalid exit code
1098 * @throws CancelledOperationException If the operation was cancelled
1100 * @see FolderUsageExecutable
1102 public static FolderUsageExecutable getFolderUsage(
1103 Context context, String directory,
1104 AsyncResultListener asyncResultListener, Console console)
1105 throws FileNotFoundException, IOException, ConsoleAllocException,
1106 NoSuchFileOrDirectory, InsufficientPermissionsException,
1107 CommandNotFoundException, OperationTimeoutException,
1108 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1109 Console c = ensureConsoleForFile(context, console, directory);
1110 FolderUsageExecutable executable =
1111 c.getExecutableFactory().newCreator().
1112 createFolderUsageExecutable(directory, asyncResultListener);
1113 execute(context, executable, c);
1118 * Method that retrieves the disk usage of all the mount points.
1120 * @param context The current context (needed if console == null)
1121 * @param console The console in which execute the program. <code>null</code>
1122 * to attach to the default console
1123 * @return List<DiskUsage> The disk usage of all the mount points
1124 * @throws FileNotFoundException If the initial directory not exists
1125 * @throws IOException If initial directory couldn't be checked
1126 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1127 * @throws NoSuchFileOrDirectory If the file or directory was not found
1128 * @throws ConsoleAllocException If the console can't be allocated
1129 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1130 * @throws CommandNotFoundException If the command was not found
1131 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1132 * @throws ExecutionException If the operation returns a invalid exit code
1133 * @throws CancelledOperationException If the operation was cancelled
1134 * @see DiskUsageExecutable
1136 public static List<DiskUsage> getDiskUsage(Context context, Console console)
1137 throws FileNotFoundException, IOException, ConsoleAllocException,
1138 NoSuchFileOrDirectory, InsufficientPermissionsException,
1139 CommandNotFoundException, OperationTimeoutException,
1140 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1141 Console c = ensureConsole(context, console);
1142 DiskUsageExecutable executable =
1143 c.getExecutableFactory().newCreator().createDiskUsageExecutable();
1144 execute(context, executable, c);
1145 List<DiskUsage> diskUsage = executable.getResult();
1146 diskUsage.addAll(VirtualMountPointConsole.getVirtualDiskUsage());
1151 * Method that retrieves the disk usage of all mount points.
1153 * @param context The current context (needed if console == null)
1154 * @param dir The directory of which obtain its disk usage
1155 * @param console The console in which execute the program. <code>null</code>
1156 * to attach to the default console
1157 * @return DiskUsage The disk usage information
1158 * @throws FileNotFoundException If the initial directory not exists
1159 * @throws IOException If initial directory couldn't be checked
1160 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1161 * @throws NoSuchFileOrDirectory If the file or directory was not found
1162 * @throws ConsoleAllocException If the console can't be allocated
1163 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1164 * @throws CommandNotFoundException If the command was not found
1165 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1166 * @throws ExecutionException If the operation returns a invalid exit code
1167 * @throws CancelledOperationException If the operation was cancelled
1168 * @see DiskUsageExecutable
1170 public static DiskUsage getDiskUsage(Context context, String dir, Console console)
1171 throws FileNotFoundException, IOException, ConsoleAllocException,
1172 NoSuchFileOrDirectory, InsufficientPermissionsException,
1173 CommandNotFoundException, OperationTimeoutException,
1174 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1176 // Virtual directories don't implement a disk usage command, just return the data if
1177 // the directory belongs to a virtual filesystem
1178 VirtualMountPointConsole vc = VirtualMountPointConsole.getVirtualConsoleForPath(dir);
1180 return vc.getDiskUsage(dir);
1182 Console c = ensureConsole(context, console);
1183 DiskUsageExecutable executable =
1184 c.getExecutableFactory().newCreator().createDiskUsageExecutable(dir);
1185 execute(context, executable, c);
1186 List<DiskUsage> du = executable.getResult();
1187 for (DiskUsage d : du) {
1188 if (d.getMountPoint().equals(dir)) {
1197 * Method that retrieves the information about all mount points.
1199 * @param context The current context (needed if console == null)
1200 * @param console The console in which execute the program. <code>null</code>
1201 * to attach to the default console
1202 * @return List<MountPoint> The filesystem mount points
1203 * @throws FileNotFoundException If the initial directory not exists
1204 * @throws IOException If initial directory couldn't be checked
1205 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1206 * @throws NoSuchFileOrDirectory If the file or directory was not found
1207 * @throws ConsoleAllocException If the console can't be allocated
1208 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1209 * @throws CommandNotFoundException If the command was not found
1210 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1211 * @throws ExecutionException If the operation returns a invalid exit code
1212 * @throws CancelledOperationException If the operation was cancelled
1213 * @see MountPointInfoExecutable
1215 public static List<MountPoint> getMountPoints(Context context, Console console)
1216 throws FileNotFoundException, IOException, ConsoleAllocException,
1217 NoSuchFileOrDirectory, InsufficientPermissionsException,
1218 CommandNotFoundException, OperationTimeoutException,
1219 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1220 Console c = ensureConsole(context, console);
1221 MountPointInfoExecutable executable =
1222 c.getExecutableFactory().newCreator().createMountPointInfoExecutable();
1223 execute(context, executable, c);
1224 List<MountPoint> mountPoints = executable.getResult();
1225 mountPoints.addAll(VirtualMountPointConsole.getVirtualMountPoints());
1230 * Method that re-mounts a filesystem from his mount point info.
1232 * @param context The current context (needed if console == null)
1233 * @param mp The mount point to re-mount
1234 * @param rw Indicates if the operation re-mounted the device as read-write
1235 * @param console The console in which execute the program. <code>null</code>
1236 * to attach to the default console
1237 * @return boolean The operation result
1238 * @throws FileNotFoundException If the initial directory not exists
1239 * @throws IOException If initial directory couldn't be checked
1240 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1241 * @throws NoSuchFileOrDirectory If the file or directory was not found
1242 * @throws ConsoleAllocException If the console can't be allocated
1243 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1244 * @throws CommandNotFoundException If the command was not found
1245 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1246 * @throws ExecutionException If the operation returns a invalid exit code
1247 * @throws CancelledOperationException If the operation was cancelled
1248 * @see MountExecutable
1250 public static boolean remount(Context context, MountPoint mp, boolean rw, Console console)
1251 throws FileNotFoundException, IOException, ConsoleAllocException,
1252 NoSuchFileOrDirectory, InsufficientPermissionsException,
1253 CommandNotFoundException, OperationTimeoutException,
1254 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1255 boolean ret = false;
1256 if (mp.isSecure()) {
1257 // Unmount the secure file system
1258 SecureConsole sc = (SecureConsole) ensureConsoleForFile(
1259 context, console, mp.getMountPoint());
1267 Console c = ensureConsole(context, console);
1268 MountExecutable executable =
1269 c.getExecutableFactory().newCreator().createMountExecutable(mp, rw);
1270 execute(context, executable, c);
1271 ret = executable.getResult().booleanValue();
1275 // Send an broadcast to notify that the mount state of this filesystem changed
1276 Intent intent = new Intent(FileManagerSettings.INTENT_MOUNT_STATUS_CHANGED);
1277 intent.putExtra(FileManagerSettings.EXTRA_MOUNTPOINT, mp.getMountPoint());
1278 intent.putExtra(FileManagerSettings.EXTRA_STATUS, rw
1279 ? MountExecutable.READWRITE : MountExecutable.READONLY);
1280 context.sendBroadcast(intent);
1287 * Method that makes a quick folder search for the passed expression.
1289 * @param context The current context (needed if console == null)
1290 * @param regexp The expression to search
1291 * @param console The console in which execute the program. <code>null</code>
1292 * to attach to the default console
1293 * @return List<String> The list of directories found
1294 * @throws FileNotFoundException If the initial directory not exists
1295 * @throws IOException If initial directory couldn't be checked
1296 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1297 * @throws NoSuchFileOrDirectory If the file or directory was not found
1298 * @throws ConsoleAllocException If the console can't be allocated
1299 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1300 * @throws CommandNotFoundException If the command was not found
1301 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1302 * @throws ExecutionException If the operation returns a invalid exit code
1303 * @throws CancelledOperationException If the operation was cancelled
1304 * @see QuickFolderSearchExecutable
1306 public static List<String> quickFolderSearch(Context context, String regexp, Console console)
1307 throws FileNotFoundException, IOException, ConsoleAllocException,
1308 NoSuchFileOrDirectory, InsufficientPermissionsException,
1309 CommandNotFoundException, OperationTimeoutException,
1310 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1311 Console c = ensureConsole(context, console);
1312 QuickFolderSearchExecutable executable =
1313 c.getExecutableFactory().newCreator().createQuickFolderSearchExecutable(regexp);
1314 execute(context, executable, c);
1315 return executable.getResult();
1319 * Method that retrieves the process identifier of all the processes (a program
1320 * owned by the main process of this application).
1322 * @param context The current context (needed if console == null)
1323 * @param pid The process id of the shell where the command is running
1324 * @param console The console in which execute the program. <code>null</code>
1325 * to attach to the default console
1326 * @return List<Integer> The processes identifiers of the program or <code>null</code> if not exists
1327 * @throws FileNotFoundException If the initial directory not exists
1328 * @throws IOException If initial directory couldn't be checked
1329 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1330 * @throws NoSuchFileOrDirectory If the file or directory was not found
1331 * @throws ConsoleAllocException If the console can't be allocated
1332 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1333 * @throws CommandNotFoundException If the command was not found
1334 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1335 * @throws ExecutionException If the operation returns a invalid exit code
1336 * @throws CancelledOperationException If the operation was cancelled
1337 * @see ProcessIdExecutable
1339 public static List<Integer> getProcessesIds(
1340 Context context, int pid, Console console)
1341 throws FileNotFoundException, IOException, ConsoleAllocException,
1342 NoSuchFileOrDirectory, InsufficientPermissionsException,
1343 CommandNotFoundException, OperationTimeoutException,
1344 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1345 Console c = ensureConsole(context, console);
1346 ProcessIdExecutable executable =
1347 c.getExecutableFactory().newCreator().createProcessIdExecutable(pid);
1348 execute(context, executable, c);
1349 return executable.getResult();
1353 * Method that retrieves the process identifier of a process (a program
1354 * owned by the main process of this application).
1356 * @param context The current context (needed if console == null)
1357 * @param pid The process id of the shell where the command is running
1358 * @param processName The process name
1359 * @param console The console in which execute the program. <code>null</code>
1360 * to attach to the default console
1361 * @return Integer The process identifier of the program or <code>null</code> if not exists
1362 * @throws FileNotFoundException If the initial directory not exists
1363 * @throws IOException If initial directory couldn't be checked
1364 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1365 * @throws NoSuchFileOrDirectory If the file or directory was not found
1366 * @throws ConsoleAllocException If the console can't be allocated
1367 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1368 * @throws CommandNotFoundException If the command was not found
1369 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1370 * @throws ExecutionException If the operation returns a invalid exit code
1371 * @throws CancelledOperationException If the operation was cancelled
1372 * @see ProcessIdExecutable
1374 public static Integer getProcessId(
1375 Context context, int pid, String processName, Console console)
1376 throws FileNotFoundException, IOException, ConsoleAllocException,
1377 NoSuchFileOrDirectory, InsufficientPermissionsException,
1378 CommandNotFoundException, OperationTimeoutException,
1379 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1380 Console c = ensureConsole(context, console);
1381 ProcessIdExecutable executable =
1382 c.getExecutableFactory().newCreator().createProcessIdExecutable(pid, processName);
1383 execute(context, executable, c);
1384 List<Integer> pids = executable.getResult();
1385 if (pids != null && pids.size() > 0) {
1392 * Method that send a signal to a process.
1394 * @param context The current context (needed if console == null)
1395 * @param process The process which to send the signal
1396 * @param signal The signal to send
1397 * @param console The console in which execute the program. <code>null</code>
1398 * to attach to the default console
1399 * @throws FileNotFoundException If the initial directory not exists
1400 * @throws IOException If initial directory couldn't be checked
1401 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1402 * @throws NoSuchFileOrDirectory If the file or directory was not found
1403 * @throws ConsoleAllocException If the console can't be allocated
1404 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1405 * @throws CommandNotFoundException If the command was not found
1406 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1407 * @throws ExecutionException If the operation returns a invalid exit code
1408 * @throws CancelledOperationException If the operation was cancelled
1409 * @see ProcessIdExecutable
1411 public static void sendSignal(
1412 Context context, int process, SIGNAL signal, Console console)
1413 throws FileNotFoundException, IOException, ConsoleAllocException,
1414 NoSuchFileOrDirectory, InsufficientPermissionsException,
1415 CommandNotFoundException, OperationTimeoutException,
1416 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1417 Console c = ensureConsole(context, console);
1418 SendSignalExecutable executable =
1419 c.getExecutableFactory().newCreator().createSendSignalExecutable(process, signal);
1420 execute(context, executable, c);
1424 * Method that send a kill signal to a process.
1426 * @param context The current context (needed if console == null)
1427 * @param process The process which to send the signal
1428 * @param console The console in which execute the program. <code>null</code>
1429 * to attach to the default console
1430 * @throws FileNotFoundException If the initial directory not exists
1431 * @throws IOException If initial directory couldn't be checked
1432 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1433 * @throws NoSuchFileOrDirectory If the file or directory was not found
1434 * @throws ConsoleAllocException If the console can't be allocated
1435 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1436 * @throws CommandNotFoundException If the command was not found
1437 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1438 * @throws ExecutionException If the operation returns a invalid exit code
1439 * @throws CancelledOperationException If the operation was cancelled
1440 * @see ProcessIdExecutable
1442 public static void sendSignal(
1443 Context context, int process, Console console)
1444 throws FileNotFoundException, IOException, ConsoleAllocException,
1445 NoSuchFileOrDirectory, InsufficientPermissionsException,
1446 CommandNotFoundException, OperationTimeoutException,
1447 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1448 Console c = ensureConsole(context, console);
1449 SendSignalExecutable executable =
1450 c.getExecutableFactory().newCreator().createKillExecutable(process);
1451 execute(context, executable, c);
1455 * Method that read data from disk.
1457 * @param context The current context (needed if console == null)
1458 * @param file The file where to read the data
1459 * @param asyncResultListener The partial result listener
1460 * @param console The console in which execute the program.
1461 * <code>null</code> to attach to the default console
1462 * @return ReadExecutable The command executed in background
1463 * @throws FileNotFoundException If the initial directory not exists
1464 * @throws IOException If initial directory couldn't be checked
1465 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1466 * @throws NoSuchFileOrDirectory If the file or directory was not found
1467 * @throws ConsoleAllocException If the console can't be allocated
1468 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1469 * @throws CommandNotFoundException If the command was not found
1470 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1471 * @throws ExecutionException If the operation returns a invalid exit code
1472 * @throws CancelledOperationException If the operation was cancelled
1474 * @see ReadExecutable
1476 public static ReadExecutable read(
1477 Context context, String file,
1478 AsyncResultListener asyncResultListener, Console console)
1479 throws FileNotFoundException, IOException, ConsoleAllocException,
1480 NoSuchFileOrDirectory, InsufficientPermissionsException,
1481 CommandNotFoundException, OperationTimeoutException,
1482 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1483 Console c = ensureConsoleForFile(context, console, file);
1484 ReadExecutable executable =
1485 c.getExecutableFactory().newCreator().
1486 createReadExecutable(file, asyncResultListener);
1487 execute(context, executable, c);
1492 * Method that writes data to disk.
1494 * @param context The current context (needed if console == null)
1495 * @param file The file where to write the data
1496 * @param asyncResultListener The partial result listener
1497 * @param console The console in which execute the program.
1498 * <code>null</code> to attach to the default console
1499 * @return WriteExecutable The command executed in background
1500 * @throws FileNotFoundException If the initial directory not exists
1501 * @throws IOException If initial directory couldn't be checked
1502 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1503 * @throws NoSuchFileOrDirectory If the file or directory was not found
1504 * @throws ConsoleAllocException If the console can't be allocated
1505 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1506 * @throws CommandNotFoundException If the command was not found
1507 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1508 * @throws ExecutionException If the operation returns a invalid exit code
1509 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1510 * @throws CancelledOperationException If the operation was cancelled
1511 * @see WriteExecutable
1513 public static WriteExecutable write(
1514 Context context, String file,
1515 AsyncResultListener asyncResultListener, Console console)
1516 throws FileNotFoundException, IOException, ConsoleAllocException,
1517 NoSuchFileOrDirectory, InsufficientPermissionsException,
1518 CommandNotFoundException, OperationTimeoutException,
1519 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
1520 CancelledOperationException {
1521 Console c = ensureConsoleForFile(context, console, file);
1523 // Create a wrapper listener, for unmount the filesystem if necessary
1524 UnmountAsyncResultListener wrapperListener = new UnmountAsyncResultListener();
1525 wrapperListener.mCtx = context;
1526 wrapperListener.mConsole = c;
1527 wrapperListener.mRef = asyncResultListener;
1529 // Prior to write to disk the data, ensure that can write to the disk using
1530 // createFile method
1532 CreateFileExecutable executable1 =
1533 c.getExecutableFactory().newCreator().createCreateFileExecutable(file);
1534 boolean unmount = writableExecute(context, executable1, c, true);
1535 if (executable1.getResult().booleanValue()) {
1536 // Configure the rest of attributes of the wrapper listener
1537 wrapperListener.mUnmount = unmount;
1538 wrapperListener.mMountPoint = executable1.getDstWritableMountPoint();
1541 WriteExecutable executable2 =
1542 c.getExecutableFactory().newCreator().
1543 createWriteExecutable(file, wrapperListener);
1544 execute(context, executable2, c);
1547 throw new ExecutionException(String.format("Fail to create file %s", file)); //$NON-NLS-1$
1551 * Method that archive-compress file system objects.
1553 * @param context The current context (needed if console == null)
1554 * @param mode The compression mode
1555 * @param dst The destination compressed file
1556 * @param src The array of source files to compress
1557 * @param asyncResultListener The partial result listener
1558 * @param console The console in which execute the program.
1559 * <code>null</code> to attach to the default console
1560 * @return CompressExecutable The command executed in background
1561 * @throws FileNotFoundException If the initial directory not exists
1562 * @throws IOException If initial directory couldn't be checked
1563 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1564 * @throws NoSuchFileOrDirectory If the file or directory was not found
1565 * @throws ConsoleAllocException If the console can't be allocated
1566 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1567 * @throws CommandNotFoundException If the command was not found
1568 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1569 * @throws ExecutionException If the operation returns a invalid exit code
1570 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1571 * @throws CancelledOperationException If the operation was cancelled
1572 * @see CompressExecutable
1574 public static CompressExecutable compress(
1575 Context context, CompressionMode mode, String dst, String[] src,
1576 AsyncResultListener asyncResultListener, Console console)
1577 throws FileNotFoundException, IOException, ConsoleAllocException,
1578 NoSuchFileOrDirectory, InsufficientPermissionsException,
1579 CommandNotFoundException, OperationTimeoutException,
1580 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
1581 CancelledOperationException {
1582 Console c = ensureConsole(context, console);
1584 // Create a wrapper listener, for unmount the filesystem if necessary
1585 UnmountAsyncResultListener wrapperListener = new UnmountAsyncResultListener();
1586 wrapperListener.mCtx = context;
1587 wrapperListener.mConsole = c;
1588 wrapperListener.mRef = asyncResultListener;
1590 CompressExecutable executable1 =
1591 c.getExecutableFactory().newCreator().
1592 createCompressExecutable(mode, dst, src, wrapperListener);
1594 // Prior to write to disk the data, ensure that can write to the disk using
1595 // createFile method
1597 String compressOutFile = executable1.getOutCompressedFile();
1598 CreateFileExecutable executable2 =
1599 c.getExecutableFactory().
1601 createCreateFileExecutable(compressOutFile);
1602 boolean unmount = writableExecute(context, executable2, c, true);
1603 if (executable2.getResult().booleanValue()) {
1604 // Configure the rest of attributes of the wrapper listener
1605 wrapperListener.mUnmount = unmount;
1606 wrapperListener.mMountPoint = executable2.getDstWritableMountPoint();
1608 // Some archive modes requires a new file. Ensure that the created
1609 // file doesn't exists
1610 DeleteFileExecutable executable3 =
1611 c.getExecutableFactory().
1613 createDeleteFileExecutable(compressOutFile);
1614 writableExecute(context, executable3, c, true);
1615 if (executable3.getResult().booleanValue()) {
1617 execute(context, executable1, c);
1621 throw new ExecutionException(
1622 String.format("Fail to create file %s", compressOutFile)); //$NON-NLS-1$
1626 * Method that compress a file system object.
1628 * @param context The current context (needed if console == null)
1629 * @param mode The compression mode
1630 * @param src The file to compress
1631 * @param asyncResultListener The partial result listener
1632 * @param console The console in which execute the program.
1633 * <code>null</code> to attach to the default console
1634 * @return CompressExecutable The command executed in background
1635 * @throws FileNotFoundException If the initial directory not exists
1636 * @throws IOException If initial directory couldn't be checked
1637 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1638 * @throws NoSuchFileOrDirectory If the file or directory was not found
1639 * @throws ConsoleAllocException If the console can't be allocated
1640 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1641 * @throws CommandNotFoundException If the command was not found
1642 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1643 * @throws ExecutionException If the operation returns a invalid exit code
1644 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1645 * @throws CancelledOperationException If the operation was cancelled
1646 * @see CompressExecutable
1648 public static CompressExecutable compress(
1649 Context context, CompressionMode mode, String src,
1650 AsyncResultListener asyncResultListener, Console console)
1651 throws FileNotFoundException, IOException, ConsoleAllocException,
1652 NoSuchFileOrDirectory, InsufficientPermissionsException,
1653 CommandNotFoundException, OperationTimeoutException,
1654 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
1655 CancelledOperationException {
1656 Console c = ensureConsole(context, console);
1658 // Create a wrapper listener, for unmount the filesystem if necessary
1659 UnmountAsyncResultListener wrapperListener = new UnmountAsyncResultListener();
1660 wrapperListener.mCtx = context;
1661 wrapperListener.mConsole = c;
1662 wrapperListener.mRef = asyncResultListener;
1664 CompressExecutable executable1 =
1665 c.getExecutableFactory().newCreator().
1666 createCompressExecutable(mode, src, wrapperListener);
1668 // Prior to write to disk the data, ensure that can write to the disk using
1669 // createFile method
1671 String compressOutFile = executable1.getOutCompressedFile();
1672 CreateFileExecutable executable2 =
1673 c.getExecutableFactory().
1675 createCreateFileExecutable(compressOutFile);
1676 boolean unmount = writableExecute(context, executable2, c, true);
1677 if (executable2.getResult().booleanValue()) {
1678 // Configure the rest of attributes of the wrapper listener
1679 wrapperListener.mUnmount = unmount;
1680 wrapperListener.mMountPoint = executable2.getDstWritableMountPoint();
1683 execute(context, executable1, c);
1686 throw new ExecutionException(
1687 String.format("Fail to compress to file %s", compressOutFile)); //$NON-NLS-1$
1691 * Method that uncompress file system objects.
1693 * @param context The current context (needed if console == null)
1694 * @param src The file to compress
1695 * @param dst The destination file of folder (if null this method resolve with the best
1696 * fit based on the src)
1697 * @param asyncResultListener The partial result listener
1698 * @param console The console in which execute the program.
1699 * <code>null</code> to attach to the default console
1700 * @return UncompressExecutable The command executed in background
1701 * @throws FileNotFoundException If the initial directory not exists
1702 * @throws IOException If initial directory couldn't be checked
1703 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1704 * @throws NoSuchFileOrDirectory If the file or directory was not found
1705 * @throws ConsoleAllocException If the console can't be allocated
1706 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1707 * @throws CommandNotFoundException If the command was not found
1708 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1709 * @throws ExecutionException If the operation returns a invalid exit code
1710 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1711 * @throws CancelledOperationException If the operation was cancelled
1712 * @see CompressExecutable
1714 public static UncompressExecutable uncompress(
1715 Context context, String src, String dst,
1716 AsyncResultListener asyncResultListener, Console console)
1717 throws FileNotFoundException, IOException, ConsoleAllocException,
1718 NoSuchFileOrDirectory, InsufficientPermissionsException,
1719 CommandNotFoundException, OperationTimeoutException,
1720 ExecutionException, InvalidCommandDefinitionException, ReadOnlyFilesystemException,
1721 CancelledOperationException {
1722 Console c = ensureConsole(context, console);
1724 // Create a wrapper listener, for unmount the filesystem if necessary
1725 UnmountAsyncResultListener wrapperListener = new UnmountAsyncResultListener();
1726 wrapperListener.mCtx = context;
1727 wrapperListener.mConsole = c;
1728 wrapperListener.mRef = asyncResultListener;
1730 UncompressExecutable executable1 =
1731 c.getExecutableFactory().newCreator().
1732 createUncompressExecutable(src, dst, wrapperListener);
1734 // Prior to write to disk the data, ensure that can write to the disk using
1735 // createFile or createFolder method
1737 String compressOutFile = executable1.getOutUncompressedFile();
1738 WritableExecutable executable2 = null;
1739 if (executable1.IsArchive()) {
1742 c.getExecutableFactory().
1744 createCreateDirectoryExecutable(compressOutFile);
1748 c.getExecutableFactory().
1750 createCreateFileExecutable(compressOutFile);
1752 boolean unmount = writableExecute(context, executable2, c, true);
1753 if (((Boolean)executable2.getResult()).booleanValue()) {
1754 // Configure the rest of attributes of the wrapper listener
1755 wrapperListener.mUnmount = unmount;
1756 wrapperListener.mMountPoint = executable2.getDstWritableMountPoint();
1759 execute(context, executable1, c);
1762 MediaScannerConnection.scanFile(context, new String[]{
1763 MediaHelper.normalizeMediaPath(compressOutFile)}, null, null);
1767 throw new ExecutionException(
1768 String.format("Fail to uncompress to %s", compressOutFile)); //$NON-NLS-1$
1772 * Method that calculates the checksum of a file system object.
1774 * @param context The current context (needed if console == null)
1775 * @param src The source file
1776 * @param asyncResultListener The partial result listener
1777 * @param console The console in which execute the program.
1778 * <code>null</code> to attach to the default console
1779 * @return ChecksumExecutable The command executed in background
1780 * @throws FileNotFoundException If the initial directory not exists
1781 * @throws IOException If initial directory couldn't be checked
1782 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1783 * @throws NoSuchFileOrDirectory If the file or directory was not found
1784 * @throws ConsoleAllocException If the console can't be allocated
1785 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1786 * @throws CommandNotFoundException If the command was not found
1787 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1788 * @throws ExecutionException If the operation returns a invalid exit code
1789 * @throws CancelledOperationException If the operation was cancelled
1790 * @see ChecksumExecutable
1792 public static ChecksumExecutable checksum(Context context, String src,
1793 AsyncResultListener asyncResultListener, Console console)
1794 throws FileNotFoundException, IOException, ConsoleAllocException,
1795 NoSuchFileOrDirectory, InsufficientPermissionsException,
1796 CommandNotFoundException, OperationTimeoutException,
1797 ExecutionException, InvalidCommandDefinitionException, CancelledOperationException {
1798 Console c = ensureConsoleForFile(context, console, src);
1799 ChecksumExecutable executable =
1800 c.getExecutableFactory().newCreator().
1801 createChecksumExecutable(src, asyncResultListener);
1802 execute(context, executable, c);
1807 * Method that re-execute the command.
1809 * @param context The current context (needed if console == null)
1810 * @param executable The executable program to execute
1811 * @param console The console in which execute the program. <code>null</code>
1812 * to attach to the default console
1813 * @return Object The result of the re-execution
1814 * @throws NoSuchFileOrDirectory If the file or directory was not found
1815 * @throws ConsoleAllocException If the console can't be allocated
1816 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1817 * @throws CommandNotFoundException If the command was not found
1818 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1819 * @throws ExecutionException If the operation returns a invalid exit code
1820 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1821 * @throws InvalidCommandDefinitionException If the command has an invalid definition
1822 * @throws IOException If initial directory couldn't be checked
1823 * @throws CancelledOperationException If the operation was cancelled
1824 * @throws FileNotFoundException If the initial directory not exists
1826 public static Object reexecute(
1827 Context context , SyncResultExecutable executable, Console console)
1828 throws ConsoleAllocException, InsufficientPermissionsException, NoSuchFileOrDirectory,
1829 OperationTimeoutException, ExecutionException,
1830 CommandNotFoundException, ReadOnlyFilesystemException,
1831 FileNotFoundException, IOException, InvalidCommandDefinitionException,
1832 CancelledOperationException {
1833 Console c = ensureConsole(context, console);
1834 c.execute(executable, context);
1835 return executable.getResult();
1840 * Method that execute a program.
1842 * @param context The current context (needed if console == null)
1843 * @param executable The executable program to execute
1844 * @param console The console in which execute the program. <code>null</code> to attach
1845 * to the default console
1846 * @throws NoSuchFileOrDirectory If the file or directory was not found
1847 * @throws ConsoleAllocException If the console can't be allocated
1848 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1849 * @throws CommandNotFoundException If the command was not found
1850 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1851 * @throws ExecutionException If the operation returns a invalid exit code
1852 * @throws CancelledOperationException If the operation was cancelled
1853 * @throws AuthenticationFailedException If the operation failed caused by an
1854 * authentication failure
1856 private static void execute(Context context, Executable executable, Console console)
1857 throws ConsoleAllocException, InsufficientPermissionsException, NoSuchFileOrDirectory,
1858 OperationTimeoutException, ExecutionException, CommandNotFoundException,
1859 CancelledOperationException, AuthenticationFailedException {
1861 console.execute(executable, context);
1862 } catch (ReadOnlyFilesystemException rofEx) {
1863 // ReadOnlyFilesystemException don't have sense if command is not writable
1864 // WritableExecutable must be used with "writableExecute" method
1865 throw new ExecutionException(rofEx.getMessage(), rofEx);
1870 * Method that execute a program that requires write permissions over the filesystem. This
1871 * method ensure mount/unmount the filesystem before/after executing the operation.
1873 * @param context The current context (needed if console == null)
1874 * @param executable The writable executable program to execute
1875 * @param console The console in which execute the program. <code>null</code> to attach
1876 * to the default console
1877 * @throws NoSuchFileOrDirectory If the file or directory was not found
1878 * @throws ConsoleAllocException If the console can't be allocated
1879 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1880 * @throws CommandNotFoundException If the command was not found
1881 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1882 * @throws ExecutionException If the operation returns a invalid exit code
1883 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1884 * @throws CancelledOperationException If the operation was cancelled
1885 * @throws AuthenticationFailedException If the operation failed caused by an
1886 * authentication failure
1888 private static void writableExecute(Context context, WritableExecutable executable,
1889 Console console) throws ConsoleAllocException, InsufficientPermissionsException,
1890 NoSuchFileOrDirectory, OperationTimeoutException, ExecutionException,
1891 CommandNotFoundException, ReadOnlyFilesystemException, CancelledOperationException,
1892 AuthenticationFailedException{
1893 writableExecute(context, executable, console, false);
1897 * Method that execute a program that requires write permissions over the filesystem. This
1898 * method ensure mount/unmount the filesystem before/after executing the operation.
1900 * @param context The current context (needed if console == null)
1901 * @param executable The writable executable program to execute
1902 * @param console The console in which execute the program. <code>null</code> to attach
1903 * to the default console
1904 * @param leaveDeviceMounted If the operation must leave the filesystem mounted after
1906 * @return boolean If the filesystem was left unmount
1907 * @throws NoSuchFileOrDirectory If the file or directory was not found
1908 * @throws ConsoleAllocException If the console can't be allocated
1909 * @throws InsufficientPermissionsException If an operation requires elevated permissions
1910 * @throws CommandNotFoundException If the command was not found
1911 * @throws OperationTimeoutException If the operation exceeded the maximum time of wait
1912 * @throws ExecutionException If the operation returns a invalid exit code
1913 * @throws ReadOnlyFilesystemException If the operation writes in a read-only filesystem
1914 * @throws CancelledOperationException If the operation was cancelled
1915 * @throws AuthenticationFailedException If the operation failed caused by an
1916 * authentication failure
1918 private static boolean writableExecute(Context context, WritableExecutable executable,
1919 Console console, boolean leaveDeviceMounted) throws ConsoleAllocException,
1920 InsufficientPermissionsException, NoSuchFileOrDirectory, OperationTimeoutException,
1921 ExecutionException, CommandNotFoundException, ReadOnlyFilesystemException,
1922 CancelledOperationException, AuthenticationFailedException {
1924 //Retrieve the mount point information to check if a remount operation is required
1925 //There are 2 mount points: destination and source. Check both
1927 boolean needMountDst = false;
1928 MountPoint mpDst = executable.getDstWritableMountPoint();
1929 if (mpDst != null) {
1930 if (MountPointHelper.isMountAllowed(mpDst)) {
1931 if (!MountPointHelper.isReadWrite(mpDst)) {
1932 needMountDst = true;
1934 //Mount point is already read-write
1937 //For security or physical reasons the mount point can't be
1938 //mounted as read-write. Execute the command
1939 //and notify to the user
1942 //Don't have information about the mount point. Execute the command
1943 //and notify to the user
1946 boolean needMountSrc = false;
1947 MountPoint mpSrc = executable.getSrcWritableMountPoint();
1948 if (mpSrc != null) {
1949 if (MountPointHelper.isMountAllowed(mpSrc)) {
1950 if (!MountPointHelper.isReadWrite(mpSrc)) {
1951 needMountSrc = true;
1953 //Mount point is already read-write
1956 //For security or physical reasons the mount point can't be
1957 //mounted as read-write. Execute the command
1958 //and notify to the user
1961 //Don't have information about the mount point. Execute the command
1962 //and notify to the user
1965 //Create the mount/unmount executables
1966 MountExecutable mountDstExecutable = null;
1967 MountExecutable unmountDstExecutable = null;
1969 mountDstExecutable =
1970 console.getExecutableFactory().newCreator().
1971 createMountExecutable(mpDst, true);
1972 unmountDstExecutable =
1973 console.getExecutableFactory().newCreator().
1974 createMountExecutable(mpDst, false);
1976 MountExecutable mountSrcExecutable = null;
1977 MountExecutable unmountSrcExecutable = null;
1979 mountSrcExecutable =
1980 console.getExecutableFactory().newCreator().
1981 createMountExecutable(mpSrc, true);
1982 unmountSrcExecutable =
1983 console.getExecutableFactory().newCreator().
1984 createMountExecutable(mpSrc, false);
1987 //Execute the commands
1988 boolean mountExecutedDst = false;
1989 boolean mountExecutedSrc = false;
1992 //Execute the mount command
1993 console.execute(mountDstExecutable, context);
1994 mountExecutedDst = true;
1997 //Execute the mount command
1998 console.execute(mountSrcExecutable, context);
1999 mountExecutedSrc = true;
2002 //Execute the command
2003 console.execute(executable, context);
2005 } catch (InsufficientPermissionsException ipEx) {
2006 //Configure the commands to execute
2007 if (needMountDst && !mountExecutedDst) {
2008 //The failed operation was the mount rw operation
2009 //This operations is already in the exception in the fifo queue
2010 ipEx.addExecutable(executable);
2011 } else if (needMountSrc && !mountExecutedSrc) {
2012 //The failed operation was the mount rw operation
2013 //This operations is already in the exception in the fifo queue
2014 ipEx.addExecutable(executable);
2017 //A mount operation was executed or will be executed
2018 ipEx.addExecutable(unmountDstExecutable);
2021 //A mount operation was executed or will be executed
2022 ipEx.addExecutable(unmountSrcExecutable);
2025 //Rethrow the exception
2029 //If previously was a mount successful execution, then execute
2030 //and unmount operation
2031 if (mountExecutedDst && !leaveDeviceMounted) {
2032 //Execute the unmount command
2033 console.execute(unmountDstExecutable, context);
2035 if (mountExecutedSrc && !leaveDeviceMounted) {
2036 //Execute the unmount command
2037 console.execute(unmountSrcExecutable, context);
2041 // If the needed unmount was executed
2042 return (mountExecutedDst || mountExecutedSrc) && leaveDeviceMounted;
2046 * Method that ensure the console retrieve the default console if a console
2049 * @param context The current context (needed if console == null)
2050 * @param console The console passed
2051 * @return Console The console passed if not is null. Otherwise, the default console
2052 * @throws InsufficientPermissionsException If an operation requires elevated permissions
2053 * @throws ConsoleAllocException If the console can't be allocated
2054 * @throws InvalidCommandDefinitionException If the command has an invalid definition
2055 * @throws IOException If initial directory couldn't be checked
2056 * @throws FileNotFoundException If the initial directory not exists
2058 private static Console ensureConsole(Context context, Console console)
2059 throws FileNotFoundException, IOException, InvalidCommandDefinitionException,
2060 ConsoleAllocException, InsufficientPermissionsException {
2061 Console c = console;
2063 c = ConsoleBuilder.getConsole(context);
2069 * Method that ensure the console retrieve the default console if a console
2072 * @param context The current context (needed if console == null)
2073 * @param console The console passed
2074 * @param src The source file to check
2075 * @return Console The console passed if not is null. Otherwise, the default console
2076 * @throws InsufficientPermissionsException If an operation requires elevated permissions
2077 * @throws ConsoleAllocException If the console can't be allocated
2078 * @throws InvalidCommandDefinitionException If the command has an invalid definition
2079 * @throws IOException If initial directory couldn't be checked
2080 * @throws FileNotFoundException If the initial directory not exists
2082 public static Console ensureConsoleForFile(Context context, Console console, String src)
2083 throws FileNotFoundException, IOException, InvalidCommandDefinitionException,
2084 ConsoleAllocException, InsufficientPermissionsException {
2086 // Check if the path belongs to a virtual mount point
2087 Console c = VirtualMountPointConsole.getVirtualConsoleForPath(src);
2092 // Recover a real console
2095 c = ConsoleBuilder.getConsole(context);