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.
16 package com.cyanogenmod.filemanager.util;
18 import android.util.Log;
20 import com.cyanogenmod.filemanager.FileManagerApplication;
21 import com.cyanogenmod.filemanager.commands.MountExecutable;
22 import com.cyanogenmod.filemanager.console.Console;
23 import com.cyanogenmod.filemanager.model.DiskUsage;
24 import com.cyanogenmod.filemanager.model.FileSystemObject;
25 import com.cyanogenmod.filemanager.model.MountPoint;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
34 * A helper class with useful methods for deal with mount points.
36 public final class MountPointHelper {
38 private static final String TAG = "MountPointHelper"; //$NON-NLS-1$
40 private static final List<String> RESTRICTED_FS_TYPE = Arrays.asList(new String[]{
41 "devpts", //$NON-NLS-1$
43 "sysfs", //$NON-NLS-1$
44 "debugfs", //$NON-NLS-1$
45 "cgroup", //$NON-NLS-1$
49 private static final long MAX_CACHED_TIME = 60000L * 5;
51 private static List<MountPoint> sMountPoints = new ArrayList<>();
52 private static long sLastCachedTime;
55 * Constructor of <code>MountPointHelper</code>.
57 private MountPointHelper() {
62 * Method that retrieve the mount point information for a directory.
64 * @param dir The directory of which recovers his mount point information
65 * @return MountPoint The mount point information
67 public static MountPoint getMountPointFromDirectory(FileSystemObject dir) {
68 return getMountPointFromDirectory(dir.getFullPath());
72 * Method that retrieve the mount point information for a directory.
74 * @param dir The directory of which recovers his mount point information
75 * @return MountPoint The mount point information
77 public static MountPoint getMountPointFromDirectory(String dir) {
79 return getMountPointFromDirectory(FileManagerApplication.getBackgroundConsole(), dir);
80 } catch (Exception e) {
81 Log.e(TAG, "Failed to retrieve mount point information.", e); //$NON-NLS-1$
87 * Method that refresh the mount point information.
89 * @return boolean refresh success or not.
91 public static boolean refreshMountPoints(Console console) {
92 synchronized(sMountPoints) {
95 sMountPoints.addAll(CommandHelper.getMountPoints(null, null));
96 sLastCachedTime = System.currentTimeMillis();
98 } catch (Exception e) {
99 Log.e(TAG, "Failed to update the mount point information", e); //$NON-NLS-1$
106 * Method that retrieve the mount point information for a directory.
108 * @param console The console in which realize the operation
109 * @param dir The directory of which recovers his mount point information
110 * @return MountPoint The mount point information
112 public static MountPoint getMountPointFromDirectory(Console console, String dir) {
114 // For non-rooted devices, which console is java and runs under a chrooted
115 // device, mount point info mustn't be a main objective. Caching the status
116 // should be enough and operation runs smoothly.
117 // Refresh mount points after some time (5 minutes should be enough)
118 long now = System.currentTimeMillis();
119 synchronized(sMountPoints) {
120 if (sMountPoints == null || (now - sLastCachedTime) > MAX_CACHED_TIME || FileManagerApplication.hasShellCommands()) {
121 //Retrieve the mount points
122 refreshMountPoints(console);
124 if (sMountPoints == null) {
128 //Sort mount points in reverse order, needed for avoid
129 //found an incorrect mount point that matches the name
130 Collections.sort(sMountPoints, new Comparator<MountPoint>() {
132 public int compare(MountPoint lhs, MountPoint rhs) {
133 return lhs.compareTo(rhs) * -1;
137 //Search for the mount point information
138 int cc = sMountPoints.size();
139 for (int i = 0; i < cc; i++) {
140 MountPoint mp = sMountPoints.get(i);
141 if (dir.startsWith(mp.getMountPoint())) {
147 } catch (Exception e) {
148 Log.e(TAG, "Failed to retrieve the mount point information", e); //$NON-NLS-1$
151 //No mount point found
156 * Method that retrieve information about the disk usage of the mount point.
158 * @param mp The mount point
159 * @return DiskUsage The disk usage information
161 public static DiskUsage getMountPointDiskUsage(MountPoint mp) {
162 return getMountPointDiskUsage(FileManagerApplication.getBackgroundConsole(), mp);
166 * Method that retrieve information about the disk usage of the mount point.
168 * @param console The console in which realize the operation
169 * @param mp The mount point
170 * @return DiskUsage The disk usage information
172 public static DiskUsage getMountPointDiskUsage(Console console, MountPoint mp) {
174 //Retrieve the mount points
175 return CommandHelper.getDiskUsage(null, mp.getMountPoint(), console);
177 } catch (Exception e) {
179 String.format("Fail to load disk usage of mount point: %s", //$NON-NLS-1$
180 mp.getMountPoint()), e);
183 //No mount point found
188 * Method that returns if the filesystem is mounted as readonly.
190 * @param mp The mount point to check
191 * @return boolean If the mount point is mounted as readonly
193 public static boolean isReadOnly(MountPoint mp) {
195 return mp.getOptions().startsWith(MountExecutable.READONLY);
196 } catch (Exception e) {
197 Log.e(TAG, "Method \"isReadOnly\" failed.", e); //$NON-NLS-1$
200 //On fail is more secure consider it as read-only
205 * Method that returns if the filesystem is mounted as read-write.
207 * @param mp The mount point to check
208 * @return boolean If the mount point is mounted as read-write
210 public static boolean isReadWrite(MountPoint mp) {
212 return mp.getOptions().startsWith(MountExecutable.READWRITE);
213 } catch (Exception e) {
214 Log.e(TAG, "Method \"isReadWrite\" failed.", e); //$NON-NLS-1$
217 //On fail is more secure consider it as read-only
222 * Method that returns if a filesystem is allowed to be mounted/unmounted (rw/ro).
224 * @param mp The mount point to check
225 * @return boolean If the mount point can be mounted/unmount (rw/ro)
227 public static boolean isMountAllowed(MountPoint mp) {
228 return !RESTRICTED_FS_TYPE.contains(mp.getType());