From f17e4cf1f9211c4e549a97b62536ae4001baad7e Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 26 Jan 2017 15:46:00 -0700 Subject: [PATCH] Push mapping from file extension to GID. An upcoming sdcardfs change will offer to automatically change the GID of files on external storage based on the extension, but we need to tell the kernel the mapping that we'd like to see. This change pushes the current mappings used by installd when kernel support is detected. Test: builds, boots, works with old/new kernels Bug: 34733702 Change-Id: I4a13b17f5a8e080a9bea51b01aa9f1cbf575e0ab --- core/java/android/os/Process.java | 7 ++++ .../android/server/StorageManagerService.java | 5 +++ .../android/server/storage/FileCollector.java | 36 +++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/core/java/android/os/Process.java b/core/java/android/os/Process.java index d6688e3c00f78..a41f45b52c1a5 100644 --- a/core/java/android/os/Process.java +++ b/core/java/android/os/Process.java @@ -189,6 +189,13 @@ public class Process { /** {@hide} */ public static final int LAST_APPLICATION_CACHE_GID = 29999; + /** {@hide} */ + public static final int MEDIA_AUDIO_GID = 1055; + /** {@hide} */ + public static final int MEDIA_VIDEO_GID = 1056; + /** {@hide} */ + public static final int MEDIA_IMAGE_GID = 1057; + /** * Standard priority of application threads. * Use with {@link #setThreadPriority(int)} and diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index c07add0267cbe..629da86889347 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -108,6 +108,8 @@ import com.android.server.NativeDaemonConnector.Command; import com.android.server.NativeDaemonConnector.SensitiveArg; import com.android.server.pm.PackageManagerService; import com.android.server.storage.AppFuseBridge; +import com.android.server.storage.FileCollector; + import libcore.io.IoUtils; import libcore.util.EmptyArray; @@ -817,6 +819,9 @@ class StorageManagerService extends IStorageManager.Stub } private void handleSystemReady() { + // Register kernel mapping from extensions to statistics GIDs + FileCollector.updateKernelExtensions(); + initIfReadyAndConnected(); resetIfReadyAndConnected(); diff --git a/services/core/java/com/android/server/storage/FileCollector.java b/services/core/java/com/android/server/storage/FileCollector.java index 90f9f13916795..59cfaf79f0390 100644 --- a/services/core/java/com/android/server/storage/FileCollector.java +++ b/services/core/java/com/android/server/storage/FileCollector.java @@ -26,7 +26,6 @@ import android.util.ArrayMap; import java.io.File; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.Map; /** * FileCollector walks over a directory and categorizes storage usage by their type. @@ -44,8 +43,9 @@ public class FileCollector { AUDIO }) private @interface FileTypes {} - - private static final Map EXTENSION_MAP = new ArrayMap(); + // NOTE: If you update these extensions, you'll also want to update + // matchgen.py over in installd which is used for non-quota stats. + private static final ArrayMap EXTENSION_MAP = new ArrayMap<>(); static { // Audio EXTENSION_MAP.put("aac", AUDIO); @@ -144,6 +144,36 @@ public class FileCollector { EXTENSION_MAP.put("xwd", IMAGES); } + private static File mkdir(File parent, String name) { + final File file = new File(parent, name); + file.mkdir(); + return file; + } + + /** + * Update the mapping used by sdcardfs to map from file extensions to GIDs + * used for statistics purposes. + */ + public static void updateKernelExtensions() { + final File root = new File("/config/sdcardfs/extensions/"); + if (!root.exists()) return; + + final File audio = mkdir(root, Integer.toString(android.os.Process.MEDIA_AUDIO_GID)); + final File video = mkdir(root, Integer.toString(android.os.Process.MEDIA_VIDEO_GID)); + final File image = mkdir(root, Integer.toString(android.os.Process.MEDIA_IMAGE_GID)); + + for (int i = 0; i < EXTENSION_MAP.size(); i++) { + final String ext = EXTENSION_MAP.keyAt(i); + final int type = EXTENSION_MAP.valueAt(i); + + switch (type) { + case AUDIO: mkdir(audio, ext); break; + case VIDEO: mkdir(video, ext); break; + case IMAGES: mkdir(image, ext); break; + } + } + } + /** * Returns the file categorization measurement result. * @param path Directory to collect and categorize storage in.