Browse Source

DirCache: Do not create duplicate tree entries

If a file (e.g.  "A") and a subtree file (e.g. "A/foo.c") both appear
in the DirCache this cache should not be written out as a tree object.
The "A" file and "A" subtree conflict with each other in the same tree
and will fail fsck.

Detect this condition during DirCacheBuilder and DirCacheEditor
finish() so the application can be halted early before it updates a
DirCache that might later write an invalid tree structure.

Change-Id: I95660787e88df336297949b383f4c5fda52e75f5
tags/v4.2.0.201601211800-r
Shawn Pearce 8 years ago
parent
commit
683c41af92

+ 46
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java View File

@@ -43,11 +43,13 @@
package org.eclipse.jgit.dircache;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
import org.eclipse.jgit.errors.DirCacheNameConflictException;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test;
@@ -212,6 +214,50 @@ public class DirCachePathEditTest {
assertEquals("e", dc.getEntry(4).getPathString());
}

@Test
public void testFileOverlapsTree() throws Exception {
DirCache dc = DirCache.newInCore();
DirCacheEditor editor = dc.editor();
editor.add(new AddEdit("a"));
editor.add(new AddEdit("a/b").setReplace(false));
try {
editor.finish();
fail("Expected DirCacheNameConflictException to be thrown");
} catch (DirCacheNameConflictException e) {
assertEquals("a a/b", e.getMessage());
assertEquals("a", e.getPath1());
assertEquals("a/b", e.getPath2());
}

editor = dc.editor();
editor.add(new AddEdit("A.c"));
editor.add(new AddEdit("A/c").setReplace(false));
editor.add(new AddEdit("A0c"));
editor.add(new AddEdit("A"));
try {
editor.finish();
fail("Expected DirCacheNameConflictException to be thrown");
} catch (DirCacheNameConflictException e) {
assertEquals("A A/c", e.getMessage());
assertEquals("A", e.getPath1());
assertEquals("A/c", e.getPath2());
}

editor = dc.editor();
editor.add(new AddEdit("A.c"));
editor.add(new AddEdit("A/b/c/d").setReplace(false));
editor.add(new AddEdit("A/b/c"));
editor.add(new AddEdit("A0c"));
try {
editor.finish();
fail("Expected DirCacheNameConflictException to be thrown");
} catch (DirCacheNameConflictException e) {
assertEquals("A/b/c A/b/c/d", e.getMessage());
assertEquals("A/b/c", e.getPath1());
assertEquals("A/b/c/d", e.getPath2());
}
}

private static DirCacheEntry createEntry(String path, int stage) {
DirCacheEntry entry = new DirCacheEntry(path, stage);
entry.setFileMode(FileMode.REGULAR_FILE);

+ 74
- 0
org.eclipse.jgit/src/org/eclipse/jgit/dircache/BaseDirCacheEditor.java View File

@@ -45,9 +45,12 @@
package org.eclipse.jgit.dircache;

import static org.eclipse.jgit.lib.FileMode.TREE;
import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;

import java.io.IOException;

import org.eclipse.jgit.errors.DirCacheNameConflictException;

/**
* Generic update/editing support for {@link DirCache}.
* <p>
@@ -170,6 +173,7 @@ abstract class BaseDirCacheEditor {
* {@link #finish()}, and only after {@link #entries} is sorted.
*/
protected void replace() {
checkNameConflicts();
if (entryCnt < entries.length / 2) {
final DirCacheEntry[] n = new DirCacheEntry[entryCnt];
System.arraycopy(entries, 0, n, 0, entryCnt);
@@ -178,6 +182,76 @@ abstract class BaseDirCacheEditor {
cache.replace(entries, entryCnt);
}

private void checkNameConflicts() {
int end = entryCnt - 1;
for (int eIdx = 0; eIdx < end; eIdx++) {
DirCacheEntry e = entries[eIdx];
if (e.getStage() != 0) {
continue;
}

byte[] ePath = e.path;
int prefixLen = lastSlash(ePath) + 1;

for (int nIdx = eIdx + 1; nIdx < entryCnt; nIdx++) {
DirCacheEntry n = entries[nIdx];
if (n.getStage() != 0) {
continue;
}

byte[] nPath = n.path;
if (!startsWith(ePath, nPath, prefixLen)) {
// Different prefix; this entry is in another directory.
break;
}

int s = nextSlash(nPath, prefixLen);
int m = s < nPath.length ? TYPE_TREE : n.getRawMode();
int cmp = pathCompare(
ePath, prefixLen, ePath.length, TYPE_TREE,
nPath, prefixLen, s, m);
if (cmp < 0) {
break;
} else if (cmp == 0) {
throw new DirCacheNameConflictException(
e.getPathString(),
n.getPathString());
}
}
}
}

private static int lastSlash(byte[] path) {
for (int i = path.length - 1; i >= 0; i--) {
if (path[i] == '/') {
return i;
}
}
return -1;
}

private static int nextSlash(byte[] b, int p) {
final int n = b.length;
for (; p < n; p++) {
if (b[p] == '/') {
return p;
}
}
return n;
}

private static boolean startsWith(byte[] a, byte[] b, int n) {
if (b.length < n) {
return false;
}
for (n--; n >= 0; n--) {
if (a[n] != b[n]) {
return false;
}
}
return true;
}

static int pathCompare(byte[] aPath, int aPos, int aEnd, int aMode,
byte[] bPath, int bPos, int bEnd, int bMode) {
while (aPos < aEnd && bPos < bEnd) {

+ 80
- 0
org.eclipse.jgit/src/org/eclipse/jgit/errors/DirCacheNameConflictException.java View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2015, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.eclipse.jgit.errors;

/**
* Thrown by DirCache code when entries overlap in impossible way.
*
* @since 4.2
*/
public class DirCacheNameConflictException extends IllegalStateException {
private static final long serialVersionUID = 1L;

private final String path1;
private final String path2;

/**
* Construct an exception for a specific path.
*
* @param path1
* one path that conflicts.
* @param path2
* another path that conflicts.
*/
public DirCacheNameConflictException(String path1, String path2) {
super(path1 + ' ' + path2);
this.path1 = path1;
this.path2 = path2;
}

/** @return one of the paths that has a conflict. */
public String getPath1() {
return path1;
}

/** @return another path that has a conflict. */
public String getPath2() {
return path2;
}
}

Loading…
Cancel
Save