]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.cms/src/org/argeo/slc/deb/DebFile.java
Adapt to changes in Argeo TP
[gpl/argeo-slc.git] / org.argeo.slc.cms / src / org / argeo / slc / deb / DebFile.java
1 package org.argeo.slc.deb;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.UncheckedIOException;
6 import java.net.URL;
7 import java.nio.charset.StandardCharsets;
8 import java.util.function.Consumer;
9
10 import org.apache.commons.compress.archivers.ArchiveEntry;
11 import org.apache.commons.compress.archivers.ArchiveInputStream;
12 import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
13 import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
14 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
15 import org.apache.commons.compress.compressors.CompressorException;
16 import org.apache.commons.compress.compressors.CompressorStreamFactory;
17
18 public class DebFile {
19 private URL url;
20
21 public DebFile(URL url) {
22 this.url = url;
23 }
24
25 public void printDataArchive() {
26 openDataArchive((tarIn) -> {
27 try {
28 ArchiveEntry entry = null;
29 while ((entry = tarIn.getNextEntry()) != null) {
30 System.out.println(entry.getName());
31 }
32 } catch (IOException e) {
33 throw new UncheckedIOException(e);
34 }
35 });
36 }
37
38 public void printControlArchive() {
39 openControlArchive((tarIn) -> {
40 try {
41 ArchiveEntry entry = null;
42 while ((entry = tarIn.getNextEntry()) != null) {
43 System.out.println(entry.getName());
44 }
45 } catch (IOException e) {
46 throw new UncheckedIOException(e);
47 }
48 });
49 }
50
51 public void printControlFile() {
52 openControlArchive((tarIn) -> {
53 try {
54 ArchiveEntry entry = null;
55 while ((entry = tarIn.getNextEntry()) != null) {
56 if ("./control".equals(entry.getName())) {
57 String controlFileStr = new String(tarIn.readAllBytes(), StandardCharsets.UTF_8);
58 System.out.println(controlFileStr);
59 }
60 }
61 } catch (IOException e) {
62 throw new UncheckedIOException(e);
63 }
64 });
65 }
66
67 protected void openDataArchive(Consumer<ArchiveInputStream> process) {
68 try (ArArchiveInputStream arIn = new ArArchiveInputStream(new BufferedInputStream(url.openStream()))) {
69 // order is specified, so we don't verify entry names
70 ArArchiveEntry debianBinaryEntry = arIn.getNextArEntry();
71 assert "debian-binary".equals(debianBinaryEntry.getName());
72 checkVersion(arIn);
73 arIn.getNextArEntry();// control archive
74 ArArchiveEntry dataArchiveEntry = arIn.getNextArEntry();
75 processArArchiveEntry(dataArchiveEntry, arIn, process);
76 } catch (IOException | CompressorException e) {
77 throw new RuntimeException("Cannot open data archive of " + url, e);
78 }
79 }
80
81 protected void openControlArchive(Consumer<ArchiveInputStream> process) {
82 try (ArArchiveInputStream arIn = new ArArchiveInputStream(new BufferedInputStream(url.openStream()))) {
83 // order is specified, so we don't verify entry names
84 ArArchiveEntry debianBinaryEntry = arIn.getNextArEntry();
85 assert "debian-binary".equals(debianBinaryEntry.getName());
86 checkVersion(arIn);
87 ArArchiveEntry controlArchiveEntry = arIn.getNextArEntry();
88 processArArchiveEntry(controlArchiveEntry, arIn, process);
89 } catch (IOException | CompressorException e) {
90 throw new RuntimeException("Cannot open control archive of " + url, e);
91 }
92 }
93
94 protected void processArArchiveEntry(ArchiveEntry archiveEntry, ArArchiveInputStream arIn,
95 Consumer<ArchiveInputStream> process) throws IOException, CompressorException {
96 String dataArchiveName = archiveEntry.getName();
97 String compressionType = null;
98 if (dataArchiveName.endsWith(".xz"))
99 compressionType = CompressorStreamFactory.XZ;
100 else if (dataArchiveName.endsWith(".gz"))
101 compressionType = CompressorStreamFactory.GZIP;
102 else if (dataArchiveName.endsWith(".bzip2"))
103 compressionType = CompressorStreamFactory.BZIP2;
104 else if (dataArchiveName.endsWith(".lzma"))
105 compressionType = CompressorStreamFactory.LZMA;
106
107 if (compressionType == null) {
108 try (TarArchiveInputStream tarIn = new TarArchiveInputStream(arIn)) {
109 process.accept(tarIn);
110 }
111 } else {
112 try (TarArchiveInputStream tarIn = new TarArchiveInputStream(
113 new CompressorStreamFactory().createCompressorInputStream(compressionType, arIn))) {
114 process.accept(tarIn);
115 }
116 }
117 }
118
119 protected void checkVersion(ArArchiveInputStream arIn) throws IOException {
120 String version = new String(arIn.readAllBytes(), StandardCharsets.US_ASCII).trim();
121 if (!"2.0".equals(version))
122 throw new IllegalStateException("Deb " + version + " is not supported");
123 }
124
125 public static void main(String[] args) throws Exception {
126 // URL url = Paths
127 // .get(System.getProperty("user.home")+"/qa/git/argeo-qa-unstable/build/repo/deb/platform/2.3/argeo-cms_2.3.18_all.deb")
128 // .toUri().toURL();
129 URL url = new URL("http://repo.netiket.eu/deb/pool/bullseye/platform/2.3/argeo-cms_2.3.18_all.deb");
130 DebFile debFile = new DebFile(url);
131 debFile.printControlArchive();
132 debFile.printControlFile();
133 debFile.printDataArchive();
134 }
135
136 }