File: filedriver.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (155 lines) | stat: -rw-r--r-- 3,983 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package filedriver

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"maps"
	"os"
	"path/filepath"
	"slices"

	"github.com/containers/storage/pkg/fileutils"
	"github.com/containers/storage/pkg/lockfile"
)

// configMapsDataFile is the file where configMaps data/payload will be stored
var configMapsDataFile = "configmapsdata.json"

// errNoSecretData indicates that there is not data associated with an id
var errNoSecretData = errors.New("no configMap data with ID")

// errNoSecretData indicates that there is configMap data already associated with an id
var errSecretIDExists = errors.New("configMap data with ID already exists")

// Driver is the filedriver object
type Driver struct {
	// configMapsDataFilePath is the path to the configMapsfile
	configMapsDataFilePath string
	// lockfile is the filedriver lockfile
	lockfile *lockfile.LockFile
}

// NewDriver creates a new file driver.
// rootPath is the directory where the configMaps data file resides.
func NewDriver(rootPath string) (*Driver, error) {
	fileDriver := new(Driver)
	fileDriver.configMapsDataFilePath = filepath.Join(rootPath, configMapsDataFile)
	// the lockfile functions require that the rootPath dir is executable
	if err := os.MkdirAll(rootPath, 0o700); err != nil {
		return nil, err
	}

	lock, err := lockfile.GetLockFile(filepath.Join(rootPath, "configMapsdata.lock"))
	if err != nil {
		return nil, err
	}
	fileDriver.lockfile = lock

	return fileDriver, nil
}

// List returns all configMap IDs
func (d *Driver) List() ([]string, error) {
	d.lockfile.Lock()
	defer d.lockfile.Unlock()
	configMapData, err := d.getAllData()
	if err != nil {
		return nil, err
	}
	return slices.Sorted(maps.Keys(configMapData)), nil
}

// Lookup returns the bytes associated with a configMap ID
func (d *Driver) Lookup(id string) ([]byte, error) {
	d.lockfile.Lock()
	defer d.lockfile.Unlock()

	configMapData, err := d.getAllData()
	if err != nil {
		return nil, err
	}
	if data, ok := configMapData[id]; ok {
		return data, nil
	}
	return nil, fmt.Errorf("%s: %w", id, errNoSecretData)
}

// Store stores the bytes associated with an ID. An error is returned if the ID already exists
func (d *Driver) Store(id string, data []byte) error {
	d.lockfile.Lock()
	defer d.lockfile.Unlock()

	configMapData, err := d.getAllData()
	if err != nil {
		return err
	}
	if _, ok := configMapData[id]; ok {
		return fmt.Errorf("%s: %w", id, errSecretIDExists)
	}
	configMapData[id] = data
	marshalled, err := json.MarshalIndent(configMapData, "", "  ")
	if err != nil {
		return err
	}
	err = os.WriteFile(d.configMapsDataFilePath, marshalled, 0o600)
	if err != nil {
		return err
	}
	return nil
}

// Delete deletes the configMap associated with the specified ID.  An error is returned if no matching configMap is found.
func (d *Driver) Delete(id string) error {
	d.lockfile.Lock()
	defer d.lockfile.Unlock()
	configMapData, err := d.getAllData()
	if err != nil {
		return err
	}
	if _, ok := configMapData[id]; ok {
		delete(configMapData, id)
	} else {
		return fmt.Errorf("%s: %w", id, errNoSecretData)
	}
	marshalled, err := json.MarshalIndent(configMapData, "", "  ")
	if err != nil {
		return err
	}
	err = os.WriteFile(d.configMapsDataFilePath, marshalled, 0o600)
	if err != nil {
		return err
	}
	return nil
}

// getAllData reads the data file and returns all data
func (d *Driver) getAllData() (map[string][]byte, error) {
	// check if the db file exists
	err := fileutils.Exists(d.configMapsDataFilePath)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			// the file will be created later on a store()
			return make(map[string][]byte), nil
		}
		return nil, err
	}

	file, err := os.Open(d.configMapsDataFilePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	byteValue, err := io.ReadAll(file)
	if err != nil {
		return nil, err
	}
	configMapData := new(map[string][]byte)
	err = json.Unmarshal(byteValue, configMapData)
	if err != nil {
		return nil, err
	}
	return *configMapData, nil
}