func (cm *ContractManager) savedSettings() savedSettings {
	ss := savedSettings{
		SectorSalt: cm.sectorSalt,
	}
	for _, sf := range cm.storageFolders {
		for _, sectorIndex := range sf.availableSectors {
			sf.clearUsage(sectorIndex)
		}
		ss.StorageFolders = append(ss.StorageFolders, sf.savedStorageFolder())
		for _, sectorIndex := range sf.availableSectors {
			sf.setUsage(sectorIndex)
		}
	}
	return ss
}
savedSettings returns the settings of the contract manager in an
easily-serializable form.
package contractmanager
import (
	"encoding/binary"
	"os"
	"path/filepath"
	"sync/atomic"
	"github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/persist"
	"github.com/NebulousLabs/fastrand"
)
type (
	savedStorageFolder struct {
		Index uint16
		Path  string
		Usage []uint64
	}
	savedSettings struct {
		SectorSalt     crypto.Hash
		StorageFolders []savedStorageFolder
	}
)
func (sf *storageFolder) savedStorageFolder() savedStorageFolder {
	ssf := savedStorageFolder{
		Index: sf.index,
		Path:  sf.path,
		Usage: make([]uint64, len(sf.usage)),
	}
	copy(ssf.Usage, sf.usage)
	return ssf
}
func (cm *ContractManager) initSettings() error {
	fastrand.Read(cm.sectorSalt[:])
	ss := cm.savedSettings()
	err := persist.SaveJSON(settingsMetadata, &ss, filepath.Join(cm.persistDir, settingsFile))
	if err != nil {
		cm.log.Println("ERROR: unable to initialize settings file for contract manager:", err)
		return build.ExtendErr("error saving contract manager after initialization", err)
	}
	return nil
}
func (cm *ContractManager) loadSettings() error {
	var ss savedSettings
	err := cm.dependencies.LoadFile(settingsMetadata, &ss, filepath.Join(cm.persistDir, settingsFile))
	if os.IsNotExist(err) {
		return cm.initSettings()
	} else if err != nil {
		cm.log.Println("ERROR: unable to load the contract manager settings file:", err)
		return build.ExtendErr("error loading the contract manager settings file", err)
	}
	cm.sectorSalt = ss.SectorSalt
	for i := range ss.StorageFolders {
		sf := new(storageFolder)
		sf.index = ss.StorageFolders[i].Index
		sf.path = ss.StorageFolders[i].Path
		sf.usage = ss.StorageFolders[i].Usage
		sf.metadataFile, err = cm.dependencies.OpenFile(filepath.Join(ss.StorageFolders[i].Path, metadataFile), os.O_RDWR, 0700)
		if err != nil {
			atomic.StoreUint64(&sf.atomicUnavailable, 1)
			cm.log.Printf("ERROR: unable to open the %v sector metadata file: %v\n", sf.path, err)
		}
		sf.sectorFile, err = cm.dependencies.OpenFile(filepath.Join(ss.StorageFolders[i].Path, sectorFile), os.O_RDWR, 0700)
		if err != nil {
			atomic.StoreUint64(&sf.atomicUnavailable, 1)
			cm.log.Printf("ERROR: unable to open the %v sector file: %v\n", sf.path, err)
			if sf.metadataFile != nil {
				sf.metadataFile.Close()
			}
		}
		sf.availableSectors = make(map[sectorID]uint32)
		cm.storageFolders[sf.index] = sf
	}
	return nil
}
func (cm *ContractManager) loadSectorLocations(sf *storageFolder) {
	sectorLookupBytes, err := readFullMetadata(sf.metadataFile, len(sf.usage)*storageFolderGranularity)
	if err != nil {
		atomic.AddUint64(&sf.atomicFailedReads, 1)
		atomic.StoreUint64(&sf.atomicUnavailable, 1)
		err = build.ComposeErrors(err, sf.metadataFile.Close())
		err = build.ComposeErrors(err, sf.sectorFile.Close())
		cm.log.Printf("ERROR: unable to read sector metadata for folder %v: %v\n", sf.path, err)
		return
	}
	atomic.AddUint64(&sf.atomicSuccessfulReads, 1)
	sf.sectors = 0  
	for _, sectorIndex := range usageSectors(sf.usage) {
		readHead := sectorMetadataDiskSize * sectorIndex
		var id sectorID
		copy(id[:], sectorLookupBytes[readHead:readHead+12])
		count := binary.LittleEndian.Uint16(sectorLookupBytes[readHead+12 : readHead+14])
		sl := sectorLocation{
			index:         sectorIndex,
			storageFolder: sf.index,
			count:         count,
		}
		cm.sectorLocations[id] = sl
		sf.sectors++
	}
	atomic.StoreUint64(&sf.atomicUnavailable, 0)
}