提交 6577d788 编写于 作者: K Kohsuke Kawaguchi

[FIXED JENKINS-9397] fixed a bug in the implementation, added tests.

上级 00485b31
......@@ -48,7 +48,6 @@ import hudson.util.IOUtils;
import static hudson.util.jna.GNUCLibrary.LIBC;
import static hudson.Util.fixEmpty;
import static hudson.FilePath.TarCompression.GZIP;
import hudson.os.PosixAPI;
import hudson.org.apache.tools.tar.TarInputStream;
import hudson.util.io.Archiver;
import hudson.util.io.ArchiverFactory;
......@@ -1132,7 +1131,7 @@ public final class FilePath implements Serializable {
if(!isUnix()) return -1;
return act(new FileCallable<Integer>() {
public Integer invoke(File f, VirtualChannel channel) throws IOException {
return PosixAPI.get().stat(f.getPath()).mode();
return IOUtils.mode(f);
}
});
}
......
package hudson.util;
import hudson.FilePath.FileCallable;
import hudson.Functions;
import hudson.os.PosixAPI;
import hudson.remoting.VirtualChannel;
import java.io.*;
import java.util.regex.Pattern;
......@@ -110,5 +115,14 @@ public class IOUtils extends org.apache.commons.io.IOUtils {
return path.startsWith("/") || DRIVE_PATTERN.matcher(path).matches();
}
/**
* Gets the mode of a file/directory, if appropriate. Returns -1 if not on Unix.
*/
public static int mode(File f) {
if(Functions.isWindows()) return -1;
return PosixAPI.get().stat(f.getPath()).mode();
}
private static final byte[] SKIP_BUFFER = new byte[8192];
}
......@@ -24,12 +24,9 @@
package hudson.util.io;
import hudson.Functions;
import hudson.os.PosixAPI;
import hudson.util.FileVisitor;
import java.io.Closeable;
import java.io.File;
/**
* {@link FileVisitor} that creates archive files.
......@@ -46,19 +43,4 @@ public abstract class Archiver extends FileVisitor implements Closeable {
public int countEntries() {
return entriesWritten;
}
/**
* Gets the mode of a file/directory, if appropriate. Returns -1 if not on Unix.
*/
public int getPathMode(String p) {
// If we're on Windows...
if (Functions.isWindows()) {
return -1;
}
// If we're on Unix...
else {
return PosixAPI.get().stat(p).mode();
}
}
}
......@@ -28,6 +28,7 @@ import hudson.Functions;
import hudson.org.apache.tools.tar.TarOutputStream;
import hudson.util.FileVisitor;
import hudson.util.IOException2;
import hudson.util.IOUtils;
import org.apache.tools.tar.TarEntry;
import java.io.BufferedOutputStream;
......@@ -64,7 +65,8 @@ final class TarArchiver extends Archiver {
@Override
public void visitSymlink(File link, String target, String relativePath) throws IOException {
TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
e.setMode(getPathMode(link.getPath()));
int mode = IOUtils.mode(link);
if (mode!=-1) e.setMode(mode);
try {
StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e);
......@@ -90,7 +92,8 @@ final class TarArchiver extends Archiver {
if(file.isDirectory())
relativePath+='/';
TarEntry te = new TarEntry(relativePath);
te.setMode(getPathMode(file.getPath()));
int mode = IOUtils.mode(file);
if (mode!=-1) te.setMode(mode);
te.setModTime(file.lastModified());
if(!file.isDirectory())
te.setSize(file.length());
......
......@@ -25,6 +25,7 @@
package hudson.util.io;
import hudson.util.FileVisitor;
import hudson.util.IOUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
......@@ -48,16 +49,18 @@ final class ZipArchiver extends Archiver {
}
public void visit(File f, String relativePath) throws IOException {
int mode = IOUtils.mode(f);
if(f.isDirectory()) {
ZipEntry dirZipEntry = new ZipEntry(relativePath+'/');
dirZipEntry.setUnixMode(getPathMode(f.getPath()));
// Setting this bit explicitly is needed by some unzipping applications (see HUDSON-3294).
// Setting this bit explicitly is needed by some unzipping applications (see JENKINS-3294).
dirZipEntry.setExternalAttributes(BITMASK_IS_DIRECTORY);
if (mode!=-1) dirZipEntry.setUnixMode(mode);
zip.putNextEntry(dirZipEntry);
zip.closeEntry();
} else {
ZipEntry fileZipEntry = new ZipEntry(relativePath);
fileZipEntry.setUnixMode(getPathMode(f.getPath()));
if (mode!=-1) fileZipEntry.setUnixMode(mode);
zip.putNextEntry(fileZipEntry);
FileInputStream in = new FileInputStream(f);
int len;
......
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* 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.util.io;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher.LocalLauncher;
import hudson.util.StreamTaskListener;
import junit.framework.TestCase;
import org.jvnet.hudson.test.Bug;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author Kohsuke Kawaguchi
*/
public class TarArchiverTest extends TestCase {
/**
* Makes sure that permissions are properly stored in the tar file.
*/
@Bug(9397)
public void testPermission() throws Exception {
if (Functions.isWindows()) return; // can't test on Windows
File tar = new File("test.tar");
File zip = new File("test.zip");
FilePath dir = new FilePath(File.createTempFile("test","dir"));
try {
dir.delete();
dir.child("subdir").mkdirs();
FilePath f = dir.child("a.txt");
f.touch(0);
f.chmod(0755);
f = dir.child("subdir/b.txt");
f.touch(0);
f.chmod(0644);
int dirMode = dir.child("subdir").mode();
dir.tar(new FileOutputStream(tar),"**/*");
dir.zip(new FileOutputStream(zip));
FilePath e = dir.child("extract");
e.mkdirs();
// extract via the tar command
assertEquals(0, new LocalLauncher(new StreamTaskListener(System.out)).launch().cmds("tar", "xvf", tar.getAbsolutePath()).pwd(e).join());
assertEquals(0100755,e.child("a.txt").mode());
assertEquals(dirMode,e.child("subdir").mode());
assertEquals(0100644,e.child("subdir/b.txt").mode());
// extract via the zip command
e.deleteContents();
assertEquals(0, new LocalLauncher(new StreamTaskListener(System.out)).launch().cmds("unzip", zip.getAbsolutePath()).pwd(e).join());
e = e.listDirectories().get(0);
assertEquals(0100755, e.child("a.txt").mode());
assertEquals(dirMode,e.child("subdir").mode());
assertEquals(0100644,e.child("subdir/b.txt").mode());
} finally {
tar.delete();
zip.delete();
dir.delete();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册