Skip to content

Commit 220fdaa

Browse files
committed
Skip adding non-existent files to HashStore
1 parent d29e07e commit 220fdaa

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

hash-utils/src/main/java/net/minecraftforge/util/hash/HashStore.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package net.minecraftforge.util.hash;
66

77
import net.minecraftforge.util.logging.Log;
8+
import org.jetbrains.annotations.NotNullByDefault;
9+
import org.jetbrains.annotations.Nullable;
810

911
import java.io.File;
1012
import java.io.IOException;
@@ -14,16 +16,18 @@
1416
import java.util.Collections;
1517
import java.util.HashMap;
1618
import java.util.Map;
19+
import java.util.Objects;
1720

1821
import static net.minecraftforge.util.hash.HashUtils.sneak;
1922

23+
@NotNullByDefault
2024
public class HashStore {
21-
private final HashFunction HASH = HashFunction.SHA1;
25+
private static final HashFunction HASH = HashFunction.SHA1;
2226

2327
private final String root;
2428
private final Map<String, String> oldHashes = new HashMap<>();
2529
private final Map<String, String> newHashes = new HashMap<>();
26-
private File target;
30+
private @Nullable File target;
2731

2832
public static HashStore fromFile(File path) {
2933
File parent = path.getAbsoluteFile().getParentFile();
@@ -37,11 +41,15 @@ public static HashStore fromDir(File path) {
3741
}
3842

3943
public HashStore() {
40-
this.root = "";
44+
this("");
4145
}
4246

4347
public HashStore(File root) {
44-
this.root = root.getAbsolutePath();
48+
this(root.getAbsolutePath());
49+
}
50+
51+
private HashStore(String root) {
52+
this.root = root;
4553
}
4654

4755
public boolean areSame(File... files) {
@@ -103,16 +111,20 @@ public boolean exists() {
103111
}
104112

105113
public HashStore add(String key, String data) {
106-
newHashes.put(key, HASH.hash(data));
114+
if (!data.isEmpty())
115+
newHashes.put(Objects.requireNonNull(key), HASH.hash(data));
107116
return this;
108117
}
109118

110119
public HashStore add(String key, byte[] data) {
111-
newHashes.put(key, HASH.hash(data));
120+
if (data.length > 0)
121+
this.newHashes.put(Objects.requireNonNull(key), HASH.hash(data));
112122
return this;
113123
}
114124

115-
public HashStore add(String key, File file) {
125+
public HashStore add(@Nullable String key, File file) {
126+
if (!file.exists()) return this;
127+
116128
try {
117129
if (key == null)
118130
key = getPath(file);
@@ -121,10 +133,10 @@ public HashStore add(String key, File file) {
121133
String prefix = getPath(file);
122134
for (File f : HashUtils.listFiles(file)) {
123135
String suffix = getPath(f).substring(prefix.length());
124-
newHashes.put(key + " - " + suffix, HASH.hash(f));
136+
this.newHashes.put(key + " - " + suffix, HASH.hash(f));
125137
}
126138
} else {
127-
newHashes.put(key, HASH.hash(file));
139+
this.newHashes.put(key, HASH.hash(file));
128140
}
129141
} catch (IOException e) {
130142
throw new RuntimeException(e);
@@ -150,22 +162,27 @@ public HashStore add(File file) {
150162
}
151163

152164
public boolean isSame() {
153-
return oldHashes.equals(newHashes);
165+
return this.oldHashes.equals(this.newHashes);
166+
}
167+
168+
/** Clears the new hashes, does not clear the old hashes read from {@link #load(File)}. */
169+
public void clear() {
170+
this.newHashes.clear();
154171
}
155172

156173
public void save() {
157-
if (target == null)
174+
if (this.target == null)
158175
throw new RuntimeException("HashStore.save() called without load(File) so we dont know where to save it! Use load(File) or save(File)");
159-
save(target);
176+
save(this.target);
160177
}
161178

162179
public void save(File file) {
163180
StringBuilder buf = new StringBuilder();
164-
ArrayList<String> keys = new ArrayList<String>(newHashes.keySet());
181+
ArrayList<String> keys = new ArrayList<>(this.newHashes.keySet());
165182
Collections.sort(keys);
166183

167184
for (String key : keys)
168-
buf.append(key).append('=').append(newHashes.get(key)).append('\n');
185+
buf.append(key).append('=').append(this.newHashes.get(key)).append('\n');
169186

170187
try {
171188
Files.write(file.toPath(), buf.toString().getBytes(StandardCharsets.UTF_8));
@@ -177,8 +194,8 @@ public void save(File file) {
177194
private String getPath(File file) {
178195
String path = file.getAbsolutePath();
179196

180-
if (path.startsWith(root))
181-
path = path.substring(root.length());
197+
if (path.startsWith(this.root))
198+
path = path.substring(this.root.length());
182199

183200
path = path.replace('\\', '/');
184201

hash-utils/src/main/java/net/minecraftforge/util/hash/HashUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.ArrayList;
1414
import java.util.List;
1515

16-
public class HashUtils {
16+
public final class HashUtils {
1717
/**
1818
* Gets the hashes of the given file using the given hash functions.
1919
*
@@ -119,4 +119,6 @@ private static List<File> listFiles(File dir, List<File> files) {
119119
static <R, E extends Throwable> R sneak(Throwable t) throws E {
120120
throw (E)t;
121121
}
122+
123+
private HashUtils() { }
122124
}

0 commit comments

Comments
 (0)