// newStorageMetaDoc creates a storageMetaDoc using the corresponding
// values from the backup Metadata.
167710
func newStorageMetaDoc(meta *Metadata) storageMetaDoc {
	var doc storageMetaDoc

	// Ignore metadata.ID. It will be set by storage later.

	doc.Checksum = meta.Checksum()
	doc.ChecksumFormat = meta.ChecksumFormat()
	doc.Size = meta.Size()
	if meta.Stored() != nil {
		stored := meta.Stored()
		doc.Stored = metadocTimeToUnix(*stored)
	}

	doc.Started = metadocTimeToUnix(meta.Started)
	if meta.Finished != nil {
		doc.Finished = metadocTimeToUnix(*meta.Finished)
	}
	doc.Notes = meta.Notes

	doc.Model = meta.Origin.Model
	doc.Machine = meta.Origin.Machine
	doc.Hostname = meta.Origin.Hostname
	doc.Version = meta.Origin.Version
	doc.Series = meta.Origin.Series

	return doc
}
1299947
func NewStorageBackend(st *State) (*storageBackend, error) {
	// TODO(wallyworld) - we should be passing in a Model not a State
	// (but need to move stuff off State first)
	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	registry, err := st.storageProviderRegistry()
	if err != nil {
		return nil, errors.Annotate(err, "getting storage provider registry")
	}

	return &storageBackend{
		mb:              st,
		registry:        registry,
		settings:        NewStateSettings(st),
		modelType:       m.Type(),
		config:          m.ModelConfig,
		application:     st.Application,
		allApplications: st.AllApplications,
		unit:            st.Unit,
		machine:         st.Machine,
	}, nil
}
1046646
func (s *storage) SaveMetadata(metadata []Metadata) error {
	return s.saveMetadata(metadata, true)
}
362846
func IsMediaMetadataFromJSON(data []byte) (IsMediaMetadata, error) {
	var t mediaMetadataUnion
	if err := json.Unmarshal(data, &t); err != nil {
		return nil, err
	}
	switch t.Tag {
	case "photo":
		return t.Photo, nil

	case "video":
		return t.Video, nil

	}
	return nil, nil
}
355649
func NewStorageAPIV3(context facade.Context) (*StorageAPIv3, error) {
	storageAPI, err := NewStorageAPIV4(context)
	if err != nil {
		return nil, err
	}
	return &StorageAPIv3{
		StorageAPIv4: *storageAPI,
	}, nil
}
1213102
func docAsMetadata(doc *storageMetaDoc) *Metadata {
	meta := NewMetadata()
	meta.Started = metadocUnixToTime(doc.Started)
	meta.Notes = doc.Notes

	meta.Origin.Model = doc.Model
	meta.Origin.Machine = doc.Machine
	meta.Origin.Hostname = doc.Hostname
	meta.Origin.Version = doc.Version
	meta.Origin.Series = doc.Series

	meta.SetID(doc.ID)

	if doc.Finished != 0 {
		finished := metadocUnixToTime(doc.Finished)
		meta.Finished = &finished
	}

	if doc.isFileInfoComplete() {
		// Set the file-related fields.

		// The doc should have already been validated when stored.
		meta.FileMetadata.Raw.Size = doc.Size
		meta.FileMetadata.Raw.Checksum = doc.Checksum
		meta.FileMetadata.Raw.ChecksumFormat = doc.ChecksumFormat
	}

	if doc.Stored != 0 {
		stored := metadocUnixToTime(doc.Stored)
		meta.SetStored(&stored)
	}

	return meta
}
683019
func (dfs *DistributedFilesystem) writeBackupMetadata(data BackupInfo, w *tar.Writer) error {
	var (
		jsonData []byte
		err      error
	)

	backupLogger := plog.WithFields(log.Fields{
		"backupversion": data.BackupVersion,
		"timestamp":     data.Timestamp,
	})

	backupLogger.Debug("Writing backup metadata")
	if jsonData, err = json.Marshal(data); err != nil {
		return err
	}
	header := &tar.Header{Name: BackupMetadataFile, Size: int64(len(jsonData))}
	if err := w.WriteHeader(header); err != nil {
		backupLogger.WithError(err).Debug("Could not create metadata header for backup")
		return err
	}
	if _, err := w.Write(jsonData); err != nil {
		backupLogger.WithError(err).Debug("Could not write backup metadata")
		return err
	}
	return nil
}
732171
func newSnapshotter(db *DB, name string, sn snapshots.Snapshotter) *snapshotter {
	return &snapshotter{
		Snapshotter: sn,
		name:        name,
		db:          db,
	}
}
195651
func NewStorageStatsReporter(destination string, interval time.Duration) (*StorageStatsReporter, error) {
	hostID, err := utils.HostID()
	if err != nil {
		plog.WithError(err).Debug("Could not determine host ID")
		return nil, err
	}

	sr := StorageStatsReporter{
		statsReporter: statsReporter{
			destination:  destination,
			closeChannel: make(chan struct{}),
		},
		hostID: hostID,
	}

	sr.storageRegistry = metrics.NewRegistry()
	sr.statsReporter.updateStatsFunc = sr.updateStats
	sr.statsReporter.gatherStatsFunc = sr.gatherStats
	go sr.report(interval)
	return &sr, nil
}
978936
func newStorageAPI(endpoint Endpoint) (storage StorageAPI, err error) {
	if endpoint.IsLocal {
		return newPosix(endpoint.Path)
	}

	return newStorageRESTClient(endpoint)
}
1144283
func NewSQLStorage(dialect string, args ...interface{}) (*SQLStorage, error) {
	gormDB, err := gorm.Open(dialect, args...)
	if err != nil {
		return nil, err
	}
	return &SQLStorage{
		DB: *gormDB,
	}, nil
}
688799
func newStorage(root string, maxFileSize int64, indexConf jsonconfig.Obj) (s *storage, err error) {
	fi, err := os.Stat(root)
	if os.IsNotExist(err) {
		return nil, fmt.Errorf("storage root %q doesn't exist", root)
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
	}
	if !fi.IsDir() {
		return nil, fmt.Errorf("storage root %q exists but is not a directory", root)
	}
	index, err := newIndex(root, indexConf)
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			index.Close()
		}
	}()
	if maxFileSize <= 0 {
		maxFileSize = defaultMaxFileSize
	}
	// Be consistent with trailing slashes.  Makes expvar stats for total
	// reads/writes consistent across diskpacked targets, regardless of what
	// people put in their low level config.
	root = strings.TrimRight(root, `\/`)
	s = &storage{
		root:         root,
		index:        index,
		maxFileSize:  maxFileSize,
		Generationer: local.NewGenerationer(root),
	}
	if err := s.openAllPacks(); err != nil {
		s.Close()
		return nil, err
	}
	s.mu.Lock()
	defer s.mu.Unlock()
	if _, _, err := s.StorageGeneration(); err != nil {
		return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
	}
	return s, nil
}
504596
func NewStorageWithOptions(fs billy.Filesystem, cache cache.Object, ops Options) *Storage {
	dirOps := dotgit.Options{
		ExclusiveAccess: ops.ExclusiveAccess,
	}
	dir := dotgit.NewWithOptions(fs, dirOps)

	return &Storage{
		fs:  fs,
		dir: dir,

		ObjectStorage:    *NewObjectStorageWithOptions(dir, cache, ops),
		ReferenceStorage: ReferenceStorage{dir: dir},
		IndexStorage:     IndexStorage{dir: dir},
		ShallowStorage:   ShallowStorage{dir: dir},
		ConfigStorage:    ConfigStorage{dir: dir},
		ModuleStorage:    ModuleStorage{dir: dir},
	}
}
78149
func NewStorage(modelUUID string, session *mgo.Session) Storage {
	return stateStorage{modelUUID, session}
}
888919
func (s *backupsDocStorage) Doc(id string) (filestorage.Document, error) {
	dbWrap := s.dbWrap.Copy()
	defer dbWrap.Close()

	doc, err := getStorageMetadata(dbWrap, id)
	if err != nil {
		return nil, errors.Trace(err)
	}

	metadata := docAsMetadata(doc)
	return metadata, nil
}
1055724
func NewSetDOMStorageItemArgs(storageID StorageID, key string, value string) *SetDOMStorageItemArgs {
	args := new(SetDOMStorageItemArgs)
	args.StorageID = storageID
	args.Key = key
	args.Value = value
	return args
}
1067630
func NewStorage(file string) (sorted.KeyValue, error) {
	return newKeyValueFromJSONConfig(jsonconfig.Obj{"file": file})
}