Navigation Menu

Skip to content

Commit

Permalink
[JENKINS-17681] Set up a test which passes on Unix but fails on Windo…
Browse files Browse the repository at this point in the history
…ws when symlinks are enabled on the platform but disabled for the current user.
  • Loading branch information
jglick committed May 1, 2013
1 parent d391bbe commit 00d5e38
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
43 changes: 26 additions & 17 deletions core/src/main/java/jenkins/model/PeepholePermalink.java
Expand Up @@ -81,12 +81,7 @@ protected File getPermalinkFile(Job<?,?> job) {
Run<?,?> b=null;

try {
String target = Util.resolveSymlink(f);
if (target==null && f.exists()) {
// if this file isn't a symlink, it must be a regular file
target = FileUtils.readFileToString(f,"UTF-8").trim();
}

String target = readSymlink(f);
if (target!=null) {
int n = Integer.parseInt(Util.getFileName(target));
if (n==RESOLVES_TO_NONE) return null;
Expand Down Expand Up @@ -135,19 +130,39 @@ protected void updateCache(@Nonnull Job<?,?> job, @Nullable Run<?,?> b) {
final int n = b==null ? RESOLVES_TO_NONE : b.getNumber();

File cache = getPermalinkFile(job);
File tmp = new File(cache.getPath()+".tmp");
cache.getParentFile().mkdirs();

try {
StringWriter w = new StringWriter();
StreamTaskListener listener = new StreamTaskListener(w);
writeSymlink(cache, String.valueOf(n));
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to update "+job+" "+getId()+" permalink for " + b, e);
cache.delete();
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "Failed to update "+job+" "+getId()+" permalink for " + b, e);
cache.delete();
}
}

Util.createSymlink(tmp.getParentFile(),String.valueOf(n),tmp.getName(),listener);
static String readSymlink(File cache) throws IOException, InterruptedException {
String target = Util.resolveSymlink(cache);
if (target==null && cache.exists()) {
// if this file isn't a symlink, it must be a regular file
target = FileUtils.readFileToString(cache,"UTF-8").trim();
}
return target;
}

static void writeSymlink(File cache, String target) throws IOException, InterruptedException {
StringWriter w = new StringWriter();
StreamTaskListener listener = new StreamTaskListener(w);
File tmp = new File(cache.getPath()+".tmp");
try {
Util.createSymlink(tmp.getParentFile(),target,tmp.getName(),listener);
if (Util.resolveSymlink(tmp)==null) {
// symlink not supported. use a regular file
AtomicFileWriter cw = new AtomicFileWriter(cache);
try {
cw.write(String.valueOf(n));
cw.write(target);
cw.commit();
} finally {
cw.abort();
Expand All @@ -156,12 +171,6 @@ protected void updateCache(@Nonnull Job<?,?> job, @Nullable Run<?,?> b) {
cache.delete();
tmp.renameTo(cache);
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to update "+job+" "+getId()+" permalink for " + b, e);
cache.delete();
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "Failed to update "+job+" "+getId()+" permalink for " + b, e);
cache.delete();
} finally {
tmp.delete();
}
Expand Down
45 changes: 45 additions & 0 deletions core/src/test/java/jenkins/model/PeepholePermalinkTest.java
@@ -0,0 +1,45 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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 jenkins.model;

import java.io.File;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Bug;

public class PeepholePermalinkTest {

@Rule public TemporaryFolder tmp = new TemporaryFolder();

@Bug(17681)
@Test public void symlinks() throws Exception {
File link = new File(tmp.getRoot(), "link");
PeepholePermalink.writeSymlink(link, "stuff");
assertEquals("stuff", PeepholePermalink.readSymlink(link));
}

}

0 comments on commit 00d5e38

Please sign in to comment.