Skip to content

Commit

Permalink
[JENKINS-26810] Added VirtualFile.mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 21, 2018
1 parent 3f01a77 commit 8e603a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
42 changes: 33 additions & 9 deletions core/src/main/java/jenkins/util/VirtualFile.java
Expand Up @@ -26,12 +26,14 @@

import hudson.FilePath;
import hudson.model.DirectoryBrowserSupport;
import hudson.os.PosixException;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.RemoteInputStream;
import hudson.remoting.VirtualChannel;
import hudson.util.DirScanner;
import hudson.util.FileVisitor;
import hudson.util.IOUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -171,6 +173,15 @@ public abstract class VirtualFile implements Comparable<VirtualFile>, Serializab
*/
public abstract long lastModified() throws IOException;

/**
* Gets the file’s Unix mode, if meaningful.
* @return for example, 0644 ~ {@code rw-r--r--}; -1 by default, meaning unknown or inapplicable
* @throws IOException if checking the mode failed
*/
public int mode() throws IOException {
return -1;
}

/**
* Checks whether this file can be read.
* @return true normally
Expand Down Expand Up @@ -339,6 +350,12 @@ private static final class FileVF extends VirtualFile {
}
return f.length();
}
@Override public int mode() throws IOException {
if (isIllegalSymlink()) {
return -1;
}
return IOUtils.mode(f);
}
@Override public long lastModified() throws IOException {
if (isIllegalSymlink()) {
return 0;
Expand Down Expand Up @@ -408,7 +425,7 @@ private static final class FilePathVF extends VirtualFile {
try {
return f.isDirectory();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public boolean isFile() throws IOException {
Expand All @@ -419,7 +436,7 @@ private static final class FilePathVF extends VirtualFile {
try {
return f.exists();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public VirtualFile[] list() throws IOException {
Expand All @@ -431,14 +448,14 @@ private static final class FilePathVF extends VirtualFile {
}
return vfs;
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public String[] list(String glob) throws IOException {
try {
return f.act(new Scanner(glob));
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public VirtualFile child(String name) {
Expand All @@ -448,35 +465,42 @@ private static final class FilePathVF extends VirtualFile {
try {
return f.length();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public int mode() throws IOException {
try {
return f.mode();
} catch (InterruptedException | PosixException x) {
throw new IOException(x);
}
}
@Override public long lastModified() throws IOException {
try {
return f.lastModified();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public boolean canRead() throws IOException {
try {
return f.act(new Readable());
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public InputStream open() throws IOException {
try {
return f.read();
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
@Override public <V> V run(Callable<V,IOException> callable) throws IOException {
try {
return f.act(callable);
} catch (InterruptedException x) {
throw (IOException) new IOException(x.toString()).initCause(x);
throw new IOException(x);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/jenkins/util/VirtualFileTest.java
Expand Up @@ -24,12 +24,15 @@

package jenkins.util;

import hudson.FilePath;
import hudson.Functions;
import hudson.Util;
import hudson.model.TaskListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.attribute.PosixFilePermissions;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
Expand Down Expand Up @@ -69,4 +72,20 @@ public class VirtualFileTest {
}
}

@Issue("JENKINS-26810")
@Test public void mode() throws Exception {
File f = tmp.newFile();
VirtualFile vf = VirtualFile.forFile(f);
FilePath fp = new FilePath(f);
VirtualFile vfp = VirtualFile.forFilePath(fp);
assertEquals(modeString(hudson.util.IOUtils.mode(f)), modeString(vf.mode()));
assertEquals(modeString(hudson.util.IOUtils.mode(f)), modeString(vfp.mode()));
fp.chmod(0755);
assertEquals(modeString(hudson.util.IOUtils.mode(f)), modeString(vf.mode()));
assertEquals(modeString(hudson.util.IOUtils.mode(f)), modeString(vfp.mode()));
}
private static String modeString(int mode) throws IOException {
return mode == -1 ? "N/A" : PosixFilePermissions.toString(Util.modeToPermissions(mode));
}

}

0 comments on commit 8e603a5

Please sign in to comment.