1 /* 2 * Copyright (C) 2022 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 android.signature.cts.api; 17 18 import android.signature.cts.VirtualPath; 19 import android.util.Log; 20 import com.google.common.base.Suppliers; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.nio.file.Files; 24 import java.nio.file.Path; 25 import java.util.HashMap; 26 import java.util.Map; 27 import java.util.function.Supplier; 28 import java.util.stream.Stream; 29 import java.util.zip.ZipFile; 30 31 /** 32 * Manages local storage of resources that need to be extracted from the Jar into the local 33 * filesystem before use. 34 */ 35 public class ResourceStore { 36 37 private static final String TAG = ResourceStore.class.getSimpleName(); 38 39 /** 40 * Supplier for the temporary directory. 41 */ 42 private static final Supplier<Path> TEMPORARY_DIRECTORY = Suppliers.memoize(() -> { 43 try { 44 return Files.createTempDirectory("signature"); 45 } catch (IOException e) { 46 throw new RuntimeException(e); 47 } 48 })::get; 49 50 /** 51 * A map from a {@link VirtualPath} to a {@link ZipFile} for zip file resources that 52 * have been extracted from the jar into the local file system. 53 */ 54 private static final Map<VirtualPath, ZipFile> extractedZipFiles = new HashMap<>(); 55 readResource(ClassLoader classLoader, String resourceName)56 public static Stream<VirtualPath> readResource(ClassLoader classLoader, String resourceName) { 57 try { 58 VirtualPath resourcePath = VirtualPath.get(classLoader, resourceName); 59 if (resourceName.endsWith(".zip")) { 60 // Extract to a temporary file and then read from there. If the resource has already 61 // been extracted before then reuse the previous file. 62 @SuppressWarnings("resource") 63 ZipFile zip = extractedZipFiles.computeIfAbsent(resourcePath, virtualPath -> { 64 try { 65 Path path = extractResourceToFile(resourceName, resourcePath); 66 return new ZipFile(path.toFile()); 67 } catch (IOException e) { 68 throw new RuntimeException(e); 69 } 70 }); 71 return zip.stream().map(entry -> VirtualPath.get(zip, entry)); 72 } else { 73 return Stream.of(resourcePath); 74 } 75 } catch (IOException e) { 76 throw new RuntimeException(e); 77 } 78 } 79 80 /** 81 * Close any previously opened {@link ZipFile} instances. 82 */ close()83 public static void close() { 84 for (ZipFile zipfile: extractedZipFiles.values()) { 85 // If an error occurred when closing the ZipFile log the failure and continue. 86 try { 87 zipfile.close(); 88 } catch (IOException e) { 89 Log.e(TAG, "Could not close ZipFile " + zipfile, e); 90 } 91 } 92 } 93 extractResourceToFile(String resourceName, VirtualPath resourcePath)94 private static Path extractResourceToFile(String resourceName, VirtualPath resourcePath) 95 throws IOException { 96 Path tempDirectory = TEMPORARY_DIRECTORY.get(); 97 Path file = tempDirectory.resolve(resourceName); 98 try (InputStream is = resourcePath.newInputStream()) { 99 Log.i(TAG, "extractResourceToFile: extracting " + resourceName + " to " + file); 100 Files.copy(is, file); 101 } 102 return file; 103 } 104 } 105