private RefUpdateResult UpdateImpl(RevWalk.RevWalk walk, StoreBase store) { if (isNameConflicting()) { return(RefUpdateResult.LockFailure); } var @lock = new LockFile(_looseFile); if ([email protected]()) { return(RefUpdateResult.LockFailure); } try { OldObjectId = _db.IdOf(Name); if (_expValue != null) { ObjectId o = OldObjectId ?? ObjectId.ZeroId; if (!AnyObjectId.equals(_expValue, o)) { return(RefUpdateResult.LockFailure); } } if (OldObjectId == null) { return(store.Store(@lock, RefUpdateResult.New)); } RevObject newObj = SafeParse(walk, _newValue); RevObject oldObj = SafeParse(walk, OldObjectId); if (newObj == oldObj) { return(store.Store(@lock, RefUpdateResult.NoChange)); } RevCommit newCom = (newObj as RevCommit); RevCommit oldCom = (oldObj as RevCommit); if (newCom != null && oldCom != null) { if (walk.isMergedInto(oldCom, newCom)) { return(store.Store(@lock, RefUpdateResult.FastForward)); } } if (IsForceUpdate) { return(store.Store(@lock, RefUpdateResult.Forced)); } return(RefUpdateResult.Rejected); } finally { @lock.Unlock(); } }
public void delete(RefDirectoryUpdate update) { Ref dst = update.getRef().getLeaf(); string name = dst.getName(); // Write the packed-refs file using an atomic update. We might // wind up reading it twice, before and after the lock, to ensure // we don't miss an edit made externally. PackedRefList packed = getPackedRefs(); if (packed.contains(name)) { var lck = new LockFile(packedRefsFile); if (!lck.Lock()) { throw new IOException("Cannot lock " + packedRefsFile); } try { PackedRefList cur = readPackedRefs(0, 0); int idx = cur.find(name); if (0 <= idx) { commitPackedRefs(lck, cur.remove(idx), packed); } } finally { lck.Unlock(); } } RefList <LooseRef> curLoose, newLoose; do { curLoose = looseRefs.get(); int idx = curLoose.find(name); if (idx < 0) { break; } newLoose = curLoose.remove(idx); } while (!looseRefs.compareAndSet(curLoose, newLoose)); int levels = levelsIn(name) - 2; delete(logFor(name), levels); if (dst.getStorage().IsLoose) { update.unlock(); delete(fileFor(name), levels); } modCnt.incrementAndGet(); fireRefsChanged(); }
/// <summary> /// Create the <code>pack-*.keep</code> file, with the given message. /// </summary> /// <param name="msg">message to store in the file.</param> /// <returns> /// true if the keep file was successfully written; false otherwise. /// </returns> /// <exception cref="IOException"> /// The keep file could not be written. /// </exception> public bool Lock(string msg) { if (msg == null) return false; if (!msg.EndsWith("\n")) msg += "\n"; using(LockFile lf = new LockFile(_keepFile)) { if (!lf.Lock()) return false; lf.Write(Constants.encode(msg)); return lf.Commit(); } }
protected override void writeFile(string file, byte[] content) { FileInfo p = PathUtil.CombineFilePath(_db.Directory, file); LockFile lck = new LockFile(p); if (!lck.Lock()) throw new ObjectWritingException("Can't write " + p); try { lck.Write(content); } catch (IOException) { throw new ObjectWritingException("Can't write " + p); } if (!lck.Commit()) throw new ObjectWritingException("Can't write " + p); }
public void save() { byte[] o = Constants.encode(toText()); using (LockFile lf = new LockFile(getFile())) { if (!lf.Lock()) { throw new IOException("Cannot lock " + getFile()); } lf.Write(o); if (!lf.Commit()) { throw new IOException("Cannot commit write to " + getFile()); } } }
protected override bool tryLock(bool deref) { Ref dst = Ref; if (deref) dst = dst.getLeaf(); string name = dst.getName(); _lock = new LockFile(_database.fileFor(name)); if (_lock.Lock()) { dst = _database.getRef(name); OldObjectId = (dst != null ? dst.getObjectId() : null); return true; } else { return false; } }
/// <summary> /// Create the <code>pack-*.keep</code> file, with the given message. /// </summary> /// <param name="msg">message to store in the file.</param> /// <returns> /// true if the keep file was successfully written; false otherwise. /// </returns> /// <exception cref="IOException"> /// The keep file could not be written. /// </exception> public bool Lock(string msg) { if (msg == null) { return(false); } if (!msg.EndsWith("\n")) { msg += "\n"; } var lf = new LockFile(_keepFile); if (!lf.Lock()) { return(false); } lf.Write(Constants.encode(msg)); return(lf.Commit()); }
protected override bool tryLock(bool deref) { Ref dst = Ref; if (deref) { dst = dst.getLeaf(); } string name = dst.getName(); _lock = new LockFile(_database.fileFor(name)); if (_lock.Lock()) { dst = _database.getRef(name); OldObjectId = (dst != null ? dst.getObjectId() : null); return(true); } else { return(false); } }
private static void LockAndWriteFile(FileInfo file, byte[] content) { string name = file.Name; var lck = new LockFile(file); if (!lck.Lock()) { throw new ObjectWritingException("Unable to lock " + name); } try { lck.Write(content); } catch (IOException ioe) { throw new ObjectWritingException("Unable to write " + name, ioe); } if (!lck.Commit()) { throw new ObjectWritingException("Unable to write " + name); } }
public void tryRenameWhenLocked(string toLock, string fromName, string toName, string headPointsTo) { // setup db.WriteSymref(Constants.HEAD, headPointsTo); ObjectId oldfromId = db.Resolve(fromName); ObjectId oldHeadId = db.Resolve(Constants.HEAD); RefLogWriter.WriteReflog(db, oldfromId, oldfromId, "Just a message", fromName); IList<ReflogReader.Entry> oldFromLog = db .ReflogReader(fromName).getReverseEntries(); IList<ReflogReader.Entry> oldHeadLog = oldHeadId != null ? db .ReflogReader(Constants.HEAD).getReverseEntries() : null; Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/" + fromName)).Exists, "internal check, we have a log"); // "someone" has branch X locked var lockFile = new LockFile(new FileInfo(Path.Combine(db.Directory.FullName, toLock))); try { Assert.IsTrue(lockFile.Lock()); // Now this is our test RefRename renameRef = db.RenameRef(fromName, toName); RefUpdate.RefUpdateResult result = renameRef.Rename(); Assert.AreEqual(RefUpdate.RefUpdateResult.LockFailure, result); // Check that the involved refs are the same despite the failure assertExists(false, toName); if (!toLock.Equals(toName)) assertExists(false, toName + ".lock"); assertExists(true, toLock + ".lock"); if (!toLock.Equals(fromName)) assertExists(false, "logs/" + fromName + ".lock"); assertExists(false, "logs/" + toName + ".lock"); Assert.AreEqual(oldHeadId, db.Resolve(Constants.HEAD)); Assert.AreEqual(oldfromId, db.Resolve(fromName)); Assert.IsNull(db.Resolve(toName)); Assert.AreEqual(oldFromLog.ToString(), db.ReflogReader(fromName) .getReverseEntries().ToString()); if (oldHeadId != null) Assert.AreEqual(oldHeadLog, db.ReflogReader(Constants.HEAD) .getReverseEntries()); } finally { lockFile.Unlock(); } }
public void testUpdateRefLockFailureLocked() { ObjectId opid = db.Resolve("refs/heads/master"); ObjectId pid = db.Resolve("refs/heads/master^"); RefUpdate updateRef = db.UpdateRef("refs/heads/master"); updateRef.NewObjectId = pid; var lockFile1 = new LockFile(new FileInfo(Path.Combine(db.Directory.FullName, "refs/heads/master"))); try { Assert.IsTrue(lockFile1.Lock()); // precondition to test RefUpdate.RefUpdateResult update = updateRef.Update(); Assert.AreEqual(RefUpdate.RefUpdateResult.LockFailure, update); Assert.AreEqual(opid, db.Resolve("refs/heads/master")); var lockFile2 = new LockFile(new FileInfo(Path.Combine(db.Directory.FullName, "refs/heads/master"))); Assert.IsFalse(lockFile2.Lock()); // was locked, still is } finally { lockFile1.Unlock(); } }
public void save() { byte[] o = Constants.encode(toText()); LockFile lf = new LockFile(getFile()); if (!lf.Lock()) throw new IOException("Cannot lock " + getFile()); try { lf.Write(o); if (!lf.Commit()) throw new IOException("Cannot commit write to " + getFile()); } finally { lf.Unlock(); } }
private RefUpdateResult UpdateImpl(RevWalk.RevWalk walk, StoreBase store) { if (isNameConflicting()) { return RefUpdateResult.LockFailure; } using (LockFile @lock = new LockFile(_looseFile)) { if ([email protected]()) { return RefUpdateResult.LockFailure; } OldObjectId = _db.IdOf(Name); if (_expValue != null) { ObjectId o = OldObjectId ?? ObjectId.ZeroId; if (!AnyObjectId.equals(_expValue, o)) { return RefUpdateResult.LockFailure; } } if (OldObjectId == null) { return store.Store(@lock, RefUpdateResult.New); } RevObject newObj = SafeParse(walk, _newValue); RevObject oldObj = SafeParse(walk, OldObjectId); if (newObj == oldObj) { return store.Store(@lock, RefUpdateResult.NoChange); } RevCommit newCom = (newObj as RevCommit); RevCommit oldCom = (oldObj as RevCommit); if (newCom != null && oldCom != null) { if (walk.isMergedInto(oldCom, newCom)) { return store.Store(@lock, RefUpdateResult.FastForward); } } if (IsForceUpdate) { return store.Store(@lock, RefUpdateResult.Forced); } return RefUpdateResult.Rejected; } }
} private static void LockAndWriteFile(FileInfo file, byte[] content) { string name = file.Name; using (LockFile lck = new LockFile(file)) { if (!lck.Lock()) { throw new ObjectWritingException("Unable to lock " + name); } try { lck.Write(content); } catch (IOException ioe) { throw new ObjectWritingException("Unable to write " + name, ioe); } if (!lck.Commit()) { throw new ObjectWritingException("Unable to write " + name); }