import java.io.File;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
+import java.rmi.RemoteException;
import java.util.regex.Matcher;
+import javax.management.remote.JMXProviderException;
+
import org.eclipse.jgit.junit.JGitTestUtil;
import org.junit.After;
import org.junit.Assume;
import org.junit.Test;
public class FileUtilsTest {
+ private static final String MSG = "Stale file handle";
+
+ private static final String SOME_ERROR_MSG = "some error message";
+
+ private static final IOException IO_EXCEPTION = new UnsupportedEncodingException(
+ MSG);
+
+ private static final IOException IO_EXCEPTION_WITH_CAUSE = new RemoteException(
+ SOME_ERROR_MSG,
+ new JMXProviderException(SOME_ERROR_MSG, IO_EXCEPTION));
+
private File trash;
@Before
return path.replaceAll("/|\\\\",
Matcher.quoteReplacement(File.separator));
}
+
+ @Test
+ public void testIsStaleFileHandleWithDirectCause() throws Exception {
+ assertTrue(FileUtils.isStaleFileHandle(IO_EXCEPTION));
+ }
+
+ @Test
+ public void testIsStaleFileHandleWithIndirectCause() throws Exception {
+ assertFalse(
+ FileUtils.isStaleFileHandle(IO_EXCEPTION_WITH_CAUSE));
+ }
+
+ @Test
+ public void testIsStaleFileHandleInCausalChainWithDirectCause()
+ throws Exception {
+ assertTrue(
+ FileUtils.isStaleFileHandleInCausalChain(IO_EXCEPTION));
+ }
+
+ @Test
+ public void testIsStaleFileHandleInCausalChainWithIndirectCause()
+ throws Exception {
+ assertTrue(FileUtils
+ .isStaleFileHandleInCausalChain(IO_EXCEPTION_WITH_CAUSE));
+ }
}
return new PackedRefList(parsePackedRefs(br), snapshot,
ObjectId.fromRaw(digest.digest()));
} catch (IOException e) {
- if (FileUtils.isStaleFileHandle(e) && retries < maxStaleRetries) {
+ if (FileUtils.isStaleFileHandleInCausalChain(e)
+ && retries < maxStaleRetries) {
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(
JGitText.get().packedRefsHandleIsStale,
.matches("stale .*file .*handle"); //$NON-NLS-1$
}
+ /**
+ * Determine if a throwable or a cause in its causal chain is a Stale NFS
+ * File Handle
+ *
+ * @param throwable
+ * @return a boolean true if the throwable or a cause in its causal chain is
+ * a Stale NFS File Handle
+ * @since 4.7
+ */
+ public static boolean isStaleFileHandleInCausalChain(Throwable throwable) {
+ while (throwable != null) {
+ if (throwable instanceof IOException
+ && isStaleFileHandle((IOException) throwable)) {
+ return true;
+ }
+ throwable = throwable.getCause();
+ }
+ return false;
+ }
+
/**
* @param file
* @return {@code true} if the passed file is a symbolic link