blob: 4db448a31ad37f0f90003499e21fbe31b07bca32 [file] [log] [blame]
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -07001/*
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.example.android.systemupdatersample.util;
18
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070019import com.example.android.systemupdatersample.PayloadSpec;
20
21import java.io.BufferedReader;
22import java.io.File;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.InputStreamReader;
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070026import java.nio.file.Files;
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070027import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Enumeration;
30import java.util.List;
31import java.util.zip.ZipEntry;
32import java.util.zip.ZipFile;
33
34/** The helper class that creates {@link PayloadSpec}. */
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070035public final class PayloadSpecs {
36
37 /**
38 * The payload PAYLOAD_ENTRY is stored in the zip package to comply with the Android OTA package
39 * format. We want to find out the offset of the entry, so that we can pass it over to the A/B
40 * updater without making an extra copy of the payload.
41 *
42 * <p>According to Android docs, the entries are listed in the order in which they appear in the
43 * zip file. So we enumerate the entries to identify the offset of the payload file.
44 * http://developer.android.com/reference/java/util/zip/ZipFile.html#entries()
45 */
46 public static PayloadSpec forNonStreaming(File packageFile) throws IOException {
47 boolean payloadFound = false;
48 long payloadOffset = 0;
49 long payloadSize = 0;
50
51 List<String> properties = new ArrayList<>();
52 try (ZipFile zip = new ZipFile(packageFile)) {
53 Enumeration<? extends ZipEntry> entries = zip.entries();
54 long offset = 0;
55 while (entries.hasMoreElements()) {
56 ZipEntry entry = entries.nextElement();
57 String name = entry.getName();
58 // Zip local file header has 30 bytes + filename + sizeof extra field.
59 // https://en.wikipedia.org/wiki/Zip_(file_format)
60 long extraSize = entry.getExtra() == null ? 0 : entry.getExtra().length;
61 offset += 30 + name.length() + extraSize;
62
63 if (entry.isDirectory()) {
64 continue;
65 }
66
67 long length = entry.getCompressedSize();
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070068 if (PackageFiles.PAYLOAD_BINARY_FILE_NAME.equals(name)) {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070069 if (entry.getMethod() != ZipEntry.STORED) {
70 throw new IOException("Invalid compression method.");
71 }
72 payloadFound = true;
73 payloadOffset = offset;
74 payloadSize = length;
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -070075 } else if (PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME.equals(name)) {
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -070076 InputStream inputStream = zip.getInputStream(entry);
77 if (inputStream != null) {
78 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
79 String line;
80 while ((line = br.readLine()) != null) {
81 properties.add(line);
82 }
83 }
84 }
85 offset += length;
86 }
87 }
88
89 if (!payloadFound) {
90 throw new IOException("Failed to find payload entry in the given package.");
91 }
92 return PayloadSpec.newBuilder()
93 .url("file://" + packageFile.getAbsolutePath())
94 .offset(payloadOffset)
95 .size(payloadSize)
96 .properties(properties)
97 .build();
98 }
99
100 /**
Zhomart Mukhamejanovda7e2372018-05-01 12:50:55 -0700101 * Creates a {@link PayloadSpec} for streaming update.
102 */
103 public static PayloadSpec forStreaming(String updateUrl,
104 long offset,
105 long size,
106 File propertiesFile) throws IOException {
107 return PayloadSpec.newBuilder()
108 .url(updateUrl)
109 .offset(offset)
110 .size(size)
111 .properties(Files.readAllLines(propertiesFile.toPath()))
112 .build();
113 }
114
115 /**
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -0700116 * Converts an {@link PayloadSpec} to a string.
117 */
118 public static String toString(PayloadSpec payloadSpec) {
119 return "<PayloadSpec url=" + payloadSpec.getUrl()
120 + ", offset=" + payloadSpec.getOffset()
121 + ", size=" + payloadSpec.getSize()
122 + ", properties=" + Arrays.toString(
123 payloadSpec.getProperties().toArray(new String[0]))
124 + ">";
125 }
126
127 private PayloadSpecs() {}
128
129}