Skip to content

Commit

Permalink
Startup behavior: Map extraction enhancements (#12766)
Browse files Browse the repository at this point in the history
Changes behavior of the system when zipped map files are encountered on startup.

For context, current system will:
- see zip folder in downloaded maps folders
- create a temp folder in the downloaded maps folder
- extracts the zip file to the new temp folder
- creates the target map folder and moves all extracted files to that folder
- deletes the zip file

Changes:
- zip file is now extracted to a temporary folder in the systems temporary folder.
This way if the process fails later on, the temp files will not be seen as potential maps
(which can create duplicates). Additionally, any such files will now be automatically
cleaned up, the system temp folder is usually emptied on system reboot.

- smarter attempt to move zip files to a 'bad-zip' folder. Only attempt to do so if
the 'bad-zip' still exists

- cleanup temporary folder where map files are extracted. The folder was not being deleted
in a successful extraction case.
  • Loading branch information
DanVanAtta committed Jul 24, 2024
1 parent 494ca21 commit 2417e5f
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,33 @@ public void unzipMapFiles() {
// Before 2.6 maps did not include a 'map.yaml' file and were zipped.
.ifPresent(MapDescriptionYaml::generateForMap);
} catch (final ZipReadException zipReadException) {
log.warn(
"Error reading zip: {}{}",
mapZip.toAbsolutePath(),
Files.exists(mapZip) ? ", moving to 'bad-zips' folder" : "",
zipReadException);

// Problem reading the zip, move it to a folder so that the user does
// not repeatedly see an error trying to read this zip.
moveBadZip(mapZip)
.ifPresent(
newLocation ->
log.warn(
"Error extracting map zip: "
+ mapZip.toAbsolutePath()
+ ", zip has been moved to: "
+ newLocation.toAbsolutePath(),
zipReadException));
if (Files.exists(mapZip)) {
moveBadZip(mapZip)
.ifPresent(
newLocation ->
log.warn(
"Error extracting map zip: {}, zip has been moved to: {}",
mapZip.toAbsolutePath(),
newLocation.toAbsolutePath(),
zipReadException));
}
} catch (final FileSystemException e) {
// Thrown if we are are out of disk space or have file system access issues.
// Do not move the zip file to a bad-zip folder as that operation could also
// fail.
log.warn("Error extracting map zip: " + mapZip + ", " + e.getMessage(), e);
log.warn("Error extracting map zip: {}, {}", mapZip, e.getMessage(), e);
} catch (final ZipExtractor.ZipSecurityException e) {
log.error(
"Malicious zip file detected: "
+ mapZip.toAbsolutePath()
+ ", please report this to TripleA and delete the zip file",
"Malicious zip file detected: {}, please report this to TripleA and delete the zip file",
mapZip.toAbsolutePath(),
e);
}
}));
Expand Down Expand Up @@ -130,7 +136,7 @@ private static Optional<Path> unzipMapThrowing(final Path mapZip) throws IOExcep

// extract into a temp folder first
// put the temp folder in the maps folder, so they're on the same partition (to move later)
final Path tempFolder = Files.createTempDirectory(mapsFolder, "triplea-unzip");
final Path tempFolder = Files.createTempDirectory("triplea-unzip");
ZipExtractor.unzipFile(mapZip, tempFolder);
tempFolder.toFile().deleteOnExit();

Expand All @@ -145,8 +151,12 @@ private static Optional<Path> unzipMapThrowing(final Path mapZip) throws IOExcep
// replace extraction target folder contents with the temp folder containing the extracted zip
final boolean folderReplaced =
FileUtils.replaceFolder(tempFolderWithExtractedMap, extractionTarget);

// Either we have moved and renamed the temp folder, or we have moved and renamed the contents
// of the temp folder. Either way, we can go ahead and remove the now possibly empty tempFolder
FileUtils.deleteDirectory(tempFolder);

if (!folderReplaced) {
FileUtils.deleteDirectory(tempFolder);
return Optional.empty();
}

Expand Down

0 comments on commit 2417e5f

Please sign in to comment.