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

Kai Reinhard
08.58.2018 55cee4ae822d8e55194c2ac78536b9a5946d8fc1
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 de.micromata.borgbutler.cache;
 
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.json.borg.RepoInfo;
import de.micromata.borgbutler.json.borg.Repository;
import lombok.Getter;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
 
public class RepoInfoCache {
    private static Logger log = LoggerFactory.getLogger(RepoInfoCache.class);
    public static final String CACHE_REPOS_FILENAME = "cache-repos.json";
 
    @JsonIgnore
    private File cacheReposFile;
    @Getter
    @JsonProperty
    private List<RepoInfo> repositories = new ArrayList<>();
 
    public RepoInfo getRepoInfo(String idOrName) {
        if (idOrName == null) {
            return null;
        }
        for (RepoInfo repoInfo : repositories) {
            Repository repository = repoInfo.getRepository();
            if (repository == null) {
                continue;
            }
            if (idOrName.equals(repository.getId()) || idOrName.equals(repository.getName()) || idOrName.equals(repository.getLocation())) {
                return repoInfo;
            }
        }
        return null;
    }
 
    public void upsert(RepoInfo repoInfo) {
        Repository repository = repoInfo.getRepository();
        if (repository == null) {
            log.error("Oups, no repository given in RepoInfo (ignoring it): " + repoInfo);
            return;
        }
        RepoInfo existingRepo = getRepoInfo(repository.getId());
        if (existingRepo == null) {
            repositories.add(repoInfo);
        } else {
            existingRepo.updateFrom(repoInfo);
        }
    }
 
    public void read() {
        try {
            String json = FileUtils.readFileToString(cacheReposFile, Charset.forName("UTF-8"));
            RepoInfoCache readCache = JsonUtils.fromJson(this.getClass(), json);
            if (readCache != null) {
                this.repositories = readCache.repositories;
            } else {
                log.error("Error while parsing repos cache: " + cacheReposFile.getAbsolutePath());
            }
        } catch (IOException ex) {
            log.error("Error while trying to read repos cache file '" + cacheReposFile.getAbsolutePath() + "': "
                    + ex.getMessage(), ex);
        }
    }
 
    public void save() {
        log.info("Saving repo infos to cache file: " + cacheReposFile);
        String json = JsonUtils.toJson(this);
        try {
            FileUtils.write(cacheReposFile, json, Charset.forName("UTF-8"));
        } catch (IOException ex) {
            log.error("Error while trying to write repos cache file '" + cacheReposFile.getAbsolutePath() + "': "
                    + ex.getMessage(), ex);
        }
    }
 
    /**
     * Needed by jackson for deserialization.
     */
    RepoInfoCache() {
    }
 
    RepoInfoCache(File cacheDir) {
        cacheReposFile = new File(cacheDir, CACHE_REPOS_FILENAME);
    }
}