updates.go 2.9 KB
Newer Older
1
package irmaclient
Sietse Ringers's avatar
Sietse Ringers committed
2
3

import (
4
	"time"
Sietse Ringers's avatar
Sietse Ringers committed
5

6
7
	"github.com/privacybydesign/irmago"
	"github.com/privacybydesign/irmago/internal/fs"
Sietse Ringers's avatar
Sietse Ringers committed
8
9
)

10
// This file contains the update mechanism for Client
Sietse Ringers's avatar
Sietse Ringers committed
11
12
// as well as updates themselves.

13
type update struct {
14
	When    irma.Timestamp
15
16
17
	Number  int
	Success bool
	Error   *string
18
19
}

20
var clientUpdates = []func(client *Client) error{
21
	// 0: Convert old cardemu.xml Android storage to our own storage format
22
	nil, // No longer necessary as the Android app was deprecated long ago
23

24
	// 1: Adding scheme manager index, signature and public key
25
26
	// Check the signatures of all scheme managers, if any is not ok,
	// copy the entire irma_configuration folder from assets
27
	nil, // made irrelevant by irma_configuration-autocopying
28

29
	// 2: Rename config -> preferences
30
31
32
33
34
	func(client *Client) (err error) {
		exists, err := fs.PathExists(client.storage.path("config"))
		if !exists || err != nil {
			return
		}
35
36
37
38
		oldStruct := &struct {
			SendCrashReports bool
		}{}
		// Load old file, convert to new struct, and save
39
		err = client.storage.load(oldStruct, "config")
40
41
42
43
44
45
46
47
		if err != nil {
			return err
		}
		client.Preferences = Preferences{
			EnableCrashReporting: oldStruct.SendCrashReports,
		}
		return client.storage.StorePreferences(client.Preferences)
	},
48

49
	// 3: Copy new irma_configuration out of assets
50
	nil, // made irrelevant by irma_configuration-autocopying
51

52
	// 4: For each keyshare server, include in its struct the identifier of its scheme manager
53
	func(client *Client) (err error) {
54
55
56
57
58
		keyshareServers, err := client.storage.LoadKeyshareServers()
		if err != nil {
			return err
		}
		for smi, kss := range keyshareServers {
59
60
			kss.SchemeManagerIdentifier = smi
		}
61
		return client.storage.StoreKeyshareServers(keyshareServers)
62
	},
63

64
	// 5: Remove the test scheme manager which was erroneously included in a production build
65
	nil, // No longer necessary, also broke many unit tests
Tomas's avatar
Tomas committed
66

67
	// 6: Remove earlier log items of wrong format
Tomas's avatar
Tomas committed
68
	func(client *Client) (err error) {
69
		return client.storage.StoreLogs([]*LogEntry{})
Tomas's avatar
Tomas committed
70
	},
71
}
72

73
// update performs any function from clientUpdates that has not
74
75
// already been executed in the past, keeping track of previously executed updates
// in the file at updatesFile.
76
func (client *Client) update() error {
77
78
	// Load and parse file containing info about already performed updates
	var err error
79
	if client.updates, err = client.storage.LoadUpdates(); err != nil {
80
81
82
83
		return err
	}

	// Perform all new updates
84
	for i := len(client.updates); i < len(clientUpdates); i++ {
85
86
87
		err = nil
		if clientUpdates[i] != nil {
			err = clientUpdates[i](client)
88
		}
Sietse Ringers's avatar
Sietse Ringers committed
89
		u := update{
90
			When:    irma.Timestamp(time.Now()),
91
92
93
94
95
			Number:  i,
			Success: err == nil,
		}
		if err != nil {
			str := err.Error()
Sietse Ringers's avatar
Sietse Ringers committed
96
			u.Error = &str
97
		}
98
		client.updates = append(client.updates, u)
99
100
101
		if err != nil {
			break
		}
102
103
	}

104
105
106
107
108
	storeErr := client.storage.StoreUpdates(client.updates)
	if storeErr != nil {
		return storeErr
	}
	return err
109
}