-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualMachineDescriptor.java
More file actions
160 lines (142 loc) · 5.3 KB
/
Copy pathVirtualMachineDescriptor.java
File metadata and controls
160 lines (142 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2022 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 android.system.virtualmachine;
import static java.util.Objects.requireNonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import java.io.IOException;
/**
* A VM descriptor that captures the state of a Virtual Machine.
*
* <p>You can capture the current state of VM by creating an instance of this class with {@link
* VirtualMachine#toDescriptor}, optionally pass it to another App, and then build an identical VM
* with the descriptor received.
*
* @hide
*/
@SystemApi
public final class VirtualMachineDescriptor implements Parcelable, AutoCloseable {
private volatile boolean mClosed = false;
@NonNull private final ParcelFileDescriptor mConfigFd;
// File descriptor of the file containing the instance id - will be null iff
// FEATURE_LLPVM_CHANGES is disabled.
@Nullable private final ParcelFileDescriptor mInstanceIdFd;
@NonNull private final ParcelFileDescriptor mInstanceImgFd;
// File descriptor of the image backing the encrypted storage - Will be null if encrypted
// storage is not enabled. */
@Nullable private final ParcelFileDescriptor mEncryptedStoreFd;
@Override
public int describeContents() {
return CONTENTS_FILE_DESCRIPTOR;
}
@Override
public void writeToParcel(@NonNull Parcel out, int flags) {
checkNotClosed();
out.writeParcelable(mConfigFd, flags);
out.writeParcelable(mInstanceIdFd, flags);
out.writeParcelable(mInstanceImgFd, flags);
out.writeParcelable(mEncryptedStoreFd, flags);
}
@NonNull
public static final Parcelable.Creator<VirtualMachineDescriptor> CREATOR =
new Parcelable.Creator<>() {
public VirtualMachineDescriptor createFromParcel(Parcel in) {
return new VirtualMachineDescriptor(in);
}
public VirtualMachineDescriptor[] newArray(int size) {
return new VirtualMachineDescriptor[size];
}
};
/**
* @return File descriptor of the VM configuration file config.xml.
*/
@NonNull
ParcelFileDescriptor getConfigFd() {
checkNotClosed();
return mConfigFd;
}
/**
* @return File descriptor of the file containing instance_id of the VM.
*/
@Nullable
ParcelFileDescriptor getInstanceIdFd() {
checkNotClosed();
return mInstanceIdFd;
}
/**
* @return File descriptor of the instance.img of the VM.
*/
@NonNull
ParcelFileDescriptor getInstanceImgFd() {
checkNotClosed();
return mInstanceImgFd;
}
/**
* @return File descriptor of image backing the encrypted storage.
* <p>This method will return null if encrypted storage is not enabled.
*/
@Nullable
ParcelFileDescriptor getEncryptedStoreFd() {
checkNotClosed();
return mEncryptedStoreFd;
}
VirtualMachineDescriptor(
@NonNull ParcelFileDescriptor configFd,
@Nullable ParcelFileDescriptor instanceIdFd,
@NonNull ParcelFileDescriptor instanceImgFd,
@Nullable ParcelFileDescriptor encryptedStoreFd) {
mConfigFd = requireNonNull(configFd);
mInstanceIdFd = instanceIdFd;
mInstanceImgFd = requireNonNull(instanceImgFd);
mEncryptedStoreFd = encryptedStoreFd;
}
private VirtualMachineDescriptor(Parcel in) {
mConfigFd = requireNonNull(readParcelFileDescriptor(in));
mInstanceIdFd = readParcelFileDescriptor(in);
mInstanceImgFd = requireNonNull(readParcelFileDescriptor(in));
mEncryptedStoreFd = readParcelFileDescriptor(in);
}
private ParcelFileDescriptor readParcelFileDescriptor(Parcel in) {
return in.readParcelable(
ParcelFileDescriptor.class.getClassLoader(), ParcelFileDescriptor.class);
}
/**
* Release any resources held by this descriptor. Calling {@code close} on an already-closed
* descriptor has no effect.
*/
@Override
public void close() {
mClosed = true;
// Let the compiler do the work: close everything, throw if any of them fail, skipping null.
try (mConfigFd;
mInstanceIdFd;
mInstanceImgFd;
mEncryptedStoreFd) {
} catch (IOException ignored) {
// PFD already swallows exceptions from closing the fd. There's no reason to propagate
// this to the caller.
}
}
private void checkNotClosed() {
if (mClosed) {
throw new IllegalStateException("Descriptor has been closed");
}
}
}