model.go 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "net"
  13. "path/filepath"
  14. "reflect"
  15. "regexp"
  16. "runtime"
  17. "strings"
  18. stdsync "sync"
  19. "time"
  20. "github.com/syncthing/syncthing/lib/config"
  21. "github.com/syncthing/syncthing/lib/connections"
  22. "github.com/syncthing/syncthing/lib/db"
  23. "github.com/syncthing/syncthing/lib/events"
  24. "github.com/syncthing/syncthing/lib/fs"
  25. "github.com/syncthing/syncthing/lib/ignore"
  26. "github.com/syncthing/syncthing/lib/osutil"
  27. "github.com/syncthing/syncthing/lib/protocol"
  28. "github.com/syncthing/syncthing/lib/scanner"
  29. "github.com/syncthing/syncthing/lib/stats"
  30. "github.com/syncthing/syncthing/lib/sync"
  31. "github.com/syncthing/syncthing/lib/upgrade"
  32. "github.com/syncthing/syncthing/lib/versioner"
  33. "github.com/thejerf/suture"
  34. )
  35. var locationLocal *time.Location
  36. func init() {
  37. var err error
  38. locationLocal, err = time.LoadLocation("Local")
  39. if err != nil {
  40. panic(err.Error())
  41. }
  42. }
  43. // How many files to send in each Index/IndexUpdate message.
  44. const (
  45. maxBatchSizeBytes = 250 * 1024 // Aim for making index messages no larger than 250 KiB (uncompressed)
  46. maxBatchSizeFiles = 1000 // Either way, don't include more files than this
  47. )
  48. type service interface {
  49. BringToFront(string)
  50. Override(*db.FileSet, func([]protocol.FileInfo))
  51. Revert(*db.FileSet, func([]protocol.FileInfo))
  52. DelayScan(d time.Duration)
  53. SchedulePull() // something relevant changed, we should try a pull
  54. Jobs() ([]string, []string) // In progress, Queued
  55. Scan(subs []string) error
  56. Serve()
  57. Stop()
  58. CheckHealth() error
  59. Errors() []FileError
  60. WatchError() error
  61. getState() (folderState, time.Time, error)
  62. setState(state folderState)
  63. setError(err error)
  64. }
  65. type Availability struct {
  66. ID protocol.DeviceID `json:"id"`
  67. FromTemporary bool `json:"fromTemporary"`
  68. }
  69. type Model interface {
  70. suture.Service
  71. connections.Model
  72. AddFolder(cfg config.FolderConfiguration)
  73. RestartFolder(from, to config.FolderConfiguration)
  74. StartFolder(folder string)
  75. ResetFolder(folder string)
  76. DelayScan(folder string, next time.Duration)
  77. ScanFolder(folder string) error
  78. ScanFolders() map[string]error
  79. ScanFolderSubdirs(folder string, subs []string) error
  80. State(folder string) (string, time.Time, error)
  81. FolderErrors(folder string) ([]FileError, error)
  82. WatchError(folder string) error
  83. Override(folder string)
  84. Revert(folder string)
  85. BringToFront(folder, file string)
  86. GetIgnores(folder string) ([]string, []string, error)
  87. SetIgnores(folder string, content []string) error
  88. GetFolderVersions(folder string) (map[string][]versioner.FileVersion, error)
  89. RestoreFolderVersions(folder string, versions map[string]time.Time) (map[string]string, error)
  90. LocalChangedFiles(folder string, page, perpage int) []db.FileInfoTruncated
  91. NeedFolderFiles(folder string, page, perpage int) ([]db.FileInfoTruncated, []db.FileInfoTruncated, []db.FileInfoTruncated)
  92. RemoteNeedFolderFiles(device protocol.DeviceID, folder string, page, perpage int) ([]db.FileInfoTruncated, error)
  93. CurrentFolderFile(folder string, file string) (protocol.FileInfo, bool)
  94. CurrentGlobalFile(folder string, file string) (protocol.FileInfo, bool)
  95. Availability(folder string, file protocol.FileInfo, block protocol.BlockInfo) []Availability
  96. GlobalSize(folder string) db.Counts
  97. LocalSize(folder string) db.Counts
  98. NeedSize(folder string) db.Counts
  99. ReceiveOnlyChangedSize(folder string) db.Counts
  100. CurrentSequence(folder string) (int64, bool)
  101. RemoteSequence(folder string) (int64, bool)
  102. Completion(device protocol.DeviceID, folder string) FolderCompletion
  103. ConnectionStats() map[string]interface{}
  104. DeviceStatistics() map[string]stats.DeviceStatistics
  105. FolderStatistics() map[string]stats.FolderStatistics
  106. UsageReportingStats(version int, preview bool) map[string]interface{}
  107. StartDeadlockDetector(timeout time.Duration)
  108. GlobalDirectoryTree(folder, prefix string, levels int, dirsonly bool) map[string]interface{}
  109. }
  110. type model struct {
  111. *suture.Supervisor
  112. cfg config.Wrapper
  113. db *db.Lowlevel
  114. finder *db.BlockFinder
  115. progressEmitter *ProgressEmitter
  116. id protocol.DeviceID
  117. shortID protocol.ShortID
  118. cacheIgnoredFiles bool
  119. protectedFiles []string
  120. clientName string
  121. clientVersion string
  122. fmut sync.RWMutex // protects the below
  123. folderCfgs map[string]config.FolderConfiguration // folder -> cfg
  124. folderFiles map[string]*db.FileSet // folder -> files
  125. deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef
  126. folderIgnores map[string]*ignore.Matcher // folder -> matcher object
  127. folderRunners map[string]service // folder -> puller or scanner
  128. folderRunnerTokens map[string][]suture.ServiceToken // folder -> tokens for puller or scanner
  129. folderStatRefs map[string]*stats.FolderStatisticsReference // folder -> statsRef
  130. folderRestartMuts syncMutexMap // folder -> restart mutex
  131. pmut sync.RWMutex // protects the below
  132. conn map[protocol.DeviceID]connections.Connection
  133. connRequestLimiters map[protocol.DeviceID]*byteSemaphore
  134. closed map[protocol.DeviceID]chan struct{}
  135. helloMessages map[protocol.DeviceID]protocol.HelloResult
  136. deviceDownloads map[protocol.DeviceID]*deviceDownloadState
  137. remotePausedFolders map[protocol.DeviceID][]string // deviceID -> folders
  138. foldersRunning int32 // for testing only
  139. }
  140. type folderFactory func(*model, config.FolderConfiguration, versioner.Versioner, fs.Filesystem) service
  141. var (
  142. folderFactories = make(map[config.FolderType]folderFactory)
  143. )
  144. var (
  145. errDeviceUnknown = errors.New("unknown device")
  146. errDevicePaused = errors.New("device is paused")
  147. errDeviceIgnored = errors.New("device is ignored")
  148. ErrFolderPaused = errors.New("folder is paused")
  149. errFolderNotRunning = errors.New("folder is not running")
  150. errFolderMissing = errors.New("no such folder")
  151. errNetworkNotAllowed = errors.New("network not allowed")
  152. // errors about why a connection is closed
  153. errIgnoredFolderRemoved = errors.New("folder no longer ignored")
  154. errReplacingConnection = errors.New("replacing connection")
  155. )
  156. // NewModel creates and starts a new model. The model starts in read-only mode,
  157. // where it sends index information to connected peers and responds to requests
  158. // for file data without altering the local folder in any way.
  159. func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersion string, ldb *db.Lowlevel, protectedFiles []string) Model {
  160. m := &model{
  161. Supervisor: suture.New("model", suture.Spec{
  162. Log: func(line string) {
  163. l.Debugln(line)
  164. },
  165. PassThroughPanics: true,
  166. }),
  167. cfg: cfg,
  168. db: ldb,
  169. finder: db.NewBlockFinder(ldb),
  170. progressEmitter: NewProgressEmitter(cfg),
  171. id: id,
  172. shortID: id.Short(),
  173. cacheIgnoredFiles: cfg.Options().CacheIgnoredFiles,
  174. protectedFiles: protectedFiles,
  175. clientName: clientName,
  176. clientVersion: clientVersion,
  177. folderCfgs: make(map[string]config.FolderConfiguration),
  178. folderFiles: make(map[string]*db.FileSet),
  179. deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference),
  180. folderIgnores: make(map[string]*ignore.Matcher),
  181. folderRunners: make(map[string]service),
  182. folderRunnerTokens: make(map[string][]suture.ServiceToken),
  183. folderStatRefs: make(map[string]*stats.FolderStatisticsReference),
  184. conn: make(map[protocol.DeviceID]connections.Connection),
  185. connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore),
  186. closed: make(map[protocol.DeviceID]chan struct{}),
  187. helloMessages: make(map[protocol.DeviceID]protocol.HelloResult),
  188. deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState),
  189. remotePausedFolders: make(map[protocol.DeviceID][]string),
  190. fmut: sync.NewRWMutex(),
  191. pmut: sync.NewRWMutex(),
  192. }
  193. if cfg.Options().ProgressUpdateIntervalS > -1 {
  194. m.Add(m.progressEmitter)
  195. }
  196. scanLimiter.setCapacity(cfg.Options().MaxConcurrentScans)
  197. cfg.Subscribe(m)
  198. return m
  199. }
  200. // StartDeadlockDetector starts a deadlock detector on the models locks which
  201. // causes panics in case the locks cannot be acquired in the given timeout
  202. // period.
  203. func (m *model) StartDeadlockDetector(timeout time.Duration) {
  204. l.Infof("Starting deadlock detector with %v timeout", timeout)
  205. detector := newDeadlockDetector(timeout)
  206. detector.Watch("fmut", m.fmut)
  207. detector.Watch("pmut", m.pmut)
  208. }
  209. // StartFolder constructs the folder service and starts it.
  210. func (m *model) StartFolder(folder string) {
  211. m.fmut.Lock()
  212. m.pmut.Lock()
  213. folderType := m.startFolderLocked(folder)
  214. folderCfg := m.folderCfgs[folder]
  215. m.pmut.Unlock()
  216. m.fmut.Unlock()
  217. l.Infof("Ready to synchronize %s (%s)", folderCfg.Description(), folderType)
  218. }
  219. func (m *model) startFolderLocked(folder string) config.FolderType {
  220. if err := m.checkFolderRunningLocked(folder); err == errFolderMissing {
  221. panic("cannot start nonexistent folder " + folder)
  222. } else if err == nil {
  223. panic("cannot start already running folder " + folder)
  224. }
  225. cfg := m.folderCfgs[folder]
  226. folderFactory, ok := folderFactories[cfg.Type]
  227. if !ok {
  228. panic(fmt.Sprintf("unknown folder type 0x%x", cfg.Type))
  229. }
  230. fs := m.folderFiles[folder]
  231. // Find any devices for which we hold the index in the db, but the folder
  232. // is not shared, and drop it.
  233. expected := mapDevices(cfg.DeviceIDs())
  234. for _, available := range fs.ListDevices() {
  235. if _, ok := expected[available]; !ok {
  236. l.Debugln("dropping", folder, "state for", available)
  237. fs.Drop(available)
  238. }
  239. }
  240. // Close connections to affected devices
  241. for _, id := range cfg.DeviceIDs() {
  242. m.closeLocked(id, fmt.Errorf("started folder %v", cfg.Description()))
  243. }
  244. v, ok := fs.Sequence(protocol.LocalDeviceID), true
  245. indexHasFiles := ok && v > 0
  246. if !indexHasFiles {
  247. // It's a blank folder, so this may the first time we're looking at
  248. // it. Attempt to create and tag with our marker as appropriate. We
  249. // don't really do anything with errors at this point except warn -
  250. // if these things don't work, we still want to start the folder and
  251. // it'll show up as errored later.
  252. if err := cfg.CreateRoot(); err != nil {
  253. l.Warnln("Failed to create folder root directory", err)
  254. } else if err = cfg.CreateMarker(); err != nil {
  255. l.Warnln("Failed to create folder marker:", err)
  256. }
  257. }
  258. ver := cfg.Versioner()
  259. if service, ok := ver.(suture.Service); ok {
  260. // The versioner implements the suture.Service interface, so
  261. // expects to be run in the background in addition to being called
  262. // when files are going to be archived.
  263. token := m.Add(service)
  264. m.folderRunnerTokens[folder] = append(m.folderRunnerTokens[folder], token)
  265. }
  266. ffs := fs.MtimeFS()
  267. // These are our metadata files, and they should always be hidden.
  268. ffs.Hide(config.DefaultMarkerName)
  269. ffs.Hide(".stversions")
  270. ffs.Hide(".stignore")
  271. p := folderFactory(m, cfg, ver, ffs)
  272. m.folderRunners[folder] = p
  273. m.warnAboutOverwritingProtectedFiles(folder)
  274. token := m.Add(p)
  275. m.folderRunnerTokens[folder] = append(m.folderRunnerTokens[folder], token)
  276. return cfg.Type
  277. }
  278. func (m *model) warnAboutOverwritingProtectedFiles(folder string) {
  279. if m.folderCfgs[folder].Type == config.FolderTypeSendOnly {
  280. return
  281. }
  282. // This is a bit of a hack.
  283. ffs := m.folderCfgs[folder].Filesystem()
  284. if ffs.Type() != fs.FilesystemTypeBasic {
  285. return
  286. }
  287. folderLocation := ffs.URI()
  288. ignores := m.folderIgnores[folder]
  289. var filesAtRisk []string
  290. for _, protectedFilePath := range m.protectedFiles {
  291. // check if file is synced in this folder
  292. if protectedFilePath != folderLocation && !fs.IsParent(protectedFilePath, folderLocation) {
  293. continue
  294. }
  295. // check if file is ignored
  296. relPath, _ := filepath.Rel(folderLocation, protectedFilePath)
  297. if ignores.Match(relPath).IsIgnored() {
  298. continue
  299. }
  300. filesAtRisk = append(filesAtRisk, protectedFilePath)
  301. }
  302. if len(filesAtRisk) > 0 {
  303. l.Warnln("Some protected files may be overwritten and cause issues. See https://docs.syncthing.net/users/config.html#syncing-configuration-files for more information. The at risk files are:", strings.Join(filesAtRisk, ", "))
  304. }
  305. }
  306. func (m *model) AddFolder(cfg config.FolderConfiguration) {
  307. if len(cfg.ID) == 0 {
  308. panic("cannot add empty folder id")
  309. }
  310. if len(cfg.Path) == 0 {
  311. panic("cannot add empty folder path")
  312. }
  313. m.fmut.Lock()
  314. m.addFolderLocked(cfg)
  315. m.fmut.Unlock()
  316. }
  317. func (m *model) addFolderLocked(cfg config.FolderConfiguration) {
  318. m.folderCfgs[cfg.ID] = cfg
  319. folderFs := cfg.Filesystem()
  320. m.folderFiles[cfg.ID] = db.NewFileSet(cfg.ID, folderFs, m.db)
  321. ignores := ignore.New(folderFs, ignore.WithCache(m.cacheIgnoredFiles))
  322. if err := ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) {
  323. l.Warnln("Loading ignores:", err)
  324. }
  325. m.folderIgnores[cfg.ID] = ignores
  326. }
  327. func (m *model) RemoveFolder(cfg config.FolderConfiguration) {
  328. m.fmut.Lock()
  329. m.pmut.Lock()
  330. // Delete syncthing specific files
  331. cfg.Filesystem().RemoveAll(config.DefaultMarkerName)
  332. m.tearDownFolderLocked(cfg, fmt.Errorf("removing folder %v", cfg.Description()))
  333. // Remove it from the database
  334. db.DropFolder(m.db, cfg.ID)
  335. m.pmut.Unlock()
  336. m.fmut.Unlock()
  337. }
  338. func (m *model) tearDownFolderLocked(cfg config.FolderConfiguration, err error) {
  339. // Close connections to affected devices
  340. // Must happen before stopping the folder service to abort ongoing
  341. // transmissions and thus allow timely service termination.
  342. for _, dev := range cfg.Devices {
  343. m.closeLocked(dev.DeviceID, err)
  344. }
  345. // Stop the services running for this folder and wait for them to finish
  346. // stopping to prevent races on restart.
  347. tokens := m.folderRunnerTokens[cfg.ID]
  348. m.pmut.Unlock()
  349. m.fmut.Unlock()
  350. for _, id := range tokens {
  351. m.RemoveAndWait(id, 0)
  352. }
  353. m.fmut.Lock()
  354. m.pmut.Lock()
  355. // Clean up our config maps
  356. delete(m.folderCfgs, cfg.ID)
  357. delete(m.folderFiles, cfg.ID)
  358. delete(m.folderIgnores, cfg.ID)
  359. delete(m.folderRunners, cfg.ID)
  360. delete(m.folderRunnerTokens, cfg.ID)
  361. delete(m.folderStatRefs, cfg.ID)
  362. }
  363. func (m *model) RestartFolder(from, to config.FolderConfiguration) {
  364. if len(to.ID) == 0 {
  365. panic("bug: cannot restart empty folder ID")
  366. }
  367. if to.ID != from.ID {
  368. panic(fmt.Sprintf("bug: folder restart cannot change ID %q -> %q", from.ID, to.ID))
  369. }
  370. // This mutex protects the entirety of the restart operation, preventing
  371. // there from being more than one folder restart operation in progress
  372. // at any given time. The usual fmut/pmut stuff doesn't cover this,
  373. // because those locks are released while we are waiting for the folder
  374. // to shut down (and must be so because the folder might need them as
  375. // part of its operations before shutting down).
  376. restartMut := m.folderRestartMuts.Get(to.ID)
  377. restartMut.Lock()
  378. defer restartMut.Unlock()
  379. m.fmut.Lock()
  380. m.pmut.Lock()
  381. defer m.fmut.Unlock()
  382. defer m.pmut.Unlock()
  383. var infoMsg string
  384. var errMsg string
  385. switch {
  386. case to.Paused:
  387. infoMsg = "Paused"
  388. errMsg = "pausing"
  389. case from.Paused:
  390. infoMsg = "Unpaused"
  391. errMsg = "unpausing"
  392. default:
  393. infoMsg = "Restarted"
  394. errMsg = "restarting"
  395. }
  396. m.tearDownFolderLocked(from, fmt.Errorf("%v folder %v", errMsg, to.Description()))
  397. if !to.Paused {
  398. m.addFolderLocked(to)
  399. m.startFolderLocked(to.ID)
  400. }
  401. l.Infof("%v folder %v (%v)", infoMsg, to.Description(), to.Type)
  402. }
  403. func (m *model) UsageReportingStats(version int, preview bool) map[string]interface{} {
  404. stats := make(map[string]interface{})
  405. if version >= 3 {
  406. // Block stats
  407. blockStatsMut.Lock()
  408. copyBlockStats := make(map[string]int)
  409. for k, v := range blockStats {
  410. copyBlockStats[k] = v
  411. if !preview {
  412. blockStats[k] = 0
  413. }
  414. }
  415. blockStatsMut.Unlock()
  416. stats["blockStats"] = copyBlockStats
  417. // Transport stats
  418. m.pmut.Lock()
  419. transportStats := make(map[string]int)
  420. for _, conn := range m.conn {
  421. transportStats[conn.Transport()]++
  422. }
  423. m.pmut.Unlock()
  424. stats["transportStats"] = transportStats
  425. // Ignore stats
  426. ignoreStats := map[string]int{
  427. "lines": 0,
  428. "inverts": 0,
  429. "folded": 0,
  430. "deletable": 0,
  431. "rooted": 0,
  432. "includes": 0,
  433. "escapedIncludes": 0,
  434. "doubleStars": 0,
  435. "stars": 0,
  436. }
  437. var seenPrefix [3]bool
  438. for folder := range m.cfg.Folders() {
  439. lines, _, err := m.GetIgnores(folder)
  440. if err != nil {
  441. continue
  442. }
  443. ignoreStats["lines"] += len(lines)
  444. for _, line := range lines {
  445. // Allow prefixes to be specified in any order, but only once.
  446. for {
  447. if strings.HasPrefix(line, "!") && !seenPrefix[0] {
  448. seenPrefix[0] = true
  449. line = line[1:]
  450. ignoreStats["inverts"] += 1
  451. } else if strings.HasPrefix(line, "(?i)") && !seenPrefix[1] {
  452. seenPrefix[1] = true
  453. line = line[4:]
  454. ignoreStats["folded"] += 1
  455. } else if strings.HasPrefix(line, "(?d)") && !seenPrefix[2] {
  456. seenPrefix[2] = true
  457. line = line[4:]
  458. ignoreStats["deletable"] += 1
  459. } else {
  460. seenPrefix[0] = false
  461. seenPrefix[1] = false
  462. seenPrefix[2] = false
  463. break
  464. }
  465. }
  466. // Noops, remove
  467. line = strings.TrimSuffix(line, "**")
  468. line = strings.TrimPrefix(line, "**/")
  469. if strings.HasPrefix(line, "/") {
  470. ignoreStats["rooted"] += 1
  471. } else if strings.HasPrefix(line, "#include ") {
  472. ignoreStats["includes"] += 1
  473. if strings.Contains(line, "..") {
  474. ignoreStats["escapedIncludes"] += 1
  475. }
  476. }
  477. if strings.Contains(line, "**") {
  478. ignoreStats["doubleStars"] += 1
  479. // Remove not to trip up star checks.
  480. line = strings.Replace(line, "**", "", -1)
  481. }
  482. if strings.Contains(line, "*") {
  483. ignoreStats["stars"] += 1
  484. }
  485. }
  486. }
  487. stats["ignoreStats"] = ignoreStats
  488. }
  489. return stats
  490. }
  491. type ConnectionInfo struct {
  492. protocol.Statistics
  493. Connected bool
  494. Paused bool
  495. Address string
  496. ClientVersion string
  497. Type string
  498. Crypto string
  499. }
  500. func (info ConnectionInfo) MarshalJSON() ([]byte, error) {
  501. return json.Marshal(map[string]interface{}{
  502. "at": info.At,
  503. "inBytesTotal": info.InBytesTotal,
  504. "outBytesTotal": info.OutBytesTotal,
  505. "connected": info.Connected,
  506. "paused": info.Paused,
  507. "address": info.Address,
  508. "clientVersion": info.ClientVersion,
  509. "type": info.Type,
  510. "crypto": info.Crypto,
  511. })
  512. }
  513. // ConnectionStats returns a map with connection statistics for each device.
  514. func (m *model) ConnectionStats() map[string]interface{} {
  515. m.fmut.RLock()
  516. m.pmut.RLock()
  517. res := make(map[string]interface{})
  518. devs := m.cfg.Devices()
  519. conns := make(map[string]ConnectionInfo, len(devs))
  520. for device, deviceCfg := range devs {
  521. hello := m.helloMessages[device]
  522. versionString := hello.ClientVersion
  523. if hello.ClientName != "syncthing" {
  524. versionString = hello.ClientName + " " + hello.ClientVersion
  525. }
  526. ci := ConnectionInfo{
  527. ClientVersion: strings.TrimSpace(versionString),
  528. Paused: deviceCfg.Paused,
  529. }
  530. if conn, ok := m.conn[device]; ok {
  531. ci.Type = conn.Type()
  532. ci.Crypto = conn.Crypto()
  533. ci.Connected = ok
  534. ci.Statistics = conn.Statistics()
  535. if addr := conn.RemoteAddr(); addr != nil {
  536. ci.Address = addr.String()
  537. }
  538. }
  539. conns[device.String()] = ci
  540. }
  541. res["connections"] = conns
  542. m.pmut.RUnlock()
  543. m.fmut.RUnlock()
  544. in, out := protocol.TotalInOut()
  545. res["total"] = ConnectionInfo{
  546. Statistics: protocol.Statistics{
  547. At: time.Now(),
  548. InBytesTotal: in,
  549. OutBytesTotal: out,
  550. },
  551. }
  552. return res
  553. }
  554. // DeviceStatistics returns statistics about each device
  555. func (m *model) DeviceStatistics() map[string]stats.DeviceStatistics {
  556. res := make(map[string]stats.DeviceStatistics)
  557. for id := range m.cfg.Devices() {
  558. res[id.String()] = m.deviceStatRef(id).GetStatistics()
  559. }
  560. return res
  561. }
  562. // FolderStatistics returns statistics about each folder
  563. func (m *model) FolderStatistics() map[string]stats.FolderStatistics {
  564. res := make(map[string]stats.FolderStatistics)
  565. for id := range m.cfg.Folders() {
  566. res[id] = m.folderStatRef(id).GetStatistics()
  567. }
  568. return res
  569. }
  570. type FolderCompletion struct {
  571. CompletionPct float64
  572. NeedBytes int64
  573. NeedItems int64
  574. GlobalBytes int64
  575. NeedDeletes int64
  576. }
  577. // Completion returns the completion status, in percent, for the given device
  578. // and folder.
  579. func (m *model) Completion(device protocol.DeviceID, folder string) FolderCompletion {
  580. m.fmut.RLock()
  581. rf, ok := m.folderFiles[folder]
  582. m.fmut.RUnlock()
  583. if !ok {
  584. return FolderCompletion{} // Folder doesn't exist, so we hardly have any of it
  585. }
  586. tot := rf.GlobalSize().Bytes
  587. if tot == 0 {
  588. // Folder is empty, so we have all of it
  589. return FolderCompletion{
  590. CompletionPct: 100,
  591. }
  592. }
  593. m.pmut.RLock()
  594. counts := m.deviceDownloads[device].GetBlockCounts(folder)
  595. m.pmut.RUnlock()
  596. var need, items, fileNeed, downloaded, deletes int64
  597. rf.WithNeedTruncated(device, func(f db.FileIntf) bool {
  598. ft := f.(db.FileInfoTruncated)
  599. // If the file is deleted, we account it only in the deleted column.
  600. if ft.Deleted {
  601. deletes++
  602. return true
  603. }
  604. // This might might be more than it really is, because some blocks can be of a smaller size.
  605. downloaded = int64(counts[ft.Name] * int(ft.BlockSize()))
  606. fileNeed = ft.FileSize() - downloaded
  607. if fileNeed < 0 {
  608. fileNeed = 0
  609. }
  610. need += fileNeed
  611. items++
  612. return true
  613. })
  614. needRatio := float64(need) / float64(tot)
  615. completionPct := 100 * (1 - needRatio)
  616. // If the completion is 100% but there are deletes we need to handle,
  617. // drop it down a notch. Hack for consumers that look only at the
  618. // percentage (our own GUI does the same calculation as here on its own
  619. // and needs the same fixup).
  620. if need == 0 && deletes > 0 {
  621. completionPct = 95 // chosen by fair dice roll
  622. }
  623. l.Debugf("%v Completion(%s, %q): %f (%d / %d = %f)", m, device, folder, completionPct, need, tot, needRatio)
  624. return FolderCompletion{
  625. CompletionPct: completionPct,
  626. NeedBytes: need,
  627. NeedItems: items,
  628. GlobalBytes: tot,
  629. NeedDeletes: deletes,
  630. }
  631. }
  632. func addSizeOfFile(s *db.Counts, f db.FileIntf) {
  633. switch {
  634. case f.IsDeleted():
  635. s.Deleted++
  636. case f.IsDirectory():
  637. s.Directories++
  638. case f.IsSymlink():
  639. s.Symlinks++
  640. default:
  641. s.Files++
  642. }
  643. s.Bytes += f.FileSize()
  644. }
  645. // GlobalSize returns the number of files, deleted files and total bytes for all
  646. // files in the global model.
  647. func (m *model) GlobalSize(folder string) db.Counts {
  648. m.fmut.RLock()
  649. defer m.fmut.RUnlock()
  650. if rf, ok := m.folderFiles[folder]; ok {
  651. return rf.GlobalSize()
  652. }
  653. return db.Counts{}
  654. }
  655. // LocalSize returns the number of files, deleted files and total bytes for all
  656. // files in the local folder.
  657. func (m *model) LocalSize(folder string) db.Counts {
  658. m.fmut.RLock()
  659. defer m.fmut.RUnlock()
  660. if rf, ok := m.folderFiles[folder]; ok {
  661. return rf.LocalSize()
  662. }
  663. return db.Counts{}
  664. }
  665. // ReceiveOnlyChangedSize returns the number of files, deleted files and
  666. // total bytes for all files that have changed locally in a receieve only
  667. // folder.
  668. func (m *model) ReceiveOnlyChangedSize(folder string) db.Counts {
  669. m.fmut.RLock()
  670. defer m.fmut.RUnlock()
  671. if rf, ok := m.folderFiles[folder]; ok {
  672. return rf.ReceiveOnlyChangedSize()
  673. }
  674. return db.Counts{}
  675. }
  676. // NeedSize returns the number and total size of currently needed files.
  677. func (m *model) NeedSize(folder string) db.Counts {
  678. m.fmut.RLock()
  679. defer m.fmut.RUnlock()
  680. var result db.Counts
  681. if rf, ok := m.folderFiles[folder]; ok {
  682. cfg := m.folderCfgs[folder]
  683. rf.WithNeedTruncated(protocol.LocalDeviceID, func(f db.FileIntf) bool {
  684. if cfg.IgnoreDelete && f.IsDeleted() {
  685. return true
  686. }
  687. addSizeOfFile(&result, f)
  688. return true
  689. })
  690. }
  691. result.Bytes -= m.progressEmitter.BytesCompleted(folder)
  692. l.Debugf("%v NeedSize(%q): %v", m, folder, result)
  693. return result
  694. }
  695. // NeedFolderFiles returns paginated list of currently needed files in
  696. // progress, queued, and to be queued on next puller iteration, as well as the
  697. // total number of files currently needed.
  698. func (m *model) NeedFolderFiles(folder string, page, perpage int) ([]db.FileInfoTruncated, []db.FileInfoTruncated, []db.FileInfoTruncated) {
  699. m.fmut.RLock()
  700. defer m.fmut.RUnlock()
  701. rf, ok := m.folderFiles[folder]
  702. if !ok {
  703. return nil, nil, nil
  704. }
  705. var progress, queued, rest []db.FileInfoTruncated
  706. var seen map[string]struct{}
  707. skip := (page - 1) * perpage
  708. get := perpage
  709. runner, ok := m.folderRunners[folder]
  710. if ok {
  711. allProgressNames, allQueuedNames := runner.Jobs()
  712. var progressNames, queuedNames []string
  713. progressNames, skip, get = getChunk(allProgressNames, skip, get)
  714. queuedNames, skip, get = getChunk(allQueuedNames, skip, get)
  715. progress = make([]db.FileInfoTruncated, len(progressNames))
  716. queued = make([]db.FileInfoTruncated, len(queuedNames))
  717. seen = make(map[string]struct{}, len(progressNames)+len(queuedNames))
  718. for i, name := range progressNames {
  719. if f, ok := rf.GetGlobalTruncated(name); ok {
  720. progress[i] = f
  721. seen[name] = struct{}{}
  722. }
  723. }
  724. for i, name := range queuedNames {
  725. if f, ok := rf.GetGlobalTruncated(name); ok {
  726. queued[i] = f
  727. seen[name] = struct{}{}
  728. }
  729. }
  730. }
  731. rest = make([]db.FileInfoTruncated, 0, perpage)
  732. cfg := m.folderCfgs[folder]
  733. rf.WithNeedTruncated(protocol.LocalDeviceID, func(f db.FileIntf) bool {
  734. if cfg.IgnoreDelete && f.IsDeleted() {
  735. return true
  736. }
  737. if skip > 0 {
  738. skip--
  739. return true
  740. }
  741. ft := f.(db.FileInfoTruncated)
  742. if _, ok := seen[ft.Name]; !ok {
  743. rest = append(rest, ft)
  744. get--
  745. }
  746. return get > 0
  747. })
  748. return progress, queued, rest
  749. }
  750. // LocalChangedFiles returns a paginated list of currently needed files in
  751. // progress, queued, and to be queued on next puller iteration, as well as the
  752. // total number of files currently needed.
  753. func (m *model) LocalChangedFiles(folder string, page, perpage int) []db.FileInfoTruncated {
  754. m.fmut.RLock()
  755. defer m.fmut.RUnlock()
  756. rf, ok := m.folderFiles[folder]
  757. if !ok {
  758. return nil
  759. }
  760. fcfg := m.folderCfgs[folder]
  761. if fcfg.Type != config.FolderTypeReceiveOnly {
  762. return nil
  763. }
  764. if rf.ReceiveOnlyChangedSize().TotalItems() == 0 {
  765. return nil
  766. }
  767. files := make([]db.FileInfoTruncated, 0, perpage)
  768. skip := (page - 1) * perpage
  769. get := perpage
  770. rf.WithHaveTruncated(protocol.LocalDeviceID, func(f db.FileIntf) bool {
  771. if !f.IsReceiveOnlyChanged() {
  772. return true
  773. }
  774. if skip > 0 {
  775. skip--
  776. return true
  777. }
  778. ft := f.(db.FileInfoTruncated)
  779. files = append(files, ft)
  780. get--
  781. return get > 0
  782. })
  783. return files
  784. }
  785. // RemoteNeedFolderFiles returns paginated list of currently needed files in
  786. // progress, queued, and to be queued on next puller iteration, as well as the
  787. // total number of files currently needed.
  788. func (m *model) RemoteNeedFolderFiles(device protocol.DeviceID, folder string, page, perpage int) ([]db.FileInfoTruncated, error) {
  789. m.fmut.RLock()
  790. m.pmut.RLock()
  791. if err := m.checkDeviceFolderConnectedLocked(device, folder); err != nil {
  792. m.pmut.RUnlock()
  793. m.fmut.RUnlock()
  794. return nil, err
  795. }
  796. rf := m.folderFiles[folder]
  797. m.pmut.RUnlock()
  798. m.fmut.RUnlock()
  799. files := make([]db.FileInfoTruncated, 0, perpage)
  800. skip := (page - 1) * perpage
  801. get := perpage
  802. rf.WithNeedTruncated(device, func(f db.FileIntf) bool {
  803. if skip > 0 {
  804. skip--
  805. return true
  806. }
  807. files = append(files, f.(db.FileInfoTruncated))
  808. get--
  809. return get > 0
  810. })
  811. return files, nil
  812. }
  813. // Index is called when a new device is connected and we receive their full index.
  814. // Implements the protocol.Model interface.
  815. func (m *model) Index(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo) {
  816. m.handleIndex(deviceID, folder, fs, false)
  817. }
  818. // IndexUpdate is called for incremental updates to connected devices' indexes.
  819. // Implements the protocol.Model interface.
  820. func (m *model) IndexUpdate(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo) {
  821. m.handleIndex(deviceID, folder, fs, true)
  822. }
  823. func (m *model) handleIndex(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo, update bool) {
  824. op := "Index"
  825. if update {
  826. op += " update"
  827. }
  828. l.Debugf("%v (in): %s / %q: %d files", op, deviceID, folder, len(fs))
  829. if cfg, ok := m.cfg.Folder(folder); !ok || !cfg.SharedWith(deviceID) {
  830. l.Infof("%v for unexpected folder ID %q sent from device %q; ensure that the folder exists and that this device is selected under \"Share With\" in the folder configuration.", op, folder, deviceID)
  831. return
  832. } else if cfg.Paused {
  833. l.Debugf("%v for paused folder (ID %q) sent from device %q.", op, folder, deviceID)
  834. return
  835. }
  836. m.fmut.RLock()
  837. files, existing := m.folderFiles[folder]
  838. runner, running := m.folderRunners[folder]
  839. m.fmut.RUnlock()
  840. if !existing {
  841. panic(fmt.Sprintf("%v for nonexistent folder %q", op, folder))
  842. }
  843. if running {
  844. defer runner.SchedulePull()
  845. } else if update {
  846. // Runner may legitimately not be set if this is the "cleanup" Index
  847. // message at startup.
  848. panic(fmt.Sprintf("%v for not running folder %q", op, folder))
  849. }
  850. m.pmut.RLock()
  851. m.deviceDownloads[deviceID].Update(folder, makeForgetUpdate(fs))
  852. m.pmut.RUnlock()
  853. if !update {
  854. files.Drop(deviceID)
  855. }
  856. for i := range fs {
  857. // The local flags should never be transmitted over the wire. Make
  858. // sure they look like they weren't.
  859. fs[i].LocalFlags = 0
  860. }
  861. files.Update(deviceID, fs)
  862. events.Default.Log(events.RemoteIndexUpdated, map[string]interface{}{
  863. "device": deviceID.String(),
  864. "folder": folder,
  865. "items": len(fs),
  866. "version": files.Sequence(deviceID),
  867. })
  868. }
  869. func (m *model) ClusterConfig(deviceID protocol.DeviceID, cm protocol.ClusterConfig) {
  870. // Check the peer device's announced folders against our own. Emits events
  871. // for folders that we don't expect (unknown or not shared).
  872. // Also, collect a list of folders we do share, and if he's interested in
  873. // temporary indexes, subscribe the connection.
  874. tempIndexFolders := make([]string, 0, len(cm.Folders))
  875. m.pmut.RLock()
  876. conn, ok := m.conn[deviceID]
  877. hello := m.helloMessages[deviceID]
  878. m.pmut.RUnlock()
  879. if !ok {
  880. panic("bug: ClusterConfig called on closed or nonexistent connection")
  881. }
  882. changed := false
  883. deviceCfg := m.cfg.Devices()[deviceID]
  884. // See issue #3802 - in short, we can't send modern symlink entries to older
  885. // clients.
  886. dropSymlinks := false
  887. if hello.ClientName == m.clientName && upgrade.CompareVersions(hello.ClientVersion, "v0.14.14") < 0 {
  888. l.Warnln("Not sending symlinks to old client", deviceID, "- please upgrade to v0.14.14 or newer")
  889. dropSymlinks = true
  890. }
  891. // Needs to happen outside of the fmut, as can cause CommitConfiguration
  892. if deviceCfg.AutoAcceptFolders {
  893. for _, folder := range cm.Folders {
  894. changed = m.handleAutoAccepts(deviceCfg, folder) || changed
  895. }
  896. }
  897. m.fmut.Lock()
  898. var paused []string
  899. for _, folder := range cm.Folders {
  900. cfg, ok := m.cfg.Folder(folder.ID)
  901. if !ok || !cfg.SharedWith(deviceID) {
  902. if deviceCfg.IgnoredFolder(folder.ID) {
  903. l.Infof("Ignoring folder %s from device %s since we are configured to", folder.Description(), deviceID)
  904. continue
  905. }
  906. m.cfg.AddOrUpdatePendingFolder(folder.ID, folder.Label, deviceID)
  907. events.Default.Log(events.FolderRejected, map[string]string{
  908. "folder": folder.ID,
  909. "folderLabel": folder.Label,
  910. "device": deviceID.String(),
  911. })
  912. l.Infof("Unexpected folder %s sent from device %q; ensure that the folder exists and that this device is selected under \"Share With\" in the folder configuration.", folder.Description(), deviceID)
  913. continue
  914. }
  915. if folder.Paused {
  916. paused = append(paused, folder.ID)
  917. continue
  918. }
  919. if cfg.Paused {
  920. continue
  921. }
  922. fs, ok := m.folderFiles[folder.ID]
  923. if !ok {
  924. // Shouldn't happen because !cfg.Paused, but might happen
  925. // if the folder is about to be unpaused, but not yet.
  926. continue
  927. }
  928. if !folder.DisableTempIndexes {
  929. tempIndexFolders = append(tempIndexFolders, folder.ID)
  930. }
  931. myIndexID := fs.IndexID(protocol.LocalDeviceID)
  932. mySequence := fs.Sequence(protocol.LocalDeviceID)
  933. var startSequence int64
  934. for _, dev := range folder.Devices {
  935. if dev.ID == m.id {
  936. // This is the other side's description of what it knows
  937. // about us. Lets check to see if we can start sending index
  938. // updates directly or need to send the index from start...
  939. if dev.IndexID == myIndexID {
  940. // They say they've seen our index ID before, so we can
  941. // send a delta update only.
  942. if dev.MaxSequence > mySequence {
  943. // Safety check. They claim to have more or newer
  944. // index data than we have - either we have lost
  945. // index data, or reset the index without resetting
  946. // the IndexID, or something else weird has
  947. // happened. We send a full index to reset the
  948. // situation.
  949. l.Infof("Device %v folder %s is delta index compatible, but seems out of sync with reality", deviceID, folder.Description())
  950. startSequence = 0
  951. continue
  952. }
  953. l.Debugf("Device %v folder %s is delta index compatible (mlv=%d)", deviceID, folder.Description(), dev.MaxSequence)
  954. startSequence = dev.MaxSequence
  955. } else if dev.IndexID != 0 {
  956. // They say they've seen an index ID from us, but it's
  957. // not the right one. Either they are confused or we
  958. // must have reset our database since last talking to
  959. // them. We'll start with a full index transfer.
  960. l.Infof("Device %v folder %s has mismatching index ID for us (%v != %v)", deviceID, folder.Description(), dev.IndexID, myIndexID)
  961. startSequence = 0
  962. }
  963. } else if dev.ID == deviceID && dev.IndexID != 0 {
  964. // This is the other side's description of themselves. We
  965. // check to see that it matches the IndexID we have on file,
  966. // otherwise we drop our old index data and expect to get a
  967. // completely new set.
  968. theirIndexID := fs.IndexID(deviceID)
  969. if dev.IndexID == 0 {
  970. // They're not announcing an index ID. This means they
  971. // do not support delta indexes and we should clear any
  972. // information we have from them before accepting their
  973. // index, which will presumably be a full index.
  974. fs.Drop(deviceID)
  975. } else if dev.IndexID != theirIndexID {
  976. // The index ID we have on file is not what they're
  977. // announcing. They must have reset their database and
  978. // will probably send us a full index. We drop any
  979. // information we have and remember this new index ID
  980. // instead.
  981. l.Infof("Device %v folder %s has a new index ID (%v)", deviceID, folder.Description(), dev.IndexID)
  982. fs.Drop(deviceID)
  983. fs.SetIndexID(deviceID, dev.IndexID)
  984. } else {
  985. // They're sending a recognized index ID and will most
  986. // likely use delta indexes. We might already have files
  987. // that we need to pull so let the folder runner know
  988. // that it should recheck the index data.
  989. if runner := m.folderRunners[folder.ID]; runner != nil {
  990. defer runner.SchedulePull()
  991. }
  992. }
  993. }
  994. }
  995. go sendIndexes(conn, folder.ID, fs, m.folderIgnores[folder.ID], startSequence, dropSymlinks)
  996. }
  997. m.pmut.Lock()
  998. m.remotePausedFolders[deviceID] = paused
  999. m.pmut.Unlock()
  1000. // This breaks if we send multiple CM messages during the same connection.
  1001. if len(tempIndexFolders) > 0 {
  1002. m.pmut.RLock()
  1003. conn, ok := m.conn[deviceID]
  1004. m.pmut.RUnlock()
  1005. // In case we've got ClusterConfig, and the connection disappeared
  1006. // from infront of our nose.
  1007. if ok {
  1008. m.progressEmitter.temporaryIndexSubscribe(conn, tempIndexFolders)
  1009. }
  1010. }
  1011. if deviceCfg.Introducer {
  1012. foldersDevices, introduced := m.handleIntroductions(deviceCfg, cm)
  1013. if introduced {
  1014. changed = true
  1015. }
  1016. // If permitted, check if the introducer has unshare devices/folders with
  1017. // some of the devices/folders that we know were introduced to us by him.
  1018. if !deviceCfg.SkipIntroductionRemovals && m.handleDeintroductions(deviceCfg, cm, foldersDevices) {
  1019. changed = true
  1020. }
  1021. }
  1022. m.fmut.Unlock()
  1023. if changed {
  1024. if err := m.cfg.Save(); err != nil {
  1025. l.Warnln("Failed to save config", err)
  1026. }
  1027. }
  1028. }
  1029. // handleIntroductions handles adding devices/shares that are shared by an introducer device
  1030. func (m *model) handleIntroductions(introducerCfg config.DeviceConfiguration, cm protocol.ClusterConfig) (folderDeviceSet, bool) {
  1031. // This device is an introducer. Go through the announced lists of folders
  1032. // and devices and add what we are missing, remove what we have extra that
  1033. // has been introducer by the introducer.
  1034. changed := false
  1035. foldersDevices := make(folderDeviceSet)
  1036. for _, folder := range cm.Folders {
  1037. // Adds devices which we do not have, but the introducer has
  1038. // for the folders that we have in common. Also, shares folders
  1039. // with devices that we have in common, yet are currently not sharing
  1040. // the folder.
  1041. fcfg, ok := m.cfg.Folder(folder.ID)
  1042. if !ok {
  1043. // Don't have this folder, carry on.
  1044. continue
  1045. }
  1046. for _, device := range folder.Devices {
  1047. // No need to share with self.
  1048. if device.ID == m.id {
  1049. continue
  1050. }
  1051. foldersDevices.set(device.ID, folder.ID)
  1052. if _, ok := m.cfg.Devices()[device.ID]; !ok {
  1053. // The device is currently unknown. Add it to the config.
  1054. m.introduceDevice(device, introducerCfg)
  1055. } else if fcfg.SharedWith(device.ID) {
  1056. // We already share the folder with this device, so
  1057. // nothing to do.
  1058. continue
  1059. }
  1060. // We don't yet share this folder with this device. Add the device
  1061. // to sharing list of the folder.
  1062. l.Infof("Sharing folder %s with %v (vouched for by introducer %v)", folder.Description(), device.ID, introducerCfg.DeviceID)
  1063. fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{
  1064. DeviceID: device.ID,
  1065. IntroducedBy: introducerCfg.DeviceID,
  1066. })
  1067. changed = true
  1068. }
  1069. if changed {
  1070. m.cfg.SetFolder(fcfg)
  1071. }
  1072. }
  1073. return foldersDevices, changed
  1074. }
  1075. // handleDeintroductions handles removals of devices/shares that are removed by an introducer device
  1076. func (m *model) handleDeintroductions(introducerCfg config.DeviceConfiguration, cm protocol.ClusterConfig, foldersDevices folderDeviceSet) bool {
  1077. changed := false
  1078. devicesNotIntroduced := make(map[protocol.DeviceID]struct{})
  1079. folders := m.cfg.FolderList()
  1080. // Check if we should unshare some folders, if the introducer has unshared them.
  1081. for i := range folders {
  1082. for k := 0; k < len(folders[i].Devices); k++ {
  1083. if folders[i].Devices[k].IntroducedBy != introducerCfg.DeviceID {
  1084. devicesNotIntroduced[folders[i].Devices[k].DeviceID] = struct{}{}
  1085. continue
  1086. }
  1087. if !foldersDevices.has(folders[i].Devices[k].DeviceID, folders[i].ID) {
  1088. // We could not find that folder shared on the
  1089. // introducer with the device that was introduced to us.
  1090. // We should follow and unshare as well.
  1091. l.Infof("Unsharing folder %s with %v as introducer %v no longer shares the folder with that device", folders[i].Description(), folders[i].Devices[k].DeviceID, folders[i].Devices[k].IntroducedBy)
  1092. folders[i].Devices = append(folders[i].Devices[:k], folders[i].Devices[k+1:]...)
  1093. k--
  1094. changed = true
  1095. }
  1096. }
  1097. }
  1098. // Check if we should remove some devices, if the introducer no longer
  1099. // shares any folder with them. Yet do not remove if we share other
  1100. // folders that haven't been introduced by the introducer.
  1101. devMap := m.cfg.Devices()
  1102. devices := make([]config.DeviceConfiguration, 0, len(devMap))
  1103. for deviceID, device := range devMap {
  1104. if device.IntroducedBy == introducerCfg.DeviceID {
  1105. if !foldersDevices.hasDevice(deviceID) {
  1106. if _, ok := devicesNotIntroduced[deviceID]; !ok {
  1107. // The introducer no longer shares any folder with the
  1108. // device, remove the device.
  1109. l.Infof("Removing device %v as introducer %v no longer shares any folders with that device", deviceID, device.IntroducedBy)
  1110. changed = true
  1111. continue
  1112. }
  1113. l.Infof("Would have removed %v as %v no longer shares any folders, yet there are other folders that are shared with this device that haven't been introduced by this introducer.", deviceID, device.IntroducedBy)
  1114. }
  1115. }
  1116. devices = append(devices, device)
  1117. }
  1118. if changed {
  1119. cfg := m.cfg.RawCopy()
  1120. cfg.Folders = folders
  1121. cfg.Devices = devices
  1122. m.cfg.Replace(cfg)
  1123. }
  1124. return changed
  1125. }
  1126. // handleAutoAccepts handles adding and sharing folders for devices that have
  1127. // AutoAcceptFolders set to true.
  1128. func (m *model) handleAutoAccepts(deviceCfg config.DeviceConfiguration, folder protocol.Folder) bool {
  1129. if cfg, ok := m.cfg.Folder(folder.ID); !ok {
  1130. defaultPath := m.cfg.Options().DefaultFolderPath
  1131. defaultPathFs := fs.NewFilesystem(fs.FilesystemTypeBasic, defaultPath)
  1132. pathAlternatives := []string{
  1133. sanitizePath(folder.Label),
  1134. sanitizePath(folder.ID),
  1135. }
  1136. for _, path := range pathAlternatives {
  1137. if _, err := defaultPathFs.Lstat(path); !fs.IsNotExist(err) {
  1138. continue
  1139. }
  1140. fcfg := config.NewFolderConfiguration(m.id, folder.ID, folder.Label, fs.FilesystemTypeBasic, filepath.Join(defaultPath, path))
  1141. fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{
  1142. DeviceID: deviceCfg.DeviceID,
  1143. })
  1144. // Need to wait for the waiter, as this calls CommitConfiguration,
  1145. // which sets up the folder and as we return from this call,
  1146. // ClusterConfig starts poking at m.folderFiles and other things
  1147. // that might not exist until the config is committed.
  1148. w, _ := m.cfg.SetFolder(fcfg)
  1149. w.Wait()
  1150. l.Infof("Auto-accepted %s folder %s at path %s", deviceCfg.DeviceID, folder.Description(), fcfg.Path)
  1151. return true
  1152. }
  1153. l.Infof("Failed to auto-accept folder %s from %s due to path conflict", folder.Description(), deviceCfg.DeviceID)
  1154. return false
  1155. } else {
  1156. for _, device := range cfg.DeviceIDs() {
  1157. if device == deviceCfg.DeviceID {
  1158. // Already shared nothing todo.
  1159. return false
  1160. }
  1161. }
  1162. cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{
  1163. DeviceID: deviceCfg.DeviceID,
  1164. })
  1165. w, _ := m.cfg.SetFolder(cfg)
  1166. w.Wait()
  1167. l.Infof("Shared %s with %s due to auto-accept", folder.ID, deviceCfg.DeviceID)
  1168. return true
  1169. }
  1170. }
  1171. func (m *model) introduceDevice(device protocol.Device, introducerCfg config.DeviceConfiguration) {
  1172. addresses := []string{"dynamic"}
  1173. for _, addr := range device.Addresses {
  1174. if addr != "dynamic" {
  1175. addresses = append(addresses, addr)
  1176. }
  1177. }
  1178. l.Infof("Adding device %v to config (vouched for by introducer %v)", device.ID, introducerCfg.DeviceID)
  1179. newDeviceCfg := config.DeviceConfiguration{
  1180. DeviceID: device.ID,
  1181. Name: device.Name,
  1182. Compression: introducerCfg.Compression,
  1183. Addresses: addresses,
  1184. CertName: device.CertName,
  1185. IntroducedBy: introducerCfg.DeviceID,
  1186. }
  1187. // The introducers' introducers are also our introducers.
  1188. if device.Introducer {
  1189. l.Infof("Device %v is now also an introducer", device.ID)
  1190. newDeviceCfg.Introducer = true
  1191. newDeviceCfg.SkipIntroductionRemovals = device.SkipIntroductionRemovals
  1192. }
  1193. m.cfg.SetDevice(newDeviceCfg)
  1194. }
  1195. // Closed is called when a connection has been closed
  1196. func (m *model) Closed(conn protocol.Connection, err error) {
  1197. device := conn.ID()
  1198. m.pmut.Lock()
  1199. conn, ok := m.conn[device]
  1200. if ok {
  1201. m.progressEmitter.temporaryIndexUnsubscribe(conn)
  1202. }
  1203. delete(m.conn, device)
  1204. delete(m.connRequestLimiters, device)
  1205. delete(m.helloMessages, device)
  1206. delete(m.deviceDownloads, device)
  1207. delete(m.remotePausedFolders, device)
  1208. closed := m.closed[device]
  1209. delete(m.closed, device)
  1210. m.pmut.Unlock()
  1211. l.Infof("Connection to %s at %s closed: %v", device, conn.Name(), err)
  1212. events.Default.Log(events.DeviceDisconnected, map[string]string{
  1213. "id": device.String(),
  1214. "error": err.Error(),
  1215. })
  1216. close(closed)
  1217. }
  1218. // close will close the underlying connection for a given device
  1219. func (m *model) close(device protocol.DeviceID, err error) {
  1220. m.pmut.Lock()
  1221. m.closeLocked(device, err)
  1222. m.pmut.Unlock()
  1223. }
  1224. // closeLocked will close the underlying connection for a given device
  1225. func (m *model) closeLocked(device protocol.DeviceID, err error) {
  1226. conn, ok := m.conn[device]
  1227. if !ok {
  1228. // There is no connection to close
  1229. return
  1230. }
  1231. conn.Close(err)
  1232. }
  1233. // Implements protocol.RequestResponse
  1234. type requestResponse struct {
  1235. data []byte
  1236. closed chan struct{}
  1237. once stdsync.Once
  1238. }
  1239. func newRequestResponse(size int) *requestResponse {
  1240. return &requestResponse{
  1241. data: protocol.BufferPool.Get(size),
  1242. closed: make(chan struct{}),
  1243. }
  1244. }
  1245. func (r *requestResponse) Data() []byte {
  1246. return r.data
  1247. }
  1248. func (r *requestResponse) Close() {
  1249. r.once.Do(func() {
  1250. protocol.BufferPool.Put(r.data)
  1251. close(r.closed)
  1252. })
  1253. }
  1254. func (r *requestResponse) Wait() {
  1255. <-r.closed
  1256. }
  1257. // Request returns the specified data segment by reading it from local disk.
  1258. // Implements the protocol.Model interface.
  1259. func (m *model) Request(deviceID protocol.DeviceID, folder, name string, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (out protocol.RequestResponse, err error) {
  1260. if size < 0 || offset < 0 {
  1261. return nil, protocol.ErrInvalid
  1262. }
  1263. m.fmut.RLock()
  1264. folderCfg, ok := m.folderCfgs[folder]
  1265. folderIgnores := m.folderIgnores[folder]
  1266. m.fmut.RUnlock()
  1267. if !ok {
  1268. // The folder might be already unpaused in the config, but not yet
  1269. // in the model.
  1270. l.Debugf("Request from %s for file %s in unstarted folder %q", deviceID, name, folder)
  1271. return nil, protocol.ErrInvalid
  1272. }
  1273. if !folderCfg.SharedWith(deviceID) {
  1274. l.Warnf("Request from %s for file %s in unshared folder %q", deviceID, name, folder)
  1275. return nil, protocol.ErrNoSuchFile
  1276. }
  1277. if folderCfg.Paused {
  1278. l.Debugf("Request from %s for file %s in paused folder %q", deviceID, name, folder)
  1279. return nil, protocol.ErrInvalid
  1280. }
  1281. // Make sure the path is valid and in canonical form
  1282. if name, err = fs.Canonicalize(name); err != nil {
  1283. l.Debugf("Request from %s in folder %q for invalid filename %s", deviceID, folder, name)
  1284. return nil, protocol.ErrInvalid
  1285. }
  1286. if deviceID != protocol.LocalDeviceID {
  1287. l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d t=%v", m, deviceID, folder, name, offset, size, fromTemporary)
  1288. }
  1289. if fs.IsInternal(name) {
  1290. l.Debugf("%v REQ(in) for internal file: %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size)
  1291. return nil, protocol.ErrNoSuchFile
  1292. }
  1293. if folderIgnores.Match(name).IsIgnored() {
  1294. l.Debugf("%v REQ(in) for ignored file: %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size)
  1295. return nil, protocol.ErrNoSuchFile
  1296. }
  1297. folderFs := folderCfg.Filesystem()
  1298. if err := osutil.TraversesSymlink(folderFs, filepath.Dir(name)); err != nil {
  1299. l.Debugf("%v REQ(in) traversal check: %s - %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size)
  1300. return nil, protocol.ErrNoSuchFile
  1301. }
  1302. // Restrict parallel requests by connection/device
  1303. m.pmut.RLock()
  1304. limiter := m.connRequestLimiters[deviceID]
  1305. m.pmut.RUnlock()
  1306. if limiter != nil {
  1307. limiter.take(int(size))
  1308. }
  1309. // The requestResponse releases the bytes to the limiter when its Close method is called.
  1310. res := newRequestResponse(int(size))
  1311. defer func() {
  1312. // Close it ourselves if it isn't returned due to an error
  1313. if err != nil {
  1314. res.Close()
  1315. }
  1316. }()
  1317. if limiter != nil {
  1318. go func() {
  1319. res.Wait()
  1320. limiter.give(int(size))
  1321. }()
  1322. }
  1323. // Only check temp files if the flag is set, and if we are set to advertise
  1324. // the temp indexes.
  1325. if fromTemporary && !folderCfg.DisableTempIndexes {
  1326. tempFn := fs.TempName(name)
  1327. if info, err := folderFs.Lstat(tempFn); err != nil || !info.IsRegular() {
  1328. // Reject reads for anything that doesn't exist or is something
  1329. // other than a regular file.
  1330. l.Debugf("%v REQ(in) failed stating temp file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size)
  1331. return nil, protocol.ErrNoSuchFile
  1332. }
  1333. err := readOffsetIntoBuf(folderFs, tempFn, offset, res.data)
  1334. if err == nil && scanner.Validate(res.data, hash, weakHash) {
  1335. return res, nil
  1336. }
  1337. // Fall through to reading from a non-temp file, just incase the temp
  1338. // file has finished downloading.
  1339. }
  1340. if info, err := folderFs.Lstat(name); err != nil || !info.IsRegular() {
  1341. // Reject reads for anything that doesn't exist or is something
  1342. // other than a regular file.
  1343. l.Debugf("%v REQ(in) failed stating file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size)
  1344. return nil, protocol.ErrNoSuchFile
  1345. }
  1346. if err := readOffsetIntoBuf(folderFs, name, offset, res.data); fs.IsNotExist(err) {
  1347. l.Debugf("%v REQ(in) file doesn't exist: %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size)
  1348. return nil, protocol.ErrNoSuchFile
  1349. } else if err != nil {
  1350. l.Debugf("%v REQ(in) failed reading file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size)
  1351. return nil, protocol.ErrGeneric
  1352. }
  1353. if !scanner.Validate(res.data, hash, weakHash) {
  1354. m.recheckFile(deviceID, folderFs, folder, name, int(offset)/int(size), hash)
  1355. l.Debugf("%v REQ(in) failed validating data (%v): %s: %q / %q o=%d s=%d", m, err, deviceID, folder, name, offset, size)
  1356. return nil, protocol.ErrNoSuchFile
  1357. }
  1358. return res, nil
  1359. }
  1360. func (m *model) recheckFile(deviceID protocol.DeviceID, folderFs fs.Filesystem, folder, name string, blockIndex int, hash []byte) {
  1361. cf, ok := m.CurrentFolderFile(folder, name)
  1362. if !ok {
  1363. l.Debugf("%v recheckFile: %s: %q / %q: no current file", m, deviceID, folder, name)
  1364. return
  1365. }
  1366. if cf.IsDeleted() || cf.IsInvalid() || cf.IsSymlink() || cf.IsDirectory() {
  1367. l.Debugf("%v recheckFile: %s: %q / %q: not a regular file", m, deviceID, folder, name)
  1368. return
  1369. }
  1370. if blockIndex >= len(cf.Blocks) {
  1371. l.Debugf("%v recheckFile: %s: %q / %q i=%d: block index too far", m, deviceID, folder, name, blockIndex)
  1372. return
  1373. }
  1374. block := cf.Blocks[blockIndex]
  1375. // Seems to want a different version of the file, whatever.
  1376. if !bytes.Equal(block.Hash, hash) {
  1377. l.Debugf("%v recheckFile: %s: %q / %q i=%d: hash mismatch %x != %x", m, deviceID, folder, name, blockIndex, block.Hash, hash)
  1378. return
  1379. }
  1380. // The hashes provided part of the request match what we expect to find according
  1381. // to what we have in the database, yet the content we've read off the filesystem doesn't
  1382. // Something is fishy, invalidate the file and rescan it.
  1383. cf.SetMustRescan(m.shortID)
  1384. // Update the index and tell others
  1385. // The file will temporarily become invalid, which is ok as the content is messed up.
  1386. m.updateLocalsFromScanning(folder, []protocol.FileInfo{cf})
  1387. if err := m.ScanFolderSubdirs(folder, []string{name}); err != nil {
  1388. l.Debugf("%v recheckFile: %s: %q / %q rescan: %s", m, deviceID, folder, name, err)
  1389. } else {
  1390. l.Debugf("%v recheckFile: %s: %q / %q", m, deviceID, folder, name)
  1391. }
  1392. }
  1393. func (m *model) CurrentFolderFile(folder string, file string) (protocol.FileInfo, bool) {
  1394. m.fmut.RLock()
  1395. fs, ok := m.folderFiles[folder]
  1396. m.fmut.RUnlock()
  1397. if !ok {
  1398. return protocol.FileInfo{}, false
  1399. }
  1400. return fs.Get(protocol.LocalDeviceID, file)
  1401. }
  1402. func (m *model) CurrentGlobalFile(folder string, file string) (protocol.FileInfo, bool) {
  1403. m.fmut.RLock()
  1404. fs, ok := m.folderFiles[folder]
  1405. m.fmut.RUnlock()
  1406. if !ok {
  1407. return protocol.FileInfo{}, false
  1408. }
  1409. return fs.GetGlobal(file)
  1410. }
  1411. type cFiler struct {
  1412. m Model
  1413. r string
  1414. }
  1415. // Implements scanner.CurrentFiler
  1416. func (cf cFiler) CurrentFile(file string) (protocol.FileInfo, bool) {
  1417. return cf.m.CurrentFolderFile(cf.r, file)
  1418. }
  1419. // Connection returns the current connection for device, and a boolean whether a connection was found.
  1420. func (m *model) Connection(deviceID protocol.DeviceID) (connections.Connection, bool) {
  1421. m.pmut.RLock()
  1422. cn, ok := m.conn[deviceID]
  1423. m.pmut.RUnlock()
  1424. if ok {
  1425. m.deviceWasSeen(deviceID)
  1426. }
  1427. return cn, ok
  1428. }
  1429. func (m *model) GetIgnores(folder string) ([]string, []string, error) {
  1430. m.fmut.RLock()
  1431. defer m.fmut.RUnlock()
  1432. cfg, ok := m.folderCfgs[folder]
  1433. if !ok {
  1434. cfg, ok = m.cfg.Folders()[folder]
  1435. if !ok {
  1436. return nil, nil, fmt.Errorf("Folder %s does not exist", folder)
  1437. }
  1438. }
  1439. // On creation a new folder with ignore patterns validly has no marker yet.
  1440. if err := cfg.CheckPath(); err != nil && err != config.ErrMarkerMissing {
  1441. return nil, nil, err
  1442. }
  1443. ignores, ok := m.folderIgnores[folder]
  1444. if !ok {
  1445. ignores = ignore.New(fs.NewFilesystem(cfg.FilesystemType, cfg.Path))
  1446. }
  1447. if err := ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) {
  1448. return nil, nil, err
  1449. }
  1450. return ignores.Lines(), ignores.Patterns(), nil
  1451. }
  1452. func (m *model) SetIgnores(folder string, content []string) error {
  1453. cfg, ok := m.cfg.Folders()[folder]
  1454. if !ok {
  1455. return fmt.Errorf("folder %s does not exist", cfg.Description())
  1456. }
  1457. err := cfg.CheckPath()
  1458. if err == config.ErrPathMissing {
  1459. if err = cfg.CreateRoot(); err != nil {
  1460. return fmt.Errorf("failed to create folder root: %v", err)
  1461. }
  1462. err = cfg.CheckPath()
  1463. }
  1464. if err != nil && err != config.ErrMarkerMissing {
  1465. return err
  1466. }
  1467. if err := ignore.WriteIgnores(cfg.Filesystem(), ".stignore", content); err != nil {
  1468. l.Warnln("Saving .stignore:", err)
  1469. return err
  1470. }
  1471. m.fmut.RLock()
  1472. runner, ok := m.folderRunners[folder]
  1473. m.fmut.RUnlock()
  1474. if ok {
  1475. return runner.Scan(nil)
  1476. }
  1477. return nil
  1478. }
  1479. // OnHello is called when an device connects to us.
  1480. // This allows us to extract some information from the Hello message
  1481. // and add it to a list of known devices ahead of any checks.
  1482. func (m *model) OnHello(remoteID protocol.DeviceID, addr net.Addr, hello protocol.HelloResult) error {
  1483. if m.cfg.IgnoredDevice(remoteID) {
  1484. return errDeviceIgnored
  1485. }
  1486. cfg, ok := m.cfg.Device(remoteID)
  1487. if !ok {
  1488. m.cfg.AddOrUpdatePendingDevice(remoteID, hello.DeviceName, addr.String())
  1489. events.Default.Log(events.DeviceRejected, map[string]string{
  1490. "name": hello.DeviceName,
  1491. "device": remoteID.String(),
  1492. "address": addr.String(),
  1493. })
  1494. return errDeviceUnknown
  1495. }
  1496. if cfg.Paused {
  1497. return errDevicePaused
  1498. }
  1499. if len(cfg.AllowedNetworks) > 0 {
  1500. if !connections.IsAllowedNetwork(addr.String(), cfg.AllowedNetworks) {
  1501. return errNetworkNotAllowed
  1502. }
  1503. }
  1504. return nil
  1505. }
  1506. // GetHello is called when we are about to connect to some remote device.
  1507. func (m *model) GetHello(id protocol.DeviceID) protocol.HelloIntf {
  1508. name := ""
  1509. if _, ok := m.cfg.Device(id); ok {
  1510. name = m.cfg.MyName()
  1511. }
  1512. return &protocol.Hello{
  1513. DeviceName: name,
  1514. ClientName: m.clientName,
  1515. ClientVersion: m.clientVersion,
  1516. }
  1517. }
  1518. // AddConnection adds a new peer connection to the model. An initial index will
  1519. // be sent to the connected peer, thereafter index updates whenever the local
  1520. // folder changes.
  1521. func (m *model) AddConnection(conn connections.Connection, hello protocol.HelloResult) {
  1522. deviceID := conn.ID()
  1523. device, ok := m.cfg.Device(deviceID)
  1524. if !ok {
  1525. l.Infoln("Trying to add connection to unknown device")
  1526. return
  1527. }
  1528. m.pmut.Lock()
  1529. if oldConn, ok := m.conn[deviceID]; ok {
  1530. l.Infoln("Replacing old connection", oldConn, "with", conn, "for", deviceID)
  1531. // There is an existing connection to this device that we are
  1532. // replacing. We must close the existing connection and wait for the
  1533. // close to complete before adding the new connection. We do the
  1534. // actual close without holding pmut as the connection will call
  1535. // back into Closed() for the cleanup.
  1536. closed := m.closed[deviceID]
  1537. m.pmut.Unlock()
  1538. oldConn.Close(errReplacingConnection)
  1539. <-closed
  1540. m.pmut.Lock()
  1541. }
  1542. m.conn[deviceID] = conn
  1543. m.closed[deviceID] = make(chan struct{})
  1544. m.deviceDownloads[deviceID] = newDeviceDownloadState()
  1545. // 0: default, <0: no limiting
  1546. switch {
  1547. case device.MaxRequestKiB > 0:
  1548. m.connRequestLimiters[deviceID] = newByteSemaphore(1024 * device.MaxRequestKiB)
  1549. case device.MaxRequestKiB == 0:
  1550. m.connRequestLimiters[deviceID] = newByteSemaphore(1024 * defaultPullerPendingKiB)
  1551. }
  1552. m.helloMessages[deviceID] = hello
  1553. event := map[string]string{
  1554. "id": deviceID.String(),
  1555. "deviceName": hello.DeviceName,
  1556. "clientName": hello.ClientName,
  1557. "clientVersion": hello.ClientVersion,
  1558. "type": conn.Type(),
  1559. }
  1560. addr := conn.RemoteAddr()
  1561. if addr != nil {
  1562. event["addr"] = addr.String()
  1563. }
  1564. events.Default.Log(events.DeviceConnected, event)
  1565. l.Infof(`Device %s client is "%s %s" named "%s" at %s`, deviceID, hello.ClientName, hello.ClientVersion, hello.DeviceName, conn)
  1566. conn.Start()
  1567. m.pmut.Unlock()
  1568. // Acquires fmut, so has to be done outside of pmut.
  1569. cm := m.generateClusterConfig(deviceID)
  1570. conn.ClusterConfig(cm)
  1571. if (device.Name == "" || m.cfg.Options().OverwriteRemoteDevNames) && hello.DeviceName != "" {
  1572. device.Name = hello.DeviceName
  1573. m.cfg.SetDevice(device)
  1574. m.cfg.Save()
  1575. }
  1576. m.deviceWasSeen(deviceID)
  1577. }
  1578. func (m *model) DownloadProgress(device protocol.DeviceID, folder string, updates []protocol.FileDownloadProgressUpdate) {
  1579. m.fmut.RLock()
  1580. cfg, ok := m.folderCfgs[folder]
  1581. m.fmut.RUnlock()
  1582. if !ok || cfg.DisableTempIndexes || !cfg.SharedWith(device) {
  1583. return
  1584. }
  1585. m.pmut.RLock()
  1586. m.deviceDownloads[device].Update(folder, updates)
  1587. state := m.deviceDownloads[device].GetBlockCounts(folder)
  1588. m.pmut.RUnlock()
  1589. events.Default.Log(events.RemoteDownloadProgress, map[string]interface{}{
  1590. "device": device.String(),
  1591. "folder": folder,
  1592. "state": state,
  1593. })
  1594. }
  1595. func (m *model) deviceStatRef(deviceID protocol.DeviceID) *stats.DeviceStatisticsReference {
  1596. m.fmut.Lock()
  1597. defer m.fmut.Unlock()
  1598. if sr, ok := m.deviceStatRefs[deviceID]; ok {
  1599. return sr
  1600. }
  1601. sr := stats.NewDeviceStatisticsReference(m.db, deviceID.String())
  1602. m.deviceStatRefs[deviceID] = sr
  1603. return sr
  1604. }
  1605. func (m *model) deviceWasSeen(deviceID protocol.DeviceID) {
  1606. m.deviceStatRef(deviceID).WasSeen()
  1607. }
  1608. func (m *model) folderStatRef(folder string) *stats.FolderStatisticsReference {
  1609. m.fmut.Lock()
  1610. defer m.fmut.Unlock()
  1611. sr, ok := m.folderStatRefs[folder]
  1612. if !ok {
  1613. sr = stats.NewFolderStatisticsReference(m.db, folder)
  1614. m.folderStatRefs[folder] = sr
  1615. }
  1616. return sr
  1617. }
  1618. func (m *model) receivedFile(folder string, file protocol.FileInfo) {
  1619. m.folderStatRef(folder).ReceivedFile(file.Name, file.IsDeleted())
  1620. }
  1621. func sendIndexes(conn protocol.Connection, folder string, fs *db.FileSet, ignores *ignore.Matcher, prevSequence int64, dropSymlinks bool) {
  1622. deviceID := conn.ID()
  1623. var err error
  1624. l.Debugf("Starting sendIndexes for %s to %s at %s (slv=%d)", folder, deviceID, conn, prevSequence)
  1625. defer l.Debugf("Exiting sendIndexes for %s to %s at %s: %v", folder, deviceID, conn, err)
  1626. // We need to send one index, regardless of whether there is something to send or not
  1627. prevSequence, err = sendIndexTo(prevSequence, conn, folder, fs, ignores, dropSymlinks)
  1628. // Subscribe to LocalIndexUpdated (we have new information to send) and
  1629. // DeviceDisconnected (it might be us who disconnected, so we should
  1630. // exit).
  1631. sub := events.Default.Subscribe(events.LocalIndexUpdated | events.DeviceDisconnected)
  1632. defer events.Default.Unsubscribe(sub)
  1633. for err == nil {
  1634. if conn.Closed() {
  1635. // Our work is done.
  1636. return
  1637. }
  1638. // While we have sent a sequence at least equal to the one
  1639. // currently in the database, wait for the local index to update. The
  1640. // local index may update for other folders than the one we are
  1641. // sending for.
  1642. if fs.Sequence(protocol.LocalDeviceID) <= prevSequence {
  1643. sub.Poll(time.Minute)
  1644. continue
  1645. }
  1646. prevSequence, err = sendIndexTo(prevSequence, conn, folder, fs, ignores, dropSymlinks)
  1647. // Wait a short amount of time before entering the next loop. If there
  1648. // are continuous changes happening to the local index, this gives us
  1649. // time to batch them up a little.
  1650. time.Sleep(250 * time.Millisecond)
  1651. }
  1652. }
  1653. // sendIndexTo sends file infos with a sequence number higher than prevSequence and
  1654. // returns the highest sent sequence number.
  1655. func sendIndexTo(prevSequence int64, conn protocol.Connection, folder string, fs *db.FileSet, ignores *ignore.Matcher, dropSymlinks bool) (int64, error) {
  1656. deviceID := conn.ID()
  1657. initial := prevSequence == 0
  1658. batch := newFileInfoBatch(nil)
  1659. batch.flushFn = func(fs []protocol.FileInfo) error {
  1660. l.Debugf("Sending indexes for %s to %s at %s: %d files (<%d bytes)", folder, deviceID, conn, len(batch.infos), batch.size)
  1661. if initial {
  1662. initial = false
  1663. return conn.Index(folder, fs)
  1664. }
  1665. return conn.IndexUpdate(folder, fs)
  1666. }
  1667. var err error
  1668. var f protocol.FileInfo
  1669. fs.WithHaveSequence(prevSequence+1, func(fi db.FileIntf) bool {
  1670. if err = batch.flushIfFull(); err != nil {
  1671. return false
  1672. }
  1673. if shouldDebug() {
  1674. if fi.SequenceNo() < prevSequence+1 {
  1675. panic(fmt.Sprintln("sequence lower than requested, got:", fi.SequenceNo(), ", asked to start at:", prevSequence+1))
  1676. }
  1677. if f.Sequence > 0 && fi.SequenceNo() <= f.Sequence {
  1678. panic(fmt.Sprintln("non-increasing sequence, current:", fi.SequenceNo(), "<= previous:", f.Sequence))
  1679. }
  1680. }
  1681. if shouldDebug() {
  1682. if fi.SequenceNo() < prevSequence+1 {
  1683. panic(fmt.Sprintln("sequence lower than requested, got:", fi.SequenceNo(), ", asked to start at:", prevSequence+1))
  1684. }
  1685. if f.Sequence > 0 && fi.SequenceNo() <= f.Sequence {
  1686. panic(fmt.Sprintln("non-increasing sequence, current:", fi.SequenceNo(), "<= previous:", f.Sequence))
  1687. }
  1688. }
  1689. f = fi.(protocol.FileInfo)
  1690. // Mark the file as invalid if any of the local bad stuff flags are set.
  1691. f.RawInvalid = f.IsInvalid()
  1692. // If the file is marked LocalReceive (i.e., changed locally on a
  1693. // receive only folder) we do not want it to ever become the
  1694. // globally best version, invalid or not.
  1695. if f.IsReceiveOnlyChanged() {
  1696. f.Version = protocol.Vector{}
  1697. }
  1698. f.LocalFlags = 0 // never sent externally
  1699. if dropSymlinks && f.IsSymlink() {
  1700. // Do not send index entries with symlinks to clients that can't
  1701. // handle it. Fixes issue #3802. Once both sides are upgraded, a
  1702. // rescan (i.e., change) of the symlink is required for it to
  1703. // sync again, due to delta indexes.
  1704. return true
  1705. }
  1706. batch.append(f)
  1707. return true
  1708. })
  1709. if err != nil {
  1710. return prevSequence, err
  1711. }
  1712. err = batch.flush()
  1713. // True if there was nothing to be sent
  1714. if f.Sequence == 0 {
  1715. return prevSequence, err
  1716. }
  1717. return f.Sequence, err
  1718. }
  1719. func (m *model) updateLocalsFromScanning(folder string, fs []protocol.FileInfo) {
  1720. m.updateLocals(folder, fs)
  1721. m.fmut.RLock()
  1722. folderCfg := m.folderCfgs[folder]
  1723. m.fmut.RUnlock()
  1724. m.diskChangeDetected(folderCfg, fs, events.LocalChangeDetected)
  1725. }
  1726. func (m *model) updateLocalsFromPulling(folder string, fs []protocol.FileInfo) {
  1727. m.updateLocals(folder, fs)
  1728. m.fmut.RLock()
  1729. folderCfg := m.folderCfgs[folder]
  1730. m.fmut.RUnlock()
  1731. m.diskChangeDetected(folderCfg, fs, events.RemoteChangeDetected)
  1732. }
  1733. func (m *model) updateLocals(folder string, fs []protocol.FileInfo) {
  1734. m.fmut.RLock()
  1735. files := m.folderFiles[folder]
  1736. m.fmut.RUnlock()
  1737. if files == nil {
  1738. // The folder doesn't exist.
  1739. return
  1740. }
  1741. files.Update(protocol.LocalDeviceID, fs)
  1742. filenames := make([]string, len(fs))
  1743. for i, file := range fs {
  1744. filenames[i] = file.Name
  1745. }
  1746. events.Default.Log(events.LocalIndexUpdated, map[string]interface{}{
  1747. "folder": folder,
  1748. "items": len(fs),
  1749. "filenames": filenames,
  1750. "version": files.Sequence(protocol.LocalDeviceID),
  1751. })
  1752. }
  1753. func (m *model) diskChangeDetected(folderCfg config.FolderConfiguration, files []protocol.FileInfo, typeOfEvent events.EventType) {
  1754. for _, file := range files {
  1755. if file.IsInvalid() {
  1756. continue
  1757. }
  1758. objType := "file"
  1759. action := "modified"
  1760. switch {
  1761. case file.IsDeleted():
  1762. action = "deleted"
  1763. // If our local vector is version 1 AND it is the only version
  1764. // vector so far seen for this file then it is a new file. Else if
  1765. // it is > 1 it's not new, and if it is 1 but another shortId
  1766. // version vector exists then it is new for us but created elsewhere
  1767. // so the file is still not new but modified by us. Only if it is
  1768. // truly new do we change this to 'added', else we leave it as
  1769. // 'modified'.
  1770. case len(file.Version.Counters) == 1 && file.Version.Counters[0].Value == 1:
  1771. action = "added"
  1772. }
  1773. if file.IsSymlink() {
  1774. objType = "symlink"
  1775. } else if file.IsDirectory() {
  1776. objType = "dir"
  1777. }
  1778. // Two different events can be fired here based on what EventType is passed into function
  1779. events.Default.Log(typeOfEvent, map[string]string{
  1780. "folder": folderCfg.ID,
  1781. "folderID": folderCfg.ID, // incorrect, deprecated, kept for historical compliance
  1782. "label": folderCfg.Label,
  1783. "action": action,
  1784. "type": objType,
  1785. "path": filepath.FromSlash(file.Name),
  1786. "modifiedBy": file.ModifiedBy.String(),
  1787. })
  1788. }
  1789. }
  1790. func (m *model) requestGlobal(deviceID protocol.DeviceID, folder, name string, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
  1791. m.pmut.RLock()
  1792. nc, ok := m.conn[deviceID]
  1793. m.pmut.RUnlock()
  1794. if !ok {
  1795. return nil, fmt.Errorf("requestGlobal: no such device: %s", deviceID)
  1796. }
  1797. l.Debugf("%v REQ(out): %s: %q / %q o=%d s=%d h=%x wh=%x ft=%t", m, deviceID, folder, name, offset, size, hash, weakHash, fromTemporary)
  1798. return nc.Request(folder, name, offset, size, hash, weakHash, fromTemporary)
  1799. }
  1800. func (m *model) ScanFolders() map[string]error {
  1801. m.fmut.RLock()
  1802. folders := make([]string, 0, len(m.folderCfgs))
  1803. for folder := range m.folderCfgs {
  1804. folders = append(folders, folder)
  1805. }
  1806. m.fmut.RUnlock()
  1807. errors := make(map[string]error, len(m.folderCfgs))
  1808. errorsMut := sync.NewMutex()
  1809. wg := sync.NewWaitGroup()
  1810. wg.Add(len(folders))
  1811. for _, folder := range folders {
  1812. folder := folder
  1813. go func() {
  1814. err := m.ScanFolder(folder)
  1815. if err != nil {
  1816. errorsMut.Lock()
  1817. errors[folder] = err
  1818. errorsMut.Unlock()
  1819. // Potentially sets the error twice, once in the scanner just
  1820. // by doing a check, and once here, if the error returned is
  1821. // the same one as returned by CheckHealth, though
  1822. // duplicate set is handled by setError.
  1823. m.fmut.RLock()
  1824. srv := m.folderRunners[folder]
  1825. m.fmut.RUnlock()
  1826. srv.setError(err)
  1827. }
  1828. wg.Done()
  1829. }()
  1830. }
  1831. wg.Wait()
  1832. return errors
  1833. }
  1834. func (m *model) ScanFolder(folder string) error {
  1835. return m.ScanFolderSubdirs(folder, nil)
  1836. }
  1837. func (m *model) ScanFolderSubdirs(folder string, subs []string) error {
  1838. m.fmut.RLock()
  1839. if err := m.checkFolderRunningLocked(folder); err != nil {
  1840. m.fmut.RUnlock()
  1841. return err
  1842. }
  1843. runner := m.folderRunners[folder]
  1844. m.fmut.RUnlock()
  1845. return runner.Scan(subs)
  1846. }
  1847. func (m *model) DelayScan(folder string, next time.Duration) {
  1848. m.fmut.Lock()
  1849. runner, ok := m.folderRunners[folder]
  1850. m.fmut.Unlock()
  1851. if !ok {
  1852. return
  1853. }
  1854. runner.DelayScan(next)
  1855. }
  1856. // numHashers returns the number of hasher routines to use for a given folder,
  1857. // taking into account configuration and available CPU cores.
  1858. func (m *model) numHashers(folder string) int {
  1859. m.fmut.Lock()
  1860. folderCfg := m.folderCfgs[folder]
  1861. numFolders := len(m.folderCfgs)
  1862. m.fmut.Unlock()
  1863. if folderCfg.Hashers > 0 {
  1864. // Specific value set in the config, use that.
  1865. return folderCfg.Hashers
  1866. }
  1867. if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
  1868. // Interactive operating systems; don't load the system too heavily by
  1869. // default.
  1870. return 1
  1871. }
  1872. // For other operating systems and architectures, lets try to get some
  1873. // work done... Divide the available CPU cores among the configured
  1874. // folders.
  1875. if perFolder := runtime.GOMAXPROCS(-1) / numFolders; perFolder > 0 {
  1876. return perFolder
  1877. }
  1878. return 1
  1879. }
  1880. // generateClusterConfig returns a ClusterConfigMessage that is correct for
  1881. // the given peer device
  1882. func (m *model) generateClusterConfig(device protocol.DeviceID) protocol.ClusterConfig {
  1883. var message protocol.ClusterConfig
  1884. m.fmut.RLock()
  1885. defer m.fmut.RUnlock()
  1886. for _, folderCfg := range m.cfg.FolderList() {
  1887. if !folderCfg.SharedWith(device) {
  1888. continue
  1889. }
  1890. protocolFolder := protocol.Folder{
  1891. ID: folderCfg.ID,
  1892. Label: folderCfg.Label,
  1893. ReadOnly: folderCfg.Type == config.FolderTypeSendOnly,
  1894. IgnorePermissions: folderCfg.IgnorePerms,
  1895. IgnoreDelete: folderCfg.IgnoreDelete,
  1896. DisableTempIndexes: folderCfg.DisableTempIndexes,
  1897. Paused: folderCfg.Paused,
  1898. }
  1899. var fs *db.FileSet
  1900. if !folderCfg.Paused {
  1901. fs = m.folderFiles[folderCfg.ID]
  1902. }
  1903. for _, device := range folderCfg.Devices {
  1904. deviceCfg, _ := m.cfg.Device(device.DeviceID)
  1905. protocolDevice := protocol.Device{
  1906. ID: deviceCfg.DeviceID,
  1907. Name: deviceCfg.Name,
  1908. Addresses: deviceCfg.Addresses,
  1909. Compression: deviceCfg.Compression,
  1910. CertName: deviceCfg.CertName,
  1911. Introducer: deviceCfg.Introducer,
  1912. }
  1913. if fs != nil {
  1914. if deviceCfg.DeviceID == m.id {
  1915. protocolDevice.IndexID = fs.IndexID(protocol.LocalDeviceID)
  1916. protocolDevice.MaxSequence = fs.Sequence(protocol.LocalDeviceID)
  1917. } else {
  1918. protocolDevice.IndexID = fs.IndexID(deviceCfg.DeviceID)
  1919. protocolDevice.MaxSequence = fs.Sequence(deviceCfg.DeviceID)
  1920. }
  1921. }
  1922. protocolFolder.Devices = append(protocolFolder.Devices, protocolDevice)
  1923. }
  1924. message.Folders = append(message.Folders, protocolFolder)
  1925. }
  1926. return message
  1927. }
  1928. func (m *model) State(folder string) (string, time.Time, error) {
  1929. m.fmut.RLock()
  1930. runner, ok := m.folderRunners[folder]
  1931. m.fmut.RUnlock()
  1932. if !ok {
  1933. // The returned error should be an actual folder error, so returning
  1934. // errors.New("does not exist") or similar here would be
  1935. // inappropriate.
  1936. return "", time.Time{}, nil
  1937. }
  1938. state, changed, err := runner.getState()
  1939. return state.String(), changed, err
  1940. }
  1941. func (m *model) FolderErrors(folder string) ([]FileError, error) {
  1942. m.fmut.RLock()
  1943. defer m.fmut.RUnlock()
  1944. if err := m.checkFolderRunningLocked(folder); err != nil {
  1945. return nil, err
  1946. }
  1947. return m.folderRunners[folder].Errors(), nil
  1948. }
  1949. func (m *model) WatchError(folder string) error {
  1950. m.fmut.RLock()
  1951. defer m.fmut.RUnlock()
  1952. if err := m.checkFolderRunningLocked(folder); err != nil {
  1953. return err
  1954. }
  1955. return m.folderRunners[folder].WatchError()
  1956. }
  1957. func (m *model) Override(folder string) {
  1958. // Grab the runner and the file set.
  1959. m.fmut.RLock()
  1960. fs, fsOK := m.folderFiles[folder]
  1961. runner, runnerOK := m.folderRunners[folder]
  1962. m.fmut.RUnlock()
  1963. if !fsOK || !runnerOK {
  1964. return
  1965. }
  1966. // Run the override, taking updates as if they came from scanning.
  1967. runner.Override(fs, func(files []protocol.FileInfo) {
  1968. m.updateLocalsFromScanning(folder, files)
  1969. })
  1970. }
  1971. func (m *model) Revert(folder string) {
  1972. // Grab the runner and the file set.
  1973. m.fmut.RLock()
  1974. fs, fsOK := m.folderFiles[folder]
  1975. runner, runnerOK := m.folderRunners[folder]
  1976. m.fmut.RUnlock()
  1977. if !fsOK || !runnerOK {
  1978. return
  1979. }
  1980. // Run the revert, taking updates as if they came from scanning.
  1981. runner.Revert(fs, func(files []protocol.FileInfo) {
  1982. m.updateLocalsFromScanning(folder, files)
  1983. })
  1984. }
  1985. // CurrentSequence returns the change version for the given folder.
  1986. // This is guaranteed to increment if the contents of the local folder has
  1987. // changed.
  1988. func (m *model) CurrentSequence(folder string) (int64, bool) {
  1989. m.fmut.RLock()
  1990. fs, ok := m.folderFiles[folder]
  1991. m.fmut.RUnlock()
  1992. if !ok {
  1993. // The folder might not exist, since this can be called with a user
  1994. // specified folder name from the REST interface.
  1995. return 0, false
  1996. }
  1997. return fs.Sequence(protocol.LocalDeviceID), true
  1998. }
  1999. // RemoteSequence returns the change version for the given folder, as
  2000. // sent by remote peers. This is guaranteed to increment if the contents of
  2001. // the remote or global folder has changed.
  2002. func (m *model) RemoteSequence(folder string) (int64, bool) {
  2003. m.fmut.RLock()
  2004. defer m.fmut.RUnlock()
  2005. fs, ok := m.folderFiles[folder]
  2006. cfg := m.folderCfgs[folder]
  2007. if !ok {
  2008. // The folder might not exist, since this can be called with a user
  2009. // specified folder name from the REST interface.
  2010. return 0, false
  2011. }
  2012. var ver int64
  2013. for _, device := range cfg.Devices {
  2014. ver += fs.Sequence(device.DeviceID)
  2015. }
  2016. return ver, true
  2017. }
  2018. func (m *model) GlobalDirectoryTree(folder, prefix string, levels int, dirsonly bool) map[string]interface{} {
  2019. m.fmut.RLock()
  2020. files, ok := m.folderFiles[folder]
  2021. m.fmut.RUnlock()
  2022. if !ok {
  2023. return nil
  2024. }
  2025. output := make(map[string]interface{})
  2026. sep := string(filepath.Separator)
  2027. prefix = osutil.NativeFilename(prefix)
  2028. if prefix != "" && !strings.HasSuffix(prefix, sep) {
  2029. prefix = prefix + sep
  2030. }
  2031. files.WithPrefixedGlobalTruncated(prefix, func(fi db.FileIntf) bool {
  2032. f := fi.(db.FileInfoTruncated)
  2033. // Don't include the prefix itself.
  2034. if f.IsInvalid() || f.IsDeleted() || strings.HasPrefix(prefix, f.Name) {
  2035. return true
  2036. }
  2037. f.Name = strings.Replace(f.Name, prefix, "", 1)
  2038. var dir, base string
  2039. if f.IsDirectory() && !f.IsSymlink() {
  2040. dir = f.Name
  2041. } else {
  2042. dir = filepath.Dir(f.Name)
  2043. base = filepath.Base(f.Name)
  2044. }
  2045. if levels > -1 && strings.Count(f.Name, sep) > levels {
  2046. return true
  2047. }
  2048. last := output
  2049. if dir != "." {
  2050. for _, path := range strings.Split(dir, sep) {
  2051. directory, ok := last[path]
  2052. if !ok {
  2053. newdir := make(map[string]interface{})
  2054. last[path] = newdir
  2055. last = newdir
  2056. } else {
  2057. last = directory.(map[string]interface{})
  2058. }
  2059. }
  2060. }
  2061. if !dirsonly && base != "" {
  2062. last[base] = []interface{}{
  2063. f.ModTime(), f.FileSize(),
  2064. }
  2065. }
  2066. return true
  2067. })
  2068. return output
  2069. }
  2070. func (m *model) GetFolderVersions(folder string) (map[string][]versioner.FileVersion, error) {
  2071. fcfg, ok := m.cfg.Folder(folder)
  2072. if !ok {
  2073. return nil, errFolderMissing
  2074. }
  2075. files := make(map[string][]versioner.FileVersion)
  2076. filesystem := fcfg.Filesystem()
  2077. err := filesystem.Walk(".stversions", func(path string, f fs.FileInfo, err error) error {
  2078. // Skip root (which is ok to be a symlink)
  2079. if path == ".stversions" {
  2080. return nil
  2081. }
  2082. // Skip walking if we cannot walk...
  2083. if err != nil {
  2084. return err
  2085. }
  2086. // Ignore symlinks
  2087. if f.IsSymlink() {
  2088. return fs.SkipDir
  2089. }
  2090. // No records for directories
  2091. if f.IsDir() {
  2092. return nil
  2093. }
  2094. // Strip .stversions prefix.
  2095. path = strings.TrimPrefix(path, ".stversions"+string(fs.PathSeparator))
  2096. name, tag := versioner.UntagFilename(path)
  2097. // Something invalid
  2098. if name == "" || tag == "" {
  2099. return nil
  2100. }
  2101. name = osutil.NormalizedFilename(name)
  2102. versionTime, err := time.ParseInLocation(versioner.TimeFormat, tag, locationLocal)
  2103. if err != nil {
  2104. return nil
  2105. }
  2106. files[name] = append(files[name], versioner.FileVersion{
  2107. VersionTime: versionTime.Truncate(time.Second),
  2108. ModTime: f.ModTime().Truncate(time.Second),
  2109. Size: f.Size(),
  2110. })
  2111. return nil
  2112. })
  2113. if err != nil {
  2114. return nil, err
  2115. }
  2116. return files, nil
  2117. }
  2118. func (m *model) RestoreFolderVersions(folder string, versions map[string]time.Time) (map[string]string, error) {
  2119. fcfg, ok := m.cfg.Folder(folder)
  2120. if !ok {
  2121. return nil, errFolderMissing
  2122. }
  2123. filesystem := fcfg.Filesystem()
  2124. ver := fcfg.Versioner()
  2125. restore := make(map[string]string)
  2126. errors := make(map[string]string)
  2127. // Validation
  2128. for file, version := range versions {
  2129. file = osutil.NativeFilename(file)
  2130. tag := version.In(locationLocal).Truncate(time.Second).Format(versioner.TimeFormat)
  2131. versionedTaggedFilename := filepath.Join(".stversions", versioner.TagFilename(file, tag))
  2132. // Check that the thing we've been asked to restore is actually a file
  2133. // and that it exists.
  2134. if info, err := filesystem.Lstat(versionedTaggedFilename); err != nil {
  2135. errors[file] = err.Error()
  2136. continue
  2137. } else if !info.IsRegular() {
  2138. errors[file] = "not a file"
  2139. continue
  2140. }
  2141. // Check that the target location of where we are supposed to restore
  2142. // either does not exist, or is actually a file.
  2143. if info, err := filesystem.Lstat(file); err == nil && !info.IsRegular() {
  2144. errors[file] = "cannot replace a non-file"
  2145. continue
  2146. } else if err != nil && !fs.IsNotExist(err) {
  2147. errors[file] = err.Error()
  2148. continue
  2149. }
  2150. restore[file] = versionedTaggedFilename
  2151. }
  2152. // Execution
  2153. var err error
  2154. for target, source := range restore {
  2155. err = nil
  2156. if _, serr := filesystem.Lstat(target); serr == nil {
  2157. if ver != nil {
  2158. err = osutil.InWritableDir(ver.Archive, filesystem, target)
  2159. } else {
  2160. err = osutil.InWritableDir(filesystem.Remove, filesystem, target)
  2161. }
  2162. }
  2163. filesystem.MkdirAll(filepath.Dir(target), 0755)
  2164. if err == nil {
  2165. err = osutil.Copy(filesystem, source, target)
  2166. }
  2167. if err != nil {
  2168. errors[target] = err.Error()
  2169. continue
  2170. }
  2171. }
  2172. // Trigger scan
  2173. if !fcfg.FSWatcherEnabled {
  2174. m.ScanFolder(folder)
  2175. }
  2176. return errors, nil
  2177. }
  2178. func (m *model) Availability(folder string, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
  2179. // The slightly unusual locking sequence here is because we need to hold
  2180. // pmut for the duration (as the value returned from foldersFiles can
  2181. // get heavily modified on Close()), but also must acquire fmut before
  2182. // pmut. (The locks can be *released* in any order.)
  2183. m.fmut.RLock()
  2184. m.pmut.RLock()
  2185. defer m.pmut.RUnlock()
  2186. fs, ok := m.folderFiles[folder]
  2187. cfg := m.folderCfgs[folder]
  2188. m.fmut.RUnlock()
  2189. if !ok {
  2190. return nil
  2191. }
  2192. var availabilities []Availability
  2193. next:
  2194. for _, device := range fs.Availability(file.Name) {
  2195. for _, pausedFolder := range m.remotePausedFolders[device] {
  2196. if pausedFolder == folder {
  2197. continue next
  2198. }
  2199. }
  2200. _, ok := m.conn[device]
  2201. if ok {
  2202. availabilities = append(availabilities, Availability{ID: device, FromTemporary: false})
  2203. }
  2204. }
  2205. for _, device := range cfg.Devices {
  2206. if m.deviceDownloads[device.DeviceID].Has(folder, file.Name, file.Version, int32(block.Offset/int64(file.BlockSize()))) {
  2207. availabilities = append(availabilities, Availability{ID: device.DeviceID, FromTemporary: true})
  2208. }
  2209. }
  2210. return availabilities
  2211. }
  2212. // BringToFront bumps the given files priority in the job queue.
  2213. func (m *model) BringToFront(folder, file string) {
  2214. m.pmut.RLock()
  2215. defer m.pmut.RUnlock()
  2216. runner, ok := m.folderRunners[folder]
  2217. if ok {
  2218. runner.BringToFront(file)
  2219. }
  2220. }
  2221. func (m *model) ResetFolder(folder string) {
  2222. l.Infof("Cleaning data for folder %q", folder)
  2223. db.DropFolder(m.db, folder)
  2224. }
  2225. func (m *model) String() string {
  2226. return fmt.Sprintf("model@%p", m)
  2227. }
  2228. func (m *model) VerifyConfiguration(from, to config.Configuration) error {
  2229. return nil
  2230. }
  2231. func (m *model) CommitConfiguration(from, to config.Configuration) bool {
  2232. // TODO: This should not use reflect, and should take more care to try to handle stuff without restart.
  2233. // Go through the folder configs and figure out if we need to restart or not.
  2234. fromFolders := mapFolders(from.Folders)
  2235. toFolders := mapFolders(to.Folders)
  2236. for folderID, cfg := range toFolders {
  2237. if _, ok := fromFolders[folderID]; !ok {
  2238. // A folder was added.
  2239. if cfg.Paused {
  2240. l.Infoln("Paused folder", cfg.Description())
  2241. } else {
  2242. l.Infoln("Adding folder", cfg.Description())
  2243. m.AddFolder(cfg)
  2244. m.StartFolder(folderID)
  2245. }
  2246. }
  2247. }
  2248. for folderID, fromCfg := range fromFolders {
  2249. toCfg, ok := toFolders[folderID]
  2250. if !ok {
  2251. // The folder was removed.
  2252. m.RemoveFolder(fromCfg)
  2253. continue
  2254. }
  2255. if fromCfg.Paused && toCfg.Paused {
  2256. continue
  2257. }
  2258. // This folder exists on both sides. Settings might have changed.
  2259. // Check if anything differs that requires a restart.
  2260. if !reflect.DeepEqual(fromCfg.RequiresRestartOnly(), toCfg.RequiresRestartOnly()) {
  2261. m.RestartFolder(fromCfg, toCfg)
  2262. }
  2263. // Emit the folder pause/resume event
  2264. if fromCfg.Paused != toCfg.Paused {
  2265. eventType := events.FolderResumed
  2266. if toCfg.Paused {
  2267. eventType = events.FolderPaused
  2268. }
  2269. events.Default.Log(eventType, map[string]string{"id": toCfg.ID, "label": toCfg.Label})
  2270. }
  2271. }
  2272. // Removing a device. We actually don't need to do anything.
  2273. // Because folder config has changed (since the device lists do not match)
  2274. // Folders for that had device got "restarted", which involves killing
  2275. // connections to all devices that we were sharing the folder with.
  2276. // At some point model.Close() will get called for that device which will
  2277. // clean residue device state that is not part of any folder.
  2278. // Pausing a device, unpausing is handled by the connection service.
  2279. fromDevices := from.DeviceMap()
  2280. toDevices := to.DeviceMap()
  2281. for deviceID, toCfg := range toDevices {
  2282. fromCfg, ok := fromDevices[deviceID]
  2283. if !ok || fromCfg.Paused == toCfg.Paused {
  2284. continue
  2285. }
  2286. // Ignored folder was removed, reconnect to retrigger the prompt.
  2287. if len(fromCfg.IgnoredFolders) > len(toCfg.IgnoredFolders) {
  2288. m.close(deviceID, errIgnoredFolderRemoved)
  2289. }
  2290. if toCfg.Paused {
  2291. l.Infoln("Pausing", deviceID)
  2292. m.close(deviceID, errDevicePaused)
  2293. events.Default.Log(events.DevicePaused, map[string]string{"device": deviceID.String()})
  2294. } else {
  2295. events.Default.Log(events.DeviceResumed, map[string]string{"device": deviceID.String()})
  2296. }
  2297. }
  2298. scanLimiter.setCapacity(to.Options.MaxConcurrentScans)
  2299. // Some options don't require restart as those components handle it fine
  2300. // by themselves. Compare the options structs containing only the
  2301. // attributes that require restart and act apprioriately.
  2302. if !reflect.DeepEqual(from.Options.RequiresRestartOnly(), to.Options.RequiresRestartOnly()) {
  2303. l.Debugln(m, "requires restart, options differ")
  2304. return false
  2305. }
  2306. return true
  2307. }
  2308. // checkFolderRunningLocked returns nil if the folder is up and running and a
  2309. // descriptive error if not.
  2310. // Need to hold (read) lock on m.fmut when calling this.
  2311. func (m *model) checkFolderRunningLocked(folder string) error {
  2312. _, ok := m.folderRunners[folder]
  2313. if ok {
  2314. return nil
  2315. }
  2316. if cfg, ok := m.cfg.Folder(folder); !ok {
  2317. return errFolderMissing
  2318. } else if cfg.Paused {
  2319. return ErrFolderPaused
  2320. }
  2321. return errFolderNotRunning
  2322. }
  2323. // checkFolderDeviceStatusLocked first checks the folder and then whether the
  2324. // given device is connected and shares this folder.
  2325. // Need to hold (read) lock on both m.fmut and m.pmut when calling this.
  2326. func (m *model) checkDeviceFolderConnectedLocked(device protocol.DeviceID, folder string) error {
  2327. if err := m.checkFolderRunningLocked(folder); err != nil {
  2328. return err
  2329. }
  2330. if cfg, ok := m.cfg.Device(device); !ok {
  2331. return errDeviceUnknown
  2332. } else if cfg.Paused {
  2333. return errDevicePaused
  2334. }
  2335. if _, ok := m.conn[device]; !ok {
  2336. return errors.New("device is not connected")
  2337. }
  2338. if cfg, ok := m.cfg.Folder(folder); !ok || !cfg.SharedWith(device) {
  2339. return errors.New("folder is not shared with device")
  2340. }
  2341. return nil
  2342. }
  2343. // mapFolders returns a map of folder ID to folder configuration for the given
  2344. // slice of folder configurations.
  2345. func mapFolders(folders []config.FolderConfiguration) map[string]config.FolderConfiguration {
  2346. m := make(map[string]config.FolderConfiguration, len(folders))
  2347. for _, cfg := range folders {
  2348. m[cfg.ID] = cfg
  2349. }
  2350. return m
  2351. }
  2352. // mapDevices returns a map of device ID to nothing for the given slice of
  2353. // device IDs.
  2354. func mapDevices(devices []protocol.DeviceID) map[protocol.DeviceID]struct{} {
  2355. m := make(map[protocol.DeviceID]struct{}, len(devices))
  2356. for _, dev := range devices {
  2357. m[dev] = struct{}{}
  2358. }
  2359. return m
  2360. }
  2361. // Skips `skip` elements and retrieves up to `get` elements from a given slice.
  2362. // Returns the resulting slice, plus how much elements are left to skip or
  2363. // copy to satisfy the values which were provided, given the slice is not
  2364. // big enough.
  2365. func getChunk(data []string, skip, get int) ([]string, int, int) {
  2366. l := len(data)
  2367. if l <= skip {
  2368. return []string{}, skip - l, get
  2369. } else if l < skip+get {
  2370. return data[skip:l], 0, get - (l - skip)
  2371. }
  2372. return data[skip : skip+get], 0, 0
  2373. }
  2374. func readOffsetIntoBuf(fs fs.Filesystem, file string, offset int64, buf []byte) error {
  2375. fd, err := fs.Open(file)
  2376. if err != nil {
  2377. l.Debugln("readOffsetIntoBuf.Open", file, err)
  2378. return err
  2379. }
  2380. defer fd.Close()
  2381. _, err = fd.ReadAt(buf, offset)
  2382. if err != nil {
  2383. l.Debugln("readOffsetIntoBuf.ReadAt", file, err)
  2384. }
  2385. return err
  2386. }
  2387. // makeForgetUpdate takes an index update and constructs a download progress update
  2388. // causing to forget any progress for files which we've just been sent.
  2389. func makeForgetUpdate(files []protocol.FileInfo) []protocol.FileDownloadProgressUpdate {
  2390. updates := make([]protocol.FileDownloadProgressUpdate, 0, len(files))
  2391. for _, file := range files {
  2392. if file.IsSymlink() || file.IsDirectory() || file.IsDeleted() {
  2393. continue
  2394. }
  2395. updates = append(updates, protocol.FileDownloadProgressUpdate{
  2396. Name: file.Name,
  2397. Version: file.Version,
  2398. UpdateType: protocol.UpdateTypeForget,
  2399. })
  2400. }
  2401. return updates
  2402. }
  2403. // folderDeviceSet is a set of (folder, deviceID) pairs
  2404. type folderDeviceSet map[string]map[protocol.DeviceID]struct{}
  2405. // set adds the (dev, folder) pair to the set
  2406. func (s folderDeviceSet) set(dev protocol.DeviceID, folder string) {
  2407. devs, ok := s[folder]
  2408. if !ok {
  2409. devs = make(map[protocol.DeviceID]struct{})
  2410. s[folder] = devs
  2411. }
  2412. devs[dev] = struct{}{}
  2413. }
  2414. // has returns true if the (dev, folder) pair is in the set
  2415. func (s folderDeviceSet) has(dev protocol.DeviceID, folder string) bool {
  2416. _, ok := s[folder][dev]
  2417. return ok
  2418. }
  2419. // hasDevice returns true if the device is set on any folder
  2420. func (s folderDeviceSet) hasDevice(dev protocol.DeviceID) bool {
  2421. for _, devices := range s {
  2422. if _, ok := devices[dev]; ok {
  2423. return true
  2424. }
  2425. }
  2426. return false
  2427. }
  2428. type fileInfoBatch struct {
  2429. infos []protocol.FileInfo
  2430. size int
  2431. flushFn func([]protocol.FileInfo) error
  2432. }
  2433. func newFileInfoBatch(fn func([]protocol.FileInfo) error) *fileInfoBatch {
  2434. return &fileInfoBatch{
  2435. infos: make([]protocol.FileInfo, 0, maxBatchSizeFiles),
  2436. flushFn: fn,
  2437. }
  2438. }
  2439. func (b *fileInfoBatch) append(f protocol.FileInfo) {
  2440. b.infos = append(b.infos, f)
  2441. b.size += f.ProtoSize()
  2442. }
  2443. func (b *fileInfoBatch) flushIfFull() error {
  2444. if len(b.infos) >= maxBatchSizeFiles || b.size >= maxBatchSizeBytes {
  2445. return b.flush()
  2446. }
  2447. return nil
  2448. }
  2449. func (b *fileInfoBatch) flush() error {
  2450. if len(b.infos) == 0 {
  2451. return nil
  2452. }
  2453. if err := b.flushFn(b.infos); err != nil {
  2454. return err
  2455. }
  2456. b.reset()
  2457. return nil
  2458. }
  2459. func (b *fileInfoBatch) reset() {
  2460. b.infos = b.infos[:0]
  2461. b.size = 0
  2462. }
  2463. // syncMutexMap is a type safe wrapper for a sync.Map that holds mutexes
  2464. type syncMutexMap struct {
  2465. inner stdsync.Map
  2466. }
  2467. func (m *syncMutexMap) Get(key string) sync.Mutex {
  2468. v, _ := m.inner.LoadOrStore(key, sync.NewMutex())
  2469. return v.(sync.Mutex)
  2470. }
  2471. // sanitizePath takes a string that might contain all kinds of special
  2472. // characters and makes a valid, similar, path name out of it.
  2473. //
  2474. // Spans of invalid characters are replaced by a single space. Invalid
  2475. // characters are control characters, the things not allowed in file names
  2476. // in Windows, and common shell metacharacters. Even if asterisks and pipes
  2477. // and stuff are allowed on Unixes in general they might not be allowed by
  2478. // the filesystem and may surprise the user and cause shell oddness. This
  2479. // function is intended for file names we generate on behalf of the user,
  2480. // and surprising them with odd shell characters in file names is unkind.
  2481. //
  2482. // We include whitespace in the invalid characters so that multiple
  2483. // whitespace is collapsed to a single space. Additionally, whitespace at
  2484. // either end is removed.
  2485. func sanitizePath(path string) string {
  2486. invalid := regexp.MustCompile(`([[:cntrl:]]|[<>:"'/\\|?*\n\r\t \[\]\{\};:!@$%&^#])+`)
  2487. return strings.TrimSpace(invalid.ReplaceAllString(path, " "))
  2488. }