check if httpc has decompressed the release file and rename the file

from .tar.gz to .tar if that happened
pull/1/head
Michael Peter Christen 13 years ago
parent 06951ef751
commit 046f3a7e8d

@ -47,10 +47,9 @@ public class tarTools {
return new GZIPInputStream(new FileInputStream(new File(tarFileName))); return new GZIPInputStream(new FileInputStream(new File(tarFileName)));
} catch (IOException e) { } catch (IOException e) {
// this might happen if the stream is not in gzip format. // this might happen if the stream is not in gzip format.
// there may be a 'gz' extension, but it may stil be a raw tar file // there may be a 'gz' extension, but it may still be a raw tar file
// this can be caused by 'one too much gzip-content header' that was attached // this can be caused by 'one too much gzip-content header' that was attached
// by a release file server // by a release file server, so just try to open is as normal stream
// so just try to open is as normal stream
return new FileInputStream(new File(tarFileName)); return new FileInputStream(new File(tarFileName));
} }
} }

@ -25,13 +25,16 @@
package net.yacy.document.parser; package net.yacy.document.parser;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import net.yacy.cora.document.MultiProtocolURI; import net.yacy.cora.document.MultiProtocolURI;
import net.yacy.cora.document.UTF8;
import net.yacy.document.AbstractParser; import net.yacy.document.AbstractParser;
import net.yacy.document.Document; import net.yacy.document.Document;
import net.yacy.document.Parser; import net.yacy.document.Parser;
@ -45,6 +48,8 @@ import org.apache.tools.tar.TarInputStream;
public class tarParser extends AbstractParser implements Parser { public class tarParser extends AbstractParser implements Parser {
private final static String MAGIC = "ustar"; // A magic for a tar archive, may appear at #101h-#105
public tarParser() { public tarParser() {
super("Tape Archive File Parser"); super("Tape Archive File Parser");
this.SUPPORTED_EXTENSIONS.add("tar"); this.SUPPORTED_EXTENSIONS.add("tar");
@ -98,4 +103,19 @@ public class tarParser extends AbstractParser implements Parser {
} }
return docacc.toArray(new Document[docacc.size()]); return docacc.toArray(new Document[docacc.size()]);
} }
public final static boolean isTar(File f) {
if (!f.exists() || f.length() < 0x105) return false;
try {
RandomAccessFile raf = new RandomAccessFile(f, "r");
raf.seek(0x101);
byte[] b = new byte[5];
raf.read(b);
return MAGIC.equals(UTF8.String(b));
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
}
} }

@ -53,6 +53,7 @@ import net.yacy.cora.protocol.ResponseHeader;
import net.yacy.cora.protocol.http.HTTPClient; import net.yacy.cora.protocol.http.HTTPClient;
import net.yacy.cora.services.federated.yacy.CacheStrategy; import net.yacy.cora.services.federated.yacy.CacheStrategy;
import net.yacy.document.Document; import net.yacy.document.Document;
import net.yacy.document.parser.tarParser;
import net.yacy.kelondro.data.meta.DigestURI; import net.yacy.kelondro.data.meta.DigestURI;
import net.yacy.kelondro.io.CharBuffer; import net.yacy.kelondro.io.CharBuffer;
import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.logging.Log;
@ -310,7 +311,7 @@ public final class yacyRelease extends yacyVersion {
final ResponseHeader header = new ResponseHeader(client.getHttpResponse().getAllHeaders()); final ResponseHeader header = new ResponseHeader(client.getHttpResponse().getAllHeaders());
final boolean unzipped = header.gzip() && (header.mime().toLowerCase().equals("application/x-tar")); // if true, then the httpc has unzipped the file final boolean unzipped = header.gzip() && (header.mime().toLowerCase().equals("application/x-tar")); // if true, then the httpc has unzipped the file
if ((unzipped) && (name.endsWith(".tar.gz"))) { if (unzipped && name.endsWith(".tar.gz")) {
download = new File(storagePath, name.substring(0, name.length() - 3)); download = new File(storagePath, name.substring(0, name.length() - 3));
} else { } else {
download = new File(storagePath, name); download = new File(storagePath, name);
@ -340,6 +341,13 @@ public final class yacyRelease extends yacyVersion {
client.writeTo(new BufferedOutputStream(new FileOutputStream(download))); client.writeTo(new BufferedOutputStream(new FileOutputStream(download)));
} }
if ((!download.exists()) || (download.length() == 0)) throw new IOException("wget of url " + getUrl() + " failed"); if ((!download.exists()) || (download.length() == 0)) throw new IOException("wget of url " + getUrl() + " failed");
// check again if this is actually a tar.gz or tar file since the httpc may have decompressed it
if (download.getName().endsWith("tar.gz") && tarParser.isTar(download)) {
String ts = download.getAbsoluteFile().toString();
File tar = new File(ts.substring(0, ts.length() - 3));
download.renameTo(tar);
download = tar;
}
} catch (final IOException e) { } catch (final IOException e) {
// Saving file failed, abort download // Saving file failed, abort download
Log.logSevere("yacyVersion", "download of " + getName() + " failed: " + e.getMessage()); Log.logSevere("yacyVersion", "download of " + getName() + " failed: " + e.getMessage());

Loading…
Cancel
Save