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 static com.android.server.backup.BackupManagerService.DEBUG; 20 import static com.android.server.backup.BackupManagerService.TAG; 21 22 import android.util.Slog; 23 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 29 /** User settings which are persisted across reboots. */ 30 final class UserBackupManagerFilePersistedSettings { 31 // File containing backup-enabled state. Contains a single byte to denote enabled status. 32 // Nonzero is enabled; file missing or a zero byte is disabled. 33 private static final String BACKUP_ENABLE_FILE = "backup_enabled"; 34 readBackupEnableState(int userId)35 static boolean readBackupEnableState(int userId) { 36 return readBackupEnableState(UserBackupManagerFiles.getBaseStateDir(userId)); 37 } 38 writeBackupEnableState(int userId, boolean enable)39 static void writeBackupEnableState(int userId, boolean enable) { 40 writeBackupEnableState(UserBackupManagerFiles.getBaseStateDir(userId), enable); 41 } 42 readBackupEnableState(File baseDir)43 private static boolean readBackupEnableState(File baseDir) { 44 File enableFile = new File(baseDir, BACKUP_ENABLE_FILE); 45 if (enableFile.exists()) { 46 try (FileInputStream fin = new FileInputStream(enableFile)) { 47 int state = fin.read(); 48 return state != 0; 49 } catch (IOException e) { 50 // can't read the file; fall through to assume disabled 51 Slog.e(TAG, "Cannot read enable state; assuming disabled"); 52 } 53 } else { 54 if (DEBUG) { 55 Slog.i(TAG, "isBackupEnabled() => false due to absent settings file"); 56 } 57 } 58 return false; 59 } 60 writeBackupEnableState(File baseDir, boolean enable)61 private static void writeBackupEnableState(File baseDir, boolean enable) { 62 File enableFile = new File(baseDir, BACKUP_ENABLE_FILE); 63 File stage = new File(baseDir, BACKUP_ENABLE_FILE + "-stage"); 64 try (FileOutputStream fout = new FileOutputStream(stage)) { 65 fout.write(enable ? 1 : 0); 66 fout.close(); 67 stage.renameTo(enableFile); 68 // will be synced immediately by the try-with-resources call to close() 69 } catch (IOException | RuntimeException e) { 70 Slog.e( 71 TAG, 72 "Unable to record backup enable state; reverting to disabled: " 73 + e.getMessage()); 74 enableFile.delete(); 75 stage.delete(); 76 } 77 } 78 } 79