• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 final class DataStoreUtils {
37     private static final String TAG = "CompanionDevice_DataStoreUtils";
38 
isStartOfTag(@onNull XmlPullParser parser, @NonNull String tag)39     static boolean isStartOfTag(@NonNull XmlPullParser parser, @NonNull String tag)
40             throws XmlPullParserException {
41         return parser.getEventType() == START_TAG && tag.equals(parser.getName());
42     }
43 
isEndOfTag(@onNull XmlPullParser parser, @NonNull String tag)44     static boolean isEndOfTag(@NonNull XmlPullParser parser, @NonNull String tag)
45             throws XmlPullParserException {
46         return parser.getEventType() == END_TAG && tag.equals(parser.getName());
47     }
48 
49     /**
50      * Creates {@link AtomicFile} object that represents the back-up for the given user.
51      *
52      * IMPORTANT: the method will ALWAYS return the same {@link AtomicFile} object, which makes it
53      * possible to synchronize reads and writes to the file using the returned object.
54      *
55      * @param userId              the userId to retrieve the storage file
56      * @param fileName         the storage file name
57      * @return an AtomicFile for the user
58      */
59     @NonNull
createStorageFileForUser(@serIdInt int userId, String fileName)60     static AtomicFile createStorageFileForUser(@UserIdInt int userId, String fileName) {
61         return new AtomicFile(getBaseStorageFileForUser(userId, fileName));
62     }
63 
64     @NonNull
getBaseStorageFileForUser(@serIdInt int userId, String fileName)65     private static File getBaseStorageFileForUser(@UserIdInt int userId, String fileName) {
66         return new File(Environment.getDataSystemDeDirectory(userId), fileName);
67     }
68 
69     /**
70      * Writing to file could fail, for example, if the user has been recently removed and so was
71      * their DE (/data/system_de/<user-id>/) directory.
72      */
writeToFileSafely( @onNull AtomicFile file, @NonNull ThrowingConsumer<FileOutputStream> consumer)73     static void writeToFileSafely(
74             @NonNull AtomicFile file, @NonNull ThrowingConsumer<FileOutputStream> consumer) {
75         try {
76             file.write(consumer);
77         } catch (Exception e) {
78             Slog.e(TAG, "Error while writing to file " + file, e);
79         }
80     }
81 
DataStoreUtils()82     private DataStoreUtils() {
83     }
84 }
85