Merge branch 'stable-4.10' into stable-4.11

* stable-4.10:
  Retry stale file handles on .git/config file

Change-Id: Ice5c8ae8c2992243a81da77e166406bc1930fe0e
This commit is contained in:
Matthias Sohn 2018-05-10 12:55:30 +02:00
commit 9bdbb06324
3 changed files with 56 additions and 30 deletions

View File

@ -136,6 +136,7 @@ compressingObjects=Compressing objects
configSubsectionContainsNewline=config subsection name contains newline configSubsectionContainsNewline=config subsection name contains newline
configSubsectionContainsNullByte=config subsection name contains byte 0x00 configSubsectionContainsNullByte=config subsection name contains byte 0x00
configValueContainsNullByte=config value contains byte 0x00 configValueContainsNullByte=config value contains byte 0x00
configHandleIsStale=config file handle is stale, {0}. retry
connectionFailed=connection failed connectionFailed=connection failed
connectionTimeOut=Connection time out: {0} connectionTimeOut=Connection time out: {0}
contextMustBeNonNegative=context must be >= 0 contextMustBeNonNegative=context must be >= 0

View File

@ -197,6 +197,7 @@ public static JGitText get() {
/***/ public String configSubsectionContainsNewline; /***/ public String configSubsectionContainsNewline;
/***/ public String configSubsectionContainsNullByte; /***/ public String configSubsectionContainsNullByte;
/***/ public String configValueContainsNullByte; /***/ public String configValueContainsNullByte;
/***/ public String configHandleIsStale;
/***/ public String connectionFailed; /***/ public String connectionFailed;
/***/ public String connectionTimeOut; /***/ public String connectionTimeOut;
/***/ public String contextMustBeNonNegative; /***/ public String contextMustBeNonNegative;

View File

@ -66,13 +66,19 @@
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils; import org.eclipse.jgit.util.RawParseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* The configuration file that is stored in the file of the file system. * The configuration file that is stored in the file of the file system.
*/ */
public class FileBasedConfig extends StoredConfig { public class FileBasedConfig extends StoredConfig {
private final static Logger LOG = LoggerFactory
.getLogger(FileBasedConfig.class);
private final File configFile; private final File configFile;
private final FS fs; private final FS fs;
@ -141,21 +147,25 @@ public final File getFile() {
*/ */
@Override @Override
public void load() throws IOException, ConfigInvalidException { public void load() throws IOException, ConfigInvalidException {
final int maxStaleRetries = 5;
int retries = 0;
while (true) {
final FileSnapshot oldSnapshot = snapshot; final FileSnapshot oldSnapshot = snapshot;
final FileSnapshot newSnapshot = FileSnapshot.save(getFile()); final FileSnapshot newSnapshot = FileSnapshot.save(getFile());
try { try {
final byte[] in = IO.readFully(getFile()); final byte[] in = IO.readFully(getFile());
final ObjectId newHash = hash(in); final ObjectId newHash = hash(in);
if (hash.equals(newHash)) { if (hash.equals(newHash)) {
if (oldSnapshot.equals(newSnapshot)) if (oldSnapshot.equals(newSnapshot)) {
oldSnapshot.setClean(newSnapshot); oldSnapshot.setClean(newSnapshot);
else } else {
snapshot = newSnapshot; snapshot = newSnapshot;
}
} else { } else {
final String decoded; final String decoded;
if (isUtf8(in)) { if (isUtf8(in)) {
decoded = RawParseUtils.decode(RawParseUtils.UTF8_CHARSET, decoded = RawParseUtils.decode(
in, 3, in.length); RawParseUtils.UTF8_CHARSET, in, 3, in.length);
utf8Bom = true; utf8Bom = true;
} else { } else {
decoded = RawParseUtils.decode(in); decoded = RawParseUtils.decode(in);
@ -164,17 +174,31 @@ public void load() throws IOException, ConfigInvalidException {
snapshot = newSnapshot; snapshot = newSnapshot;
hash = newHash; hash = newHash;
} }
return;
} catch (FileNotFoundException noFile) { } catch (FileNotFoundException noFile) {
if (configFile.exists()) { if (configFile.exists()) {
throw noFile; throw noFile;
} }
clear(); clear();
snapshot = newSnapshot; snapshot = newSnapshot;
return;
} catch (IOException e) { } catch (IOException e) {
if (FileUtils.isStaleFileHandle(e)
&& retries < maxStaleRetries) {
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(
JGitText.get().configHandleIsStale,
Integer.valueOf(retries)), e);
}
retries++;
continue;
}
throw new IOException(MessageFormat throw new IOException(MessageFormat
.format(JGitText.get().cannotReadFile, getFile()), e); .format(JGitText.get().cannotReadFile, getFile()), e);
} catch (ConfigInvalidException e) { } catch (ConfigInvalidException e) {
throw new ConfigInvalidException(MessageFormat.format(JGitText.get().cannotReadFile, getFile()), e); throw new ConfigInvalidException(MessageFormat
.format(JGitText.get().cannotReadFile, getFile()), e);
}
} }
} }