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 */ 16package libcore.tzdata.update; 17 18import android.util.Slog; 19 20import java.io.File; 21import java.io.IOException; 22 23/** 24 * A bundle-validation / extraction class. Separate from the services code that uses it for easier 25 * testing. 26 */ 27public final class TzDataBundleInstaller { 28 29 static final String CURRENT_TZ_DATA_DIR_NAME = "current"; 30 static final String WORKING_DIR_NAME = "working"; 31 static final String OLD_TZ_DATA_DIR_NAME = "old"; 32 33 private final String logTag; 34 private final File installDir; 35 36 public TzDataBundleInstaller(String logTag, File installDir) { 37 this.logTag = logTag; 38 this.installDir = installDir; 39 } 40 41 /** 42 * Install the supplied content. 43 * 44 * <p>Errors during unpacking or installation will throw an {@link IOException}. 45 * If the content is invalid this method returns {@code false}. 46 * If the installation completed successfully this method returns {@code true}. 47 */ 48 public boolean install(byte[] content) throws IOException { 49 File oldTzDataDir = new File(installDir, OLD_TZ_DATA_DIR_NAME); 50 if (oldTzDataDir.exists()) { 51 FileUtils.deleteRecursive(oldTzDataDir); 52 } 53 54 File currentTzDataDir = new File(installDir, CURRENT_TZ_DATA_DIR_NAME); 55 File workingDir = new File(installDir, WORKING_DIR_NAME); 56 57 Slog.i(logTag, "Applying time zone update"); 58 File unpackedContentDir = unpackBundle(content, workingDir); 59 try { 60 if (!checkBundleFilesExist(unpackedContentDir)) { 61 Slog.i(logTag, "Update not applied: Bundle is missing files"); 62 return false; 63 } 64 65 if (verifySystemChecksums(unpackedContentDir)) { 66 FileUtils.makeDirectoryWorldAccessible(unpackedContentDir); 67 68 if (currentTzDataDir.exists()) { 69 Slog.i(logTag, "Moving " + currentTzDataDir + " to " + oldTzDataDir); 70 FileUtils.rename(currentTzDataDir, oldTzDataDir); 71 } 72 Slog.i(logTag, "Moving " + unpackedContentDir + " to " + currentTzDataDir); 73 FileUtils.rename(unpackedContentDir, currentTzDataDir); 74 Slog.i(logTag, "Update applied: " + currentTzDataDir + " successfully created"); 75 return true; 76 } 77 Slog.i(logTag, "Update not applied: System checksum did not match"); 78 return false; 79 } finally { 80 deleteBestEffort(oldTzDataDir); 81 deleteBestEffort(unpackedContentDir); 82 } 83 } 84 85 private void deleteBestEffort(File dir) { 86 if (dir.exists()) { 87 try { 88 FileUtils.deleteRecursive(dir); 89 } catch (IOException e) { 90 // Logged but otherwise ignored. 91 Slog.w(logTag, "Unable to delete " + dir, e); 92 } 93 } 94 } 95 96 private File unpackBundle(byte[] content, File targetDir) throws IOException { 97 Slog.i(logTag, "Unpacking update content to: " + targetDir); 98 ConfigBundle bundle = new ConfigBundle(content); 99 bundle.extractTo(targetDir); 100 return targetDir; 101 } 102 103 private boolean checkBundleFilesExist(File unpackedContentDir) throws IOException { 104 Slog.i(logTag, "Verifying bundle contents"); 105 return FileUtils.filesExist(unpackedContentDir, 106 ConfigBundle.TZ_DATA_VERSION_FILE_NAME, 107 ConfigBundle.CHECKSUMS_FILE_NAME, 108 ConfigBundle.ZONEINFO_FILE_NAME, 109 ConfigBundle.ICU_DATA_FILE_NAME); 110 } 111 112 private boolean verifySystemChecksums(File unpackedContentDir) throws IOException { 113 Slog.i(logTag, "Verifying system file checksums"); 114 File checksumsFile = new File(unpackedContentDir, ConfigBundle.CHECKSUMS_FILE_NAME); 115 for (String line : FileUtils.readLines(checksumsFile)) { 116 int delimiterPos = line.indexOf(','); 117 if (delimiterPos <= 0 || delimiterPos == line.length() - 1) { 118 throw new IOException("Bad checksum entry: " + line); 119 } 120 long expectedChecksum; 121 try { 122 expectedChecksum = Long.parseLong(line.substring(0, delimiterPos)); 123 } catch (NumberFormatException e) { 124 throw new IOException("Invalid checksum value: " + line); 125 } 126 String filePath = line.substring(delimiterPos + 1); 127 File file = new File(filePath); 128 if (!file.exists()) { 129 Slog.i(logTag, "Failed checksum test for file: " + file + ": file not found"); 130 return false; 131 } 132 long actualChecksum = FileUtils.calculateChecksum(file); 133 if (actualChecksum != expectedChecksum) { 134 Slog.i(logTag, "Failed checksum test for file: " + file 135 + ": required=" + expectedChecksum + ", actual=" + actualChecksum); 136 return false; 137 } 138 } 139 return true; 140 } 141} 142