List of usage examples for org.eclipse.jgit.internal.storage.file LockFile LockFile
public LockFile(File f)
From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsProjectLocks.java
License:Apache License
LfsLock createLock(CurrentUser user, LfsCreateLockInput input) throws LfsException { log.atFine().log("Create lock for %s in project %s", input.path, project); String lockId = toLockId.apply(input.path); LfsLock lock = locks.getIfPresent(lockId); if (lock != null) { throw new LfsLockExistsException(lock); }//www. jav a2 s . c o m lock = new LfsLock(lockId, input.path, LfsDateTime.now(), new LfsLockOwner(user.getUserName().get())); LockFile fileLock = new LockFile(locksPath.resolve(lockId).toFile()); try { if (!fileLock.lock()) { log.atWarning().log("Cannot lock path [%s] in project %s", input.path, project); throw new LfsLockExistsException(lock); } } catch (IOException e) { String error = String.format("Locking path [%s] in project %s failed with error %s", input.path, project, e.getMessage()); log.atWarning().log(error); throw new LfsException(error); } try { try (OutputStreamWriter out = new OutputStreamWriter(fileLock.getOutputStream())) { gson.toJson(lock, out); } catch (IOException e) { String error = String.format("Locking path [%s] in project %s failed during write with error %s", input.path, project, e.getMessage()); log.atWarning().log(error); throw new LfsException(error); } if (!fileLock.commit()) { String error = String.format("Committing lock to path [%s] in project %s failed", input.path, project); log.atWarning().log(error); throw new LfsException(error); } // put lock object to cache while file lock is being hold so that // there is no chance that other process performs lock operation // in the meantime (either cache returns with existing object or // LockFile.lock fails on locking attempt) locks.put(lockId, lock); } finally { fileLock.unlock(); } return lock; }
From source file:com.googlesource.gerrit.plugins.lfs.locks.LfsProjectLocks.java
License:Apache License
void deleteLock(LfsLock lock) throws LfsException { LockFile fileLock = new LockFile(locksPath.resolve(lock.id).toFile()); try {/* www . j ava2 s. c o m*/ if (!fileLock.lock()) { String error = String.format("Deleting lock on path [%s] in project %s is not possible", lock.path, project); log.atWarning().log(error); throw new LfsException(error); } } catch (IOException e) { String error = String.format("Getting lock on path [%s] in project %s failed with error %s", lock.path, project, e.getMessage()); log.atWarning().log(error); throw new LfsException(error); } try { Files.deleteIfExists(locksPath.resolve(lock.id)); locks.invalidate(lock.id); } catch (IOException e) { String error = String.format("Deleting lock on path [%s] in project %s failed with error %s", lock.path, project, e.getMessage()); log.atWarning().log(error); throw new LfsException(error); } finally { fileLock.unlock(); } }
From source file:com.googlesource.gerrit.plugins.secureconfig.SecureConfigStore.java
License:Apache License
private void saveSecure(final FileBasedConfig sec) throws IOException { if (FileUtil.modified(sec)) { final byte[] out = Constants.encode(sec.toText()); final File path = sec.getFile(); final LockFile lf = new LockFile(path); if (!lf.lock()) { throw new IOException("Cannot lock " + path); }/* w w w. jav a 2 s . com*/ try { FileUtil.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(); } } }
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java
License:Open Source License
@Override public void lockItem(String site, String path) { Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX); synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) { try (TreeWalk tw = new TreeWalk(repo)) { RevTree tree = helper.getTreeForLastCommit(repo); tw.addTree(tree); // tree 0 tw.setRecursive(false);/*from w ww. ja va2 s .com*/ tw.setFilter(PathFilter.create(path)); if (!tw.next()) { return; } File repoRoot = repo.getWorkTree(); Paths.get(repoRoot.getPath(), tw.getPathString()); File file = new File(tw.getPathString()); LockFile lock = new LockFile(file); lock.lock(); tw.close(); } catch (IOException e) { logger.error("Error while locking file for site: " + site + " path: " + path, e); } } }
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java
License:Open Source License
@Override public void unLockItem(String site, String path) { Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX); synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : SANDBOX)) { try (TreeWalk tw = new TreeWalk(repo)) { RevTree tree = helper.getTreeForLastCommit(repo); tw.addTree(tree); // tree 0 tw.setRecursive(false);//from www . ja va2 s. c o m tw.setFilter(PathFilter.create(path)); if (!tw.next()) { return; } File repoRoot = repo.getWorkTree(); Paths.get(repoRoot.getPath(), tw.getPathString()); File file = new File(tw.getPathString()); LockFile lock = new LockFile(file); lock.unlock(); tw.close(); } catch (IOException e) { logger.error("Error while unlocking file for site: " + site + " path: " + path, e); } } }