فهرست منبع

fix(config): properly apply defaults when reading folder configuration (#10034)

Tommy van der Vorst 6 ماه پیش
والد
کامیت
f7c8efd93c
2فایلهای تغییر یافته به همراه32 افزوده شده و 5 حذف شده
  1. 8 4
      lib/config/config_test.go
  2. 24 1
      lib/config/folderconfiguration.go

+ 8 - 4
lib/config/config_test.go

@@ -101,7 +101,7 @@ func TestDefaultValues(t *testing.T) {
 		Defaults: Defaults{
 			Folder: FolderConfiguration{
 				FilesystemType:   FilesystemTypeBasic,
-				Path:             "~",
+				Path:             "",
 				Type:             FolderTypeSendReceive,
 				Devices:          []FolderDeviceConfiguration{{DeviceID: device1}},
 				RescanIntervalS:  3600,
@@ -181,7 +181,7 @@ func TestDeviceConfig(t *testing.T) {
 				Devices:          []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
 				Type:             FolderTypeSendOnly,
 				RescanIntervalS:  600,
-				FSWatcherEnabled: false,
+				FSWatcherEnabled: true,
 				FSWatcherDelayS:  10,
 				Copiers:          0,
 				Hashers:          0,
@@ -189,14 +189,18 @@ func TestDeviceConfig(t *testing.T) {
 				MinDiskFree:      Size{1, "%"},
 				MaxConflicts:     -1,
 				Versioning: VersioningConfiguration{
-					Params: map[string]string{},
+					CleanupIntervalS: 3600,
+					FSType:           FilesystemTypeBasic,
+					Params:           map[string]string{},
 				},
 				WeakHashThresholdPct: 25,
 				MarkerName:           DefaultMarkerName,
 				JunctionsAsDirs:      true,
 				MaxConcurrentWrites:  maxConcurrentWritesDefault,
 				XattrFilter: XattrFilter{
-					Entries: []XattrFilterEntry{},
+					MaxSingleEntrySize: 1024,
+					MaxTotalSize:       4096,
+					Entries:            []XattrFilterEntry{},
 				},
 			},
 		}

+ 24 - 1
lib/config/folderconfiguration.go

@@ -9,6 +9,8 @@ package config
 import (
 	"bytes"
 	"crypto/sha256"
+	"encoding/json"
+	"encoding/xml"
 	"errors"
 	"fmt"
 	"path"
@@ -23,6 +25,7 @@ import (
 	"github.com/syncthing/syncthing/lib/db"
 	"github.com/syncthing/syncthing/lib/fs"
 	"github.com/syncthing/syncthing/lib/protocol"
+	"github.com/syncthing/syncthing/lib/structutil"
 )
 
 var (
@@ -48,7 +51,7 @@ type FolderConfiguration struct {
 	ID                      string                      `json:"id" xml:"id,attr" nodefault:"true"`
 	Label                   string                      `json:"label" xml:"label,attr" restart:"false"`
 	FilesystemType          FilesystemType              `json:"filesystemType" xml:"filesystemType" default:"basic"`
-	Path                    string                      `json:"path" xml:"path,attr" default:"~"`
+	Path                    string                      `json:"path" xml:"path,attr"`
 	Type                    FolderType                  `json:"type" xml:"type,attr"`
 	Devices                 []FolderDeviceConfiguration `json:"devices" xml:"device"`
 	RescanIntervalS         int                         `json:"rescanIntervalS" xml:"rescanIntervalS,attr" default:"3600"`
@@ -399,3 +402,23 @@ func (f XattrFilter) GetMaxSingleEntrySize() int {
 func (f XattrFilter) GetMaxTotalSize() int {
 	return f.MaxTotalSize
 }
+
+func (f *FolderConfiguration) UnmarshalJSON(data []byte) error {
+	structutil.SetDefaults(f)
+
+	// avoid recursing into this method
+	type noCustomUnmarshal FolderConfiguration
+	ptr := (*noCustomUnmarshal)(f)
+
+	return json.Unmarshal(data, ptr)
+}
+
+func (f *FolderConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
+	structutil.SetDefaults(f)
+
+	// avoid recursing into this method
+	type noCustomUnmarshal FolderConfiguration
+	ptr := (*noCustomUnmarshal)(f)
+
+	return d.DecodeElement(ptr, &start)
+}