-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add support for .tgz files in GeoIpDownloader #70725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e14e0b0
dc7e08f
12af219
0a5fe48
54d90e7
0b20d22
152458c
52967ca
f326eb1
45697de
ffed31d
79c6787
8c14d7c
2da15f0
a282186
35ffd36
854efe7
63d9e19
1c1317e
ee5aa36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import org.elasticsearch.search.SearchHit; | ||
| import org.elasticsearch.watcher.ResourceWatcherService; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
|
|
@@ -261,6 +262,25 @@ void retrieveAndUpdateDatabase(String databaseName, GeoIpTaskState.Metadata meta | |
| decompress(databaseTmpGzFile, databaseTmpFile); | ||
|
|
||
| Path databaseFile = geoipTmpDirectory.resolve(databaseName); | ||
| // tarball contains <database_name>.mmdb, LICENSE.txt, COPYRIGHTS.txt and optional README.txt files. | ||
| // we store mmdb file as is and prepend database name to all other entries to avoid conflicts | ||
| try (TarInputStream is = new TarInputStream(new BufferedInputStream(Files.newInputStream(databaseTmpFile)))) { | ||
| TarInputStream.TarEntry entry; | ||
| while ((entry = is.getNextEntry()) != null) { | ||
| //there might be ./ entry in tar, we should skip it | ||
| if (entry.isNotFile()) { | ||
| continue; | ||
| } | ||
| // flatten structure, remove any directories present from the path (should be ./ only) | ||
| String name = entry.getName().substring(entry.getName().lastIndexOf('/') + 1); | ||
| if (name.startsWith(databaseName)) { | ||
| Files.copy(is, databaseTmpFile, StandardCopyOption.REPLACE_EXISTING); | ||
| } else { | ||
| Files.copy(is, geoipTmpDirectory.resolve(databaseName + "_" + name), StandardCopyOption.REPLACE_EXISTING); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that was exactly my thinking, I think we can leave them behind as they are small and doesn't interfere with geoip processor itself. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| LOGGER.debug("moving database from [{}] to [{}]", databaseTmpFile, databaseFile); | ||
| Files.move(databaseTmpFile, databaseFile, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); | ||
| updateDatabase(databaseName, recordedMd5, databaseFile); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.ingest.geoip; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.FilterInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * {@link InputStream} with very basic support for tar format, just enough to parse archives provided by GeoIP database service from Infra. | ||
| * This class is not suitable for general purpose tar processing! | ||
| */ | ||
| class TarInputStream extends FilterInputStream { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| private TarEntry currentEntry; | ||
| private long remaining; | ||
| private long reminder; | ||
| private final byte[] buf = new byte[512]; | ||
|
|
||
| TarInputStream(InputStream in) { | ||
| super(in); | ||
| } | ||
|
|
||
| public TarEntry getNextEntry() throws IOException { | ||
| if (currentEntry != null) { | ||
| //go to the end of the current entry | ||
| skipN(remaining); | ||
| if (reminder != 0) { | ||
| skipN(512 - reminder); | ||
| } | ||
| } | ||
| int read = in.readNBytes(buf, 0, 512); | ||
| if (read == 0) { | ||
| return null; | ||
| } | ||
| if (read != 512) { | ||
| throw new EOFException(); | ||
| } | ||
| if (Arrays.compare(buf, new byte[512]) == 0) { | ||
| return null; | ||
| } | ||
|
|
||
| String name = getString(0, 100); | ||
|
|
||
| boolean notFile = (buf[156] != 0 && buf[156] != '0') || name.endsWith("/"); | ||
|
|
||
| if(notFile){ | ||
| remaining = 0; | ||
| reminder = 0; | ||
| } else { | ||
| String sizeString = getString(124, 12); | ||
| remaining = sizeString.isEmpty() ? 0 : Long.parseLong(sizeString, 8); | ||
| reminder = remaining % 512; | ||
| } | ||
|
|
||
| currentEntry = new TarEntry(name, notFile); | ||
| return currentEntry; | ||
| } | ||
|
|
||
| @Override | ||
| public int read() throws IOException { | ||
| if (remaining == 0) { | ||
| return -1; | ||
| } | ||
| remaining--; | ||
| return in.read(); | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b, int off, int len) throws IOException { | ||
| if (remaining <= 0) { | ||
| return -1; | ||
| } | ||
| int read = in.read(b, off, remaining > Integer.MAX_VALUE ? len : (int) Math.min(len, remaining)); | ||
| remaining -= read; | ||
| return read; | ||
| } | ||
|
|
||
| private String getString(int offset, int maxLen) { | ||
| return new String(buf, offset, maxLen, StandardCharsets.UTF_8).trim(); | ||
| } | ||
|
|
||
| private void skipN(long n) throws IOException { | ||
| while (n > 0) { | ||
| long skip = in.skip(n); | ||
| if (skip < n) { | ||
| int read = in.read(); | ||
| if (read == -1) { | ||
| throw new EOFException(); | ||
| } | ||
| n--; | ||
| } | ||
| n -= skip; | ||
| } | ||
| } | ||
|
|
||
| static class TarEntry { | ||
| private final String name; | ||
| private final boolean notFile; | ||
|
|
||
| TarEntry(String name, boolean notFile) { | ||
| this.name = name; | ||
| this.notFile = notFile; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public boolean isNotFile() { | ||
| return notFile; | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.