Skip to content

Commit e96a4b0

Browse files
committed
Use util.CloneMap where appropriate
Signed-off-by: montag451 <[email protected]>
1 parent 355ee53 commit e96a4b0

File tree

11 files changed

+28
-89
lines changed

11 files changed

+28
-89
lines changed

cmd/incusd/api_1.0.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/lxc/incus/v6/shared/osarch"
3030
"github.com/lxc/incus/v6/shared/revert"
3131
localtls "github.com/lxc/incus/v6/shared/tls"
32+
"github.com/lxc/incus/v6/shared/util"
3233
)
3334

3435
var api10Cmd = APIEndpoint{
@@ -459,10 +460,7 @@ func api10Put(d *Daemon, r *http.Request) response.Response {
459460
// for reacting to the values that changed.
460461
if isClusterNotification(r) {
461462
logger.Debug("Handling config changed notification")
462-
changed := make(map[string]string)
463-
for key, value := range req.Config {
464-
changed[key] = value
465-
}
463+
changed := util.CloneMap(req.Config)
466464

467465
// Get the current (updated) config.
468466
var config *clusterConfig.Config
@@ -587,7 +585,7 @@ func doApi10Update(d *Daemon, r *http.Request, req api.ServerPut, patch bool) re
587585

588586
nodeChanged := map[string]string{}
589587
var newNodeConfig *node.Config
590-
oldNodeConfig := make(map[string]string)
588+
var oldNodeConfig map[string]string
591589

592590
err := s.DB.Node.Transaction(r.Context(), func(ctx context.Context, tx *db.NodeTx) error {
593591
var err error
@@ -597,9 +595,7 @@ func doApi10Update(d *Daemon, r *http.Request, req api.ServerPut, patch bool) re
597595
}
598596

599597
// Keep old config around in case something goes wrong. In that case the config will be reverted.
600-
for k, v := range newNodeConfig.Dump() {
601-
oldNodeConfig[k] = v
602-
}
598+
oldNodeConfig = util.CloneMap(newNodeConfig.Dump())
603599

604600
// We currently don't allow changing the cluster.https_address once it's set.
605601
if s.ServerClustered {
@@ -687,7 +683,7 @@ func doApi10Update(d *Daemon, r *http.Request, req api.ServerPut, patch bool) re
687683
// Then deal with cluster wide configuration
688684
var clusterChanged map[string]string
689685
var newClusterConfig *clusterConfig.Config
690-
oldClusterConfig := make(map[string]string)
686+
var oldClusterConfig map[string]string
691687

692688
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
693689
var err error
@@ -697,9 +693,7 @@ func doApi10Update(d *Daemon, r *http.Request, req api.ServerPut, patch bool) re
697693
}
698694

699695
// Keep old config around in case something goes wrong. In that case the config will be reverted.
700-
for k, v := range newClusterConfig.Dump() {
701-
oldClusterConfig[k] = v
702-
}
696+
oldClusterConfig = util.CloneMap(newClusterConfig.Dump())
703697

704698
if patch {
705699
clusterChanged, err = newClusterConfig.Patch(req.Config)
@@ -760,11 +754,8 @@ func doApi10Update(d *Daemon, r *http.Request, req api.ServerPut, patch bool) re
760754
}
761755

762756
serverPut := server.Writable()
763-
serverPut.Config = make(map[string]string)
764757
// Only propagated cluster-wide changes
765-
for key, value := range clusterChanged {
766-
serverPut.Config[key] = value
767-
}
758+
serverPut.Config = util.CloneMap(clusterChanged)
768759

769760
return client.UpdateServer(serverPut, etag)
770761
})

cmd/incusd/api_cluster.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,7 @@ func clusterPutJoin(d *Daemon, r *http.Request, req api.ClusterPut) response.Res
759759
d.globalConfig = currentClusterConfig
760760
d.globalConfigMu.Unlock()
761761

762-
existingConfigDump := currentClusterConfig.Dump()
763-
changes := make(map[string]string, len(existingConfigDump))
764-
for k, v := range existingConfigDump {
765-
changes[k] = v
766-
}
762+
changes := util.CloneMap(currentClusterConfig.Dump())
767763

768764
err = doApi10UpdateTriggers(d, nil, changes, nodeConfig, currentClusterConfig)
769765
if err != nil {

cmd/incusd/networks.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,7 @@ func networksPostCluster(ctx context.Context, s *state.State, projectName string
716716

717717
// Clone the network config for this node so we don't modify it and potentially end up sending
718718
// this node's config to another node.
719-
nodeConfig := make(map[string]string, len(netConfig))
720-
for k, v := range netConfig {
721-
nodeConfig[k] = v
722-
}
719+
nodeConfig := util.CloneMap(netConfig)
723720

724721
// Merge node specific config items into global config.
725722
for key, value := range nodeConfigs[server.Environment.ServerName] {

cmd/incusd/storage_pools.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,7 @@ func storagePoolsPostCluster(ctx context.Context, s *state.State, pool *api.Stor
554554

555555
// Clone fresh node config so we don't modify req.Config with this node's specific config which
556556
// could result in it being sent to other nodes later.
557-
nodeReq.Config = make(map[string]string, len(req.Config))
558-
for k, v := range req.Config {
559-
nodeReq.Config[k] = v
560-
}
557+
nodeReq.Config = util.CloneMap(req.Config)
561558

562559
// Merge node specific config items into global config.
563560
for key, value := range configs[server.Environment.ServerName] {

internal/server/device/config/devices.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ import (
66
"strings"
77

88
"github.com/lxc/incus/v6/shared/api"
9+
"github.com/lxc/incus/v6/shared/util"
910
)
1011

1112
// Device represents an instance device.
1213
type Device map[string]string
1314

1415
// Clone returns a copy of the Device.
1516
func (device Device) Clone() Device {
16-
copy := make(map[string]string, len(device))
17-
18-
for k, v := range device {
19-
copy[k] = v
20-
}
21-
22-
return copy
17+
return util.CloneMap(device)
2318
}
2419

2520
// Validate accepts a map of field/validation functions to run against the device's config.

internal/server/operations/operations.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lxc/incus/v6/shared/api"
2020
"github.com/lxc/incus/v6/shared/cancel"
2121
"github.com/lxc/incus/v6/shared/logger"
22+
"github.com/lxc/incus/v6/shared/util"
2223
)
2324

2425
var debug bool
@@ -66,12 +67,7 @@ func Clone() map[string]*Operation {
6667
operationsLock.Lock()
6768
defer operationsLock.Unlock()
6869

69-
localOperations := make(map[string]*Operation, len(operations))
70-
for k, v := range operations {
71-
localOperations[k] = v
72-
}
73-
74-
return localOperations
70+
return util.CloneMap(operations)
7571
}
7672

7773
// OperationGetInternal returns the operation with the given id. It returns an

internal/server/storage/backend.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -4032,10 +4032,7 @@ func (b *backend) ImportBucket(projectName string, poolVol *backupConfig.Config,
40324032
defer revert.Fail()
40334033

40344034
// Copy bucket config from backup file if present (so BucketDBCreate can safely modify the copy if needed).
4035-
bucketConfig := make(map[string]string, len(poolVol.Bucket.Config))
4036-
for k, v := range poolVol.Bucket.Config {
4037-
bucketConfig[k] = v
4038-
}
4035+
bucketConfig := util.CloneMap(poolVol.Bucket.Config)
40394036

40404037
bucket := &api.StorageBucketsPost{
40414038
Name: poolVol.Bucket.Name,
@@ -5810,10 +5807,7 @@ func (b *backend) ImportCustomVolume(projectName string, poolVol *backupConfig.C
58105807
defer revert.Fail()
58115808

58125809
// Copy volume config from backup file if present (so VolumeDBCreate can safely modify the copy if needed).
5813-
volumeConfig := make(map[string]string, len(poolVol.Volume.Config))
5814-
for k, v := range poolVol.Volume.Config {
5815-
volumeConfig[k] = v
5816-
}
5810+
volumeConfig := util.CloneMap(poolVol.Volume.Config)
58175811

58185812
// Validate config and create database entry for restored storage volume.
58195813
err := VolumeDBCreate(b, projectName, poolVol.Volume.Name, poolVol.Volume.Description, drivers.VolumeTypeCustom, false, volumeConfig, poolVol.Volume.CreatedAt, time.Time{}, drivers.ContentType(poolVol.Volume.ContentType), false, true)
@@ -5829,10 +5823,7 @@ func (b *backend) ImportCustomVolume(projectName string, poolVol *backupConfig.C
58295823

58305824
// Copy volume config from backup file if present
58315825
// (so VolumeDBCreate can safely modify the copy if needed).
5832-
snapVolumeConfig := make(map[string]string, len(poolVolSnap.Config))
5833-
for k, v := range poolVolSnap.Config {
5834-
snapVolumeConfig[k] = v
5835-
}
5826+
snapVolumeConfig := util.CloneMap(poolVolSnap.Config)
58365827

58375828
// Validate config and create database entry for restored storage volume.
58385829
err = VolumeDBCreate(b, projectName, fullSnapName, poolVolSnap.Description, drivers.VolumeTypeCustom, true, snapVolumeConfig, poolVolSnap.CreatedAt, time.Time{}, drivers.ContentType(poolVolSnap.ContentType), false, true)
@@ -6881,10 +6872,7 @@ func (b *backend) ImportInstance(inst instance.Instance, poolVol *backupConfig.C
68816872
// Copy volume config from backup file config if present,
68826873
// so VolumeDBCreate can safely modify the copy if needed.
68836874
if poolVol.Volume != nil {
6884-
volumeConfig = make(map[string]string, len(poolVol.Volume.Config))
6885-
for k, v := range poolVol.Volume.Config {
6886-
volumeConfig[k] = v
6887-
}
6875+
volumeConfig = util.CloneMap(poolVol.Volume.Config)
68886876

68896877
if !poolVol.Volume.CreatedAt.IsZero() {
68906878
creationDate = poolVol.Volume.CreatedAt
@@ -6906,10 +6894,7 @@ func (b *backend) ImportInstance(inst instance.Instance, poolVol *backupConfig.C
69066894

69076895
// Copy volume config from backup file if present,
69086896
// so VolumeDBCreate can safely modify the copy if needed.
6909-
snapVolumeConfig := make(map[string]string, len(poolVolSnap.Config))
6910-
for k, v := range poolVolSnap.Config {
6911-
snapVolumeConfig[k] = v
6912-
}
6897+
snapVolumeConfig := util.CloneMap(poolVolSnap.Config)
69136898

69146899
// Validate config and create database entry for recovered storage volume.
69156900
err = VolumeDBCreate(b, inst.Project().Name, fullSnapName, poolVolSnap.Description, volType, true, snapVolumeConfig, poolVolSnap.CreatedAt, time.Time{}, contentType, false, true)

internal/server/storage/drivers/driver_common.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,7 @@ func (d *common) Logger() logger.Logger {
240240

241241
// Config returns the storage pool config (as a copy, so not modifiable).
242242
func (d *common) Config() map[string]string {
243-
confCopy := make(map[string]string, len(d.config))
244-
for k, v := range d.config {
245-
confCopy[k] = v
246-
}
247-
248-
return confCopy
243+
return util.CloneMap(d.config)
249244
}
250245

251246
// ApplyPatch looks for a suitable patch and runs it.

internal/server/storage/drivers/volume.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,10 @@ func (v *Volume) SetHasSource(hasSource bool) {
577577
// Clone returns a copy of the volume.
578578
func (v Volume) Clone() Volume {
579579
// Copy the config map to avoid internal modifications affecting external state.
580-
newConfig := make(map[string]string, len(v.config))
581-
for k, v := range v.config {
582-
newConfig[k] = v
583-
}
580+
newConfig := util.CloneMap(v.config)
584581

585582
// Copy the pool config map to avoid internal modifications affecting external state.
586-
newPoolConfig := make(map[string]string, len(v.poolConfig))
587-
for k, v := range v.poolConfig {
588-
newPoolConfig[k] = v
589-
}
583+
newPoolConfig := util.CloneMap(v.poolConfig)
590584

591585
return NewVolume(v.driver, v.pool, v.volType, v.contentType, v.name, newConfig, newPoolConfig)
592586
}

internal/server/util/config.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"slices"
66
"sort"
77
"strings"
8+
9+
"github.com/lxc/incus/v6/shared/util"
810
)
911

1012
// CompareConfigs compares two config maps and returns an error if they differ.
@@ -52,10 +54,5 @@ func CompareConfigs(config1, config2 map[string]string, exclude []string) error
5254

5355
// CopyConfig creates a new map with a copy of the given config.
5456
func CopyConfig(config map[string]string) map[string]string {
55-
copy := make(map[string]string, len(config))
56-
for key, value := range config {
57-
copy[key] = value
58-
}
59-
60-
return copy
57+
return util.CloneMap(config)
6158
}

shared/cliconfig/default.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cliconfig
22

3+
import "github.com/lxc/incus/v6/shared/util"
4+
35
// LocalRemote is the default local remote (over the unix socket).
46
var LocalRemote = Remote{
57
Addr: "unix://",
@@ -28,14 +30,8 @@ var DefaultRemotes = map[string]Remote{
2830

2931
// DefaultConfig returns the default configuration.
3032
func DefaultConfig() *Config {
31-
// Duplicate remotes from DefaultRemotes.
32-
defaultRoutes := make(map[string]Remote, len(DefaultRemotes))
33-
for k, v := range DefaultRemotes {
34-
defaultRoutes[k] = v
35-
}
36-
3733
return &Config{
38-
Remotes: defaultRoutes,
34+
Remotes: util.CloneMap(DefaultRemotes),
3935
Aliases: make(map[string]string),
4036
DefaultRemote: "local",
4137
}

0 commit comments

Comments
 (0)