Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4c38c040 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "SettingsProvider: It maybe generated an error...

Merge "SettingsProvider: It maybe generated an error settings_ssaid.xml.fallback file from the original xml." into main
parents f5307871 b6e6dbcf
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -3185,15 +3185,15 @@ public class SettingsProvider extends ContentProvider {
    }

    /**
     * For each file in the given list, if it exists, copy it to a back up file. Ignore failures.
     * For each file in the given list, if it exists and is complete, copy it to a back up file.
     * Ignore failures.
     * @param filePaths List of paths of files that need to be backed up
     */
    public static void writeFallBackSettingsFiles(List<String> filePaths) {
        final int numFiles = filePaths.size();
        for (int i = 0; i < numFiles; i++) {
            final String filePath = filePaths.get(i);
        for (String filePath : filePaths) {
            final File originalFile = new File(filePath);
            if (SettingsState.stateFileExists(originalFile)) {
            if (SettingsState.stateFileExists(originalFile) &&
                    SettingsState.verifySettingsFileIntegrity(originalFile)) {
                final File fallBackFile = new File(filePath + FALLBACK_FILE_SUFFIX);
                try {
                    FileUtils.copy(originalFile, fallBackFile);
+32 −0
Original line number Diff line number Diff line
@@ -1503,6 +1503,38 @@ public class SettingsState {
        }
    }

    public static boolean verifySettingsFileIntegrity(File file) {
        if (!file.exists()) {
            return false;
        }
        try {
            FileInputStream in = new AtomicFile(file).openRead();
            try {
                TypedXmlPullParser parser = Xml.resolvePullParser(in);
                int outerDepth = parser.getDepth();
                int type;
                while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                        && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                        continue;
                    }

                    String tagName = parser.getName();
                    if (tagName.equals(TAG_SETTINGS)) {
                        return true;
                    } else if (tagName.equals(TAG_NAMESPACE_HASHES)) {
                        return true;
                    }
                }
            } finally {
                IoUtils.closeQuietly(in);
            }
        } catch (XmlPullParserException | IOException e) {
            Slog.e(LOG_TAG, "verify settings xml failed", e);
        }
        return false;
    }

    /**
     * Uses AtomicFile to check if the file or its backup exists.
     *