mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
05.03.2019 67dd1243073e766178dd70dd2f45aa5bc77ec529
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
package de.micromata.borgbutler;
 
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/**
 * Extracts the differences between two archives of one repo.
 */
public class DiffTool {
    private static Logger log = LoggerFactory.getLogger(DiffTool.class);
 
    /**
     * @param items      Sorted list of items from the current archive.
     * @param otherItems Sorted list of items of the archive to extract differences.
     * @return A list of differing items (new, removed and modified ones).
     */
    public static List<BorgFilesystemItem> extractDifferences(List<BorgFilesystemItem> items, List<BorgFilesystemItem> otherItems) {
        List<BorgFilesystemItem> currentList = items != null ? items : new ArrayList<>();
        List<BorgFilesystemItem> otherList = otherItems != null ? otherItems : new ArrayList<>();
        List<BorgFilesystemItem> result = new ArrayList<>();
        Iterator<BorgFilesystemItem> currentIt = currentList.iterator();
        Iterator<BorgFilesystemItem> otherIt = otherList.iterator();
        BorgFilesystemItem current = null;
        BorgFilesystemItem other = null;
        while (true) {
            if (current == null && currentIt.hasNext())
                current = currentIt.next();
            if (other == null && otherIt.hasNext())
                other = otherIt.next();
            if (current == null || other == null) {
                break;
            }
            int cmp = current.compareTo(other);
            if (cmp == 0) { // Items represents both the same file system item.
                if (current.equals(other)) {
                    current = other = null; // increment both iterators.
                    continue;
                }
                // Current entry differs:
                current.setDiffStatus(BorgFilesystemItem.DiffStatus.MODIFIED);
                current.setDiffItem(other);
                current.buildDifferencesString();
                result.add(current);
                current = other = null; // increment both iterators.
            } else if (cmp < 0) {
                result.add(current.setDiffStatus(BorgFilesystemItem.DiffStatus.NEW));
                current = currentIt.hasNext() ? currentIt.next() : null;
            } else {
                result.add(other.setDiffStatus(BorgFilesystemItem.DiffStatus.REMOVED));
                other = otherIt.hasNext() ? otherIt.next() : null;
            }
        }
        while (current != null) {
            result.add(current.setDiffStatus(BorgFilesystemItem.DiffStatus.NEW));
            current = currentIt.hasNext() ? currentIt.next() : null;
        }
        while (other != null) {
            result.add(other.setDiffStatus(BorgFilesystemItem.DiffStatus.REMOVED));
            other = otherIt.hasNext() ? otherIt.next() : null;
        }
        return result;
    }
}