github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/storagemanager/persist.go (about)

     1  package storagemanager
     2  
     3  import (
     4  	"crypto/rand"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/NebulousLabs/Sia/crypto"
     9  	"github.com/NebulousLabs/Sia/persist"
    10  
    11  	"github.com/NebulousLabs/bolt"
    12  )
    13  
    14  // persistence is the data from the storage manager that gets saved to disk.
    15  type persistence struct {
    16  	SectorSalt     crypto.Hash
    17  	StorageFolders []*storageFolder
    18  }
    19  
    20  // persistData returns the data in the StorageManager that will be saved to
    21  // disk.
    22  func (sm *StorageManager) persistData() persistence {
    23  	return persistence{
    24  		SectorSalt:     sm.sectorSalt,
    25  		StorageFolders: sm.storageFolders,
    26  	}
    27  }
    28  
    29  // establishDefaults configures the default settings for the storage manager,
    30  // overwriting any existing settings.
    31  func (sm *StorageManager) establishDefaults() error {
    32  	_, err := rand.Read(sm.sectorSalt[:])
    33  	if err != nil {
    34  		return err
    35  	}
    36  	// If the sector salt is lost, the storage manager is not going to be able
    37  	// to figure out where sectors are stored on disk.
    38  	return sm.saveSync()
    39  }
    40  
    41  // initDB will check that the database has been initialized and if not, will
    42  // initialize the database.
    43  func (sm *StorageManager) initDB() error {
    44  	return sm.db.Update(func(tx *bolt.Tx) error {
    45  		// The storage obligation bucket does not exist, which means the
    46  		// database needs to be initialized. Create the database buckets.
    47  		buckets := [][]byte{
    48  			bucketSectorUsage,
    49  		}
    50  		for _, bucket := range buckets {
    51  			_, err := tx.CreateBucketIfNotExists(bucket)
    52  			if err != nil {
    53  				return err
    54  			}
    55  		}
    56  		return nil
    57  	})
    58  }
    59  
    60  // load extracts the saved data from disk and applies it to the storage
    61  // manager.
    62  func (sm *StorageManager) load() error {
    63  	p := new(persistence)
    64  	err := sm.dependencies.loadFile(persistMetadata, p, filepath.Join(sm.persistDir, settingsFile))
    65  	if os.IsNotExist(err) {
    66  		// There is no host.json file, set up sane defaults.
    67  		return sm.establishDefaults()
    68  	} else if err != nil {
    69  		return err
    70  	}
    71  
    72  	sm.sectorSalt = p.SectorSalt
    73  	sm.storageFolders = p.StorageFolders
    74  	return nil
    75  }
    76  
    77  // save stores all of the persistent data of the storage manager to disk.
    78  func (sm *StorageManager) save() error {
    79  	return persist.SaveFile(persistMetadata, sm.persistData(), filepath.Join(sm.persistDir, settingsFile))
    80  }
    81  
    82  // save stores all of the persistent data of the storage manager to disk.
    83  func (sm *StorageManager) saveSync() error {
    84  	return persist.SaveFileSync(persistMetadata, sm.persistData(), filepath.Join(sm.persistDir, settingsFile))
    85  }