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

Commit 5c791040 authored by Al Sutton's avatar Al Sutton
Browse files

Address issues with ancestral serial number file

There were a few issues with the ancestral file storage methods which
I've fixed in this refactoring;

1) The log message for "get" said it was doing a write
2) The logic to get the file created a new file even if this was a read
3) The reading of the file asked for write permissions which it didn't
need
4) By asking for the correct permissions we can use the RandomAccessFile
constructor behaviour to create a file if needed.
5) By asking for the correct permissions we can catch the FileNotFound
exception thrown when the file doesn't exist and avoid creating an empty
unused file

Fixes: 157922462
Test: atest UserBackupManagerServiceTest ProfileSerialNumberHostSideTest
Change-Id: Ia5b070d2dbe9a778ec39473ec484f889ca8a8214
parent 3e89574d
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -129,7 +129,6 @@ 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;
@@ -2720,7 +2719,9 @@ public class UserBackupManagerService {
                TAG,
                addUserIdToLogMessage(
                        mUserId, "Setting ancestral work profile id to " + ancestralSerialNumber));
        try (RandomAccessFile af = getAncestralSerialNumberFile()) {

        try (RandomAccessFile af =
                new RandomAccessFile(getAncestralSerialNumberFile(), /* mode */ "rwd")) {
            af.writeLong(ancestralSerialNumber);
        } catch (IOException e) {
            Slog.w(
@@ -2736,26 +2737,28 @@ public class UserBackupManagerService {
     * {@link #setAncestralSerialNumber(long)}. Will return {@code -1} if not set.
     */
    public long getAncestralSerialNumber() {
        try (RandomAccessFile af = getAncestralSerialNumberFile()) {
        try (RandomAccessFile af =
                new RandomAccessFile(getAncestralSerialNumberFile(), /* mode */ "r")) {
            return af.readLong();
        } catch (FileNotFoundException e) {
            // It's OK not to have the file present, so we just return -1 to indicate no value.
        } catch (IOException e) {
            Slog.w(
                    TAG,
                    addUserIdToLogMessage(
                            mUserId, "Unable to write to work profile serial number file:"),
                            mUserId, "Unable to read work profile serial number file:"),
                    e);
            return -1;
        }
        return -1;
    }

    private RandomAccessFile getAncestralSerialNumberFile() throws FileNotFoundException {
    private File getAncestralSerialNumberFile() {
        if (mAncestralSerialNumberFile == null) {
            mAncestralSerialNumberFile = new File(
                UserBackupManagerFiles.getBaseStateDir(getUserId()),
                SERIAL_ID_FILE);
            FileUtils.createNewFile(mAncestralSerialNumberFile);
        }
        return new RandomAccessFile(mAncestralSerialNumberFile, "rwd");
        return mAncestralSerialNumberFile;
    }

    @VisibleForTesting