Browse Source

Add exception class for when locking a file fails

This will allows calling classes to handle lock failures
without checking against the message and will also provide
access to the file that could not be locked.

Change-Id: I95bc59e1330a7af71ae3b0485c4516299193f504
tags/v1.3.0.201202121842-rc4
Kevin Sawicki 12 years ago
parent
commit
656461a991

+ 0
- 1
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties View File

@@ -56,7 +56,6 @@ cannotExecute=cannot execute: {0}
cannotGet=Cannot get {0}
cannotListRefs=cannot list refs
cannotLock=Cannot lock {0}
cannotLockFile=Cannot lock file {0}
cannotLockPackIn=Cannot lock pack in {0}
cannotMatchOnEmptyString=Cannot match on empty string.
cannotMoveIndexTo=Cannot move index to {0}

+ 0
- 1
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java View File

@@ -116,7 +116,6 @@ public class JGitText extends TranslationBundle {
/***/ public String cannotGet;
/***/ public String cannotListRefs;
/***/ public String cannotLock;
/***/ public String cannotLockFile;
/***/ public String cannotLockPackIn;
/***/ public String cannotMatchOnEmptyString;
/***/ public String cannotMoveIndexTo;

+ 2
- 1
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCache.java View File

@@ -62,6 +62,7 @@ import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.events.IndexChangedEvent;
@@ -190,7 +191,7 @@ public class DirCache {
throws CorruptObjectException, IOException {
final DirCache c = new DirCache(indexLocation, fs);
if (!c.lock())
throw new IOException(MessageFormat.format(JGitText.get().cannotLock, indexLocation));
throw new LockFailedException(indexLocation);

try {
c.read();

+ 90
- 0
org.eclipse.jgit/src/org/eclipse/jgit/errors/LockFailedException.java View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2011, GitHub 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;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;

import org.eclipse.jgit.JGitText;

/**
* An exception occurring when a file cannot be locked
*/
public class LockFailedException extends IOException {
private static final long serialVersionUID = 1L;

private File file;

/**
* Construct a CannotLockException for the given file and message
*
* @param file
* file that could not be locked
* @param message
* exception message
*/
public LockFailedException(File file, String message) {
super(message);
this.file = file;
}

/**
* Construct a CannotLockException for the given file
*
* @param file
* file that could not be locked
*/
public LockFailedException(File file) {
this(file, MessageFormat.format(JGitText.get().cannotLock, file));
}

/**
* Get the file that could not be locked
*
* @return file
*/
public File getFile() {
return file;
}
}

+ 2
- 1
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/FileBasedConfig.java View File

@@ -55,6 +55,7 @@ import java.io.IOException;
import java.text.MessageFormat;

import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Constants;
@@ -172,7 +173,7 @@ public class FileBasedConfig extends StoredConfig {
final byte[] out = Constants.encode(toText());
final LockFile lf = new LockFile(getFile(), fs);
if (!lf.lock())
throw new IOException(MessageFormat.format(JGitText.get().cannotLockFile, getFile()));
throw new LockFailedException(getFile());
try {
lf.setNeedSnapshot(true);
lf.write(out);

+ 4
- 2
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/ObjectDirectoryPackParser.java View File

@@ -58,6 +58,7 @@ import java.util.zip.CRC32;
import java.util.zip.Deflater;

import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.CoreConfig;
@@ -452,8 +453,9 @@ public class ObjectDirectoryPackParser extends PackParser {
//
try {
if (!keep.lock(lockMessage))
throw new IOException(MessageFormat.format(
JGitText.get().cannotLockPackIn, finalPack));
throw new LockFailedException(finalPack,
MessageFormat.format(
JGitText.get().cannotLockPackIn, finalPack));
} catch (IOException e) {
cleanupTemporaryFiles();
throw e;

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java View File

@@ -78,6 +78,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jgit.JGitText;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.errors.ObjectWritingException;
import org.eclipse.jgit.events.RefsChangedEvent;
@@ -562,8 +563,7 @@ public class RefDirectory extends RefDatabase {
LockFile lck = new LockFile(packedRefsFile,
update.getRepository().getFS());
if (!lck.lock())
throw new IOException(MessageFormat.format(
JGitText.get().cannotLockFile, packedRefsFile));
throw new LockFailedException(packedRefsFile);
try {
PackedRefList cur = readPackedRefs();
int idx = cur.find(name);

Loading…
Cancel
Save