| 1 | package backup |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "runtime/debug" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Compatibiliteit tussen BlueGit en bluegit-backup. |
| 11 | // |
| 12 | // Elke kant heeft een protocolversie (wat hij zelf spreekt) en een |
| 13 | // minimumversie (de oudste versie van de andere kant die hij nog |
| 14 | // ondersteunt). Ze zijn compatibel als elk de minimumversie van de ander |
| 15 | // haalt. Een kant die de koppen niet stuurt, spreekt protocol 1 (zo was het |
| 16 | // tot en met bluegit-backup v0.1.0). |
| 17 | // |
| 18 | // De waarden zijn strings, zodat een test-build ze met |
| 19 | // -ldflags "-X gitlab.com/Timfa/bluegit/internal/backup.protocolVersion=…" |
| 20 | // kan overschrijven. |
| 21 | var ( |
| 22 | protocolVersion = "1" |
| 23 | minBackupProtocol = "1" |
| 24 | ) |
| 25 | |
| 26 | // Koppen met de protocolversie. BlueGit zet de X-BlueGit-koppen op zijn |
| 27 | // verzoeken aan en antwoorden van de backup-API; de backup server zet de |
| 28 | // X-Backup-koppen op zijn verzoeken en antwoorden. |
| 29 | const ( |
| 30 | HeaderBlueGitProtocol = "X-BlueGit-Protocol" |
| 31 | HeaderBlueGitMinProtocol = "X-BlueGit-Min-Protocol" |
| 32 | HeaderBlueGitVersion = "X-BlueGit-Version" |
| 33 | HeaderBackupProtocol = "X-Backup-Protocol" |
| 34 | HeaderBackupMinProtocol = "X-Backup-Min-Protocol" |
| 35 | HeaderBackupVersion = "X-Backup-Version" |
| 36 | ) |
| 37 | |
| 38 | // Uitkomsten van Compat. |
| 39 | const ( |
| 40 | CompatOK = "ok" |
| 41 | CompatBlueGitTooOld = "bluegit_too_old" |
| 42 | CompatBackupTooOld = "backup_too_old" |
| 43 | ) |
| 44 | |
| 45 | func atoiMin1(s string) int { |
| 46 | n, err := strconv.Atoi(strings.TrimSpace(s)) |
| 47 | if err != nil || n < 1 { |
| 48 | return 1 |
| 49 | } |
| 50 | return n |
| 51 | } |
| 52 | |
| 53 | // Protocol is de protocolversie van deze BlueGit. |
| 54 | func Protocol() int { return atoiMin1(protocolVersion) } |
| 55 | |
| 56 | // MinProtocol is de oudste protocolversie van een backup server die deze |
| 57 | // BlueGit ondersteunt. |
| 58 | func MinProtocol() int { |
| 59 | if m := atoiMin1(minBackupProtocol); m <= Protocol() { |
| 60 | return m |
| 61 | } |
| 62 | return Protocol() |
| 63 | } |
| 64 | |
| 65 | // Version is de commit waarvan deze BlueGit gebouwd is (of "dev"). |
| 66 | func Version() string { |
| 67 | if bi, ok := debug.ReadBuildInfo(); ok { |
| 68 | for _, s := range bi.Settings { |
| 69 | if s.Key == "vcs.revision" && len(s.Value) >= 12 { |
| 70 | return s.Value[:12] |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | return "dev" |
| 75 | } |
| 76 | |
| 77 | // Compat vergelijkt BlueGit (protocol, minimum voor de backup server) met |
| 78 | // een backup server (protocol, minimum voor BlueGit). |
| 79 | func Compat(bluegit, bluegitMin, backup, backupMin int) string { |
| 80 | switch { |
| 81 | case backup < bluegitMin: |
| 82 | return CompatBackupTooOld |
| 83 | case bluegit < backupMin: |
| 84 | return CompatBlueGitTooOld |
| 85 | } |
| 86 | return CompatOK |
| 87 | } |
| 88 | |
| 89 | // Peer is wat een backup server over zijn protocol zegt. |
| 90 | type Peer struct { |
| 91 | Protocol int |
| 92 | MinProtocol int |
| 93 | Version string |
| 94 | } |
| 95 | |
| 96 | // PeerFromHeader leest de X-Backup-koppen. Zonder koppen is het protocol 1. |
| 97 | // ok is false als er geen protocolkop is. |
| 98 | func PeerFromHeader(h http.Header) (Peer, bool) { |
| 99 | p := Peer{Protocol: 1, MinProtocol: 1} |
| 100 | raw := h.Get(HeaderBackupProtocol) |
| 101 | if raw == "" { |
| 102 | return p, false |
| 103 | } |
| 104 | p.Protocol = atoiMin1(raw) |
| 105 | if m := h.Get(HeaderBackupMinProtocol); m != "" { |
| 106 | p.MinProtocol = atoiMin1(m) |
| 107 | } |
| 108 | p.Version = CleanVersion(h.Get(HeaderBackupVersion)) |
| 109 | return p, true |
| 110 | } |
| 111 | |
| 112 | // CleanVersion houdt een versie kort en toonbaar. |
| 113 | func CleanVersion(v string) string { |
| 114 | v = strings.TrimSpace(v) |
| 115 | out := make([]rune, 0, len(v)) |
| 116 | for _, r := range v { |
| 117 | if r > ' ' && r < 0x7f && r != '<' && r != '>' && r != '"' { |
| 118 | out = append(out, r) |
| 119 | } |
| 120 | if len(out) == 40 { |
| 121 | break |
| 122 | } |
| 123 | } |
| 124 | return string(out) |
| 125 | } |
| 126 | |
| 127 | // SetOwnHeaders zet de X-BlueGit-koppen. |
| 128 | func SetOwnHeaders(h http.Header) { |
| 129 | h.Set(HeaderBlueGitProtocol, strconv.Itoa(Protocol())) |
| 130 | h.Set(HeaderBlueGitMinProtocol, strconv.Itoa(MinProtocol())) |
| 131 | h.Set(HeaderBlueGitVersion, Version()) |
| 132 | } |
| 133 | |
| 134 | // Compat zegt of deze backup server bij deze BlueGit past. Leeg als de |
| 135 | // backup server zich nog niet gemeld heeft. |
| 136 | func (c Conn) Compat() string { |
| 137 | if !c.Connected() || c.PeerProtocol == 0 { |
| 138 | return "" |
| 139 | } |
| 140 | min := c.PeerMinProtocol |
| 141 | if min == 0 { |
| 142 | min = 1 |
| 143 | } |
| 144 | return Compat(Protocol(), MinProtocol(), c.PeerProtocol, min) |
| 145 | } |
| 146 | |
| 147 | // Incompatible zegt of er iets geüpdatet moet worden. |
| 148 | func (c Conn) Incompatible() bool { |
| 149 | r := c.Compat() |
| 150 | return r == CompatBlueGitTooOld || r == CompatBackupTooOld |
| 151 | } |
| 152 | |
| 153 | // SetPeer legt het protocol van de backup server vast. Alleen bij een |
| 154 | // verandering wordt backup.json herschreven. |
| 155 | func (m *Manager) SetPeer(id string, p Peer) { |
| 156 | c, ok := m.Get(id) |
| 157 | if !ok || (c.PeerProtocol == p.Protocol && c.PeerMinProtocol == p.MinProtocol && c.PeerVersion == p.Version) { |
| 158 | return |
| 159 | } |
| 160 | _, _ = m.update(id, func(c *Conn) error { |
| 161 | c.PeerProtocol, c.PeerMinProtocol, c.PeerVersion = p.Protocol, p.MinProtocol, p.Version |
| 162 | return nil |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | // OverrideProtocol zet protocol en minimum tijdelijk anders (alleen voor |
| 167 | // tests). De teruggegeven functie zet ze terug. |
| 168 | func OverrideProtocol(protocol, min string) (restore func()) { |
| 169 | oldP, oldM := protocolVersion, minBackupProtocol |
| 170 | protocolVersion, minBackupProtocol = protocol, min |
| 171 | return func() { protocolVersion, minBackupProtocol = oldP, oldM } |
| 172 | } |