From f7df8aec924665d47cad4e1f722e7bee4bd27659 Mon Sep 17 00:00:00 2001 From: Esta Nagy Date: Wed, 21 Feb 2024 23:12:24 +0100 Subject: [PATCH] Save the OS name in the backup (#152) - Defines OS enum - Implements OS name parsing logic - Adds OS name to manifests - Shows OS name during restore Resolves #151 {minor} Signed-off-by: Esta Nagy --- .../core/common/ManifestManagerImpl.java | 4 ++ .../worker/ManifestToSummaryConverter.java | 1 + .../core/model/BackupIncrementManifest.java | 5 ++ .../filebarj/core/model/RestoreManifest.java | 4 ++ .../core/model/enums/OperatingSystem.java | 57 +++++++++++++++++++ .../nagyesta/filebarj/core/util/OsUtil.java | 25 +++++++- .../ManifestToSummaryConverterTest.java | 2 + .../model/BackupIncrementManifestTest.java | 2 + 8 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/enums/OperatingSystem.java diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestManagerImpl.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestManagerImpl.java index beff903..c56f514 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestManagerImpl.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/common/ManifestManagerImpl.java @@ -6,7 +6,9 @@ import com.github.nagyesta.filebarj.core.model.*; import com.github.nagyesta.filebarj.core.model.enums.BackupType; import com.github.nagyesta.filebarj.core.model.enums.Change; +import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem; import com.github.nagyesta.filebarj.core.util.LogUtil; +import com.github.nagyesta.filebarj.core.util.OsUtil; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; @@ -49,6 +51,7 @@ public BackupIncrementManifest generateManifest( .archivedEntries(new ConcurrentHashMap<>()) .startTimeUtcEpochSeconds(startTimeEpochSecond) .fileNamePrefix(fileNamePrefix) + .operatingSystem(OsUtil.getRawOsName()) .build(); Optional.ofNullable(jobConfiguration.getEncryptionKey()) .ifPresent(manifest::generateDataEncryptionKeys); @@ -171,6 +174,7 @@ public RestoreManifest mergeForRestore( .versions(versions) .configuration(lastIncrementManifest.getConfiguration()) .fileNamePrefixes(fileNamePrefixes) + .operatingSystem(OperatingSystem.forOsName(lastIncrementManifest.getOperatingSystem())) .files(files) .archivedEntries(archivedEntries) .encryptionKeys(keys) diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverter.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverter.java index 7048f3c..aa49f38 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverter.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverter.java @@ -27,6 +27,7 @@ public String convertToSummaryString(final @NonNull BackupIncrementManifest mani + "\tStarted at : " + Instant.ofEpochSecond(epochSeconds) + " (Epoch seconds: " + epochSeconds + ")\n" + "\tContains " + manifest.getFiles().size() + " files (" + totalSize + " MiB)\n" + "\tVersions : " + manifest.getVersions() + "\n" + + "\tOS name : " + Optional.ofNullable(manifest.getOperatingSystem()).orElse("unknown") + "\n" + "\tEncrypted : " + Optional.ofNullable(manifest.getConfiguration().getEncryptionKey()).isPresent() + "\n" + "\tHash alg. : " + manifest.getConfiguration().getHashAlgorithm().name() + "\n" + "\tCompression: " + manifest.getConfiguration().getCompression().name(); diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifest.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifest.java index 52e8f3e..a8b06af 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifest.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifest.java @@ -48,6 +48,11 @@ public class BackupIncrementManifest extends EncryptionKeyStore { @NonNull @JsonProperty("backup_type") private BackupType backupType; + /** + * The OS of the backup. + */ + @JsonProperty("operating_system") + private String operatingSystem; /** * The snapshot of the backup configuration at the time of backup. */ diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/RestoreManifest.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/RestoreManifest.java index 0e27213..7d41a53 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/RestoreManifest.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/RestoreManifest.java @@ -3,6 +3,7 @@ import com.github.nagyesta.filebarj.core.config.BackupJobConfiguration; import com.github.nagyesta.filebarj.core.model.enums.Change; import com.github.nagyesta.filebarj.core.model.enums.FileType; +import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; @@ -41,6 +42,8 @@ public class RestoreManifest extends EncryptionKeyStore { */ @NonNull private final BackupJobConfiguration configuration; + @NonNull + private final OperatingSystem operatingSystem; /** * The map of matching files identified during backup keyed by filename and Id. */ @@ -70,6 +73,7 @@ protected RestoreManifest(final RestoreManifestBuilder builder) { this.lastStartTimeUtcEpochSeconds = builder.lastStartTimeUtcEpochSeconds; this.fileNamePrefixes = Collections.unmodifiableSortedMap(builder.fileNamePrefixes); this.configuration = builder.configuration; + this.operatingSystem = builder.operatingSystem; this.files = builder.files.entrySet().stream() .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, entry -> Collections.unmodifiableMap(entry.getValue()))); this.archivedEntries = builder.archivedEntries.entrySet().stream() diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/enums/OperatingSystem.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/enums/OperatingSystem.java new file mode 100644 index 0000000..a3ffaa0 --- /dev/null +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/model/enums/OperatingSystem.java @@ -0,0 +1,57 @@ +package com.github.nagyesta.filebarj.core.model.enums; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.Optional; + +/** + * Represents the current OS. + */ +public enum OperatingSystem { + /** + * Windows. + */ + WINDOWS("win"), + /** + * Mac OS. + */ + MAC("mac"), + /** + * Linux. + */ + LINUX("linux"), + /** + * Catch-all for any OS not matching the above. + */ + UNKNOWN(null); + + private final String nameToken; + + /** + * Creates a new instance. + * + * @param nameToken the OS name token we need to search for in the OS name. + */ + OperatingSystem(final String nameToken) { + this.nameToken = nameToken; + } + + /** + * Parses the OS name. + * + * @param name the OS name + * @return the matching constant or UNKNOWN + */ + @NotNull + public static OperatingSystem forOsName(@Nullable final String name) { + final var osName = Optional.ofNullable(name) + .map(String::toLowerCase) + .orElse("unknown"); + return Arrays.stream(values()) + .filter(os -> os.nameToken != null && osName.contains(os.nameToken)) + .findFirst() + .orElse(UNKNOWN); + } +} diff --git a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/util/OsUtil.java b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/util/OsUtil.java index 81e9569..b8fbef2 100644 --- a/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/util/OsUtil.java +++ b/file-barj-core/src/main/java/com/github/nagyesta/filebarj/core/util/OsUtil.java @@ -1,6 +1,8 @@ package com.github.nagyesta.filebarj.core.util; +import com.github.nagyesta.filebarj.core.model.enums.OperatingSystem; import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; /** * Utility class for OS specific operations. @@ -15,6 +17,27 @@ public final class OsUtil { * @return true if the OS is Windows */ public static boolean isWindows() { - return System.getProperty("os.name").toLowerCase().startsWith("win"); + return getOs() == OperatingSystem.WINDOWS; + } + + /** + * Parses the current OS name. + * + * @return the current OS + */ + @NotNull + public static OperatingSystem getOs() { + return OperatingSystem.forOsName(getRawOsName()); + } + + + /** + * Returns the raw OS name. + * + * @return the raw OS name + */ + @NotNull + public static String getRawOsName() { + return System.getProperty("os.name"); } } diff --git a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverterTest.java b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverterTest.java index c167f2d..af4a491 100644 --- a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverterTest.java +++ b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/inspect/worker/ManifestToSummaryConverterTest.java @@ -82,6 +82,7 @@ void testConvertToSummaryStringShouldReturnExpectedValueWhenCalledWithValidManif .versions(new TreeSet<>(Set.of(0))) .fileNamePrefix("prefix") .startTimeUtcEpochSeconds(1L) + .operatingSystem(null) .build(); //when final var actual = underTest.convertToSummaryString(manifest); @@ -92,6 +93,7 @@ void testConvertToSummaryStringShouldReturnExpectedValueWhenCalledWithValidManif \tStarted at : 1970-01-01T00:00:01Z (Epoch seconds: 1) \tContains 1 files (2 MiB) \tVersions : [0] + \tOS name : unknown \tEncrypted : true \tHash alg. : SHA256 \tCompression: GZIP""", actual); diff --git a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifestTest.java b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifestTest.java index a246e02..351ef67 100644 --- a/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifestTest.java +++ b/file-barj-core/src/test/java/com/github/nagyesta/filebarj/core/model/BackupIncrementManifestTest.java @@ -10,6 +10,7 @@ import com.github.nagyesta.filebarj.core.model.enums.BackupType; import com.github.nagyesta.filebarj.core.model.enums.Change; import com.github.nagyesta.filebarj.core.model.enums.FileType; +import com.github.nagyesta.filebarj.core.util.OsUtil; import com.github.nagyesta.filebarj.io.stream.crypto.EncryptionUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -82,6 +83,7 @@ void testDeserializeShouldRecreatePreviousStateWhenCalledOnSerializedStateOfFull .appVersion(new AppVersion(0, 0, 1)) .versions(new TreeSet<>(Set.of(0))) .backupType(BackupType.FULL) + .operatingSystem(OsUtil.getRawOsName()) .encryptionKeys(Map.of(0, Map.of(0, dek))) .startTimeUtcEpochSeconds(Instant.now().getEpochSecond()) .fileNamePrefix("backup-")