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