public void lock(Map pathsToRevisions, String comment, boolean force, ISVNLockHandler handler) throws SVNException { try { openConnection(); Object[] buffer = new Object[] {"lock-many", comment, Boolean.valueOf(force)}; write("(w((s)w(", buffer); buffer = new Object[2]; for (Iterator paths = pathsToRevisions.keySet().iterator(); paths.hasNext(); ) { buffer[0] = paths.next(); buffer[1] = pathsToRevisions.get(buffer[0]); write("(s(n))", buffer); } write(")))", buffer); try { authenticate(); } catch (SVNException e) { if (e.getErrorMessage() != null && e.getErrorMessage().getErrorCode() == SVNErrorCode.RA_SVN_UNKNOWN_CMD) { closeConnection(); closeSession(); openConnection(); lock12(pathsToRevisions, comment, force, handler); return; } closeSession(); throw e; } for (Iterator paths = pathsToRevisions.keySet().iterator(); paths.hasNext(); ) { String path = (String) paths.next(); SVNLock lock = null; SVNErrorMessage error = null; try { read("[L]", buffer, false); lock = (SVNLock) buffer[0]; path = lock.getPath(); } catch (SVNException e) { path = getRepositoryPath(path); error = e.getErrorMessage(); } if (handler != null) { handler.handleLock(path, lock, error); } } read("x", buffer, true); read("[()]", buffer, true); } catch (SVNException e) { closeSession(); handleUnsupportedCommand(e, "Server doesn't support the lock command"); } finally { closeConnection(); } }
public void removeLock(DAVResource resource, String lockToken) throws DAVException { DAVResourceURI resourceURI = resource.getResourceURI(); if (resourceURI.getPath() == null) { return; } if (isKeepLocks()) { return; } // TODO: add here authz check later String token = null; SVNLock lock = null; if (lockToken == null) { try { lock = resource.getLock(); } catch (SVNException svne) { throw DAVException.convertError( svne.getErrorMessage(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to check path for a lock.", null); } if (lock != null) { token = lock.getID(); } } else { token = lockToken; } if (token != null) { try { resource.unlock(token, isBreakLock()); } catch (SVNException svne) { if (svne.getErrorMessage().getErrorCode() == SVNErrorCode.FS_NO_USER) { throw new DAVException( "Anonymous lock removal is not allowed.", HttpServletResponse.SC_UNAUTHORIZED, DAVErrorCode.LOCK_SAVE_LOCK); } throw DAVException.convertError( svne.getErrorMessage(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to remove a lock.", null); } // TODO: add logging here } }
@Test public void testCommitOfLockedFile() throws SVNException { final String fullFilePath = "Project/Prueba/Modify/prueba.txt"; final String filePath = "Prueba/Modify/prueba.txt"; final TestOptions options = TestOptions.getInstance(); final Sandbox sandbox = Sandbox.createWithCleanup(getClass().getSimpleName() + ".testModifyLocked", options); final SVNURL url = sandbox.createSvnRepository(); final CommitBuilder commitBuilder = new CommitBuilder(url); commitBuilder.addFile(fullFilePath); commitBuilder.commit(); // user paths relative to Project directory. SVNRepository repository = SVNRepositoryFactory.create(url.appendPath("Project", false)); repository.setAuthenticationManager(new BasicAuthenticationManager("user", "password")); final Map<String, Long> pathsToRevisions = new HashMap<String, Long>(); pathsToRevisions.put(filePath, 1l); repository.lock(pathsToRevisions, null, false, null); repository.closeSession(); // same user as one who owns the lock. repository.setAuthenticationManager(new BasicAuthenticationManager("user", "password")); final SVNLock lock = repository.getLock(filePath); Assert.assertNotNull(lock); Assert.assertNotNull(lock.getID()); Assert.assertEquals("user", lock.getOwner()); final Map<String, String> locks = new HashMap<String, String>(); try { tryCommit(filePath, repository, locks); Assert.fail(); } catch (SVNException e) { // no lock token. } locks.put(filePath, lock.getID()); SVNCommitInfo info = tryCommit(filePath, repository, locks); Assert.assertNotNull(info); Assert.assertEquals(2, info.getNewRevision()); }
private void unlock12(Map pathToTokens, boolean force, ISVNLockHandler handler) throws SVNException { for (Iterator paths = pathToTokens.keySet().iterator(); paths.hasNext(); ) { String path = (String) paths.next(); String id = (String) pathToTokens.get(path); path = getRepositoryPath(path); if (id == null) { Object[] buffer = new Object[] {"get-lock", path}; write("(w(s))", buffer); authenticate(); read("[((?L))]", buffer, true); SVNLock lock = (SVNLock) buffer[0]; if (lock == null) { lock = new SVNLock(path, "", null, null, null, null); SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_NOT_LOCKED, "No lock on path ''{0}''", path); handler.handleUnlock(path, lock, err); continue; } id = lock.getID(); } Object[] buffer = new Object[] {"unlock", path, id, Boolean.valueOf(force)}; write("(w(s(s)w))", buffer); authenticate(); SVNErrorMessage error = null; try { read("[()]", buffer, true); } catch (SVNException e) { if (e.getErrorMessage() != null && e.getErrorMessage().getErrorCode() == SVNErrorCode.RA_NOT_LOCKED) { error = e.getErrorMessage(); error = SVNErrorMessage.create(error.getErrorCode(), error.getMessageTemplate(), path); } else { throw e; } } if (handler != null) { SVNLock lock = new SVNLock(path, id, null, null, null, null); handler.handleUnlock(path, lock, error); } } }
private static void verifyLock(SVNLock lock, Collection lockTokens, String username) throws SVNException { if (username == null || "".equals(username)) { SVNErrorMessage err = SVNErrorMessage.create( SVNErrorCode.FS_NO_USER, "Cannot verify lock on path ''{0}''; no username available", lock.getPath()); SVNErrorManager.error(err, SVNLogType.FSFS); } else if (username.compareTo(lock.getOwner()) != 0) { SVNErrorMessage err = SVNErrorMessage.create( SVNErrorCode.FS_LOCK_OWNER_MISMATCH, "User {0} does not own lock on path ''{1}'' (currently locked by {2})", new Object[] {username, lock.getPath(), lock.getOwner()}); SVNErrorManager.error(err, SVNLogType.FSFS); } if (lockTokens.contains(lock.getID())) { return; } SVNErrorMessage err = SVNErrorMessage.create( SVNErrorCode.FS_BAD_LOCK_TOKEN, "Cannot verify lock on path ''{0}''; no matching lock-token available", lock.getPath()); SVNErrorManager.error(err, SVNLogType.FSFS); }