2015-10-27 03:37:03 -07:00
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2017-02-08 23:52:18 -07:00
// You can obtain one at https://mozilla.org/MPL/2.0/.
2015-10-27 03:37:03 -07:00
package config
import (
2016-11-17 08:12:41 -07:00
"fmt"
2015-10-27 03:37:03 -07:00
"os"
"path/filepath"
"runtime"
"strings"
"github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/protocol"
)
type FolderConfiguration struct {
ID string ` xml:"id,attr" json:"id" `
2016-03-11 02:48:46 -07:00
Label string ` xml:"label,attr" json:"label" `
2015-10-27 03:37:03 -07:00
RawPath string ` xml:"path,attr" json:"path" `
2016-05-04 04:26:36 -07:00
Type FolderType ` xml:"type,attr" json:"type" `
2015-10-27 03:37:03 -07:00
Devices [ ] FolderDeviceConfiguration ` xml:"device" json:"devices" `
RescanIntervalS int ` xml:"rescanIntervalS,attr" json:"rescanIntervalS" `
IgnorePerms bool ` xml:"ignorePerms,attr" json:"ignorePerms" `
AutoNormalize bool ` xml:"autoNormalize,attr" json:"autoNormalize" `
MinDiskFreePct float64 ` xml:"minDiskFreePct" json:"minDiskFreePct" `
Versioning VersioningConfiguration ` xml:"versioning" json:"versioning" `
Copiers int ` xml:"copiers" json:"copiers" ` // This defines how many files are handled concurrently.
Pullers int ` xml:"pullers" json:"pullers" ` // Defines how many blocks are fetched at the same time, possibly between separate copier routines.
Hashers int ` xml:"hashers" json:"hashers" ` // Less than one sets the value to the number of cores. These are CPU bound due to hashing.
Order PullOrder ` xml:"order" json:"order" `
IgnoreDelete bool ` xml:"ignoreDelete" json:"ignoreDelete" `
ScanProgressIntervalS int ` xml:"scanProgressIntervalS" json:"scanProgressIntervalS" ` // Set to a negative value to disable. Value of 0 will get replaced with value of 2 (default value)
PullerSleepS int ` xml:"pullerSleepS" json:"pullerSleepS" `
PullerPauseS int ` xml:"pullerPauseS" json:"pullerPauseS" `
MaxConflicts int ` xml:"maxConflicts" json:"maxConflicts" `
2015-11-21 08:30:53 -07:00
DisableSparseFiles bool ` xml:"disableSparseFiles" json:"disableSparseFiles" `
2016-04-15 03:59:41 -07:00
DisableTempIndexes bool ` xml:"disableTempIndexes" json:"disableTempIndexes" `
2016-11-21 10:09:29 -07:00
Fsync bool ` xml:"fsync" json:"fsync" `
2016-12-21 11:41:25 -07:00
Paused bool ` xml:"paused" json:"paused" `
2017-01-04 14:04:13 -07:00
WeakHashThresholdPct int ` xml:"weakHashThresholdPct" json:"weakHashThresholdPct" ` // Use weak hash if more than X percent of the file has changed. Set to -1 to always use weak hash.
2015-10-27 03:37:03 -07:00
2015-11-05 01:31:36 -07:00
cachedPath string
2016-05-04 03:47:33 -07:00
2016-05-12 01:23:18 -07:00
DeprecatedReadOnly bool ` xml:"ro,attr,omitempty" json:"-" `
2015-10-27 03:37:03 -07:00
}
type FolderDeviceConfiguration struct {
2016-11-07 09:40:48 -07:00
DeviceID protocol . DeviceID ` xml:"id,attr" json:"deviceID" `
IntroducedBy protocol . DeviceID ` xml:"introducedBy,attr" json:"introducedBy" `
2015-10-27 03:37:03 -07:00
}
2016-05-04 04:26:36 -07:00
func NewFolderConfiguration ( id , path string ) FolderConfiguration {
2015-11-07 01:47:31 -07:00
f := FolderConfiguration {
ID : id ,
RawPath : path ,
}
f . prepare ( )
return f
}
2015-10-27 03:37:03 -07:00
func ( f FolderConfiguration ) Copy ( ) FolderConfiguration {
c := f
c . Devices = make ( [ ] FolderDeviceConfiguration , len ( f . Devices ) )
copy ( c . Devices , f . Devices )
2015-10-27 03:53:42 -07:00
c . Versioning = f . Versioning . Copy ( )
2015-10-27 03:37:03 -07:00
return c
}
func ( f FolderConfiguration ) Path ( ) string {
// This is intentionally not a pointer method, because things like
// cfg.Folders["default"].Path() should be valid.
2016-07-02 12:38:39 -07:00
if f . cachedPath == "" && f . RawPath != "" {
2015-11-05 01:31:36 -07:00
l . Infoln ( "bug: uncached path call (should only happen in tests)" )
return f . cleanedPath ( )
2015-10-27 03:37:03 -07:00
}
2015-11-05 01:31:36 -07:00
return f . cachedPath
2015-10-27 03:37:03 -07:00
}
func ( f * FolderConfiguration ) CreateMarker ( ) error {
if ! f . HasMarker ( ) {
marker := filepath . Join ( f . Path ( ) , ".stfolder" )
fd , err := os . Create ( marker )
if err != nil {
return err
}
fd . Close ( )
2016-11-21 10:09:29 -07:00
if err := osutil . SyncDir ( filepath . Dir ( marker ) ) ; err != nil {
l . Infof ( "fsync %q failed: %v" , filepath . Dir ( marker ) , err )
}
2015-10-27 03:37:03 -07:00
osutil . HideFile ( marker )
}
return nil
}
func ( f * FolderConfiguration ) HasMarker ( ) bool {
_ , err := os . Stat ( filepath . Join ( f . Path ( ) , ".stfolder" ) )
2016-12-18 11:57:41 -07:00
return err == nil
2015-10-27 03:37:03 -07:00
}
2016-11-17 08:12:41 -07:00
func ( f FolderConfiguration ) Description ( ) string {
2016-12-19 02:12:06 -07:00
if f . Label == "" {
return f . ID
}
2016-11-17 08:12:41 -07:00
return fmt . Sprintf ( "%q (%s)" , f . Label , f . ID )
}
2015-10-27 03:37:03 -07:00
func ( f * FolderConfiguration ) DeviceIDs ( ) [ ] protocol . DeviceID {
deviceIDs := make ( [ ] protocol . DeviceID , len ( f . Devices ) )
for i , n := range f . Devices {
deviceIDs [ i ] = n . DeviceID
}
return deviceIDs
}
2015-11-05 01:01:47 -07:00
func ( f * FolderConfiguration ) prepare ( ) {
2016-07-02 12:38:39 -07:00
if f . RawPath != "" {
// The reason it's done like this:
// C: -> C:\ -> C:\ (issue that this is trying to fix)
// C:\somedir -> C:\somedir\ -> C:\somedir
// C:\somedir\ -> C:\somedir\\ -> C:\somedir
// This way in the tests, we get away without OS specific separators
// in the test configs.
f . RawPath = filepath . Dir ( f . RawPath + string ( filepath . Separator ) )
// If we're not on Windows, we want the path to end with a slash to
// penetrate symlinks. On Windows, paths must not end with a slash.
if runtime . GOOS != "windows" && f . RawPath [ len ( f . RawPath ) - 1 ] != filepath . Separator {
f . RawPath = f . RawPath + string ( filepath . Separator )
}
2015-11-05 01:01:47 -07:00
}
2015-11-05 01:31:36 -07:00
f . cachedPath = f . cleanedPath ( )
2015-11-05 01:01:47 -07:00
if f . RescanIntervalS > MaxRescanIntervalS {
f . RescanIntervalS = MaxRescanIntervalS
} else if f . RescanIntervalS < 0 {
f . RescanIntervalS = 0
}
2016-05-09 04:30:19 -07:00
if f . Versioning . Params == nil {
f . Versioning . Params = make ( map [ string ] string )
}
2017-01-04 14:04:13 -07:00
if f . WeakHashThresholdPct == 0 {
f . WeakHashThresholdPct = 25
}
2015-11-05 01:01:47 -07:00
}
2015-11-05 01:31:36 -07:00
func ( f * FolderConfiguration ) cleanedPath ( ) string {
2016-07-02 12:38:39 -07:00
if f . RawPath == "" {
return ""
}
2015-11-05 01:31:36 -07:00
cleaned := f . RawPath
// Attempt tilde expansion; leave unchanged in case of error
if path , err := osutil . ExpandTilde ( cleaned ) ; err == nil {
cleaned = path
}
// Attempt absolutification; leave unchanged in case of error
if ! filepath . IsAbs ( cleaned ) {
// Abs() looks like a fairly expensive syscall on Windows, while
// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
// somewhat faster in the general case, hence the outer if...
if path , err := filepath . Abs ( cleaned ) ; err == nil {
cleaned = path
}
}
// Attempt to enable long filename support on Windows. We may still not
// have an absolute path here if the previous steps failed.
if runtime . GOOS == "windows" && filepath . IsAbs ( cleaned ) && ! strings . HasPrefix ( f . RawPath , ` \\ ` ) {
return ` \\?\ ` + cleaned
}
2016-06-26 03:17:20 -07:00
// If we're not on Windows, we want the path to end with a slash to
// penetrate symlinks. On Windows, paths must not end with a slash.
if runtime . GOOS != "windows" && cleaned [ len ( cleaned ) - 1 ] != filepath . Separator {
cleaned = cleaned + string ( filepath . Separator )
}
2015-11-05 01:31:36 -07:00
return cleaned
}
2015-10-27 03:37:03 -07:00
type FolderDeviceConfigurationList [ ] FolderDeviceConfiguration
func ( l FolderDeviceConfigurationList ) Less ( a , b int ) bool {
return l [ a ] . DeviceID . Compare ( l [ b ] . DeviceID ) == - 1
}
func ( l FolderDeviceConfigurationList ) Swap ( a , b int ) {
l [ a ] , l [ b ] = l [ b ] , l [ a ]
}
func ( l FolderDeviceConfigurationList ) Len ( ) int {
return len ( l )
}