2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.data;
10 import java.security.MessageDigest;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.EnumSet;
14 import java.util.List;
15 import java.util.Locale;
16 import java.util.jar.JarEntry;
17 import java.util.jar.JarFile;
19 import com.threerings.getdown.util.FileUtil;
20 import com.threerings.getdown.util.ProgressObserver;
21 import com.threerings.getdown.util.StringUtil;
23 import static com.threerings.getdown.Log.log;
26 * Models a single file resource used by an {@link Application}.
28 public class Resource implements Comparable<Resource>
30 /** Defines special attributes for resources. */
31 public static enum Attr {
32 /** Indicates that the resource should be unpacked. */
34 /** If present, when unpacking a resource, any directories created by the newly
35 * unpacked resource will first be cleared of files before unpacking. */
37 /** Indicates that the resource should be marked executable. */
39 /** Indicates that the resource should be downloaded before a UI is displayed. */
41 /** Indicates that the resource is a jar containing native libs. */
45 public static final EnumSet<Attr> NORMAL = EnumSet.noneOf(Attr.class);
46 public static final EnumSet<Attr> UNPACK = EnumSet.of(Attr.UNPACK);
47 public static final EnumSet<Attr> EXEC = EnumSet.of(Attr.EXEC);
48 public static final EnumSet<Attr> PRELOAD = EnumSet.of(Attr.PRELOAD);
49 public static final EnumSet<Attr> NATIVE = EnumSet.of(Attr.NATIVE);
52 * Computes the MD5 hash of the supplied file.
53 * @param version the version of the digest protocol to use.
55 public static String computeDigest (int version, File target, MessageDigest md,
60 byte[] buffer = new byte[DIGEST_BUFFER_SIZE];
63 boolean isJar = isJar(target.getPath());
64 boolean isPacked200Jar = isPacked200Jar(target.getPath());
66 // if this is a jar, we need to compute the digest in a "timestamp and file order" agnostic
67 // manner to properly correlate jardiff patched jars with their unpatched originals
68 if (isJar || isPacked200Jar){
69 File tmpJarFile = null;
72 // if this is a compressed jar file, uncompress it to compute the jar file digest
74 tmpJarFile = new File(target.getPath() + ".tmp");
75 FileUtil.unpackPacked200Jar(target, tmpJarFile);
76 jar = new JarFile(tmpJarFile);
78 jar = new JarFile(target);
81 List<JarEntry> entries = Collections.list(jar.entries());
82 Collections.sort(entries, ENTRY_COMP);
85 for (JarEntry entry : entries) {
86 // old versions of the digest code skipped metadata
88 if (entry.getName().startsWith("META-INF")) {
89 updateProgress(obs, eidx, entries.size());
94 try (InputStream in = jar.getInputStream(entry)) {
95 while ((read = in.read(buffer)) != -1) {
96 md.update(buffer, 0, read);
100 updateProgress(obs, eidx, entries.size());
107 } catch (IOException ioe) {
108 log.warning("Error closing jar", "path", target, "jar", jar, "error", ioe);
111 if (tmpJarFile != null) {
112 FileUtil.deleteHarder(tmpJarFile);
117 long totalSize = target.length(), position = 0L;
118 try (FileInputStream fin = new FileInputStream(target)) {
119 while ((read = fin.read(buffer)) != -1) {
120 md.update(buffer, 0, read);
122 updateProgress(obs, position, totalSize);
126 return StringUtil.hexlate(md.digest());
130 * Creates a resource with the supplied remote URL and local path.
132 public Resource (String path, URL remote, File local, EnumSet<Attr> attrs)
137 _localNew = new File(local.toString() + "_new");
138 String lpath = _local.getPath();
139 _marker = new File(lpath + "v");
142 _isJar = isJar(lpath);
143 _isPacked200Jar = isPacked200Jar(lpath);
144 boolean unpack = attrs.contains(Attr.UNPACK);
145 if (unpack && _isJar) {
146 _unpacked = _local.getParentFile();
147 } else if(unpack && _isPacked200Jar) {
148 String dotJar = ".jar", lname = _local.getName();
149 String uname = lname.substring(0, lname.lastIndexOf(dotJar) + dotJar.length());
150 _unpacked = new File(_local.getParent(), uname);
155 * Returns the path associated with this resource.
157 public String getPath ()
163 * Returns the local location of this resource.
165 public File getLocal ()
171 * Returns the location of the to-be-installed new version of this resource.
173 public File getLocalNew ()
179 * Returns the location of the unpacked resource.
181 public File getUnpacked ()
187 * Returns the final target of this resource, whether it has been unpacked or not.
189 public File getFinalTarget ()
191 return shouldUnpack() ? getUnpacked() : getLocal();
195 * Returns the remote location of this resource.
197 public URL getRemote ()
203 * Returns true if this resource should be unpacked as a part of the validation process.
205 public boolean shouldUnpack ()
207 return _attrs.contains(Attr.UNPACK) && !SysProps.noUnpack();
211 * Returns true if this resource should be pre-downloaded.
213 public boolean shouldPredownload ()
215 return _attrs.contains(Attr.PRELOAD);
219 * Returns true if this resource is a native lib jar.
221 public boolean isNative ()
223 return _attrs.contains(Attr.NATIVE);
227 * Computes the MD5 hash of this resource's underlying file.
228 * <em>Note:</em> This is both CPU and I/O intensive.
229 * @param version the version of the digest protocol to use.
231 public String computeDigest (int version, MessageDigest md, ProgressObserver obs)
235 if (_local.toString().toLowerCase(Locale.ROOT).endsWith(Application.CONFIG_FILE)) {
238 file = _localNew.exists() ? _localNew : _local;
240 return computeDigest(version, file, md, obs);
244 * Returns true if this resource has an associated "validated" marker
247 public boolean isMarkedValid ()
249 if (!_local.exists()) {
253 return _marker.exists();
257 * Creates a "validated" marker file for this resource to indicate
258 * that its MD5 hash has been computed and compared with the value in
261 * @throws IOException if we fail to create the marker file.
263 public void markAsValid ()
266 _marker.createNewFile();
270 * Removes any "validated" marker file associated with this resource.
272 public void clearMarker ()
274 if (_marker.exists() && !FileUtil.deleteHarder(_marker)) {
275 log.warning("Failed to erase marker file '" + _marker + "'.");
280 * Installs the {@code getLocalNew} version of this resource to {@code getLocal}.
281 * @param validate whether or not to mark the resource as valid after installing.
283 public void install (boolean validate) throws IOException {
284 File source = getLocalNew(), dest = getLocal();
285 log.info("- " + source);
286 if (!FileUtil.renameTo(source, dest)) {
287 throw new IOException("Failed to rename " + source + " to " + dest);
296 * Unpacks this resource file into the directory that contains it.
298 public void unpack () throws IOException
301 if (!_isJar && !_isPacked200Jar) {
302 throw new IOException("Requested to unpack non-jar file '" + _local + "'.");
305 try (JarFile jar = new JarFile(_local)) {
306 FileUtil.unpackJar(jar, _unpacked, _attrs.contains(Attr.CLEAN));
309 FileUtil.unpackPacked200Jar(_local, _unpacked);
314 * Applies this resources special attributes: unpacks this resource if needed, marks it as
315 * executable if needed.
317 public void applyAttrs () throws IOException {
318 if (shouldUnpack()) {
321 if (_attrs.contains(Attr.EXEC)) {
322 FileUtil.makeExecutable(_local);
327 * Wipes this resource file along with any "validated" marker file that may be associated with
333 if (_local.exists() && !FileUtil.deleteHarder(_local)) {
334 log.warning("Failed to erase resource '" + _local + "'.");
338 @Override public int compareTo (Resource other) {
339 return _path.compareTo(other._path);
342 @Override public boolean equals (Object other)
344 if (other instanceof Resource) {
345 return _path.equals(((Resource)other)._path);
351 @Override public int hashCode ()
353 return _path.hashCode();
356 @Override public String toString ()
361 /** Helper function to simplify the process of reporting progress. */
362 protected static void updateProgress (ProgressObserver obs, long pos, long total)
365 obs.progress((int)(100 * pos / total));
369 protected static boolean isJar (String path)
371 return path.endsWith(".jar") || path.endsWith(".jar_new");
374 protected static boolean isPacked200Jar (String path)
376 return path.endsWith(".jar.pack") || path.endsWith(".jar.pack_new") ||
377 path.endsWith(".jar.pack.gz")|| path.endsWith(".jar.pack.gz_new");
380 protected String _path;
381 protected URL _remote;
382 protected File _local, _localNew, _marker, _unpacked;
383 protected EnumSet<Attr> _attrs;
384 protected boolean _isJar, _isPacked200Jar;
386 /** Used to sort the entries in a jar file. */
387 protected static final Comparator<JarEntry> ENTRY_COMP = new Comparator<JarEntry>() {
388 @Override public int compare (JarEntry e1, JarEntry e2) {
389 return e1.getName().compareTo(e2.getName());
393 protected static final int DIGEST_BUFFER_SIZE = 5 * 1025;