github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/gateway/persist.go (about) 1 package gateway 2 3 import ( 4 "path/filepath" 5 6 "github.com/NebulousLabs/Sia/modules" 7 "github.com/NebulousLabs/Sia/persist" 8 ) 9 10 const ( 11 // nodesFile is the name of the file that contains all seen nodes. 12 nodesFile = "nodes.json" 13 14 // logFile is the name of the log file. 15 logFile = modules.GatewayDir + ".log" 16 ) 17 18 // persistMetadata contains the header and version strings that identify the 19 // gateway persist file. 20 var persistMetadata = persist.Metadata{ 21 Header: "Sia Node List", 22 Version: "0.3.3", 23 } 24 25 // persistData returns the data in the Gateway that will be saved to disk. 26 func (g *Gateway) persistData() (nodes []modules.NetAddress) { 27 for node := range g.nodes { 28 nodes = append(nodes, node) 29 } 30 return 31 } 32 33 // load loads the Gateway's persistent data from disk. 34 func (g *Gateway) load() error { 35 var nodes []modules.NetAddress 36 err := persist.LoadFile(persistMetadata, &nodes, filepath.Join(g.persistDir, nodesFile)) 37 if err != nil { 38 return err 39 } 40 for _, node := range nodes { 41 err := g.addNode(node) 42 if err != nil { 43 g.log.Printf("WARN: error loading node '%v' from persist: %v", node, err) 44 } 45 } 46 return nil 47 } 48 49 // save stores the Gateway's persistent data on disk. 50 func (g *Gateway) save() error { 51 return persist.SaveFile(persistMetadata, g.persistData(), filepath.Join(g.persistDir, nodesFile)) 52 } 53 54 // saveSync stores the Gateway's persistent data on disk, and then syncs to 55 // disk to minimize the possibility of data loss. 56 func (g *Gateway) saveSync() error { 57 return persist.SaveFileSync(persistMetadata, g.persistData(), filepath.Join(g.persistDir, nodesFile)) 58 }