blob: c5d11158c0702da2fcba6a532bb1888e683fdd00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package com.keuin.kbackupfabric.backup.incremental.serializer;
import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2;
import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionConverter;
import com.keuin.kbackupfabric.backup.name.BackupFileNameEncoder;
import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder;
import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
public class IncBackupInfoSerializer {
/**
* Load incremental backup index file into object, no matter what version it is.
*
* @param file a valid incremental backup file. (with a valid file name)
* @return the object. Not null.
* @throws IOException when failed due to an I/O error.
*/
public static SavedIncrementalBackup fromFile(File file) throws IOException {
Objects.requireNonNull(file);
try (FileInputStream fileInputStream = new FileInputStream(file)) {
try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
Object o = objectInputStream.readObject();
if (o instanceof SavedIncrementalBackup) {
return (SavedIncrementalBackup) o;
} else if (o instanceof ObjectCollection) {
// backward compatibility with old-style (v0) incremental backup
BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(file.getName());
if (info == null)
throw new IOException("Invalid backup file name.");
return new SavedIncBackupV0(ObjectCollectionConverter.convert((ObjectCollection) o), info);
} else if (o instanceof ObjectCollection2) {
// compatible with 1.4.6 implementation
BackupFileNameEncoder.BackupBasicInformation info = IncrementalBackupFileNameEncoder.INSTANCE.decode(file.getName());
if (info == null)
throw new IOException("Invalid backup file name.");
return new SavedIncBackupV0((ObjectCollection2) o, info);
} else {
throw new RuntimeException("Unrecognized backup file format: unknown class " + o.getClass().getCanonicalName());
}
} catch (ClassNotFoundException e) {
// this should not happen
e.printStackTrace();
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Failed to deserialize file " + file.getName());
throw e;
}
}
/**
* Save incremental backup index and metadata into file.
*
* @param file the file.
* @param backup the backup.
* @throws IOException when failed due to an I/O error.
*/
public static void toFile(File file, SavedIncrementalBackup backup) throws IOException {
Objects.requireNonNull(file);
Objects.requireNonNull(backup);
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(backup);
}
}
}
public static Iterable<SavedIncrementalBackup> fromDirectory(File directory) throws IOException {
if (!directory.isDirectory()) {
throw new IllegalArgumentException("Given directory is invalid.");
}
List<Path> pathList = new ArrayList<>();
try (Stream<Path> walk = Files.walk(directory.toPath(), 1)) {
walk.filter(p -> {
File f = p.toFile();
return f.isFile() && f.getName().endsWith(".kbi");
}).forEach(pathList::add);
}
List<SavedIncrementalBackup> objectList = new ArrayList<>();
for (Path path : pathList) {
SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile());
objectList.add(info);
}
return Collections.unmodifiableCollection(objectList);
}
}
|