Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f01ac39f authored by Chandan Nath's avatar Chandan Nath Committed by Android (Google) Code Review
Browse files

Merge "[Multi-user]: R backwards compatibility in activating/deactivating the service"

parents af731715 95368218
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.DumpUtils;
import com.android.server.backup.utils.FileUtils;
import com.android.server.backup.utils.RandomAccessFileUtils;

import java.io.File;
import java.io.FileDescriptor;
@@ -89,6 +91,12 @@ public class Trampoline extends IBackupManager.Stub {
     */
    private static final String BACKUP_ACTIVATED_FILENAME = "backup-activated";

    /**
     * Name of file for non-system users that remembers whether backup was explicitly activated or
     * deactivated with a call to setBackupServiceActive.
     */
    private static final String REMEMBER_ACTIVATED_FILENAME_PREFIX = "backup-remember-activated";

    // Product-level suppression of backup/restore.
    private static final String BACKUP_DISABLE_PROPERTY = "ro.backup.disable";

@@ -133,12 +141,18 @@ public class Trampoline extends IBackupManager.Stub {
                BACKUP_SUPPRESS_FILENAME);
    }

    /** Stored in the system user's directory and the file is indexed by the user it refers to. */
    protected File getRememberActivatedFileForNonSystemUser(int userId) {
        return FileUtils.createNewFile(UserBackupManagerFiles.getStateFileInSystemDir(
                REMEMBER_ACTIVATED_FILENAME_PREFIX, userId));
    }

    /** Stored in the system user's directory and the file is indexed by the user it refers to. */
    protected File getActivatedFileForNonSystemUser(int userId) {
        return new File(UserBackupManagerFiles.getBaseStateDir(UserHandle.USER_SYSTEM),
                BACKUP_ACTIVATED_FILENAME + "-" + userId);
        return UserBackupManagerFiles.getStateFileInSystemDir(BACKUP_ACTIVATED_FILENAME, userId);
    }

    // TODO (b/124359804) move to util method in FileUtils
    private void createFile(File file) throws IOException {
        if (file.exists()) {
            return;
@@ -150,6 +164,7 @@ public class Trampoline extends IBackupManager.Stub {
        }
    }

    // TODO (b/124359804) move to util method in FileUtils
    private void deleteFile(File file) {
        if (!file.exists()) {
            return;
@@ -312,6 +327,19 @@ public class Trampoline extends IBackupManager.Stub {
    public void setBackupServiceActive(int userId, boolean makeActive) {
        enforcePermissionsOnUser(userId);

        // In Q, backup is OFF by default for non-system users. In the future, we will change that
        // to ON unless backup was explicitly deactivated with a (permissioned) call to
        // setBackupServiceActive.
        // Therefore, remember this for use in the future. Basically the default in the future will
        // be: rememberFile.exists() ? rememberFile.value() : ON
        // Note that this has to be done right after the permission checks and before any other
        // action since we need to remember that a permissioned call was made irrespective of
        // whether the call changes the state or not.
        if (userId != UserHandle.USER_SYSTEM) {
            RandomAccessFileUtils.writeBoolean(getRememberActivatedFileForNonSystemUser(userId),
                    makeActive);
        }

        if (mGlobalDisable) {
            Slog.i(TAG, "Backup service not supported");
            return;
+5 −0
Original line number Diff line number Diff line
@@ -48,4 +48,9 @@ final class UserBackupManagerFiles {
        // is a staging dir, we dont need to copy below dir to new system user dir
        return new File(Environment.getDownloadCacheDirectory(), BACKUP_STAGING_DIR);
    }

    /** Stored in the system user's directory and the file is indexed by the user it refers to. */
    static File getStateFileInSystemDir(String prefix, int userId) {
        return new File(getBaseStateDir(UserHandle.USER_SYSTEM), prefix + "-" + userId);
    }
}
+4 −7
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@ import com.android.server.backup.transport.TransportNotRegisteredException;
import com.android.server.backup.utils.AppBackupUtils;
import com.android.server.backup.utils.BackupManagerMonitorUtils;
import com.android.server.backup.utils.BackupObserverUtils;
import com.android.server.backup.utils.FileUtils;
import com.android.server.backup.utils.SparseArrayUtils;

import com.google.android.collect.Sets;
@@ -2319,6 +2320,7 @@ public class UserBackupManagerService {
        mContext.enforceCallingPermission(android.Manifest.permission.BACKUP,
                "setAncestralSerialNumber");
        Slog.v(TAG, "Setting ancestral work profile id to " + ancestralSerialNumber);
        // TODO (b/124359804)
        try (RandomAccessFile af = getAncestralSerialNumberFile()) {
            af.writeLong(ancestralSerialNumber);
        } catch (IOException e) {
@@ -2331,6 +2333,7 @@ public class UserBackupManagerService {
     * {@link #setAncestralSerialNumber(long)}. Will return {@code -1} if not set.
     */
    public long getAncestralSerialNumber() {
        // TODO (b/124359804)
        try (RandomAccessFile af = getAncestralSerialNumberFile()) {
            return af.readLong();
        } catch (IOException e) {
@@ -2344,13 +2347,7 @@ public class UserBackupManagerService {
            mAncestralSerialNumberFile = new File(
                UserBackupManagerFiles.getBaseStateDir(getUserId()),
                SERIAL_ID_FILE);
            if (!mAncestralSerialNumberFile.exists()) {
                try {
                    mAncestralSerialNumberFile.createNewFile();
                } catch (IOException e) {
                    Slog.w(TAG, "serial number mapping file creation failed", e);
                }
            }
            FileUtils.createNewFile(mAncestralSerialNumberFile);
        }
        return new RandomAccessFile(mAncestralSerialNumberFile, "rwd");
    }
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.backup.utils;

import static com.android.server.backup.BackupManagerService.TAG;

import android.util.Slog;

import java.io.File;
import java.io.IOException;

/** Utility methods useful for working with backup related files. */
public final class FileUtils {
    /**
     * Ensure that the file exists in the file system. If an IOException is thrown, it is ignored.
     * This method is useful to avoid code duplication of the "try-catch-ignore exception" block.
     */
    public static File createNewFile(File file) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            Slog.w(TAG, "Failed to create file:" + file.getAbsolutePath(), e);
        }
        return file;
    }
}
+52 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.backup.utils;

import static com.android.server.backup.BackupManagerService.TAG;

import android.util.Slog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/** Utility methods useful for working with backup related RandomAccessFiles. */
public final class RandomAccessFileUtils {
    private static RandomAccessFile getRandomAccessFile(File file) throws FileNotFoundException {
        return new RandomAccessFile(file, "rwd");
    }

    /** Write a boolean to a File by wrapping it using a RandomAccessFile. */
    public static void writeBoolean(File file, boolean b) {
        try (RandomAccessFile af = getRandomAccessFile(file)) {
            af.writeBoolean(b);
        } catch (IOException e) {
            Slog.w(TAG, "Error writing file:" + file.getAbsolutePath(), e);
        }
    }

    /** Read a boolean from a File by wrapping it using a RandomAccessFile. */
    public static boolean readBoolean(File file, boolean def) {
        try (RandomAccessFile af = getRandomAccessFile(file)) {
            return af.readBoolean();
        } catch (IOException e) {
            Slog.w(TAG, "Error reading file:" + file.getAbsolutePath(), e);
        }
        return def;
    }
}
Loading