/*
- * Copyright (C) 2012, GitHub Inc. and others
+ * Copyright (C) 2012, 2021 GitHub Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
*/
package org.eclipse.jgit.internal.storage.file;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.File;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.errors.LockFailedException;
}
}
}
+
+ @Test
+ public void testLockTwice() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ lock.write("other".getBytes(StandardCharsets.US_ASCII));
+ lock.commit();
+ assertFalse(lock.isLocked());
+ checkFile(f, "other");
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("second".getBytes(StandardCharsets.US_ASCII));
+ }
+ lock.commit();
+ assertFalse(lock.isLocked());
+ checkFile(f, "second");
+ }
+
+ @Test
+ public void testLockTwiceUnlock() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ lock.write("other".getBytes(StandardCharsets.US_ASCII));
+ lock.unlock();
+ assertFalse(lock.isLocked());
+ checkFile(f, "content");
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("second".getBytes(StandardCharsets.US_ASCII));
+ }
+ lock.commit();
+ assertFalse(lock.isLocked());
+ checkFile(f, "second");
+ }
+
+ @Test
+ public void testLockWriteTwiceThrows1() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ lock.write("other".getBytes(StandardCharsets.US_ASCII));
+ assertThrows(Exception.class,
+ () -> lock.write("second".getBytes(StandardCharsets.US_ASCII)));
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockWriteTwiceThrows2() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("other".getBytes(StandardCharsets.US_ASCII));
+ }
+ assertThrows(Exception.class,
+ () -> lock.write("second".getBytes(StandardCharsets.US_ASCII)));
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockWriteTwiceThrows3() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ lock.write("other".getBytes(StandardCharsets.US_ASCII));
+ assertThrows(Exception.class, () -> {
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("second".getBytes(StandardCharsets.US_ASCII));
+ }
+ });
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockWriteTwiceThrows4() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("other".getBytes(StandardCharsets.US_ASCII));
+ }
+ assertThrows(Exception.class, () -> {
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("second".getBytes(StandardCharsets.US_ASCII));
+ }
+ });
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockUnclosedCommitThrows() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ try (OutputStream out = lock.getOutputStream()) {
+ out.write("other".getBytes(StandardCharsets.US_ASCII));
+ assertThrows(Exception.class, () -> lock.commit());
+ }
+ }
+
+ @Test
+ public void testLockNested() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ assertThrows(IllegalStateException.class, () -> lock.lock());
+ assertTrue(lock.isLocked());
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockHeld() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lock());
+ assertTrue(lock.isLocked());
+ LockFile lock2 = new LockFile(f);
+ assertFalse(lock2.lock());
+ assertFalse(lock2.isLocked());
+ assertTrue(lock.isLocked());
+ lock.unlock();
+ }
+
+ @Test
+ public void testLockForAppend() throws Exception {
+ File f = writeTrashFile("somefile", "content");
+ LockFile lock = new LockFile(f);
+ assertTrue(lock.lockForAppend());
+ assertTrue(lock.isLocked());
+ lock.write("other".getBytes(StandardCharsets.US_ASCII));
+ lock.commit();
+ assertFalse(lock.isLocked());
+ checkFile(f, "contentother");
+ }
}
/*
* Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
- * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others
+ * Copyright (C) 2006-2021, Shawn O. Pearce <spearce@spearce.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
private boolean haveLck;
- FileOutputStream os;
+ private FileOutputStream os;
private boolean needSnapshot;
- boolean fsync;
+ private boolean fsync;
+
+ private boolean isAppend;
+
+ private boolean written;
private FileSnapshot commitSnapshot;
* does not hold the lock.
*/
public boolean lock() throws IOException {
+ if (haveLck) {
+ throw new IllegalStateException(
+ MessageFormat.format(JGitText.get().lockAlreadyHeld, ref));
+ }
FileUtils.mkdirs(lck.getParentFile(), true);
try {
token = FS.DETECTED.createNewFileAtomic(lck);
LOG.error(JGitText.get().failedCreateLockFile, lck, e);
throw e;
}
- if (token.isCreated()) {
+ boolean obtainedLock = token.isCreated();
+ if (obtainedLock) {
haveLck = true;
- try {
- os = new FileOutputStream(lck);
- } catch (IOException ioe) {
- unlock();
- throw ioe;
- }
+ isAppend = false;
+ written = false;
} else {
closeToken();
}
- return haveLck;
+ return obtainedLock;
}
/**
* does not hold the lock.
*/
public boolean lockForAppend() throws IOException {
- if (!lock())
+ if (!lock()) {
return false;
+ }
copyCurrentContent();
+ isAppend = true;
+ written = false;
return true;
}
+ // For tests only
+ boolean isLocked() {
+ return haveLck;
+ }
+
+ private FileOutputStream getStream() throws IOException {
+ return new FileOutputStream(lck, isAppend);
+ }
+
/**
* Copy the current file content into the temporary file.
* <p>
*/
public void copyCurrentContent() throws IOException {
requireLock();
- try {
+ try (FileOutputStream out = getStream()) {
try (FileInputStream fis = new FileInputStream(ref)) {
if (fsync) {
FileChannel in = fis.getChannel();
long pos = 0;
long cnt = in.size();
while (0 < cnt) {
- long r = os.getChannel().transferFrom(in, pos, cnt);
+ long r = out.getChannel().transferFrom(in, pos, cnt);
pos += r;
cnt -= r;
}
} else {
final byte[] buf = new byte[2048];
int r;
- while ((r = fis.read(buf)) >= 0)
- os.write(buf, 0, r);
+ while ((r = fis.read(buf)) >= 0) {
+ out.write(buf, 0, r);
+ }
}
+ } catch (FileNotFoundException fnfe) {
+ if (ref.exists()) {
+ throw fnfe;
+ }
+ // Don't worry about a file that doesn't exist yet, it
+ // conceptually has no current content to copy.
}
- } catch (FileNotFoundException fnfe) {
- if (ref.exists()) {
- unlock();
- throw fnfe;
- }
- // Don't worry about a file that doesn't exist yet, it
- // conceptually has no current content to copy.
- //
} catch (IOException | RuntimeException | Error ioe) {
unlock();
throw ioe;
*/
public void write(byte[] content) throws IOException {
requireLock();
- try {
+ try (FileOutputStream out = getStream()) {
+ if (written) {
+ throw new IOException(MessageFormat
+ .format(JGitText.get().lockStreamClosed, ref));
+ }
if (fsync) {
- FileChannel fc = os.getChannel();
+ FileChannel fc = out.getChannel();
ByteBuffer buf = ByteBuffer.wrap(content);
- while (0 < buf.remaining())
+ while (0 < buf.remaining()) {
fc.write(buf);
+ }
fc.force(true);
} else {
- os.write(content);
+ out.write(content);
}
- os.close();
- os = null;
+ written = true;
} catch (IOException | RuntimeException | Error ioe) {
unlock();
throw ioe;
public OutputStream getOutputStream() {
requireLock();
- final OutputStream out;
- if (fsync)
- out = Channels.newOutputStream(os.getChannel());
- else
- out = os;
+ if (written || os != null) {
+ throw new IllegalStateException(MessageFormat
+ .format(JGitText.get().lockStreamMultiple, ref));
+ }
return new OutputStream() {
+
+ private OutputStream out;
+
+ private boolean closed;
+
+ private OutputStream get() throws IOException {
+ if (written) {
+ throw new IOException(MessageFormat
+ .format(JGitText.get().lockStreamMultiple, ref));
+ }
+ if (out == null) {
+ os = getStream();
+ if (fsync) {
+ out = Channels.newOutputStream(os.getChannel());
+ } else {
+ out = os;
+ }
+ }
+ return out;
+ }
+
@Override
public void write(byte[] b, int o, int n)
throws IOException {
- out.write(b, o, n);
+ get().write(b, o, n);
}
@Override
public void write(byte[] b) throws IOException {
- out.write(b);
+ get().write(b);
}
@Override
public void write(int b) throws IOException {
- out.write(b);
+ get().write(b);
}
@Override
public void close() throws IOException {
+ if (closed) {
+ return;
+ }
+ closed = true;
try {
- if (fsync)
- os.getChannel().force(true);
- out.close();
- os = null;
+ if (written) {
+ throw new IOException(MessageFormat
+ .format(JGitText.get().lockStreamClosed, ref));
+ }
+ if (out != null) {
+ if (fsync) {
+ os.getChannel().force(true);
+ }
+ out.close();
+ os = null;
+ }
+ written = true;
} catch (IOException | RuntimeException | Error ioe) {
unlock();
throw ioe;
}
void requireLock() {
- if (os == null) {
+ if (!haveLck) {
unlock();
throw new IllegalStateException(MessageFormat.format(JGitText.get().lockOnNotHeld, ref));
}
try {
FileUtils.rename(lck, ref, StandardCopyOption.ATOMIC_MOVE);
haveLck = false;
+ isAppend = false;
+ written = false;
closeToken();
return true;
} catch (IOException e) {
closeToken();
}
}
+ isAppend = false;
+ written = false;
}
/** {@inheritDoc} */