Package org.eclipse.jgit.storage.file

Examples of org.eclipse.jgit.storage.file.LockFile


   * Unlock this file and abort this change.
   * <p>
   * The temporary file (if created) is deleted before returning.
   */
  public void unlock() {
    final LockFile tmp = myLock;
    if (tmp != null) {
      myLock = null;
      tmp.unlock();
    }
  }
View Full Code Here


  static void saveSecure(final FileBasedConfig sec) throws IOException {
    if (modified(sec)) {
      final byte[] out = Constants.encode(sec.toText());
      final File path = sec.getFile();
      final LockFile lf = new LockFile(path, FS.DETECTED);
      if (!lf.lock()) {
        throw new IOException("Cannot lock " + path);
      }
      try {
        chmod(0600, new File(path.getParentFile(), path.getName() + ".lock"));
        lf.write(out);
        if (!lf.commit()) {
          throw new IOException("Cannot commit write to " + path);
        }
      } finally {
        lf.unlock();
      }
    }
  }
View Full Code Here

    } catch (FileNotFoundException notFound) {
      // Fall through and write the file.
    }

    dst.getParentFile().mkdirs();
    LockFile lf = new LockFile(dst, FS.DETECTED);
    if (!lf.lock()) {
      throw new IOException("Cannot lock " + dst);
    }
    try {
      final OutputStream out = lf.getOutputStream();
      try {
        final byte[] tmp = new byte[4096];
        while (0 < buf.remaining()) {
          int n = Math.min(buf.remaining(), tmp.length);
          buf.get(tmp, 0, n);
          out.write(tmp, 0, n);
        }
      } finally {
        out.close();
      }
      if (!lf.commit()) {
        throw new IOException("Cannot commit " + dst);
      }
    } finally {
      lf.unlock();
    }
  }
View Full Code Here

        FileInputStream in = new FileInputStream(myWar);
        try {
          siteWar.getParentFile().mkdirs();

          LockFile lf = new LockFile(siteWar, FS.DETECTED);
          if (!lf.lock()) {
            throw new IOException("Cannot lock " + siteWar);
          }

          try {
            final OutputStream out = lf.getOutputStream();
            try {
              final byte[] tmp = new byte[4096];
              for (;;) {
                int n = in.read(tmp);
                if (n < 0) {
                  break;
                }
                out.write(tmp, 0, n);
              }
            } finally {
              out.close();
            }
            if (!lf.commit()) {
              throw new IOException("Cannot commit " + siteWar);
            }
          } finally {
            lf.unlock();
          }
        } finally {
          in.close();
        }
      }
View Full Code Here

   *             hold the lock.
   */
  public boolean lock() throws IOException {
    if (liveFile == null)
      throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
    final LockFile tmp = new LockFile(liveFile, fs);
    if (tmp.lock()) {
      tmp.setNeedStatInformation(true);
      myLock = tmp;
      return true;
    }
    return false;
  }
View Full Code Here

   * @throws IOException
   *             the output file could not be created. The caller no longer
   *             holds the lock.
   */
  public void write() throws IOException {
    final LockFile tmp = myLock;
    requireLocked(tmp);
    try {
      writeTo(new BufferedOutputStream(tmp.getOutputStream()));
    } catch (IOException err) {
      tmp.unlock();
      throw err;
    } catch (RuntimeException err) {
      tmp.unlock();
      throw err;
    } catch (Error err) {
      tmp.unlock();
      throw err;
    }
  }
View Full Code Here

   *         old data.
   * @throws IllegalStateException
   *             the lock is not held.
   */
  public boolean commit() {
    final LockFile tmp = myLock;
    requireLocked(tmp);
    myLock = null;
    if (!tmp.commit())
      return false;
    lastModified = tmp.getCommitLastModified();
    return true;
  }
View Full Code Here

   * Unlock this file and abort this change.
   * <p>
   * The temporary file (if created) is deleted before returning.
   */
  public void unlock() {
    final LockFile tmp = myLock;
    if (tmp != null) {
      myLock = null;
      tmp.unlock();
    }
  }
View Full Code Here

  public void testUpdateRefLockFailureLocked() throws IOException {
    ObjectId opid = db.resolve("refs/heads/master");
    ObjectId pid = db.resolve("refs/heads/master^");
    RefUpdate updateRef = db.updateRef("refs/heads/master");
    updateRef.setNewObjectId(pid);
    LockFile lockFile1 = new LockFile(new File(db.getDirectory(),
        "refs/heads/master"), db.getFS());
    try {
      assertTrue(lockFile1.lock()); // precondition to test
      Result update = updateRef.update();
      assertEquals(Result.LOCK_FAILURE, update);
      assertEquals(opid, db.resolve("refs/heads/master"));
      LockFile lockFile2 = new LockFile(new File(db.getDirectory(),"refs/heads/master"),
          db.getFS());
      assertFalse(lockFile2.lock()); // was locked, still is
    } finally {
      lockFile1.unlock();
    }
  }
View Full Code Here

    assertTrue("internal check, we have a log", new File(db.getDirectory(),
        "logs/" + fromName).exists());

    // "someone" has branch X locked
    LockFile lockFile = new LockFile(new File(db.getDirectory(), toLock),
        db.getFS());
    try {
      assertTrue(lockFile.lock());

      // Now this is our test
      RefRename renameRef = db.renameRef(fromName, toName);
      Result result = renameRef.rename();
      assertEquals(Result.LOCK_FAILURE, 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");
      assertEquals(oldHeadId, db.resolve(Constants.HEAD));
      assertEquals(oldfromId, db.resolve(fromName));
      assertNull(db.resolve(toName));
      assertEquals(oldFromLog.toString(), db.getReflogReader(fromName)
          .getReverseEntries().toString());
      if (oldHeadId != null)
        assertEquals(oldHeadLog.toString(), db.getReflogReader(
            Constants.HEAD).getReverseEntries().toString());
    } finally {
      lockFile.unlock();
    }
  }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.storage.file.LockFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.