Skip to content

Commit

Permalink
Merge pull request #65 from synopsys-arc-oss/externalsfile-conflicts
Browse files Browse the repository at this point in the history
[FIXED JENKINS-15098] - Access conflicts to svnexternals.txt
  • Loading branch information
kutzi committed May 1, 2014
2 parents cd7b24e + fb93368 commit 23168bd
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/main/java/hudson/scm/SubversionSCM.java
Expand Up @@ -65,7 +65,6 @@
import hudson.Functions;
import hudson.Launcher;
import hudson.Util;
import hudson.XmlFile;
import hudson.init.InitMilestone;
import hudson.model.AbstractDescribableImpl;
import hudson.model.BuildListener;
Expand Down Expand Up @@ -117,7 +116,6 @@
import hudson.util.Scrambler;
import hudson.util.Secret;
import hudson.util.TimeUnit2;
import hudson.util.XStream2;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
Expand Down Expand Up @@ -205,7 +203,6 @@
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import com.thoughtworks.xstream.XStream;
import com.trilead.ssh2.DebugLogger;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.crypto.Base64;
Expand Down Expand Up @@ -558,7 +555,7 @@ private List<External> getExternals(AbstractProject context) throws IOException
}

if (projectExternals == null) {
projectExternals = parseExternalsFile(context);
projectExternals = SvnExternalsFileManager.parseExternalsFile(context);

synchronized (projectExternalsCache) {
if (!projectExternalsCache.containsKey(context)) {
Expand Down Expand Up @@ -828,30 +825,7 @@ private boolean calcChangeLog(AbstractBuild<?,?> build, File changelogFile, Buil
return revisions;
}

/**
* Parses the file that stores the locations in the workspace where modules loaded by svn:external
* is placed.
*
* <p>
* Note that the format of the file has changed in 1.180 from simple text file to XML.
*
* @return
* immutable list. Can be empty but never null.
*/
/*package*/ @SuppressWarnings("unchecked")
static List<External> parseExternalsFile(AbstractProject project) throws IOException {
File file = getExternalsFile(project);
if(file.exists()) {
try {
return (List<External>)new XmlFile(External.XSTREAM,file).read();
} catch (IOException e) {
// in < 1.180 this file was a text file, so it may fail to parse as XML,
// in which case let's just fall back
}
}

return Collections.emptyList();
}


/**
* Polling can happen on the master and does not require a workspace.
Expand Down Expand Up @@ -891,7 +865,7 @@ public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspa
}

// write out the externals info
new XmlFile(External.XSTREAM,getExternalsFile(build.getProject())).write(externals);
SvnExternalsFileManager.writeExternalsFile(build.getProject(), externals);
Map<AbstractProject, List<External>> projectExternalsCache = getProjectExternalsCache();
synchronized (projectExternalsCache) {
projectExternalsCache.put(build.getProject(), externals);
Expand Down Expand Up @@ -1246,11 +1220,6 @@ public boolean isRevisionFixed() {
}

private static final long serialVersionUID = 1L;

private static final XStream XSTREAM = new XStream2();
static {
XSTREAM.alias("external",External.class);
}
}


Expand Down Expand Up @@ -1346,12 +1315,7 @@ public static File getRevisionFile(AbstractBuild build) {
return new File(build.getRootDir(),"revision.txt");
}

/**
* Gets the file that stores the externals.
*/
private static File getExternalsFile(AbstractProject project) {
return new File(project.getRootDir(),"svnexternals.txt");
}


@Override
public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
Expand Down
128 changes: 128 additions & 0 deletions src/main/java/hudson/scm/SvnExternalsFileManager.java
@@ -0,0 +1,128 @@
/*
* The MIT License
*
* Copyright (c) 2004-2013, Sun Microsystems, Inc., Kohsuke Kawaguchi, Fulvio Cavarretta,
* Jean-Baptiste Quenot, Luca Domenico Milanesio, Renaud Bruyeron, Stephen Connolly,
* Tom Huybrechts, Yahoo! Inc., Manufacture Francaise des Pneumatiques Michelin,
* Romain Seguy, OHTAKE Tomohiro (original method implementations)
* Copyright (c) 2013, Synopsys Inc., Oleg Nenashev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.scm;

import com.thoughtworks.xstream.XStream;
import hudson.XmlFile;
import hudson.model.AbstractProject;
import hudson.util.XStream2;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.Nonnull;

/**
* Implements local file storage of externals information.
* Most of functionality has been copied from {@link SubversionSCM}.
* The class also prevents conflicts between read/write operations using
* {@link SVN_EXTERNALS_FILE}.
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
* @since TODO
*/
//TODO: This class should also handle MultipleSCMs (JENKINS-20450)
class SvnExternalsFileManager {
private static final String SVN_EXTERNALS_FILE = "svnexternals.txt";
private static final XStream XSTREAM = new XStream2();
private static Map<AbstractProject, Object> projectExternalsCache;
static {
XSTREAM.alias("external", SubversionSCM.External.class);
}

/**
* Provides a lock item for the project.
* @param project Project to be used
* @return A lock object (will be created on-demand)
*/
@Nonnull
private static synchronized Object getFileLockItem(AbstractProject project) {
if (projectExternalsCache == null) {
projectExternalsCache = new WeakHashMap<AbstractProject, Object>();
}

Object item = projectExternalsCache.get(project);
if (item == null) {
item = new Object();
projectExternalsCache.put(project, item);
}
return item;
}

/**
* Gets the file that stores the externals.
*/
@Nonnull
private static File getExternalsFile(AbstractProject project) {
return new File(project.getRootDir(), SVN_EXTERNALS_FILE);
}

/**
* Parses the file that stores the locations in the workspace where modules
* loaded by svn:external is placed.
*
* <p>
* Note that the format of the file has changed in 1.180 from simple text
* file to XML.
*
* @return immutable list. Can be empty but never null.
*/
@Nonnull
@SuppressWarnings("unchecked")
public static List<SubversionSCM.External> parseExternalsFile(AbstractProject project) throws IOException {
File file = getExternalsFile(project);
Object lock = getFileLockItem(project);

synchronized(lock) {
if (file.exists()) {
try {
return (List<SubversionSCM.External>) new XmlFile(XSTREAM, file).read();
} catch (IOException e) {
// in < 1.180 this file was a text file, so it may fail to parse as XML,
// in which case let's just fall back
}
}
return Collections.emptyList();
}
}

/**
* Writes a list of externals to the file.
* @param project Project, which uses provided externals.
* @param externals List of externals
* @throws IOException File write error
*/
public static void writeExternalsFile(AbstractProject project, List<SubversionSCM.External> externals) throws IOException {
Object lock = getFileLockItem(project);

synchronized (lock) {
new XmlFile(XSTREAM, getExternalsFile(project)).write(externals);
}
}
}

0 comments on commit 23168bd

Please sign in to comment.