Loading services/core/java/com/android/server/pm/ShortcutLauncher.java +30 −38 Original line number Diff line number Diff line Loading @@ -23,7 +23,6 @@ import android.content.pm.ShortcutInfo; import android.content.pm.UserPackage; import android.util.ArrayMap; import android.util.ArraySet; import android.util.AtomicFile; import android.util.Slog; import android.util.Xml; Loading @@ -32,8 +31,6 @@ import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.TypedXmlSerializer; import com.android.server.pm.ShortcutService.DumpFilter; import libcore.io.IoUtils; import org.json.JSONException; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; Loading @@ -41,7 +38,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; Loading Loading @@ -264,19 +260,15 @@ class ShortcutLauncher extends ShortcutPackageItem { public static ShortcutLauncher loadFromFile(File path, ShortcutUser shortcutUser, int ownerUserId, boolean fromBackup) { final AtomicFile file = new AtomicFile(path); final FileInputStream in; try (ResilientAtomicFile file = getResilientFile(path)) { FileInputStream in = null; try { in = file.openRead(); } catch (FileNotFoundException e) { if (ShortcutService.DEBUG) { if (in == null) { Slog.d(TAG, "Not found " + path); } return null; } try { ShortcutLauncher ret = null; TypedXmlPullParser parser = Xml.resolvePullParser(in); Loading @@ -298,11 +290,11 @@ class ShortcutLauncher extends ShortcutPackageItem { ShortcutService.throwForInvalidTag(depth, tag); } return ret; } catch (IOException | XmlPullParserException e) { } catch (Exception e) { Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); return null; } finally { IoUtils.closeQuietly(in); file.failRead(in, e); return loadFromFile(path, shortcutUser, ownerUserId, fromBackup); } } } Loading services/core/java/com/android/server/pm/ShortcutPackage.java +30 −38 Original line number Diff line number Diff line Loading @@ -50,7 +50,6 @@ import android.os.StrictMode; import android.text.format.Formatter; import android.util.ArrayMap; import android.util.ArraySet; import android.util.AtomicFile; import android.util.Log; import android.util.Slog; import android.util.Xml; Loading @@ -69,8 +68,6 @@ import com.android.server.pm.ShortcutService.DumpFilter; import com.android.server.pm.ShortcutService.ShortcutOperation; import com.android.server.pm.ShortcutService.Stats; import libcore.io.IoUtils; import org.json.JSONException; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; Loading @@ -78,7 +75,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; Loading Loading @@ -1969,19 +1965,15 @@ class ShortcutPackage extends ShortcutPackageItem { public static ShortcutPackage loadFromFile(ShortcutService s, ShortcutUser shortcutUser, File path, boolean fromBackup) { final AtomicFile file = new AtomicFile(path); final FileInputStream in; try (ResilientAtomicFile file = getResilientFile(path)) { FileInputStream in = null; try { in = file.openRead(); } catch (FileNotFoundException e) { if (ShortcutService.DEBUG) { if (in == null) { Slog.d(TAG, "Not found " + path); } return null; } try { ShortcutPackage ret = null; TypedXmlPullParser parser = Xml.resolvePullParser(in); Loading @@ -2003,11 +1995,11 @@ class ShortcutPackage extends ShortcutPackageItem { ShortcutService.throwForInvalidTag(depth, tag); } return ret; } catch (IOException | XmlPullParserException e) { } catch (Exception e) { Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); return null; } finally { IoUtils.closeQuietly(in); file.failRead(in, e); return loadFromFile(s, shortcutUser, path, fromBackup); } } } Loading services/core/java/com/android/server/pm/ShortcutPackageItem.java +34 −31 Original line number Diff line number Diff line Loading @@ -20,14 +20,13 @@ import android.annotation.Nullable; import android.content.pm.PackageInfo; import android.content.pm.ShortcutInfo; import android.graphics.Bitmap; import android.util.AtomicFile; import android.os.FileUtils; import android.util.Slog; import android.util.Xml; import com.android.internal.annotations.GuardedBy; import com.android.internal.util.Preconditions; import com.android.modules.utils.TypedXmlSerializer; import com.android.server.security.FileIntegrity; import org.json.JSONException; import org.json.JSONObject; Loading Loading @@ -160,7 +159,7 @@ abstract class ShortcutPackageItem { @GuardedBy("mLock") public void saveToFileLocked(File path, boolean forBackup) { final AtomicFile file = new AtomicFile(path); try (ResilientAtomicFile file = getResilientFile(path)) { FileOutputStream os = null; try { os = file.startWrite(); Loading @@ -181,17 +180,12 @@ abstract class ShortcutPackageItem { os.flush(); file.finishWrite(os); try { FileIntegrity.setUpFsVerity(path); } catch (IOException e) { Slog.e(TAG, "Failed to verity-protect " + path, e); } } catch (XmlPullParserException | IOException e) { Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); file.failWrite(os); } } } @GuardedBy("mLock") void scheduleSaveToAppSearchLocked() { Loading Loading @@ -265,9 +259,18 @@ abstract class ShortcutPackageItem { void removeShortcutPackageItem() { synchronized (mLock) { getShortcutPackageItemFile().delete(); getResilientFile(getShortcutPackageItemFile()).delete(); } } protected abstract File getShortcutPackageItemFile(); protected static ResilientAtomicFile getResilientFile(File file) { String path = file.getPath(); File temporaryBackup = new File(path + ".backup"); File reserveCopy = new File(path + ".reservecopy"); int fileMode = FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IXOTH; return new ResilientAtomicFile(file, temporaryBackup, reserveCopy, fileMode, "shortcut package item", null); } } services/core/java/com/android/server/pm/ShortcutUser.java +6 −4 Original line number Diff line number Diff line Loading @@ -437,14 +437,14 @@ class ShortcutUser { } else { final File root = s.injectUserDataPath(userId); forAllFilesIn(new File(root, DIRECTORY_PACKAGES), (File f) -> { forMainFilesIn(new File(root, DIRECTORY_PACKAGES), (File f) -> { final ShortcutPackage sp = ShortcutPackage.loadFromFile(s, ret, f, fromBackup); if (sp != null) { ret.mPackages.put(sp.getPackageName(), sp); } }); forAllFilesIn(new File(root, DIRECTORY_LUANCHERS), (File f) -> { forMainFilesIn(new File(root, DIRECTORY_LUANCHERS), (File f) -> { final ShortcutLauncher sl = ShortcutLauncher.loadFromFile(f, ret, userId, fromBackup); if (sl != null) { Loading @@ -456,15 +456,17 @@ class ShortcutUser { return ret; } private static void forAllFilesIn(File path, Consumer<File> callback) { private static void forMainFilesIn(File path, Consumer<File> callback) { if (!path.exists()) { return; } File[] list = path.listFiles(); for (File f : list) { if (!f.getName().endsWith(".reservecopy") && !f.getName().endsWith(".backup")) { callback.accept(f); } } } public void setCachedLauncher(String launcher) { mCachedLauncher = launcher; Loading services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java +10 −1 Original line number Diff line number Diff line Loading @@ -125,6 +125,8 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Locale; Loading Loading @@ -4014,8 +4016,10 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest { ShortcutUser user = new ShortcutUser(mService, 0); File corruptedShortcutPackage = new File("/data/local/tmp/cts/content/", File corruptedShortcutPackage = new File(getTestContext().getFilesDir(), "broken_shortcut.xml"); Files.copy(new File("/data/local/tmp/cts/content/", "broken_shortcut.xml").toPath(), corruptedShortcutPackage.toPath(), StandardCopyOption.REPLACE_EXISTING); assertNull(ShortcutPackage.loadFromFile(mService, user, corruptedShortcutPackage, false)); } Loading Loading @@ -4116,6 +4120,11 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest { try (Writer os = new FileWriter(mService.getUserFile(USER_10).getBaseFile())) { os.write("<?xml version='1.0' encoding='utf"); } ShortcutPackage sp = mService.getUserShortcutsLocked(USER_0).getPackageShortcutsIfExists( CALLING_PACKAGE_1); try (Writer os = new FileWriter(sp.getShortcutPackageItemFile().getPath())) { os.write("<?xml version='1.0' encoding='utf"); } // Restore. initService(); Loading Loading
services/core/java/com/android/server/pm/ShortcutLauncher.java +30 −38 Original line number Diff line number Diff line Loading @@ -23,7 +23,6 @@ import android.content.pm.ShortcutInfo; import android.content.pm.UserPackage; import android.util.ArrayMap; import android.util.ArraySet; import android.util.AtomicFile; import android.util.Slog; import android.util.Xml; Loading @@ -32,8 +31,6 @@ import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.TypedXmlSerializer; import com.android.server.pm.ShortcutService.DumpFilter; import libcore.io.IoUtils; import org.json.JSONException; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; Loading @@ -41,7 +38,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; Loading Loading @@ -264,19 +260,15 @@ class ShortcutLauncher extends ShortcutPackageItem { public static ShortcutLauncher loadFromFile(File path, ShortcutUser shortcutUser, int ownerUserId, boolean fromBackup) { final AtomicFile file = new AtomicFile(path); final FileInputStream in; try (ResilientAtomicFile file = getResilientFile(path)) { FileInputStream in = null; try { in = file.openRead(); } catch (FileNotFoundException e) { if (ShortcutService.DEBUG) { if (in == null) { Slog.d(TAG, "Not found " + path); } return null; } try { ShortcutLauncher ret = null; TypedXmlPullParser parser = Xml.resolvePullParser(in); Loading @@ -298,11 +290,11 @@ class ShortcutLauncher extends ShortcutPackageItem { ShortcutService.throwForInvalidTag(depth, tag); } return ret; } catch (IOException | XmlPullParserException e) { } catch (Exception e) { Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); return null; } finally { IoUtils.closeQuietly(in); file.failRead(in, e); return loadFromFile(path, shortcutUser, ownerUserId, fromBackup); } } } Loading
services/core/java/com/android/server/pm/ShortcutPackage.java +30 −38 Original line number Diff line number Diff line Loading @@ -50,7 +50,6 @@ import android.os.StrictMode; import android.text.format.Formatter; import android.util.ArrayMap; import android.util.ArraySet; import android.util.AtomicFile; import android.util.Log; import android.util.Slog; import android.util.Xml; Loading @@ -69,8 +68,6 @@ import com.android.server.pm.ShortcutService.DumpFilter; import com.android.server.pm.ShortcutService.ShortcutOperation; import com.android.server.pm.ShortcutService.Stats; import libcore.io.IoUtils; import org.json.JSONException; import org.json.JSONObject; import org.xmlpull.v1.XmlPullParser; Loading @@ -78,7 +75,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; Loading Loading @@ -1969,19 +1965,15 @@ class ShortcutPackage extends ShortcutPackageItem { public static ShortcutPackage loadFromFile(ShortcutService s, ShortcutUser shortcutUser, File path, boolean fromBackup) { final AtomicFile file = new AtomicFile(path); final FileInputStream in; try (ResilientAtomicFile file = getResilientFile(path)) { FileInputStream in = null; try { in = file.openRead(); } catch (FileNotFoundException e) { if (ShortcutService.DEBUG) { if (in == null) { Slog.d(TAG, "Not found " + path); } return null; } try { ShortcutPackage ret = null; TypedXmlPullParser parser = Xml.resolvePullParser(in); Loading @@ -2003,11 +1995,11 @@ class ShortcutPackage extends ShortcutPackageItem { ShortcutService.throwForInvalidTag(depth, tag); } return ret; } catch (IOException | XmlPullParserException e) { } catch (Exception e) { Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); return null; } finally { IoUtils.closeQuietly(in); file.failRead(in, e); return loadFromFile(s, shortcutUser, path, fromBackup); } } } Loading
services/core/java/com/android/server/pm/ShortcutPackageItem.java +34 −31 Original line number Diff line number Diff line Loading @@ -20,14 +20,13 @@ import android.annotation.Nullable; import android.content.pm.PackageInfo; import android.content.pm.ShortcutInfo; import android.graphics.Bitmap; import android.util.AtomicFile; import android.os.FileUtils; import android.util.Slog; import android.util.Xml; import com.android.internal.annotations.GuardedBy; import com.android.internal.util.Preconditions; import com.android.modules.utils.TypedXmlSerializer; import com.android.server.security.FileIntegrity; import org.json.JSONException; import org.json.JSONObject; Loading Loading @@ -160,7 +159,7 @@ abstract class ShortcutPackageItem { @GuardedBy("mLock") public void saveToFileLocked(File path, boolean forBackup) { final AtomicFile file = new AtomicFile(path); try (ResilientAtomicFile file = getResilientFile(path)) { FileOutputStream os = null; try { os = file.startWrite(); Loading @@ -181,17 +180,12 @@ abstract class ShortcutPackageItem { os.flush(); file.finishWrite(os); try { FileIntegrity.setUpFsVerity(path); } catch (IOException e) { Slog.e(TAG, "Failed to verity-protect " + path, e); } } catch (XmlPullParserException | IOException e) { Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); file.failWrite(os); } } } @GuardedBy("mLock") void scheduleSaveToAppSearchLocked() { Loading Loading @@ -265,9 +259,18 @@ abstract class ShortcutPackageItem { void removeShortcutPackageItem() { synchronized (mLock) { getShortcutPackageItemFile().delete(); getResilientFile(getShortcutPackageItemFile()).delete(); } } protected abstract File getShortcutPackageItemFile(); protected static ResilientAtomicFile getResilientFile(File file) { String path = file.getPath(); File temporaryBackup = new File(path + ".backup"); File reserveCopy = new File(path + ".reservecopy"); int fileMode = FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IXOTH; return new ResilientAtomicFile(file, temporaryBackup, reserveCopy, fileMode, "shortcut package item", null); } }
services/core/java/com/android/server/pm/ShortcutUser.java +6 −4 Original line number Diff line number Diff line Loading @@ -437,14 +437,14 @@ class ShortcutUser { } else { final File root = s.injectUserDataPath(userId); forAllFilesIn(new File(root, DIRECTORY_PACKAGES), (File f) -> { forMainFilesIn(new File(root, DIRECTORY_PACKAGES), (File f) -> { final ShortcutPackage sp = ShortcutPackage.loadFromFile(s, ret, f, fromBackup); if (sp != null) { ret.mPackages.put(sp.getPackageName(), sp); } }); forAllFilesIn(new File(root, DIRECTORY_LUANCHERS), (File f) -> { forMainFilesIn(new File(root, DIRECTORY_LUANCHERS), (File f) -> { final ShortcutLauncher sl = ShortcutLauncher.loadFromFile(f, ret, userId, fromBackup); if (sl != null) { Loading @@ -456,15 +456,17 @@ class ShortcutUser { return ret; } private static void forAllFilesIn(File path, Consumer<File> callback) { private static void forMainFilesIn(File path, Consumer<File> callback) { if (!path.exists()) { return; } File[] list = path.listFiles(); for (File f : list) { if (!f.getName().endsWith(".reservecopy") && !f.getName().endsWith(".backup")) { callback.accept(f); } } } public void setCachedLauncher(String launcher) { mCachedLauncher = launcher; Loading
services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java +10 −1 Original line number Diff line number Diff line Loading @@ -125,6 +125,8 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Locale; Loading Loading @@ -4014,8 +4016,10 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest { ShortcutUser user = new ShortcutUser(mService, 0); File corruptedShortcutPackage = new File("/data/local/tmp/cts/content/", File corruptedShortcutPackage = new File(getTestContext().getFilesDir(), "broken_shortcut.xml"); Files.copy(new File("/data/local/tmp/cts/content/", "broken_shortcut.xml").toPath(), corruptedShortcutPackage.toPath(), StandardCopyOption.REPLACE_EXISTING); assertNull(ShortcutPackage.loadFromFile(mService, user, corruptedShortcutPackage, false)); } Loading Loading @@ -4116,6 +4120,11 @@ public class ShortcutManagerTest1 extends BaseShortcutManagerTest { try (Writer os = new FileWriter(mService.getUserFile(USER_10).getBaseFile())) { os.write("<?xml version='1.0' encoding='utf"); } ShortcutPackage sp = mService.getUserShortcutsLocked(USER_0).getPackageShortcutsIfExists( CALLING_PACKAGE_1); try (Writer os = new FileWriter(sp.getShortcutPackageItemFile().getPath())) { os.write("<?xml version='1.0' encoding='utf"); } // Restore. initService(); Loading