提交 00d5e387 编写于 作者: J Jesse Glick

[JENKINS-17681] Set up a test which passes on Unix but fails on Windows when...

[JENKINS-17681] Set up a test which passes on Unix but fails on Windows when symlinks are enabled on the platform but disabled for the current user.
上级 d391bbee
......@@ -81,12 +81,7 @@ public abstract class PeepholePermalink extends Permalink implements Predicate<R
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;
......@@ -135,19 +130,39 @@ public abstract class PeepholePermalink extends Permalink implements Predicate<R
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();
......@@ -156,12 +171,6 @@ public abstract class PeepholePermalink extends Permalink implements Predicate<R
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();
}
......
/*
* 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));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册