1  /*
2   * Copyright (C) 2018 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.backup;
18  
19  import android.os.Environment;
20  import android.os.UserHandle;
21  
22  import java.io.File;
23  
24  /** Directories used for user specific backup/restore persistent state and book-keeping. */
25  final class UserBackupManagerFiles {
26      // Name of the directories the service stores bookkeeping data under.
27      private static final String BACKUP_PERSISTENT_DIR = "backup";
28      private static final String BACKUP_STAGING_DIR = "backup_stage";
29  
getBaseDir(int userId)30      private static File getBaseDir(int userId) {
31          return Environment.getDataSystemCeDirectory(userId);
32      }
33  
getBaseStateDir(int userId)34      static File getBaseStateDir(int userId) {
35          if (userId != UserHandle.USER_SYSTEM) {
36              return new File(getBaseDir(userId), BACKUP_PERSISTENT_DIR);
37          }
38          // TODO (b/120424138) remove if clause above and use same logic for system user.
39          // simultaneously, copy below dir to new system user dir
40          return new File(Environment.getDataDirectory(), BACKUP_PERSISTENT_DIR);
41      }
42  
getDataDir(int userId)43      static File getDataDir(int userId) {
44          if (userId != UserHandle.USER_SYSTEM) {
45              return new File(getBaseDir(userId), BACKUP_STAGING_DIR);
46          }
47          // TODO (b/120424138) remove if clause above and use same logic for system user. Since this
48          // is a staging dir, we dont need to copy below dir to new system user dir
49          return new File(Environment.getDownloadCacheDirectory(), BACKUP_STAGING_DIR);
50      }
51  
52      /** A user specific dir within the system user's directory. */
getStateDirInSystemDir(int userId)53      static File getStateDirInSystemDir(int userId) {
54          return new File(getBaseStateDir(UserHandle.USER_SYSTEM), "" + userId);
55      }
56  
57      /** Stored in a user specific dir within the system user's directory. */
getStateFileInSystemDir(String filename, int userId)58      static File getStateFileInSystemDir(String filename, int userId) {
59          return new File(getStateDirInSystemDir(userId), filename);
60      }
61  }
62