use of org.apache.tools.tar.TarEntry in project gradle by gradle.
the class TarTaskOutputPacker method storeDirectoryProperty.
private void storeDirectoryProperty(String propertyPath, File directory, final TarOutputStream outputStream) throws IOException {
if (!directory.isDirectory()) {
throw new IllegalArgumentException(String.format("Expected '%s' to be a directory", directory));
}
final String propertyRoot = propertyPath + "/";
outputStream.putNextEntry(new TarEntry(propertyRoot));
outputStream.closeEntry();
FileVisitor visitor = new FileVisitor() {
@Override
public void visitDir(FileVisitDetails dirDetails) {
try {
storeDirectoryEntry(dirDetails, propertyRoot, outputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public void visitFile(FileVisitDetails fileDetails) {
try {
String path = propertyRoot + fileDetails.getRelativePath().getPathString();
storeFileEntry(fileDetails.getFile(), path, fileDetails.getLastModified(), fileDetails.getSize(), fileDetails.getMode(), outputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
};
directoryWalkerFactory.create().walkDir(directory, RelativePath.EMPTY_ROOT, visitor, Specs.satisfyAll(), new AtomicBoolean(), false);
}
use of org.apache.tools.tar.TarEntry in project gradle by gradle.
the class TarTaskOutputPacker method unpack.
private void unpack(TaskOutputsInternal taskOutputs, TarInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException {
Map<String, TaskOutputFilePropertySpec> propertySpecs = Maps.uniqueIndex(taskOutputs.getFileProperties(), new Function<TaskFilePropertySpec, String>() {
@Override
public String apply(TaskFilePropertySpec propertySpec) {
return propertySpec.getPropertyName();
}
});
boolean originSeen = false;
TarEntry entry;
while ((entry = tarInput.getNextEntry()) != null) {
String name = entry.getName();
if (name.equals(METADATA_PATH)) {
// handle origin metadata
originSeen = true;
readOriginAction.execute(new CloseShieldInputStream(tarInput));
} else {
// handle output property
Matcher matcher = PROPERTY_PATH.matcher(name);
if (!matcher.matches()) {
throw new IllegalStateException("Cached result format error, invalid contents: " + name);
}
String propertyName = matcher.group(2);
CacheableTaskOutputFilePropertySpec propertySpec = (CacheableTaskOutputFilePropertySpec) propertySpecs.get(propertyName);
if (propertySpec == null) {
throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
}
boolean outputMissing = matcher.group(1) != null;
String childPath = matcher.group(3);
unpackPropertyEntry(propertySpec, tarInput, entry, childPath, outputMissing);
}
}
if (!originSeen) {
throw new IllegalStateException("Cached result format error, no origin metadata was found.");
}
}
use of org.apache.tools.tar.TarEntry in project gradle by gradle.
the class TarTaskOutputPacker method storeMissingProperty.
private void storeMissingProperty(String propertyPath, TarOutputStream outputStream) throws IOException {
TarEntry entry = new TarEntry("missing-" + propertyPath);
outputStream.putNextEntry(entry);
outputStream.closeEntry();
}
use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.
the class TarArchiver method visit.
public void visit(File file, String relativePath) throws IOException {
if (Functions.isWindows())
relativePath = relativePath.replace('\\', '/');
if (file.isDirectory())
relativePath += '/';
TarEntry te = new TarEntry(relativePath);
te.setModTime(file.lastModified());
if (!file.isDirectory())
te.setSize(file.length());
tar.putNextEntry(te);
if (!file.isDirectory()) {
FileInputStream in = new FileInputStream(file);
try {
int len;
while ((len = in.read(buf)) >= 0) tar.write(buf, 0, len);
} finally {
in.close();
}
}
tar.closeEntry();
entriesWritten++;
}
use of org.apache.tools.tar.TarEntry in project hudson-2.x by hudson.
the class FilePath method readFromTar.
/**
* Reads from a tar stream and stores obtained files to the base dir.
*/
private static void readFromTar(String name, File baseDir, InputStream in) throws IOException {
TarInputStream t = new TarInputStream(in);
try {
TarEntry te;
while ((te = t.getNextEntry()) != null) {
File f = new File(baseDir, te.getName());
if (te.isDirectory()) {
f.mkdirs();
} else {
File parent = f.getParentFile();
if (parent != null)
parent.mkdirs();
IOUtils.copy(t, f);
f.setLastModified(te.getModTime().getTime());
int mode = te.getMode() & 0777;
if (// be defensive
mode != 0 && !Functions.isWindows())
_chmod(f, mode);
}
}
} catch (IOException e) {
throw new IOException2("Failed to extract " + name, e);
} finally {
t.close();
}
}
Aggregations