blob: 8a95877be71d1997ec4afc1040b59cca8d9de989 [file] [log] [blame]
Richard Uhlerb29f1452018-09-12 16:38:15 +01001/*
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
17package com.android.server.rollback;
18
Richard Uhler6f8a33b2019-02-26 10:40:36 +000019import android.annotation.IntDef;
20import android.annotation.NonNull;
Richard Uhlercca637a2019-02-27 11:50:48 +000021import android.content.rollback.RollbackInfo;
Richard Uhlerb29f1452018-09-12 16:38:15 +010022
23import java.io.File;
Richard Uhler6f8a33b2019-02-26 10:40:36 +000024import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
Richard Uhlerb29f1452018-09-12 16:38:15 +010026import java.time.Instant;
Richard Uhler01b06152019-01-09 13:51:54 +000027import java.util.ArrayList;
Richard Uhlerb29f1452018-09-12 16:38:15 +010028
29/**
Richard Uhler01b06152019-01-09 13:51:54 +000030 * Information about a rollback available for a set of atomically installed
31 * packages.
Richard Uhlerb29f1452018-09-12 16:38:15 +010032 */
Richard Uhler32f63a72019-01-09 10:43:52 +000033class RollbackData {
Richard Uhler6f8a33b2019-02-26 10:40:36 +000034 @IntDef(flag = true, prefix = { "ROLLBACK_STATE_" }, value = {
Richard Uhlere9aaf632019-03-01 16:03:01 +000035 ROLLBACK_STATE_ENABLING,
Richard Uhler6f8a33b2019-02-26 10:40:36 +000036 ROLLBACK_STATE_AVAILABLE,
37 ROLLBACK_STATE_COMMITTED,
38 })
39 @Retention(RetentionPolicy.SOURCE)
40 @interface RollbackState {}
41
42 /**
Richard Uhlere9aaf632019-03-01 16:03:01 +000043 * The rollback is in the process of being enabled. It is not yet
44 * available for use.
Richard Uhler6f8a33b2019-02-26 10:40:36 +000045 */
Richard Uhlere9aaf632019-03-01 16:03:01 +000046 static final int ROLLBACK_STATE_ENABLING = 0;
Richard Uhler6f8a33b2019-02-26 10:40:36 +000047
48 /**
49 * The rollback is currently available.
50 */
51 static final int ROLLBACK_STATE_AVAILABLE = 1;
52
53 /**
54 * The rollback has been committed.
55 */
56 static final int ROLLBACK_STATE_COMMITTED = 3;
57
Richard Uhler01b06152019-01-09 13:51:54 +000058 /**
Richard Uhlercca637a2019-02-27 11:50:48 +000059 * The rollback info for this rollback.
Richard Uhlerb9d54472019-01-22 12:50:08 +000060 */
Richard Uhlercca637a2019-02-27 11:50:48 +000061 public final RollbackInfo info;
Richard Uhlerb29f1452018-09-12 16:38:15 +010062
63 /**
Richard Uhlerc0e57422019-01-09 11:25:56 +000064 * The directory where the rollback data is stored.
Richard Uhlerb29f1452018-09-12 16:38:15 +010065 */
66 public final File backupDir;
67
68 /**
69 * The time when the upgrade occurred, for purposes of expiring
70 * rollback data.
Richard Uhler6f8a33b2019-02-26 10:40:36 +000071 *
72 * The timestamp is not applicable for all rollback states, but we make
73 * sure to keep it non-null to avoid potential errors there.
Richard Uhlerb29f1452018-09-12 16:38:15 +010074 */
Richard Uhler6f8a33b2019-02-26 10:40:36 +000075 public @NonNull Instant timestamp;
Richard Uhlerb29f1452018-09-12 16:38:15 +010076
Narayan Kamathbc36f8d2019-01-23 12:00:08 +000077 /**
Narayan Kamathfcd4a042019-02-01 14:16:37 +000078 * The session ID for the staged session if this rollback data represents a staged session,
79 * {@code -1} otherwise.
80 */
Richard Uhler6f8a33b2019-02-26 10:40:36 +000081 public final int stagedSessionId;
Narayan Kamathfcd4a042019-02-01 14:16:37 +000082
83 /**
Richard Uhler6f8a33b2019-02-26 10:40:36 +000084 * The current state of the rollback.
Richard Uhlere9aaf632019-03-01 16:03:01 +000085 * ENABLING, AVAILABLE, or COMMITTED.
Richard Uhler60ac7062019-02-05 13:25:39 +000086 */
Richard Uhler6f8a33b2019-02-26 10:40:36 +000087 public @RollbackState int state;
Richard Uhler60ac7062019-02-05 13:25:39 +000088
89 /**
Richard Uhlerba13ab22019-02-05 15:27:12 +000090 * The id of the post-reboot apk session for a staged install, if any.
91 */
92 public int apkSessionId = -1;
93
94 /**
Richard Uhler479a2962019-02-27 10:59:10 +000095 * True if we are expecting the package manager to call restoreUserData
96 * for this rollback because it has just been committed but the rollback
97 * has not yet been fully applied.
Narayan Kamathbc36f8d2019-01-23 12:00:08 +000098 */
99 // NOTE: All accesses to this field are from the RollbackManager handler thread.
Richard Uhler479a2962019-02-27 10:59:10 +0000100 public boolean restoreUserDataInProgress = false;
Narayan Kamathbc36f8d2019-01-23 12:00:08 +0000101
Richard Uhlercca637a2019-02-27 11:50:48 +0000102 /**
103 * Constructs a new, empty RollbackData instance.
104 *
105 * @param rollbackId the id of the rollback.
106 * @param backupDir the directory where the rollback data is stored.
107 * @param stagedSessionId the session id if this is a staged rollback, -1 otherwise.
108 */
109 RollbackData(int rollbackId, File backupDir, int stagedSessionId) {
110 this.info = new RollbackInfo(rollbackId,
111 /* packages */ new ArrayList<>(),
112 /* isStaged */ stagedSessionId != -1,
113 /* causePackages */ new ArrayList<>(),
114 /* committedSessionId */ -1);
Richard Uhlerb29f1452018-09-12 16:38:15 +0100115 this.backupDir = backupDir;
Narayan Kamathfcd4a042019-02-01 14:16:37 +0000116 this.stagedSessionId = stagedSessionId;
Richard Uhlere9aaf632019-03-01 16:03:01 +0000117 this.state = ROLLBACK_STATE_ENABLING;
Richard Uhler6f8a33b2019-02-26 10:40:36 +0000118 this.timestamp = Instant.now();
Richard Uhlercca637a2019-02-27 11:50:48 +0000119 }
120
121 /**
122 * Constructs a RollbackData instance with full rollback data information.
123 */
124 RollbackData(RollbackInfo info, File backupDir, Instant timestamp, int stagedSessionId,
Richard Uhler6f8a33b2019-02-26 10:40:36 +0000125 @RollbackState int state, int apkSessionId, boolean restoreUserDataInProgress) {
Richard Uhlercca637a2019-02-27 11:50:48 +0000126 this.info = info;
127 this.backupDir = backupDir;
128 this.timestamp = timestamp;
129 this.stagedSessionId = stagedSessionId;
Richard Uhler6f8a33b2019-02-26 10:40:36 +0000130 this.state = state;
Richard Uhlercca637a2019-02-27 11:50:48 +0000131 this.apkSessionId = apkSessionId;
132 this.restoreUserDataInProgress = restoreUserDataInProgress;
Richard Uhlerb29f1452018-09-12 16:38:15 +0100133 }
Richard Uhler72fb9612019-02-04 14:37:36 +0000134
135 /**
136 * Whether the rollback is for rollback of a staged install.
137 */
138 public boolean isStaged() {
Richard Uhlercca637a2019-02-27 11:50:48 +0000139 return info.isStaged();
Richard Uhler72fb9612019-02-04 14:37:36 +0000140 }
Richard Uhlerb29f1452018-09-12 16:38:15 +0100141}