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

Commit 4feeca11 authored by Sergey Nikolaienkov's avatar Sergey Nikolaienkov
Browse files

Catch exceptions when writing to persistent storage in CDM

Bug: 212652483
Test: atest CtsCompanionDeviceManagerCoreTestCases
Test: atest CtsCompanionDeviceManagerUiAutomationTestCases
Test: atest CtsOsTestCases:CompanionDeviceManagerTest
Change-Id: If2622236d52d62c644e2dfac7340cee9f783dd30
parent 728f8b49
Loading
Loading
Loading
Loading
+28 −21
Original line number Diff line number Diff line
@@ -38,13 +38,13 @@ import android.net.MacAddress;
import android.os.Environment;
import android.util.ArrayMap;
import android.util.AtomicFile;
import android.util.ExceptionUtils;
import android.util.Slog;
import android.util.SparseArray;
import android.util.TypedXmlPullParser;
import android.util.TypedXmlSerializer;
import android.util.Xml;

import com.android.internal.util.FunctionalUtils.ThrowingConsumer;
import com.android.internal.util.XmlUtils;

import org.xmlpull.v1.XmlPullParser;
@@ -53,6 +53,7 @@ import org.xmlpull.v1.XmlSerializer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
@@ -307,8 +308,9 @@ final class PersistentDataStore {
    private void persistStateToFileLocked(@NonNull AtomicFile file,
            @Nullable Collection<AssociationInfo> associations,
            @NonNull Map<String, Set<Integer>> previouslyUsedIdsPerPackage) {
        file.write(out -> {
            try {
        // Writing to file could fail, for example, if the user has been recently removed and so was
        // their DE (/data/system_de/<user-id>/) directory.
        writeToFileSafely(file, out -> {
            final TypedXmlSerializer serializer = Xml.resolveSerializer(out);
            serializer.setFeature(
                    "http://xmlpull.org/v1/doc/features.html#indent-output", true);
@@ -323,10 +325,6 @@ final class PersistentDataStore {

            serializer.endTag(null, XML_TAG_STATE);
            serializer.endDocument();
            } catch (Exception e) {
                Slog.e(LOG_TAG, "Error while writing associations file", e);
                throw ExceptionUtils.propagate(e);
            }
        });
    }

@@ -524,4 +522,13 @@ final class PersistentDataStore {
        }
        return associationInfo;
    }

    private static void writeToFileSafely(@NonNull AtomicFile file,
            @NonNull ThrowingConsumer<FileOutputStream> consumer) {
        try {
            file.write(consumer);
        } catch (Exception e) {
            Slog.e(LOG_TAG, "Error while writing to file " + file, e);
        }
    }
}