Skip to content

Commit

Permalink
Fix object size being 0 when retrying request (#14)
Browse files Browse the repository at this point in the history
Because the code only put the hash in the cache and not the size, the
size that was sent in a retried request was 0.

Some server implementations check the size, so those requests may fail.

To fix it, also put the size in the cache and use it when creating a
request from the cache.
  • Loading branch information
robinst committed Jul 19, 2016
1 parent 179985e commit 7186b36
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/main/java/git/lfs/migrate/GitConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class GitConverter implements AutoCloseable {
@NotNull
private final DB cache;
@NotNull
private final HTreeMap<String, String> cacheSha256;
private final HTreeMap<String, MetaData> cacheMeta;

public GitConverter(@NotNull File cachePath, @NotNull File basePath, @NotNull String[] globs) throws IOException, InvalidPatternException {
this.basePath = basePath;
Expand All @@ -60,7 +60,7 @@ public GitConverter(@NotNull File cachePath, @NotNull File basePath, @NotNull St
.mmapFileEnable()
.cacheSoftRefEnable()
.make();
cacheSha256 = cache.getHashMap("sha256");
cacheMeta = cache.getHashMap("meta");
}

@Override
Expand Down Expand Up @@ -283,7 +283,7 @@ public ObjectId convert(@NotNull ObjectInserter inserter, @NotNull ConvertResolv
private String createRemoteFile(@NotNull ObjectId id, @NotNull ObjectLoader loader, @NotNull Uploader uploader) throws IOException {
// Create LFS stream.
final String hash;
final String cached = cacheSha256.get(id.name());
final MetaData cached = cacheMeta.get(id.name());
long size = 0;
if (cached == null) {
final MessageDigest md = createSha256();
Expand All @@ -297,10 +297,11 @@ private String createRemoteFile(@NotNull ObjectId id, @NotNull ObjectLoader load
}
}
hash = new String(Hex.encodeHex(md.digest(), true));
cacheSha256.put(id.name(), hash);
cacheMeta.put(id.name(), new MetaData(hash, size));
cache.commit();
} else {
hash = cached;
hash = cached.oid;
size = cached.size;
}
uploader.upload(id, new Meta(hash, size));
return hash;
Expand All @@ -311,18 +312,20 @@ private String createLocalFile(@NotNull ObjectId id, @NotNull ObjectLoader loade
// Create LFS stream.
final File tmpFile = new File(tempPath, UUID.randomUUID().toString());
final MessageDigest md = createSha256();
int size = 0;
try (InputStream istream = loader.openStream();
OutputStream ostream = new FileOutputStream(tmpFile)) {
byte[] buffer = new byte[0x10000];
while (true) {
int size = istream.read(buffer);
if (size <= 0) break;
ostream.write(buffer, 0, size);
md.update(buffer, 0, size);
int read = istream.read(buffer);
if (read <= 0) break;
ostream.write(buffer, 0, read);
md.update(buffer, 0, read);
size += read;
}
}
final String hash = new String(Hex.encodeHex(md.digest(), true));
cacheSha256.putIfAbsent(id.name(), hash);
cacheMeta.putIfAbsent(id.name(), new MetaData(hash, size));
cache.commit();
// Rename file.
final File lfsFile = new File(basePath, "lfs/objects/" + hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash);
Expand Down Expand Up @@ -460,4 +463,14 @@ public interface ConvertTask {
public interface Uploader {
void upload(@NotNull ObjectId oid, @NotNull Meta meta);
}

private static class MetaData implements Serializable {
private final String oid;
private final long size;

MetaData(String oid, long size) {
this.oid = oid;
this.size = size;
}
}
}

0 comments on commit 7186b36

Please sign in to comment.