Skip to content

Commit

Permalink
resolves issue arquillian#15 by changing zip4j to commons-compress. A…
Browse files Browse the repository at this point in the history
…lso adds a TarTool.
  • Loading branch information
lordofthejars committed Oct 12, 2014
1 parent 66da963 commit 3301929
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 41 deletions.
12 changes: 8 additions & 4 deletions spacelift-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<relativePath>../pom.xml</relativePath>
</parent>

<properties>
<version.commons.compress>1.8.1</version.commons.compress>
</properties>

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -44,11 +48,11 @@
<type>pom</type>
</dependency>

<!-- Unzip tool -->
<!-- Uncompress tool -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${version.commons.compress}</version>
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.arquillian.spacelift.tool.basic;

/**
* File permission
*
* @author <a href="asotobu@gmail.com">Alex Soto</a>
*
*/
public class FilePermission {

private boolean ownerCanRead;
private boolean ownerCanWrite;
private boolean ownerCanExecute;

private boolean groupCanRead;
private boolean groupCanWrite;
private boolean groupCanExecute;

private boolean othersCanRead;
private boolean othersCanWrite;
private boolean othersCanExecute;

public boolean isOwnerCanRead() {
return ownerCanRead;
}

public void setOwnerCanRead(boolean ownerCanRead) {
this.ownerCanRead = ownerCanRead;
}

public boolean isOwnerCanWrite() {
return ownerCanWrite;
}

public void setOwnerCanWrite(boolean ownerCanWrite) {
this.ownerCanWrite = ownerCanWrite;
}

public boolean isOwnerCanExecute() {
return ownerCanExecute;
}

public void setOwnerCanExecute(boolean ownerCanExecute) {
this.ownerCanExecute = ownerCanExecute;
}

public boolean isGroupCanRead() {
return groupCanRead;
}

public void setGroupCanRead(boolean groupCanRead) {
this.groupCanRead = groupCanRead;
}

public boolean isGroupCanWrite() {
return groupCanWrite;
}

public void setGroupCanWrite(boolean groupCanWrite) {
this.groupCanWrite = groupCanWrite;
}

public boolean isGroupCanExecute() {
return groupCanExecute;
}

public void setGroupCanExecute(boolean groupCanExecute) {
this.groupCanExecute = groupCanExecute;
}

public boolean isOthersCanRead() {
return othersCanRead;
}

public void setOthersCanRead(boolean othersCanRead) {
this.othersCanRead = othersCanRead;
}

public boolean isOthersCanWrite() {
return othersCanWrite;
}

public void setOthersCanWrite(boolean othersCanWrite) {
this.othersCanWrite = othersCanWrite;
}

public boolean isOthersCanExecute() {
return othersCanExecute;
}

public void setOthersCanExecute(boolean othersCanExecute) {
this.othersCanExecute = othersCanExecute;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.arquillian.spacelift.tool.basic;

import java.io.File;

/**
* File util
*
* @author <a href="asotobu@gmail.com">Alex Soto</a>
*
*/
public class PermissionsUtil {

private static final int OWNER_READ_FLAG = 0400;
private static final int OWNER_WRITE_FLAG = 0200;
private static final int OWNER_EXECUTE_FLAG = 0100;

private static final int GROUP_READ_FLAG = 0040;
private static final int GROUP_WRITE_FLAG = 0020;
private static final int GROUP_EXECUTE_FLAG = 0010;

private static final int OTHERS_READ_FLAG = 0004;
private static final int OTHERS_WRITE_FLAG = 0002;
private static final int OTHERS_EXECUTE_FLAG = 0001;

public static FilePermission toFilePermission(int mode) {

int maskedMode = mode & 0777;
FilePermission filePermission = new FilePermission();

if ((maskedMode & OWNER_READ_FLAG) > 0)
filePermission.setOwnerCanRead(true);
if ((maskedMode & OWNER_WRITE_FLAG) > 0)
filePermission.setOwnerCanWrite(true);
if ((maskedMode & OWNER_EXECUTE_FLAG) > 0)
filePermission.setOwnerCanExecute(true);

if ((maskedMode & GROUP_READ_FLAG) > 0)
filePermission.setGroupCanRead(true);
if ((maskedMode & GROUP_WRITE_FLAG) > 0)
filePermission.setGroupCanWrite(true);
if ((maskedMode & GROUP_EXECUTE_FLAG) > 0)
filePermission.setGroupCanExecute(true);

if ((maskedMode & OTHERS_READ_FLAG) > 0)
filePermission.setOthersCanRead(true);
if ((maskedMode & OTHERS_WRITE_FLAG) > 0)
filePermission.setOthersCanWrite(true);
if ((maskedMode & OTHERS_EXECUTE_FLAG) > 0)
filePermission.setOthersCanExecute(true);

return filePermission;

}

public static void applyPermission(File file, FilePermission permissions) {

setExecutable(file, permissions.isOwnerCanExecute(),
!permissions.isGroupCanExecute() && !permissions.isOthersCanExecute());
setWritable(file, permissions.isOwnerCanWrite(),
!permissions.isGroupCanWrite() && !permissions.isOthersCanWrite());
setReadable(file, permissions.isOwnerCanRead(), !permissions.isGroupCanRead() && !permissions.isOthersCanRead());

}

private static void setReadable(File file, boolean ownerCanRead, boolean group) {
file.setReadable(ownerCanRead, group);
}

private static void setWritable(File file, boolean ownerCanWrite, boolean group) {
file.setWritable(ownerCanWrite, group);
}

private static void setExecutable(File file, boolean ownerCanExecute, boolean group) {
file.setExecutable(ownerCanExecute, group);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.arquillian.spacelift.tool.basic;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.arquillian.spacelift.tool.Tool;

/**
* Uncompress Tool
*
* @author <a href="asotobu@gmail.com">Alex Soto</a>
*
*/
public abstract class UncompressTool extends Tool<File, File> {

private static final int BUFFER = 2048;

private File dest;

protected abstract ArchiveInputStream compressedInputStream(InputStream compressedFile);

protected abstract int permissionsMode(ArchiveEntry archiveEntry);

/**
*
* @param pathToDestination
* destination where to unzip a file
* @return
*/
public UncompressTool toDir(String pathToDestination) {
return toDir(new File(pathToDestination));
}

/**
*
* @param destination
* destination where to unzip a file
* @return
*/
public UncompressTool toDir(File destination) {
this.dest = destination;
return this;
}

@Override
protected File process(File input) throws Exception {
ArchiveEntry entry = null;

/** Read the tar entries using the getNextEntry method **/

ArchiveInputStream compressedInputStream = compressedInputStream(new FileInputStream(input));

while ((entry = compressedInputStream.getNextEntry()) != null) {

File file = new File(this.dest, entry.getName());

if (entry.isDirectory()) {
File f = file;
f.mkdirs();
} else {

if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}

int count;
byte data[] = new byte[BUFFER];

FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((count = compressedInputStream.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.close();

int permissionsMode = permissionsMode(entry);
if (permissionsMode != 0) {
FilePermission filePermission = PermissionsUtil.toFilePermission(permissionsMode);
PermissionsUtil.applyPermission(file, filePermission);
}
}
}

compressedInputStream.close();

return this.dest;
}

}

0 comments on commit 3301929

Please sign in to comment.