1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.server.companion; 18 19 import static org.xmlpull.v1.XmlPullParser.END_TAG; 20 import static org.xmlpull.v1.XmlPullParser.START_TAG; 21 22 import android.annotation.NonNull; 23 import android.annotation.UserIdInt; 24 import android.os.Environment; 25 import android.util.AtomicFile; 26 import android.util.Slog; 27 28 import com.android.internal.util.FunctionalUtils.ThrowingConsumer; 29 30 import org.xmlpull.v1.XmlPullParser; 31 import org.xmlpull.v1.XmlPullParserException; 32 33 import java.io.File; 34 import java.io.FileOutputStream; 35 36 /** 37 * Util class for CDM data stores 38 */ 39 public final class DataStoreUtils { 40 private static final String TAG = "CDM_DataStoreUtils"; 41 42 /** 43 * Check if the parser pointer is at the start of the tag 44 */ isStartOfTag(@onNull XmlPullParser parser, @NonNull String tag)45 public static boolean isStartOfTag(@NonNull XmlPullParser parser, @NonNull String tag) 46 throws XmlPullParserException { 47 return parser.getEventType() == START_TAG && tag.equals(parser.getName()); 48 } 49 50 /** 51 * Check if the parser pointer is at the end of the tag 52 */ isEndOfTag(@onNull XmlPullParser parser, @NonNull String tag)53 public static boolean isEndOfTag(@NonNull XmlPullParser parser, @NonNull String tag) 54 throws XmlPullParserException { 55 return parser.getEventType() == END_TAG && tag.equals(parser.getName()); 56 } 57 58 /** 59 * Creates {@link AtomicFile} object that represents the back-up for the given user. 60 * 61 * IMPORTANT: the method will ALWAYS return the same {@link AtomicFile} object, which makes it 62 * possible to synchronize reads and writes to the file using the returned object. 63 * 64 * @param userId the userId to retrieve the storage file 65 * @param fileName the storage file name 66 * @return an AtomicFile for the user 67 */ 68 @NonNull createStorageFileForUser(@serIdInt int userId, String fileName)69 public static AtomicFile createStorageFileForUser(@UserIdInt int userId, String fileName) { 70 return new AtomicFile(getBaseStorageFileForUser(userId, fileName)); 71 } 72 73 @NonNull getBaseStorageFileForUser(@serIdInt int userId, String fileName)74 private static File getBaseStorageFileForUser(@UserIdInt int userId, String fileName) { 75 return new File(Environment.getDataSystemDeDirectory(userId), fileName); 76 } 77 78 /** 79 * Writing to file could fail, for example, if the user has been recently removed and so was 80 * their DE (/data/system_de/<user-id>/) directory. 81 */ writeToFileSafely( @onNull AtomicFile file, @NonNull ThrowingConsumer<FileOutputStream> consumer)82 public static void writeToFileSafely( 83 @NonNull AtomicFile file, @NonNull ThrowingConsumer<FileOutputStream> consumer) { 84 try { 85 file.write(consumer); 86 } catch (Exception e) { 87 Slog.e(TAG, "Error while writing to file " + file, e); 88 } 89 } 90 DataStoreUtils()91 private DataStoreUtils() { 92 } 93 } 94