• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package libcore.tzdata.update;
17 
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Arrays;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26 
27 /**
28  * A configuration bundle. This is a thin wrapper around some in-memory bytes representing a zip
29  * archive and logic for its safe extraction.
30  */
31 public final class ConfigBundle {
32 
33     /** The name of the file inside the bundle containing the TZ data version. */
34     public static final String TZ_DATA_VERSION_FILE_NAME = "tzdata_version";
35 
36     /** The name of the file inside the bundle containing the expected device checksums. */
37     public static final String CHECKSUMS_FILE_NAME = "checksums";
38 
39     /** The name of the file inside the bundle containing bionic/libcore TZ data. */
40     public static final String ZONEINFO_FILE_NAME = "tzdata";
41 
42     /** The name of the file inside the bundle containing ICU TZ data. */
43     public static final String ICU_DATA_FILE_NAME = "icu/icu_tzdata.dat";
44 
45     private static final int BUFFER_SIZE = 8192;
46 
47     private final byte[] bytes;
48 
ConfigBundle(byte[] bytes)49     public ConfigBundle(byte[] bytes) {
50         this.bytes = bytes;
51     }
52 
getBundleBytes()53     public byte[] getBundleBytes() {
54         return bytes;
55     }
56 
extractTo(File targetDir)57     public void extractTo(File targetDir) throws IOException {
58         extractZipSafely(new ByteArrayInputStream(bytes), targetDir, true /* makeWorldReadable */);
59     }
60 
61     /** Visible for testing */
extractZipSafely(InputStream is, File targetDir, boolean makeWorldReadable)62     static void extractZipSafely(InputStream is, File targetDir, boolean makeWorldReadable)
63             throws IOException {
64 
65         // Create the extraction dir, if needed.
66         FileUtils.ensureDirectoriesExist(targetDir, makeWorldReadable);
67 
68         try (ZipInputStream zipInputStream = new ZipInputStream(is)) {
69             byte[] buffer = new byte[BUFFER_SIZE];
70             ZipEntry entry;
71             while ((entry = zipInputStream.getNextEntry()) != null) {
72                 // Validate the entry name: make sure the unpacked file will exist beneath the
73                 // targetDir.
74                 String name = entry.getName();
75                 // Note, we assume that nothing will quickly insert a symlink after createSubFile()
76                 // that might invalidate the guarantees about name existing beneath targetDir.
77                 File entryFile = FileUtils.createSubFile(targetDir, name);
78 
79                 if (entry.isDirectory()) {
80                     FileUtils.ensureDirectoriesExist(entryFile, makeWorldReadable);
81                 } else {
82                     // Create the path if there was no directory entry.
83                     if (!entryFile.getParentFile().exists()) {
84                         FileUtils.ensureDirectoriesExist(
85                                 entryFile.getParentFile(), makeWorldReadable);
86                     }
87 
88                     try (FileOutputStream fos = new FileOutputStream(entryFile)) {
89                         int count;
90                         while ((count = zipInputStream.read(buffer)) != -1) {
91                             fos.write(buffer, 0, count);
92                         }
93                         // sync to disk
94                         fos.getFD().sync();
95                     }
96                     // mark entryFile -rw-r--r--
97                     if (makeWorldReadable) {
98                         FileUtils.makeWorldReadable(entryFile);
99                     }
100                 }
101             }
102         }
103     }
104 
105     @Override
equals(Object o)106     public boolean equals(Object o) {
107         if (this == o) {
108             return true;
109         }
110         if (o == null || getClass() != o.getClass()) {
111             return false;
112         }
113 
114         ConfigBundle that = (ConfigBundle) o;
115 
116         if (!Arrays.equals(bytes, that.bytes)) {
117             return false;
118         }
119 
120         return true;
121     }
122 
123 }
124