From 11e2e746c13cc94cabc96e708f3c6a2b159b5995 Mon Sep 17 00:00:00 2001 From: Carsten Pfeiffer Date: Wed, 16 Mar 2011 16:57:35 +0100 Subject: Support reading first SHA-1 from large FETCH_HEAD files When reading refs, avoid reading huge files that were put there accidentally, but still read the top of e.g. FETCH_HEAD, which may be longer than our limit. We're only interested in the first line anyway. Bug: 340880 Change-Id: I11029b9b443f22019bf80bd3dd942b48b531bc11 Signed-off-by: Carsten Pfieffer Signed-off-by: Chris Aniszczyk --- .../eclipse/jgit/storage/file/RefDirectory.java | 6 +++- org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java | 41 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java index c056d5a195..5a36a71ee4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java @@ -850,9 +850,10 @@ public class RefDirectory extends RefDatabase { } else if (modified == 0) return null; + final int limit = 4096; final byte[] buf; try { - buf = IO.readFully(path, 4096); + buf = IO.readSome(path, limit); } catch (FileNotFoundException noFile) { return null; // doesn't exist; not a reference. } @@ -862,6 +863,9 @@ public class RefDirectory extends RefDatabase { return null; // empty file; not a reference. if (isSymRef(buf, n)) { + if (n == limit) + return null; // possibly truncated ref + // trim trailing whitespace while (0 < n && Character.isWhitespace(buf[n - 1])) n--; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java index a9c3853a9d..4b9c572013 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java @@ -77,6 +77,47 @@ public class IO { return IO.readFully(path, Integer.MAX_VALUE); } + /** + * Read at most limit bytes from the local file into memory as a byte array. + * + * @param path + * location of the file to read. + * @param limit + * maximum number of bytes to read, if the file is larger than + * only the first limit number of bytes are returned + * @return complete contents of the requested local file. If the contents + * exceeds the limit, then only the limit is returned. + * @throws FileNotFoundException + * the file does not exist. + * @throws IOException + * the file exists, but its contents cannot be read. + */ + public static final byte[] readSome(final File path, final int limit) + throws FileNotFoundException, IOException { + FileInputStream in = new FileInputStream(path); + try { + byte[] buf = new byte[limit]; + int cnt = 0; + for (;;) { + int n = in.read(buf, cnt, buf.length - cnt); + if (n <= 0) + break; + cnt += n; + } + if (cnt == buf.length) + return buf; + byte[] res = new byte[cnt]; + System.arraycopy(buf, 0, res, 0, cnt); + return res; + } finally { + try { + in.close(); + } catch (IOException ignored) { + // do nothing + } + } + } + /** * Read an entire local file into memory as a byte array. * -- cgit v1.2.3