model.go 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497
  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. //go:generate go tool counterfeiter -o mocks/model.go --fake-name Model . Model
  7. package model
  8. import (
  9. "bytes"
  10. "context"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "iter"
  16. "log/slog"
  17. "net"
  18. "os"
  19. "path/filepath"
  20. "reflect"
  21. "runtime"
  22. "slices"
  23. "strings"
  24. "sync"
  25. "sync/atomic"
  26. "time"
  27. "github.com/thejerf/suture/v4"
  28. "github.com/syncthing/syncthing/internal/db"
  29. "github.com/syncthing/syncthing/internal/itererr"
  30. "github.com/syncthing/syncthing/internal/slogutil"
  31. "github.com/syncthing/syncthing/lib/build"
  32. "github.com/syncthing/syncthing/lib/config"
  33. "github.com/syncthing/syncthing/lib/connections"
  34. "github.com/syncthing/syncthing/lib/events"
  35. "github.com/syncthing/syncthing/lib/fs"
  36. "github.com/syncthing/syncthing/lib/ignore"
  37. "github.com/syncthing/syncthing/lib/osutil"
  38. "github.com/syncthing/syncthing/lib/protocol"
  39. "github.com/syncthing/syncthing/lib/rand"
  40. "github.com/syncthing/syncthing/lib/scanner"
  41. "github.com/syncthing/syncthing/lib/semaphore"
  42. "github.com/syncthing/syncthing/lib/stats"
  43. "github.com/syncthing/syncthing/lib/svcutil"
  44. "github.com/syncthing/syncthing/lib/ur/contract"
  45. "github.com/syncthing/syncthing/lib/versioner"
  46. )
  47. type service interface {
  48. suture.Service
  49. BringToFront(string)
  50. Override()
  51. Revert()
  52. DelayScan(d time.Duration)
  53. ScheduleScan()
  54. SchedulePull() // something relevant changed, we should try a pull
  55. Jobs(page, perpage int) ([]string, []string, int) // In progress, Queued, skipped
  56. Scan(subs []string) error
  57. Errors() []FileError
  58. WatchError() error
  59. ScheduleForceRescan(path string)
  60. GetStatistics() (stats.FolderStatistics, error)
  61. getState() (folderState, time.Time, error)
  62. }
  63. type Availability struct {
  64. ID protocol.DeviceID `json:"id"`
  65. FromTemporary bool `json:"fromTemporary"`
  66. }
  67. type Model interface {
  68. suture.Service
  69. connections.Model
  70. ResetFolder(folder string) error
  71. DelayScan(folder string, next time.Duration)
  72. ScanFolder(folder string) error
  73. ScanFolders() map[string]error
  74. ScanFolderSubdirs(folder string, subs []string) error
  75. State(folder string) (string, time.Time, error)
  76. FolderErrors(folder string) ([]FileError, error)
  77. WatchError(folder string) error
  78. Override(folder string)
  79. Revert(folder string)
  80. BringToFront(folder, file string)
  81. LoadIgnores(folder string) ([]string, []string, error)
  82. CurrentIgnores(folder string) ([]string, []string, error)
  83. SetIgnores(folder string, content []string) error
  84. GetFolderVersions(folder string) (map[string][]versioner.FileVersion, error)
  85. RestoreFolderVersions(folder string, versions map[string]time.Time) (map[string]error, error)
  86. LocalFiles(folder string, device protocol.DeviceID) (iter.Seq[protocol.FileInfo], func() error)
  87. LocalFilesSequenced(folder string, device protocol.DeviceID, startSet int64) (iter.Seq[protocol.FileInfo], func() error)
  88. LocalSize(folder string, device protocol.DeviceID) (db.Counts, error)
  89. GlobalSize(folder string) (db.Counts, error)
  90. NeedSize(folder string, device protocol.DeviceID) (db.Counts, error)
  91. ReceiveOnlySize(folder string) (db.Counts, error)
  92. Sequence(folder string, device protocol.DeviceID) (int64, error)
  93. AllGlobalFiles(folder string) (iter.Seq[db.FileMetadata], func() error)
  94. RemoteSequences(folder string) (map[protocol.DeviceID]int64, error)
  95. NeedFolderFiles(folder string, page, perpage int) ([]protocol.FileInfo, []protocol.FileInfo, []protocol.FileInfo, error)
  96. RemoteNeedFolderFiles(folder string, device protocol.DeviceID, page, perpage int) ([]protocol.FileInfo, error)
  97. LocalChangedFolderFiles(folder string, page, perpage int) ([]protocol.FileInfo, error)
  98. FolderProgressBytesCompleted(folder string) int64
  99. CurrentFolderFile(folder string, file string) (protocol.FileInfo, bool, error)
  100. CurrentGlobalFile(folder string, file string) (protocol.FileInfo, bool, error)
  101. Availability(folder string, file protocol.FileInfo, block protocol.BlockInfo) ([]Availability, error)
  102. Completion(device protocol.DeviceID, folder string) (FolderCompletion, error)
  103. ConnectionStats() map[string]interface{}
  104. DeviceStatistics() (map[protocol.DeviceID]stats.DeviceStatistics, error)
  105. FolderStatistics() (map[string]stats.FolderStatistics, error)
  106. UsageReportingStats(report *contract.Report, version int, preview bool)
  107. ConnectedTo(remoteID protocol.DeviceID) bool
  108. PendingDevices() (map[protocol.DeviceID]db.ObservedDevice, error)
  109. PendingFolders(device protocol.DeviceID) (map[string]db.PendingFolder, error)
  110. DismissPendingDevice(device protocol.DeviceID) error
  111. DismissPendingFolder(device protocol.DeviceID, folder string) error
  112. GlobalDirectoryTree(folder, prefix string, levels int, dirsOnly bool) ([]*TreeEntry, error)
  113. RequestGlobal(ctx context.Context, deviceID protocol.DeviceID, folder, name string, blockNo int, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error)
  114. }
  115. type model struct {
  116. *suture.Supervisor
  117. // constructor parameters
  118. cfg config.Wrapper
  119. id protocol.DeviceID
  120. sdb db.DB
  121. protectedFiles []string
  122. evLogger events.Logger
  123. // constant or concurrency safe fields
  124. progressEmitter *ProgressEmitter
  125. shortID protocol.ShortID
  126. // globalRequestLimiter limits the amount of data in concurrent incoming
  127. // requests
  128. globalRequestLimiter *semaphore.Semaphore
  129. // folderIOLimiter limits the number of concurrent I/O heavy operations,
  130. // such as scans and pulls.
  131. folderIOLimiter *semaphore.Semaphore
  132. fatalChan chan error
  133. started chan struct{}
  134. keyGen *protocol.KeyGenerator
  135. promotionTimer *time.Timer
  136. observed *db.ObservedDB
  137. // fields protected by mut
  138. mut sync.RWMutex
  139. folderCfgs map[string]config.FolderConfiguration // folder -> cfg
  140. deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef
  141. folderIgnores map[string]*ignore.Matcher // folder -> matcher object
  142. folderRunners *serviceMap[string, service] // folder -> puller or scanner
  143. folderRestartMuts syncMutexMap // folder -> restart mutex
  144. folderVersioners map[string]versioner.Versioner // folder -> versioner (may be nil)
  145. folderEncryptionPasswordTokens map[string][]byte // folder -> encryption token (may be missing, and only for encryption type folders)
  146. folderEncryptionFailures map[string]map[protocol.DeviceID]error // folder -> device -> error regarding encryption consistency (may be missing)
  147. connections map[string]protocol.Connection // connection ID -> connection
  148. deviceConnIDs map[protocol.DeviceID][]string // device -> connection IDs (invariant: if the key exists, the value is len >= 1, with the primary connection at the start of the slice)
  149. promotedConnID map[protocol.DeviceID]string // device -> latest promoted connection ID
  150. connRequestLimiters map[protocol.DeviceID]*semaphore.Semaphore
  151. closed map[string]chan struct{} // connection ID -> closed channel
  152. helloMessages map[protocol.DeviceID]protocol.Hello
  153. deviceDownloads map[protocol.DeviceID]*deviceDownloadState
  154. remoteFolderStates map[protocol.DeviceID]map[string]remoteFolderState // deviceID -> folders
  155. indexHandlers *serviceMap[protocol.DeviceID, *indexHandlerRegistry]
  156. // for testing only
  157. foldersRunning atomic.Int32
  158. }
  159. var _ config.Verifier = &model{}
  160. type folderFactory func(*model, *ignore.Matcher, config.FolderConfiguration, versioner.Versioner, events.Logger, *semaphore.Semaphore) service
  161. var folderFactories = make(map[config.FolderType]folderFactory)
  162. var (
  163. errDeviceUnknown = errors.New("unknown device")
  164. errDevicePaused = errors.New("device is paused")
  165. ErrFolderPaused = errors.New("folder is paused")
  166. ErrFolderNotRunning = errors.New("folder is not running")
  167. ErrFolderMissing = errors.New("no such folder")
  168. errNoVersioner = errors.New("folder has no versioner")
  169. // errors about why a connection is closed
  170. errStopped = errors.New("Syncthing is being stopped") //nolint:staticcheck
  171. errEncryptionInvConfigLocal = errors.New("can't encrypt outgoing data because local data is encrypted (folder-type receive-encrypted)")
  172. errEncryptionInvConfigRemote = errors.New("remote has encrypted data and encrypts that data for us - this is impossible")
  173. errEncryptionNotEncryptedLocal = errors.New("remote expects to exchange encrypted data, but is configured for plain data")
  174. errEncryptionPlainForReceiveEncrypted = errors.New("remote expects to exchange plain data, but is configured to be encrypted")
  175. errEncryptionPlainForRemoteEncrypted = errors.New("remote expects to exchange plain data, but local data is encrypted (folder-type receive-encrypted)")
  176. errEncryptionNotEncryptedUntrusted = errors.New("device is untrusted, but configured to receive plain data")
  177. errEncryptionPassword = errors.New("different encryption passwords used")
  178. errEncryptionTokenRead = errors.New("failed to read encryption token")
  179. errEncryptionTokenWrite = errors.New("failed to write encryption token")
  180. errMissingRemoteInClusterConfig = errors.New("remote device missing in cluster config")
  181. errMissingLocalInClusterConfig = errors.New("local device missing in cluster config")
  182. )
  183. // NewModel creates and starts a new model. The model starts in read-only mode,
  184. // where it sends index information to connected peers and responds to requests
  185. // for file data without altering the local folder in any way.
  186. func NewModel(cfg config.Wrapper, id protocol.DeviceID, sdb db.DB, protectedFiles []string, evLogger events.Logger, keyGen *protocol.KeyGenerator) Model {
  187. spec := svcutil.SpecWithDebugLogger()
  188. m := &model{
  189. Supervisor: suture.New("model", spec),
  190. // constructor parameters
  191. cfg: cfg,
  192. id: id,
  193. sdb: sdb,
  194. protectedFiles: protectedFiles,
  195. evLogger: evLogger,
  196. // constant or concurrency safe fields
  197. progressEmitter: NewProgressEmitter(cfg, evLogger),
  198. shortID: id.Short(),
  199. globalRequestLimiter: semaphore.New(1024 * cfg.Options().MaxConcurrentIncomingRequestKiB()),
  200. folderIOLimiter: semaphore.New(cfg.Options().MaxFolderConcurrency()),
  201. fatalChan: make(chan error),
  202. started: make(chan struct{}),
  203. keyGen: keyGen,
  204. promotionTimer: time.NewTimer(0),
  205. observed: db.NewObservedDB(sdb),
  206. // fields protected by mut
  207. folderCfgs: make(map[string]config.FolderConfiguration),
  208. deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference),
  209. folderIgnores: make(map[string]*ignore.Matcher),
  210. folderRunners: newServiceMap[string, service](evLogger),
  211. folderVersioners: make(map[string]versioner.Versioner),
  212. folderEncryptionPasswordTokens: make(map[string][]byte),
  213. folderEncryptionFailures: make(map[string]map[protocol.DeviceID]error),
  214. connections: make(map[string]protocol.Connection),
  215. deviceConnIDs: make(map[protocol.DeviceID][]string),
  216. promotedConnID: make(map[protocol.DeviceID]string),
  217. connRequestLimiters: make(map[protocol.DeviceID]*semaphore.Semaphore),
  218. closed: make(map[string]chan struct{}),
  219. helloMessages: make(map[protocol.DeviceID]protocol.Hello),
  220. deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState),
  221. remoteFolderStates: make(map[protocol.DeviceID]map[string]remoteFolderState),
  222. indexHandlers: newServiceMap[protocol.DeviceID, *indexHandlerRegistry](evLogger),
  223. }
  224. for devID, cfg := range cfg.Devices() {
  225. m.deviceStatRefs[devID] = stats.NewDeviceStatisticsReference(db.NewTyped(sdb, "devicestats/"+devID.String()))
  226. m.setConnRequestLimitersLocked(cfg)
  227. }
  228. m.Add(m.folderRunners)
  229. m.Add(m.progressEmitter)
  230. m.Add(m.indexHandlers)
  231. m.Add(svcutil.AsService(m.serve, m.String()))
  232. return m
  233. }
  234. func (m *model) serve(ctx context.Context) error {
  235. defer m.closeAllConnectionsAndWait()
  236. cfg := m.cfg.Subscribe(m)
  237. defer m.cfg.Unsubscribe(m)
  238. if err := m.initFolders(cfg); err != nil {
  239. close(m.started)
  240. return svcutil.AsFatalErr(err, svcutil.ExitError)
  241. }
  242. close(m.started)
  243. for {
  244. select {
  245. case <-ctx.Done():
  246. l.Debugln(m, "context closed, stopping", ctx.Err())
  247. return ctx.Err()
  248. case err := <-m.fatalChan:
  249. l.Debugln(m, "fatal error, stopping", err)
  250. return svcutil.AsFatalErr(err, svcutil.ExitError)
  251. case <-m.promotionTimer.C:
  252. slog.Debug("Promotion timer fired")
  253. m.promoteConnections()
  254. }
  255. }
  256. }
  257. func (m *model) initFolders(cfg config.Configuration) error {
  258. clusterConfigDevices := make(deviceIDSet, len(cfg.Devices))
  259. for _, folderCfg := range cfg.Folders {
  260. if folderCfg.Paused {
  261. folderCfg.CreateRoot()
  262. continue
  263. }
  264. err := m.newFolder(folderCfg, cfg.Options.CacheIgnoredFiles)
  265. if err != nil {
  266. return err
  267. }
  268. clusterConfigDevices.add(folderCfg.DeviceIDs())
  269. }
  270. ignoredDevices := observedDeviceSet(m.cfg.IgnoredDevices())
  271. m.cleanPending(cfg.DeviceMap(), cfg.FolderMap(), ignoredDevices, nil)
  272. m.sendClusterConfig(clusterConfigDevices.AsSlice())
  273. return nil
  274. }
  275. func (m *model) closeAllConnectionsAndWait() {
  276. m.mut.RLock()
  277. closed := make([]chan struct{}, 0, len(m.connections))
  278. for connID, conn := range m.connections {
  279. closed = append(closed, m.closed[connID])
  280. go conn.Close(errStopped)
  281. }
  282. m.mut.RUnlock()
  283. for _, c := range closed {
  284. <-c
  285. }
  286. }
  287. func (m *model) fatal(err error) {
  288. select {
  289. case m.fatalChan <- err:
  290. default:
  291. }
  292. }
  293. // Need to hold lock on m.mut when calling this.
  294. func (m *model) addAndStartFolderLocked(cfg config.FolderConfiguration, cacheIgnoredFiles bool) {
  295. ignores := ignore.New(cfg.Filesystem(), ignore.WithCache(cacheIgnoredFiles))
  296. if cfg.Type != config.FolderTypeReceiveEncrypted {
  297. if err := ignores.Load(".stignore"); err != nil && !fs.IsNotExist(err) {
  298. slog.Error("Failed to load ignores", slogutil.Error(err))
  299. }
  300. }
  301. m.addAndStartFolderLockedWithIgnores(cfg, ignores)
  302. }
  303. // Only needed for testing, use addAndStartFolderLocked instead.
  304. func (m *model) addAndStartFolderLockedWithIgnores(cfg config.FolderConfiguration, ignores *ignore.Matcher) {
  305. m.folderCfgs[cfg.ID] = cfg
  306. m.folderIgnores[cfg.ID] = ignores
  307. _, ok := m.folderRunners.Get(cfg.ID)
  308. if ok {
  309. slog.Error("Cannot start already running folder", cfg.LogAttr())
  310. panic("cannot start already running folder")
  311. }
  312. folderFactory, ok := folderFactories[cfg.Type]
  313. if !ok {
  314. panic(fmt.Sprintf("unknown folder type 0x%x", cfg.Type))
  315. }
  316. folder := cfg.ID
  317. // Find any devices for which we hold the index in the db, but the folder
  318. // is not shared, and drop it.
  319. expected := mapDevices(cfg.DeviceIDs())
  320. devs, _ := m.sdb.ListDevicesForFolder(cfg.ID)
  321. for _, available := range devs {
  322. if _, ok := expected[available]; !ok {
  323. l.Debugln("dropping", folder, "state for", available)
  324. _ = m.sdb.DropAllFiles(folder, available)
  325. }
  326. }
  327. seq, err := m.sdb.GetDeviceSequence(folder, protocol.LocalDeviceID)
  328. if err != nil {
  329. panic(fmt.Errorf("error getting sequence number: %w", err))
  330. }
  331. if seq == 0 {
  332. // It's a blank folder, so this may the first time we're looking at
  333. // it. Attempt to create and tag with our marker as appropriate. We
  334. // don't really do anything with errors at this point except warn -
  335. // if these things don't work, we still want to start the folder and
  336. // it'll show up as errored later.
  337. if err := cfg.CreateRoot(); err != nil {
  338. slog.Error("Failed to create folder root directory", cfg.LogAttr(), slogutil.Error(err))
  339. } else if err = cfg.CreateMarker(); err != nil {
  340. slog.Error("Failed to create folder marker", cfg.LogAttr(), slogutil.Error(err))
  341. }
  342. }
  343. if cfg.Type == config.FolderTypeReceiveEncrypted {
  344. if encryptionToken, err := readEncryptionToken(cfg); err == nil {
  345. m.folderEncryptionPasswordTokens[folder] = encryptionToken
  346. } else if !fs.IsNotExist(err) {
  347. slog.Error("Failed to read encryption token", cfg.LogAttr(), slogutil.Error(err))
  348. }
  349. }
  350. // These are our metadata files, and they should always be hidden.
  351. ffs := cfg.Filesystem()
  352. _ = ffs.Hide(config.DefaultMarkerName)
  353. _ = ffs.Hide(versioner.DefaultPath)
  354. _ = ffs.Hide(".stignore")
  355. var ver versioner.Versioner
  356. if cfg.Versioning.Type != "" {
  357. var err error
  358. ver, err = versioner.New(cfg)
  359. if err != nil {
  360. panic(fmt.Errorf("creating versioner: %w", err))
  361. }
  362. }
  363. m.folderVersioners[folder] = ver
  364. m.warnAboutOverwritingProtectedFiles(cfg, ignores)
  365. p := folderFactory(m, ignores, cfg, ver, m.evLogger, m.folderIOLimiter)
  366. m.folderRunners.Add(folder, p)
  367. slog.Info("Ready to synchronize", cfg.LogAttr())
  368. }
  369. func (m *model) warnAboutOverwritingProtectedFiles(cfg config.FolderConfiguration, ignores *ignore.Matcher) {
  370. if cfg.Type == config.FolderTypeSendOnly {
  371. return
  372. }
  373. // This is a bit of a hack.
  374. ffs := cfg.Filesystem()
  375. if ffs.Type() != fs.FilesystemTypeBasic {
  376. return
  377. }
  378. folderLocation := ffs.URI()
  379. var filesAtRisk []string
  380. for _, protectedFilePath := range m.protectedFiles {
  381. // check if file is synced in this folder
  382. if protectedFilePath != folderLocation && !fs.IsParent(protectedFilePath, folderLocation) {
  383. continue
  384. }
  385. // check if file is ignored
  386. relPath, _ := filepath.Rel(folderLocation, protectedFilePath)
  387. if ignores.Match(relPath).IsIgnored() {
  388. continue
  389. }
  390. filesAtRisk = append(filesAtRisk, protectedFilePath)
  391. }
  392. if len(filesAtRisk) > 0 {
  393. slog.Warn("Some protected files may be overwritten and cause issues; see https://docs.syncthing.net/users/config.html#syncing-configuration-files for more information", slog.Any("filesAtRisk", filesAtRisk))
  394. }
  395. }
  396. func (m *model) removeFolder(cfg config.FolderConfiguration) {
  397. slog.Info("Removing folder", cfg.LogAttr())
  398. defer slog.Info("Removed folder", cfg.LogAttr())
  399. m.mut.RLock()
  400. wait := m.folderRunners.StopAndWaitChan(cfg.ID, 0)
  401. m.mut.RUnlock()
  402. <-wait
  403. m.mut.Lock()
  404. isPathUnique := true
  405. for folderID, folderCfg := range m.folderCfgs {
  406. if folderID != cfg.ID && folderCfg.Path == cfg.Path {
  407. isPathUnique = false
  408. break
  409. }
  410. }
  411. if isPathUnique {
  412. // Remove (if empty and removable) or move away (if non-empty or
  413. // otherwise not removable) Syncthing-specific marker files.
  414. if err := cfg.RemoveMarker(); err != nil && !errors.Is(err, os.ErrNotExist) {
  415. moved := config.DefaultMarkerName + time.Now().Format(".removed-20060102-150405")
  416. fs := cfg.Filesystem()
  417. _ = fs.Rename(config.DefaultMarkerName, moved)
  418. }
  419. }
  420. m.cleanupFolderLocked(cfg)
  421. m.indexHandlers.Each(func(_ protocol.DeviceID, r *indexHandlerRegistry) error {
  422. r.Remove(cfg.ID)
  423. return nil
  424. })
  425. m.mut.Unlock()
  426. // Remove it from the database
  427. _ = m.sdb.DropFolder(cfg.ID)
  428. }
  429. // Need to hold lock on m.mut when calling this.
  430. func (m *model) cleanupFolderLocked(cfg config.FolderConfiguration) {
  431. // clear up our config maps
  432. m.folderRunners.Remove(cfg.ID)
  433. delete(m.folderCfgs, cfg.ID)
  434. delete(m.folderIgnores, cfg.ID)
  435. delete(m.folderVersioners, cfg.ID)
  436. delete(m.folderEncryptionPasswordTokens, cfg.ID)
  437. delete(m.folderEncryptionFailures, cfg.ID)
  438. }
  439. func (m *model) restartFolder(from, to config.FolderConfiguration, cacheIgnoredFiles bool) error {
  440. if to.ID == "" {
  441. panic("bug: cannot restart empty folder ID")
  442. }
  443. if to.ID != from.ID {
  444. slog.Error("Bug: folder restart cannot change ID", "from", from.ID, "to", to.ID)
  445. panic("bug: folder restart cannot change ID")
  446. }
  447. folder := to.ID
  448. // This mutex protects the entirety of the restart operation, preventing
  449. // there from being more than one folder restart operation in progress
  450. // at any given time. The usual locking stuff doesn't cover this,
  451. // because those locks are released while we are waiting for the folder
  452. // to shut down (and must be so because the folder might need them as
  453. // part of its operations before shutting down).
  454. restartMut := m.folderRestartMuts.Get(folder)
  455. restartMut.Lock()
  456. defer restartMut.Unlock()
  457. m.mut.RLock()
  458. wait := m.folderRunners.StopAndWaitChan(from.ID, 0)
  459. m.mut.RUnlock()
  460. <-wait
  461. m.mut.Lock()
  462. defer m.mut.Unlock()
  463. m.cleanupFolderLocked(from)
  464. if !to.Paused {
  465. m.addAndStartFolderLocked(to, cacheIgnoredFiles)
  466. }
  467. runner, _ := m.folderRunners.Get(to.ID)
  468. m.indexHandlers.Each(func(_ protocol.DeviceID, r *indexHandlerRegistry) error {
  469. r.RegisterFolderState(to, runner)
  470. return nil
  471. })
  472. switch {
  473. case to.Paused:
  474. slog.Info("Paused folder", to.LogAttr())
  475. case from.Paused:
  476. slog.Info("Unpaused folder", to.LogAttr())
  477. default:
  478. slog.Info("Restarted folder", to.LogAttr())
  479. }
  480. return nil
  481. }
  482. func (m *model) newFolder(cfg config.FolderConfiguration, cacheIgnoredFiles bool) error {
  483. m.mut.Lock()
  484. defer m.mut.Unlock()
  485. m.addAndStartFolderLocked(cfg, cacheIgnoredFiles)
  486. // Cluster configs might be received and processed before reaching this
  487. // point, i.e. before the folder is started. If that's the case, start
  488. // index senders here.
  489. m.indexHandlers.Each(func(_ protocol.DeviceID, r *indexHandlerRegistry) error {
  490. runner, _ := m.folderRunners.Get(cfg.ID)
  491. r.RegisterFolderState(cfg, runner)
  492. return nil
  493. })
  494. return nil
  495. }
  496. func (m *model) UsageReportingStats(report *contract.Report, version int, preview bool) {
  497. if version >= 3 {
  498. // Block stats
  499. blockStatsMut.Lock()
  500. for k, v := range blockStats {
  501. switch k {
  502. case "total":
  503. report.BlockStats.Total = v
  504. case "renamed":
  505. report.BlockStats.Renamed = v
  506. case "reused":
  507. report.BlockStats.Reused = v
  508. case "pulled":
  509. report.BlockStats.Pulled = v
  510. case "copyOrigin":
  511. report.BlockStats.CopyOrigin = v
  512. case "copyElsewhere":
  513. report.BlockStats.CopyElsewhere = v
  514. }
  515. // Reset counts, as these are incremental
  516. if !preview {
  517. blockStats[k] = 0
  518. }
  519. }
  520. blockStatsMut.Unlock()
  521. // Transport stats
  522. m.mut.RLock()
  523. for _, conn := range m.connections {
  524. report.TransportStats[conn.Transport()]++
  525. }
  526. m.mut.RUnlock()
  527. // Ignore stats
  528. var seenPrefix [3]bool
  529. for folder := range m.cfg.Folders() {
  530. lines, _, err := m.CurrentIgnores(folder)
  531. if err != nil {
  532. continue
  533. }
  534. report.IgnoreStats.Lines += len(lines)
  535. for _, line := range lines {
  536. // Allow prefixes to be specified in any order, but only once.
  537. loop:
  538. for {
  539. switch {
  540. case strings.HasPrefix(line, "!") && !seenPrefix[0]:
  541. seenPrefix[0] = true
  542. line = line[1:]
  543. report.IgnoreStats.Inverts++
  544. case strings.HasPrefix(line, "(?i)") && !seenPrefix[1]:
  545. seenPrefix[1] = true
  546. line = line[4:]
  547. report.IgnoreStats.Folded++
  548. case strings.HasPrefix(line, "(?d)") && !seenPrefix[2]:
  549. seenPrefix[2] = true
  550. line = line[4:]
  551. report.IgnoreStats.Deletable++
  552. default:
  553. seenPrefix[0] = false
  554. seenPrefix[1] = false
  555. seenPrefix[2] = false
  556. break loop
  557. }
  558. }
  559. // Noops, remove
  560. line = strings.TrimSuffix(line, "**")
  561. line = strings.TrimPrefix(line, "**/")
  562. if strings.HasPrefix(line, "/") {
  563. report.IgnoreStats.Rooted++
  564. } else if strings.HasPrefix(line, "#include ") {
  565. report.IgnoreStats.Includes++
  566. if strings.Contains(line, "..") {
  567. report.IgnoreStats.EscapedIncludes++
  568. }
  569. }
  570. if strings.Contains(line, "**") {
  571. report.IgnoreStats.DoubleStars++
  572. // Remove not to trip up star checks.
  573. line = strings.ReplaceAll(line, "**", "")
  574. }
  575. if strings.Contains(line, "*") {
  576. report.IgnoreStats.Stars++
  577. }
  578. }
  579. }
  580. }
  581. }
  582. type ConnectionStats struct {
  583. protocol.Statistics // Total for primary + secondaries
  584. Connected bool `json:"connected"`
  585. Paused bool `json:"paused"`
  586. ClientVersion string `json:"clientVersion"`
  587. Address string `json:"address"` // mirror values from Primary, for compatibility with <1.24.0
  588. Type string `json:"type"` // mirror values from Primary, for compatibility with <1.24.0
  589. IsLocal bool `json:"isLocal"` // mirror values from Primary, for compatibility with <1.24.0
  590. Crypto string `json:"crypto"` // mirror values from Primary, for compatibility with <1.24.0
  591. Primary ConnectionInfo `json:"primary,omitempty"`
  592. Secondary []ConnectionInfo `json:"secondary,omitempty"`
  593. }
  594. type ConnectionInfo struct {
  595. protocol.Statistics
  596. Address string `json:"address"`
  597. Type string `json:"type"`
  598. IsLocal bool `json:"isLocal"`
  599. Crypto string `json:"crypto"`
  600. }
  601. // ConnectionStats returns a map with connection statistics for each device.
  602. func (m *model) ConnectionStats() map[string]interface{} {
  603. m.mut.RLock()
  604. defer m.mut.RUnlock()
  605. res := make(map[string]interface{})
  606. devs := m.cfg.Devices()
  607. conns := make(map[string]ConnectionStats, len(devs))
  608. for device, deviceCfg := range devs {
  609. if device == m.id {
  610. continue
  611. }
  612. hello := m.helloMessages[device]
  613. versionString := hello.ClientVersion
  614. if hello.ClientName != "syncthing" {
  615. versionString = hello.ClientName + " " + hello.ClientVersion
  616. }
  617. connIDs, ok := m.deviceConnIDs[device]
  618. cs := ConnectionStats{
  619. Connected: ok,
  620. Paused: deviceCfg.Paused,
  621. ClientVersion: strings.TrimSpace(versionString),
  622. }
  623. if ok {
  624. conn := m.connections[connIDs[0]]
  625. cs.Primary.Type = conn.Type()
  626. cs.Primary.IsLocal = conn.IsLocal()
  627. cs.Primary.Crypto = conn.Crypto()
  628. cs.Primary.Statistics = conn.Statistics()
  629. cs.Primary.Address = conn.RemoteAddr().String()
  630. cs.Type = cs.Primary.Type
  631. cs.IsLocal = cs.Primary.IsLocal
  632. cs.Crypto = cs.Primary.Crypto
  633. cs.Address = cs.Primary.Address
  634. cs.Statistics = cs.Primary.Statistics
  635. for _, connID := range connIDs[1:] {
  636. conn = m.connections[connID]
  637. sec := ConnectionInfo{
  638. Statistics: conn.Statistics(),
  639. Address: conn.RemoteAddr().String(),
  640. Type: conn.Type(),
  641. IsLocal: conn.IsLocal(),
  642. Crypto: conn.Crypto(),
  643. }
  644. if sec.At.After(cs.At) {
  645. cs.At = sec.At
  646. }
  647. if sec.StartedAt.Before(cs.StartedAt) {
  648. cs.StartedAt = sec.StartedAt
  649. }
  650. cs.InBytesTotal += sec.InBytesTotal
  651. cs.OutBytesTotal += sec.OutBytesTotal
  652. cs.Secondary = append(cs.Secondary, sec)
  653. }
  654. }
  655. conns[device.String()] = cs
  656. }
  657. res["connections"] = conns
  658. in, out := protocol.TotalInOut()
  659. res["total"] = map[string]interface{}{
  660. "at": time.Now().Truncate(time.Second),
  661. "inBytesTotal": in,
  662. "outBytesTotal": out,
  663. }
  664. return res
  665. }
  666. // DeviceStatistics returns statistics about each device
  667. func (m *model) DeviceStatistics() (map[protocol.DeviceID]stats.DeviceStatistics, error) {
  668. m.mut.RLock()
  669. defer m.mut.RUnlock()
  670. res := make(map[protocol.DeviceID]stats.DeviceStatistics, len(m.deviceStatRefs))
  671. for id, sr := range m.deviceStatRefs {
  672. stats, err := sr.GetStatistics()
  673. if err != nil {
  674. return nil, err
  675. }
  676. if len(m.deviceConnIDs[id]) > 0 {
  677. // If a device is currently connected, we can see them right
  678. // now.
  679. stats.LastSeen = time.Now().Truncate(time.Second)
  680. }
  681. res[id] = stats
  682. }
  683. return res, nil
  684. }
  685. // FolderStatistics returns statistics about each folder
  686. func (m *model) FolderStatistics() (map[string]stats.FolderStatistics, error) {
  687. res := make(map[string]stats.FolderStatistics)
  688. m.mut.RLock()
  689. defer m.mut.RUnlock()
  690. err := m.folderRunners.Each(func(id string, runner service) error {
  691. stats, err := runner.GetStatistics()
  692. if err != nil {
  693. return err
  694. }
  695. res[id] = stats
  696. return nil
  697. })
  698. if err != nil {
  699. return nil, err
  700. }
  701. return res, nil
  702. }
  703. type FolderCompletion struct {
  704. CompletionPct float64
  705. GlobalBytes int64
  706. NeedBytes int64
  707. GlobalItems int
  708. NeedItems int
  709. NeedDeletes int
  710. Sequence int64
  711. RemoteState remoteFolderState
  712. }
  713. func newFolderCompletion(global, need db.Counts, sequence int64, state remoteFolderState) FolderCompletion {
  714. comp := FolderCompletion{
  715. GlobalBytes: global.Bytes,
  716. NeedBytes: need.Bytes,
  717. GlobalItems: global.Files + global.Directories + global.Symlinks,
  718. NeedItems: need.Files + need.Directories + need.Symlinks,
  719. NeedDeletes: need.Deleted,
  720. Sequence: sequence,
  721. RemoteState: state,
  722. }
  723. comp.setCompletionPct()
  724. return comp
  725. }
  726. func (comp *FolderCompletion) add(other FolderCompletion) {
  727. comp.GlobalBytes += other.GlobalBytes
  728. comp.NeedBytes += other.NeedBytes
  729. comp.GlobalItems += other.GlobalItems
  730. comp.NeedItems += other.NeedItems
  731. comp.NeedDeletes += other.NeedDeletes
  732. comp.setCompletionPct()
  733. }
  734. func (comp *FolderCompletion) setCompletionPct() {
  735. if comp.GlobalBytes == 0 {
  736. comp.CompletionPct = 100
  737. } else {
  738. needRatio := float64(comp.NeedBytes) / float64(comp.GlobalBytes)
  739. comp.CompletionPct = 100 * (1 - needRatio)
  740. }
  741. // If the completion is 100% but there are deletes we need to handle,
  742. // drop it down a notch. Hack for consumers that look only at the
  743. // percentage (our own GUI does the same calculation as here on its own
  744. // and needs the same fixup).
  745. if comp.NeedBytes == 0 && comp.NeedDeletes > 0 {
  746. comp.CompletionPct = 95 // chosen by fair dice roll
  747. }
  748. }
  749. // Map returns the members as a map, e.g. used in api to serialize as JSON.
  750. func (comp *FolderCompletion) Map() map[string]interface{} {
  751. return map[string]interface{}{
  752. "completion": comp.CompletionPct,
  753. "globalBytes": comp.GlobalBytes,
  754. "needBytes": comp.NeedBytes,
  755. "globalItems": comp.GlobalItems,
  756. "needItems": comp.NeedItems,
  757. "needDeletes": comp.NeedDeletes,
  758. "sequence": comp.Sequence,
  759. "remoteState": comp.RemoteState,
  760. }
  761. }
  762. // Completion returns the completion status, in percent with some counters,
  763. // for the given device and folder. The device can be any known device ID
  764. // (including the local device) or explicitly protocol.LocalDeviceID. An
  765. // empty folder string means the aggregate of all folders shared with the
  766. // given device.
  767. func (m *model) Completion(device protocol.DeviceID, folder string) (FolderCompletion, error) {
  768. // The user specifically asked for our own device ID. Internally that is
  769. // known as protocol.LocalDeviceID so translate.
  770. if device == m.id {
  771. device = protocol.LocalDeviceID
  772. }
  773. if folder != "" {
  774. // We want completion for a specific folder.
  775. return m.folderCompletion(device, folder)
  776. }
  777. // We want completion for all (shared) folders as an aggregate.
  778. var comp FolderCompletion
  779. for _, fcfg := range m.cfg.FolderList() {
  780. if fcfg.Paused {
  781. continue
  782. }
  783. if device == protocol.LocalDeviceID || fcfg.SharedWith(device) {
  784. folderComp, err := m.folderCompletion(device, fcfg.ID)
  785. if errors.Is(err, ErrFolderPaused) {
  786. continue
  787. } else if err != nil {
  788. return FolderCompletion{}, err
  789. }
  790. comp.add(folderComp)
  791. }
  792. }
  793. return comp, nil
  794. }
  795. func (m *model) folderCompletion(device protocol.DeviceID, folder string) (FolderCompletion, error) {
  796. m.mut.RLock()
  797. err := m.checkFolderRunningRLocked(folder)
  798. m.mut.RUnlock()
  799. if err != nil {
  800. return FolderCompletion{}, err
  801. }
  802. m.mut.RLock()
  803. state := m.remoteFolderStates[device][folder]
  804. downloaded := m.deviceDownloads[device].BytesDownloaded(folder)
  805. m.mut.RUnlock()
  806. need, err := m.sdb.CountNeed(folder, device)
  807. if err != nil {
  808. return FolderCompletion{}, err
  809. }
  810. need.Bytes -= downloaded
  811. // This might be more than it really is, because some blocks can be of a smaller size.
  812. if need.Bytes < 0 {
  813. need.Bytes = 0
  814. }
  815. seq, err := m.sdb.GetDeviceSequence(folder, device)
  816. if err != nil {
  817. return FolderCompletion{}, err
  818. }
  819. glob, err := m.sdb.CountGlobal(folder)
  820. if err != nil {
  821. return FolderCompletion{}, err
  822. }
  823. comp := newFolderCompletion(glob, need, seq, state)
  824. l.Debugf("%v Completion(%s, %q): %v", m, device, folder, comp.Map())
  825. return comp, nil
  826. }
  827. func (m *model) LocalFiles(folder string, device protocol.DeviceID) (iter.Seq[protocol.FileInfo], func() error) {
  828. return m.sdb.AllLocalFiles(folder, device)
  829. }
  830. func (m *model) LocalFilesSequenced(folder string, device protocol.DeviceID, startSeq int64) (iter.Seq[protocol.FileInfo], func() error) {
  831. return m.sdb.AllLocalFilesBySequence(folder, device, startSeq, 0)
  832. }
  833. func (m *model) AllForBlocksHash(folder string, h []byte) (iter.Seq[db.FileMetadata], func() error) {
  834. return m.sdb.AllLocalFilesWithBlocksHash(folder, h)
  835. }
  836. func (m *model) LocalSize(folder string, device protocol.DeviceID) (db.Counts, error) {
  837. return m.sdb.CountLocal(folder, device)
  838. }
  839. func (m *model) GlobalSize(folder string) (db.Counts, error) {
  840. return m.sdb.CountGlobal(folder)
  841. }
  842. func (m *model) NeedSize(folder string, device protocol.DeviceID) (db.Counts, error) {
  843. return m.sdb.CountNeed(folder, device)
  844. }
  845. func (m *model) ReceiveOnlySize(folder string) (db.Counts, error) {
  846. return m.sdb.CountReceiveOnlyChanged(folder)
  847. }
  848. func (m *model) Sequence(folder string, device protocol.DeviceID) (int64, error) {
  849. return m.sdb.GetDeviceSequence(folder, device)
  850. }
  851. func (m *model) AllGlobalFiles(folder string) (iter.Seq[db.FileMetadata], func() error) {
  852. return m.sdb.AllGlobalFiles(folder)
  853. }
  854. func (m *model) RemoteSequences(folder string) (map[protocol.DeviceID]int64, error) {
  855. return m.sdb.RemoteSequences(folder)
  856. }
  857. func (m *model) FolderProgressBytesCompleted(folder string) int64 {
  858. return m.progressEmitter.BytesCompleted(folder)
  859. }
  860. // NeedFolderFiles returns paginated list of currently needed files in
  861. // progress, queued, and to be queued on next puller iteration.
  862. func (m *model) NeedFolderFiles(folder string, page, perpage int) ([]protocol.FileInfo, []protocol.FileInfo, []protocol.FileInfo, error) {
  863. m.mut.RLock()
  864. runner, runnerOk := m.folderRunners.Get(folder)
  865. cfg, cfgOK := m.folderCfgs[folder]
  866. m.mut.RUnlock()
  867. if !cfgOK {
  868. return nil, nil, nil, ErrFolderMissing
  869. }
  870. var progress, queued, rest []protocol.FileInfo
  871. var seen map[string]struct{}
  872. p := newPager(page, perpage)
  873. if runnerOk {
  874. progressNames, queuedNames, skipped := runner.Jobs(page, perpage)
  875. progress = make([]protocol.FileInfo, len(progressNames))
  876. queued = make([]protocol.FileInfo, len(queuedNames))
  877. seen = make(map[string]struct{}, len(progressNames)+len(queuedNames))
  878. for i, name := range progressNames {
  879. if f, ok, err := m.sdb.GetGlobalFile(folder, name); err == nil && ok {
  880. progress[i] = f
  881. seen[name] = struct{}{}
  882. }
  883. }
  884. for i, name := range queuedNames {
  885. if f, ok, err := m.sdb.GetGlobalFile(folder, name); err == nil && ok {
  886. queued[i] = f
  887. seen[name] = struct{}{}
  888. }
  889. }
  890. p.get -= len(seen)
  891. if p.get == 0 {
  892. return progress, queued, nil, nil
  893. }
  894. p.toSkip -= skipped
  895. }
  896. if p.get > 0 {
  897. rest = make([]protocol.FileInfo, 0, p.get)
  898. it, errFn := m.sdb.AllNeededGlobalFiles(folder, protocol.LocalDeviceID, config.PullOrderAlphabetic, 0, 0)
  899. for f := range it {
  900. if cfg.IgnoreDelete && f.IsDeleted() {
  901. continue
  902. }
  903. if p.skip() {
  904. continue
  905. }
  906. if _, ok := seen[f.Name]; !ok {
  907. rest = append(rest, f)
  908. p.get--
  909. }
  910. if p.get == 0 {
  911. break
  912. }
  913. }
  914. if err := errFn(); err != nil {
  915. return nil, nil, nil, err
  916. }
  917. }
  918. return progress, queued, rest, nil
  919. }
  920. // RemoteNeedFolderFiles returns paginated list of currently needed files for a
  921. // remote device to become synced with a folder.
  922. func (m *model) RemoteNeedFolderFiles(folder string, device protocol.DeviceID, page, perpage int) ([]protocol.FileInfo, error) {
  923. m.mut.RLock()
  924. _, ok := m.folderCfgs[folder]
  925. m.mut.RUnlock()
  926. if !ok {
  927. return nil, ErrFolderMissing
  928. }
  929. it, errFn := m.sdb.AllNeededGlobalFiles(folder, device, config.PullOrderAlphabetic, perpage, (page-1)*perpage)
  930. files := slices.Collect(it)
  931. return files, errFn()
  932. }
  933. func (m *model) LocalChangedFolderFiles(folder string, page, perpage int) ([]protocol.FileInfo, error) {
  934. m.mut.RLock()
  935. _, ok := m.folderCfgs[folder]
  936. m.mut.RUnlock()
  937. if !ok {
  938. return nil, ErrFolderMissing
  939. }
  940. ros, err := m.sdb.CountReceiveOnlyChanged(folder)
  941. if err != nil {
  942. return nil, err
  943. }
  944. if ros.TotalItems() == 0 {
  945. return nil, nil
  946. }
  947. p := newPager(page, perpage)
  948. files := make([]protocol.FileInfo, 0, perpage)
  949. // This could be made more efficient with a specifically targeted DB
  950. // call
  951. it, errFn := m.sdb.AllLocalFiles(folder, protocol.LocalDeviceID)
  952. for f := range it {
  953. if !f.IsReceiveOnlyChanged() {
  954. continue
  955. }
  956. if p.skip() {
  957. continue
  958. }
  959. files = append(files, f)
  960. if p.done() {
  961. break
  962. }
  963. }
  964. if err := errFn(); err != nil {
  965. return nil, err
  966. }
  967. return files, nil
  968. }
  969. type pager struct {
  970. toSkip, get int
  971. }
  972. func newPager(page, perpage int) *pager {
  973. return &pager{
  974. toSkip: (page - 1) * perpage,
  975. get: perpage,
  976. }
  977. }
  978. func (p *pager) skip() bool {
  979. if p.toSkip == 0 {
  980. return false
  981. }
  982. p.toSkip--
  983. return true
  984. }
  985. func (p *pager) done() bool {
  986. if p.get > 0 {
  987. p.get--
  988. }
  989. return p.get == 0
  990. }
  991. // Index is called when a new device is connected and we receive their full index.
  992. // Implements the protocol.Model interface.
  993. func (m *model) Index(conn protocol.Connection, idx *protocol.Index) error {
  994. return m.handleIndex(conn, idx.Folder, idx.Files, false, 0, idx.LastSequence)
  995. }
  996. // IndexUpdate is called for incremental updates to connected devices' indexes.
  997. // Implements the protocol.Model interface.
  998. func (m *model) IndexUpdate(conn protocol.Connection, idxUp *protocol.IndexUpdate) error {
  999. return m.handleIndex(conn, idxUp.Folder, idxUp.Files, true, idxUp.PrevSequence, idxUp.LastSequence)
  1000. }
  1001. func (m *model) handleIndex(conn protocol.Connection, folder string, fs []protocol.FileInfo, update bool, prevSequence, lastSequence int64) error {
  1002. op := "Index"
  1003. if update {
  1004. op += " update"
  1005. }
  1006. deviceID := conn.DeviceID()
  1007. l.Debugf("%v (in): %s / %q: %d files", op, deviceID, folder, len(fs))
  1008. if cfg, ok := m.cfg.Folder(folder); !ok || !cfg.SharedWith(deviceID) {
  1009. slog.Warn(`Operation for unexpected folder ID; ensure that the folder exists and that this device is selected under "Share With" in the folder configuration.`, slog.String("operation", op), cfg.LogAttr(), deviceID.LogAttr())
  1010. return fmt.Errorf("%s: %w", folder, ErrFolderMissing)
  1011. } else if cfg.Paused {
  1012. l.Debugf("%v for paused folder (ID %q) sent from device %q.", op, folder, deviceID)
  1013. return fmt.Errorf("%s: %w", folder, ErrFolderPaused)
  1014. }
  1015. m.mut.RLock()
  1016. indexHandler, ok := m.getIndexHandlerRLocked(conn)
  1017. m.mut.RUnlock()
  1018. if !ok {
  1019. // This should be impossible, as an index handler is registered when
  1020. // we send a cluster config, and that is what triggers index
  1021. // sending.
  1022. m.evLogger.Log(events.Failure, "index sender does not exist for connection on which indexes were received")
  1023. l.Debugf("%v for folder (ID %q) sent from device %q: missing index handler", op, folder, deviceID)
  1024. return fmt.Errorf("%s: %w", folder, ErrFolderNotRunning)
  1025. }
  1026. return indexHandler.ReceiveIndex(folder, fs, update, op, prevSequence, lastSequence)
  1027. }
  1028. type clusterConfigDeviceInfo struct {
  1029. local, remote protocol.Device
  1030. }
  1031. type ClusterConfigReceivedEventData struct {
  1032. Device protocol.DeviceID `json:"device"`
  1033. }
  1034. func (m *model) ClusterConfig(conn protocol.Connection, cm *protocol.ClusterConfig) error {
  1035. deviceID := conn.DeviceID()
  1036. if cm.Secondary {
  1037. // No handling of secondary connection ClusterConfigs; they merely
  1038. // indicate the connection is ready to start.
  1039. l.Debugf("Skipping secondary ClusterConfig from %v at %s", deviceID.Short(), conn)
  1040. return nil
  1041. }
  1042. // Check the peer device's announced folders against our own. Emits events
  1043. // for folders that we don't expect (unknown or not shared).
  1044. // Also, collect a list of folders we do share, and if he's interested in
  1045. // temporary indexes, subscribe the connection.
  1046. l.Debugf("Handling ClusterConfig from %v at %s", deviceID.Short(), conn)
  1047. indexHandlerRegistry := m.ensureIndexHandler(conn)
  1048. deviceCfg, ok := m.cfg.Device(deviceID)
  1049. if !ok {
  1050. l.Debugf("Device %s disappeared from config while processing cluster-config", deviceID.Short())
  1051. return errDeviceUnknown
  1052. }
  1053. // Assemble the device information from the connected device about
  1054. // themselves and us for all folders.
  1055. ccDeviceInfos := make(map[string]*clusterConfigDeviceInfo, len(cm.Folders))
  1056. for _, folder := range cm.Folders {
  1057. info := &clusterConfigDeviceInfo{}
  1058. for _, dev := range folder.Devices {
  1059. switch dev.ID {
  1060. case m.id:
  1061. info.local = dev
  1062. case deviceID:
  1063. info.remote = dev
  1064. }
  1065. if info.local.ID != protocol.EmptyDeviceID && info.remote.ID != protocol.EmptyDeviceID {
  1066. break
  1067. }
  1068. }
  1069. if info.remote.ID == protocol.EmptyDeviceID {
  1070. slog.Warn("Device sent cluster-config without the device info for the remote", folder.LogAttr(), deviceID.LogAttr())
  1071. return errMissingRemoteInClusterConfig
  1072. }
  1073. if info.local.ID == protocol.EmptyDeviceID {
  1074. slog.Warn("Device sent cluster-config without the device info for us locally", folder.LogAttr(), deviceID.LogAttr())
  1075. return errMissingLocalInClusterConfig
  1076. }
  1077. ccDeviceInfos[folder.ID] = info
  1078. }
  1079. for _, info := range ccDeviceInfos {
  1080. if deviceCfg.Introducer && info.local.Introducer {
  1081. slog.Error("Remote is an introducer to us, and we are to them - only one should be introducer to the other, see https://docs.syncthing.net/users/introducer.html", deviceCfg.DeviceID.LogAttr())
  1082. }
  1083. break
  1084. }
  1085. // Needs to happen outside of the mut, as can cause CommitConfiguration
  1086. if deviceCfg.AutoAcceptFolders {
  1087. w, _ := m.cfg.Modify(func(cfg *config.Configuration) {
  1088. changedFcfg := make(map[string]config.FolderConfiguration)
  1089. haveFcfg := cfg.FolderMap()
  1090. for _, folder := range cm.Folders {
  1091. from, ok := haveFcfg[folder.ID]
  1092. if to, changed := m.handleAutoAccepts(deviceID, folder, ccDeviceInfos[folder.ID], from, ok, cfg.Defaults.Folder); changed {
  1093. changedFcfg[folder.ID] = to
  1094. }
  1095. }
  1096. if len(changedFcfg) == 0 {
  1097. return
  1098. }
  1099. for i := range cfg.Folders {
  1100. if fcfg, ok := changedFcfg[cfg.Folders[i].ID]; ok {
  1101. cfg.Folders[i] = fcfg
  1102. delete(changedFcfg, cfg.Folders[i].ID)
  1103. }
  1104. }
  1105. for _, fcfg := range changedFcfg {
  1106. cfg.Folders = append(cfg.Folders, fcfg)
  1107. }
  1108. })
  1109. // Need to wait for the waiter, as this calls CommitConfiguration,
  1110. // which sets up the folder and as we return from this call,
  1111. // ClusterConfig starts poking at m.folderFiles and other things
  1112. // that might not exist until the config is committed.
  1113. w.Wait()
  1114. }
  1115. tempIndexFolders, states, err := m.ccHandleFolders(cm.Folders, deviceCfg, ccDeviceInfos, indexHandlerRegistry)
  1116. if err != nil {
  1117. return err
  1118. }
  1119. m.mut.Lock()
  1120. m.remoteFolderStates[deviceID] = states
  1121. m.mut.Unlock()
  1122. m.evLogger.Log(events.ClusterConfigReceived, ClusterConfigReceivedEventData{
  1123. Device: deviceID,
  1124. })
  1125. if len(tempIndexFolders) > 0 {
  1126. var connOK bool
  1127. var conn protocol.Connection
  1128. m.mut.RLock()
  1129. if connIDs, connIDOK := m.deviceConnIDs[deviceID]; connIDOK {
  1130. conn, connOK = m.connections[connIDs[0]]
  1131. }
  1132. m.mut.RUnlock()
  1133. // In case we've got ClusterConfig, and the connection disappeared
  1134. // from infront of our nose.
  1135. if connOK {
  1136. m.progressEmitter.temporaryIndexSubscribe(conn, tempIndexFolders)
  1137. }
  1138. }
  1139. if deviceCfg.Introducer {
  1140. m.cfg.Modify(func(cfg *config.Configuration) {
  1141. folders, devices, foldersDevices, introduced := m.handleIntroductions(deviceCfg, cm, cfg.FolderMap(), cfg.DeviceMap())
  1142. folders, devices, deintroduced := m.handleDeintroductions(deviceCfg, foldersDevices, folders, devices)
  1143. if !introduced && !deintroduced {
  1144. return
  1145. }
  1146. cfg.Folders = make([]config.FolderConfiguration, 0, len(folders))
  1147. for _, fcfg := range folders {
  1148. cfg.Folders = append(cfg.Folders, fcfg)
  1149. }
  1150. cfg.Devices = make([]config.DeviceConfiguration, 0, len(devices))
  1151. for _, dcfg := range devices {
  1152. cfg.Devices = append(cfg.Devices, dcfg)
  1153. }
  1154. })
  1155. }
  1156. return nil
  1157. }
  1158. func (m *model) ensureIndexHandler(conn protocol.Connection) *indexHandlerRegistry {
  1159. deviceID := conn.DeviceID()
  1160. connID := conn.ConnectionID()
  1161. m.mut.Lock()
  1162. defer m.mut.Unlock()
  1163. indexHandlerRegistry, ok := m.indexHandlers.Get(deviceID)
  1164. if ok && indexHandlerRegistry.conn.ConnectionID() == connID {
  1165. // This is an existing and proper index handler for this connection.
  1166. return indexHandlerRegistry
  1167. }
  1168. if ok {
  1169. // A handler exists, but it's for another connection than the one we
  1170. // now got a ClusterConfig on. This should be unusual as it means
  1171. // the other side has decided to start using a new primary
  1172. // connection but we haven't seen it close yet. Ideally it will
  1173. // close shortly by itself...
  1174. slog.Warn("Abandoning old index handler in favour of new connection", deviceID.LogAttr(), slog.String("old", indexHandlerRegistry.conn.ConnectionID()), slog.String("new", connID))
  1175. m.indexHandlers.RemoveAndWait(deviceID, 0)
  1176. }
  1177. // Create a new index handler for this device.
  1178. indexHandlerRegistry = newIndexHandlerRegistry(conn, m.sdb, m.deviceDownloads[deviceID], m.evLogger)
  1179. for id, fcfg := range m.folderCfgs {
  1180. l.Debugln("Registering folder", id, "for", deviceID.Short())
  1181. runner, _ := m.folderRunners.Get(id)
  1182. indexHandlerRegistry.RegisterFolderState(fcfg, runner)
  1183. }
  1184. m.indexHandlers.Add(deviceID, indexHandlerRegistry)
  1185. return indexHandlerRegistry
  1186. }
  1187. func (m *model) getIndexHandlerRLocked(conn protocol.Connection) (*indexHandlerRegistry, bool) {
  1188. // Reads from index handlers, which requires the mutex to be read locked
  1189. deviceID := conn.DeviceID()
  1190. connID := conn.ConnectionID()
  1191. indexHandlerRegistry, ok := m.indexHandlers.Get(deviceID)
  1192. if ok && indexHandlerRegistry.conn.ConnectionID() == connID {
  1193. // This is an existing and proper index handler for this connection.
  1194. return indexHandlerRegistry, true
  1195. }
  1196. // There is no index handler, or it's not registered for this connection.
  1197. return nil, false
  1198. }
  1199. func (m *model) ccHandleFolders(folders []protocol.Folder, deviceCfg config.DeviceConfiguration, ccDeviceInfos map[string]*clusterConfigDeviceInfo, indexHandlers *indexHandlerRegistry) ([]string, map[string]remoteFolderState, error) {
  1200. var folderDevice config.FolderDeviceConfiguration
  1201. tempIndexFolders := make([]string, 0, len(folders))
  1202. seenFolders := make(map[string]remoteFolderState, len(folders))
  1203. updatedPending := make([]updatedPendingFolder, 0, len(folders))
  1204. deviceID := deviceCfg.DeviceID
  1205. expiredPending, err := m.observed.PendingFoldersForDevice(deviceID)
  1206. if err != nil {
  1207. slog.Warn("Failed to list pending folders for cleanup", slogutil.Error(err))
  1208. }
  1209. of := db.ObservedFolder{Time: time.Now().Truncate(time.Second)}
  1210. for _, folder := range folders {
  1211. seenFolders[folder.ID] = remoteFolderValid
  1212. cfg, ok := m.cfg.Folder(folder.ID)
  1213. if ok {
  1214. folderDevice, ok = cfg.Device(deviceID)
  1215. }
  1216. if !ok {
  1217. indexHandlers.Remove(folder.ID)
  1218. if deviceCfg.IgnoredFolder(folder.ID) {
  1219. slog.Info("Ignoring announced folder", folder.LogAttr(), deviceID.LogAttr())
  1220. continue
  1221. }
  1222. delete(expiredPending, folder.ID)
  1223. of.Label = folder.Label
  1224. of.ReceiveEncrypted = len(ccDeviceInfos[folder.ID].local.EncryptionPasswordToken) > 0
  1225. of.RemoteEncrypted = len(ccDeviceInfos[folder.ID].remote.EncryptionPasswordToken) > 0
  1226. if err := m.observed.AddOrUpdatePendingFolder(folder.ID, of, deviceID); err != nil {
  1227. slog.Warn("Failed to persist pending folder entry to database", slogutil.Error(err))
  1228. }
  1229. if folder.IsRunning() {
  1230. indexHandlers.AddIndexInfo(folder.ID, ccDeviceInfos[folder.ID])
  1231. }
  1232. updatedPending = append(updatedPending, updatedPendingFolder{
  1233. FolderID: folder.ID,
  1234. FolderLabel: folder.Label,
  1235. DeviceID: deviceID,
  1236. ReceiveEncrypted: of.ReceiveEncrypted,
  1237. RemoteEncrypted: of.RemoteEncrypted,
  1238. })
  1239. // DEPRECATED: Only for backwards compatibility, should be removed.
  1240. m.evLogger.Log(events.FolderRejected, map[string]string{
  1241. "folder": folder.ID,
  1242. "folderLabel": folder.Label,
  1243. "device": deviceID.String(),
  1244. })
  1245. slog.Warn(`Unexpected folder ID in ClusterConfig; ensure that the folder exists and that this device is selected under "Share With" in the folder configuration.`, folder.LogAttr(), deviceID.LogAttr())
  1246. continue
  1247. }
  1248. if !folder.IsRunning() {
  1249. indexHandlers.Remove(folder.ID)
  1250. seenFolders[cfg.ID] = remoteFolderPaused
  1251. continue
  1252. }
  1253. if cfg.Paused {
  1254. indexHandlers.AddIndexInfo(folder.ID, ccDeviceInfos[folder.ID])
  1255. continue
  1256. }
  1257. if err := m.ccCheckEncryption(cfg, folderDevice, ccDeviceInfos[folder.ID], deviceCfg.Untrusted); err != nil {
  1258. sameError := false
  1259. m.mut.Lock()
  1260. if devs, ok := m.folderEncryptionFailures[folder.ID]; ok {
  1261. sameError = devs[deviceID] == err //nolint:errorlint
  1262. } else {
  1263. m.folderEncryptionFailures[folder.ID] = make(map[protocol.DeviceID]error)
  1264. }
  1265. m.folderEncryptionFailures[folder.ID][deviceID] = err
  1266. m.mut.Unlock()
  1267. const msg = "Failed to verify encryption consistency"
  1268. if sameError {
  1269. slog.Debug(msg, cfg.LogAttr(), deviceID.LogAttr(), slogutil.Error(err))
  1270. } else {
  1271. var rerr *redactedError
  1272. if errors.As(err, &rerr) {
  1273. err = rerr.redacted
  1274. }
  1275. m.evLogger.Log(events.Failure, err.Error())
  1276. slog.Error(msg, cfg.LogAttr(), deviceID.LogAttr(), slogutil.Error(err))
  1277. }
  1278. return tempIndexFolders, seenFolders, err
  1279. }
  1280. m.mut.Lock()
  1281. if devErrs, ok := m.folderEncryptionFailures[folder.ID]; ok {
  1282. if len(devErrs) == 1 {
  1283. delete(m.folderEncryptionFailures, folder.ID)
  1284. } else {
  1285. delete(m.folderEncryptionFailures[folder.ID], deviceID)
  1286. }
  1287. }
  1288. m.mut.Unlock()
  1289. // Handle indexes
  1290. if folder.Type != protocol.FolderTypeReceiveEncrypted {
  1291. tempIndexFolders = append(tempIndexFolders, folder.ID)
  1292. }
  1293. indexHandlers.AddIndexInfo(folder.ID, ccDeviceInfos[folder.ID])
  1294. }
  1295. indexHandlers.RemoveAllExcept(seenFolders)
  1296. // Explicitly mark folders we offer, but the remote has not accepted
  1297. for folderID, cfg := range m.cfg.Folders() {
  1298. if _, seen := seenFolders[folderID]; !seen && cfg.SharedWith(deviceID) {
  1299. l.Debugf("Remote device %v has not accepted sharing folder %s", deviceID.Short(), cfg.Description())
  1300. seenFolders[folderID] = remoteFolderNotSharing
  1301. }
  1302. }
  1303. expiredPendingList := make([]map[string]string, 0, len(expiredPending))
  1304. for folder := range expiredPending {
  1305. if err = m.observed.RemovePendingFolderForDevice(folder, deviceID); err != nil {
  1306. const msg = "Failed to remove pending folder-device entry"
  1307. slog.Warn(msg, slog.String("folder", folder), deviceID.LogAttr(), slogutil.Error(err))
  1308. m.evLogger.Log(events.Failure, msg)
  1309. continue
  1310. }
  1311. expiredPendingList = append(expiredPendingList, map[string]string{
  1312. "folderID": folder,
  1313. "deviceID": deviceID.String(),
  1314. })
  1315. }
  1316. if len(updatedPending) > 0 || len(expiredPendingList) > 0 {
  1317. m.evLogger.Log(events.PendingFoldersChanged, map[string]interface{}{
  1318. "added": updatedPending,
  1319. "removed": expiredPendingList,
  1320. })
  1321. }
  1322. return tempIndexFolders, seenFolders, nil
  1323. }
  1324. func (m *model) ccCheckEncryption(fcfg config.FolderConfiguration, folderDevice config.FolderDeviceConfiguration, ccDeviceInfos *clusterConfigDeviceInfo, deviceUntrusted bool) error {
  1325. hasTokenRemote := len(ccDeviceInfos.remote.EncryptionPasswordToken) > 0
  1326. hasTokenLocal := len(ccDeviceInfos.local.EncryptionPasswordToken) > 0
  1327. isEncryptedRemote := folderDevice.EncryptionPassword != ""
  1328. isEncryptedLocal := fcfg.Type == config.FolderTypeReceiveEncrypted
  1329. if !isEncryptedRemote && !isEncryptedLocal && deviceUntrusted {
  1330. return errEncryptionNotEncryptedUntrusted
  1331. }
  1332. if !(hasTokenRemote || hasTokenLocal || isEncryptedRemote || isEncryptedLocal) {
  1333. // No one cares about encryption here
  1334. return nil
  1335. }
  1336. if isEncryptedRemote && isEncryptedLocal {
  1337. // Should never happen, but config raciness and be safe.
  1338. return errEncryptionInvConfigLocal
  1339. }
  1340. if hasTokenRemote && hasTokenLocal {
  1341. return errEncryptionInvConfigRemote
  1342. }
  1343. if !(hasTokenRemote || hasTokenLocal) {
  1344. if isEncryptedRemote {
  1345. return errEncryptionPlainForRemoteEncrypted
  1346. } else {
  1347. return errEncryptionPlainForReceiveEncrypted
  1348. }
  1349. }
  1350. if !(isEncryptedRemote || isEncryptedLocal) {
  1351. return errEncryptionNotEncryptedLocal
  1352. }
  1353. if isEncryptedRemote {
  1354. passwordToken := protocol.PasswordToken(m.keyGen, fcfg.ID, folderDevice.EncryptionPassword)
  1355. var match bool
  1356. if hasTokenLocal {
  1357. match = bytes.Equal(passwordToken, ccDeviceInfos.local.EncryptionPasswordToken)
  1358. } else {
  1359. // hasTokenRemote == true
  1360. match = bytes.Equal(passwordToken, ccDeviceInfos.remote.EncryptionPasswordToken)
  1361. }
  1362. if !match {
  1363. return errEncryptionPassword
  1364. }
  1365. return nil
  1366. }
  1367. // isEncryptedLocal == true
  1368. var ccToken []byte
  1369. if hasTokenLocal {
  1370. ccToken = ccDeviceInfos.local.EncryptionPasswordToken
  1371. } else {
  1372. // hasTokenRemote == true
  1373. ccToken = ccDeviceInfos.remote.EncryptionPasswordToken
  1374. }
  1375. m.mut.RLock()
  1376. token, ok := m.folderEncryptionPasswordTokens[fcfg.ID]
  1377. m.mut.RUnlock()
  1378. if !ok {
  1379. var err error
  1380. token, err = readEncryptionToken(fcfg)
  1381. if err != nil && !fs.IsNotExist(err) {
  1382. if rerr, ok := redactPathError(err); ok {
  1383. return rerr
  1384. }
  1385. return &redactedError{
  1386. error: err,
  1387. redacted: errEncryptionTokenRead,
  1388. }
  1389. }
  1390. if err == nil {
  1391. m.mut.Lock()
  1392. m.folderEncryptionPasswordTokens[fcfg.ID] = token
  1393. m.mut.Unlock()
  1394. } else {
  1395. if err := writeEncryptionToken(ccToken, fcfg); err != nil {
  1396. if rerr, ok := redactPathError(err); ok {
  1397. return rerr
  1398. } else {
  1399. return &redactedError{
  1400. error: err,
  1401. redacted: errEncryptionTokenWrite,
  1402. }
  1403. }
  1404. }
  1405. m.mut.Lock()
  1406. m.folderEncryptionPasswordTokens[fcfg.ID] = ccToken
  1407. m.mut.Unlock()
  1408. // We can only announce ourselves once we have the token,
  1409. // thus we need to resend CCs now that we have it.
  1410. m.sendClusterConfig(fcfg.DeviceIDs())
  1411. return nil
  1412. }
  1413. }
  1414. if !bytes.Equal(token, ccToken) {
  1415. return errEncryptionPassword
  1416. }
  1417. return nil
  1418. }
  1419. func (m *model) sendClusterConfig(ids []protocol.DeviceID) {
  1420. if len(ids) == 0 {
  1421. return
  1422. }
  1423. ccConns := make([]protocol.Connection, 0, len(ids))
  1424. m.mut.RLock()
  1425. for _, id := range ids {
  1426. if connIDs, ok := m.deviceConnIDs[id]; ok {
  1427. ccConns = append(ccConns, m.connections[connIDs[0]])
  1428. }
  1429. }
  1430. m.mut.RUnlock()
  1431. // Generating cluster-configs acquires the mutex.
  1432. for _, conn := range ccConns {
  1433. cm, passwords := m.generateClusterConfig(conn.DeviceID())
  1434. go conn.ClusterConfig(cm, passwords)
  1435. }
  1436. }
  1437. // handleIntroductions handles adding devices/folders that are shared by an introducer device
  1438. func (m *model) handleIntroductions(introducerCfg config.DeviceConfiguration, cm *protocol.ClusterConfig, folders map[string]config.FolderConfiguration, devices map[protocol.DeviceID]config.DeviceConfiguration) (map[string]config.FolderConfiguration, map[protocol.DeviceID]config.DeviceConfiguration, folderDeviceSet, bool) {
  1439. changed := false
  1440. foldersDevices := make(folderDeviceSet)
  1441. for _, folder := range cm.Folders {
  1442. // Adds devices which we do not have, but the introducer has
  1443. // for the folders that we have in common. Also, shares folders
  1444. // with devices that we have in common, yet are currently not sharing
  1445. // the folder.
  1446. fcfg, ok := folders[folder.ID]
  1447. if !ok {
  1448. // Don't have this folder, carry on.
  1449. continue
  1450. }
  1451. folderChanged := false
  1452. for _, device := range folder.Devices {
  1453. // No need to share with self.
  1454. if device.ID == m.id {
  1455. continue
  1456. }
  1457. foldersDevices.set(device.ID, folder.ID)
  1458. if _, ok := devices[device.ID]; !ok {
  1459. // The device is currently unknown. Add it to the config.
  1460. devices[device.ID] = m.introduceDevice(device, introducerCfg)
  1461. } else if fcfg.SharedWith(device.ID) {
  1462. // We already share the folder with this device, so
  1463. // nothing to do.
  1464. continue
  1465. }
  1466. if fcfg.Type != config.FolderTypeReceiveEncrypted && device.EncryptionPasswordToken != nil {
  1467. slog.Warn("Cannot share folder in untrusted mode with introduced device because it requires a password", folder.LogAttr(), slog.Any("device", device.ID), slog.Any("introducer", introducerCfg.DeviceID))
  1468. continue
  1469. }
  1470. // We don't yet share this folder with this device. Add the device
  1471. // to sharing list of the folder.
  1472. slog.Info("Sharing folder vouched for by introducer", folder.LogAttr(), slog.Any("device", device.ID), slog.Any("introducer", introducerCfg.DeviceID))
  1473. fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{
  1474. DeviceID: device.ID,
  1475. IntroducedBy: introducerCfg.DeviceID,
  1476. })
  1477. folderChanged = true
  1478. }
  1479. if folderChanged {
  1480. folders[fcfg.ID] = fcfg
  1481. changed = true
  1482. }
  1483. }
  1484. return folders, devices, foldersDevices, changed
  1485. }
  1486. // handleDeintroductions handles removals of devices/shares that are removed by an introducer device
  1487. func (*model) handleDeintroductions(introducerCfg config.DeviceConfiguration, foldersDevices folderDeviceSet, folders map[string]config.FolderConfiguration, devices map[protocol.DeviceID]config.DeviceConfiguration) (map[string]config.FolderConfiguration, map[protocol.DeviceID]config.DeviceConfiguration, bool) {
  1488. if introducerCfg.SkipIntroductionRemovals {
  1489. return folders, devices, false
  1490. }
  1491. changed := false
  1492. devicesNotIntroduced := make(map[protocol.DeviceID]struct{})
  1493. // Check if we should unshare some folders, if the introducer has unshared them.
  1494. for folderID, folderCfg := range folders {
  1495. for k := 0; k < len(folderCfg.Devices); k++ {
  1496. if folderCfg.Devices[k].IntroducedBy != introducerCfg.DeviceID {
  1497. devicesNotIntroduced[folderCfg.Devices[k].DeviceID] = struct{}{}
  1498. continue
  1499. }
  1500. if !foldersDevices.has(folderCfg.Devices[k].DeviceID, folderCfg.ID) {
  1501. // We could not find that folder shared on the
  1502. // introducer with the device that was introduced to us.
  1503. // We should follow and unshare as well.
  1504. slog.Info("Unsharing folder as introducer no longer shares the folder with that device", folderCfg.LogAttr(), slog.Any("device", folderCfg.Devices[k].DeviceID), slog.Any("introducer", folderCfg.Devices[k].IntroducedBy))
  1505. folderCfg.Devices = append(folderCfg.Devices[:k], folderCfg.Devices[k+1:]...)
  1506. folders[folderID] = folderCfg
  1507. k--
  1508. changed = true
  1509. }
  1510. }
  1511. }
  1512. // Check if we should remove some devices, if the introducer no longer
  1513. // shares any folder with them. Yet do not remove if we share other
  1514. // folders that haven't been introduced by the introducer.
  1515. for deviceID, device := range devices {
  1516. if device.IntroducedBy == introducerCfg.DeviceID {
  1517. if !foldersDevices.hasDevice(deviceID) {
  1518. if _, ok := devicesNotIntroduced[deviceID]; !ok {
  1519. // The introducer no longer shares any folder with the
  1520. // device, remove the device.
  1521. slog.Info("Removing device as introducer no longer shares any folders with that device", "device", deviceID, "introducer", device.IntroducedBy)
  1522. changed = true
  1523. delete(devices, deviceID)
  1524. continue
  1525. }
  1526. }
  1527. }
  1528. }
  1529. return folders, devices, changed
  1530. }
  1531. // handleAutoAccepts handles adding and sharing folders for devices that have
  1532. // AutoAcceptFolders set to true.
  1533. func (m *model) handleAutoAccepts(deviceID protocol.DeviceID, folder protocol.Folder, ccDeviceInfos *clusterConfigDeviceInfo, cfg config.FolderConfiguration, haveCfg bool, defaultFolderCfg config.FolderConfiguration) (config.FolderConfiguration, bool) {
  1534. if !haveCfg {
  1535. defaultPathFs := fs.NewFilesystem(defaultFolderCfg.FilesystemType.ToFS(), defaultFolderCfg.Path)
  1536. var pathAlternatives []string
  1537. if alt := fs.SanitizePath(folder.Label); alt != "" {
  1538. pathAlternatives = append(pathAlternatives, alt)
  1539. }
  1540. if alt := fs.SanitizePath(folder.ID); alt != "" {
  1541. pathAlternatives = append(pathAlternatives, alt)
  1542. }
  1543. if len(pathAlternatives) == 0 {
  1544. slog.Error("Failed to auto-accept folder due to lack of path alternatives", folder.LogAttr(), deviceID.LogAttr())
  1545. return config.FolderConfiguration{}, false
  1546. }
  1547. for _, path := range pathAlternatives {
  1548. // Make sure the folder path doesn't already exist.
  1549. if _, err := defaultPathFs.Lstat(path); !fs.IsNotExist(err) {
  1550. continue
  1551. }
  1552. // Attempt to create it to make sure it does, now.
  1553. fullPath := filepath.Join(defaultFolderCfg.Path, path)
  1554. if err := defaultPathFs.MkdirAll(path, 0o700); err != nil {
  1555. slog.Error("Failed to create path for auto-accepted folder", folder.LogAttr(), slogutil.FilePath(fullPath), slogutil.Error(err))
  1556. continue
  1557. }
  1558. fcfg := newFolderConfiguration(m.cfg, folder.ID, folder.Label, defaultFolderCfg.FilesystemType, fullPath)
  1559. fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{
  1560. DeviceID: deviceID,
  1561. })
  1562. if len(ccDeviceInfos.remote.EncryptionPasswordToken) > 0 || len(ccDeviceInfos.local.EncryptionPasswordToken) > 0 {
  1563. fcfg.Type = config.FolderTypeReceiveEncrypted
  1564. // Override the user-configured defaults, as normally done by the GUI
  1565. fcfg.FSWatcherEnabled = false
  1566. if fcfg.RescanIntervalS != 0 {
  1567. minRescanInterval := 3600 * 24
  1568. if fcfg.RescanIntervalS < minRescanInterval {
  1569. fcfg.RescanIntervalS = minRescanInterval
  1570. }
  1571. }
  1572. fcfg.Versioning.Reset()
  1573. // Other necessary settings are ensured by FolderConfiguration itself
  1574. } else {
  1575. ignores := m.cfg.DefaultIgnores()
  1576. if err := m.setIgnores(fcfg, ignores.Lines); err != nil {
  1577. slog.Error("Failed to apply default ignores to auto-accepted folder", folder.LogAttr(), slogutil.FilePath(fullPath), slogutil.Error(err))
  1578. }
  1579. }
  1580. slog.Info("Auto-accepted folder", fcfg.LogAttr(), slogutil.FilePath(fcfg.Path))
  1581. return fcfg, true
  1582. }
  1583. slog.Error("Failed to auto-accept folder due to path conflict", folder.LogAttr(), deviceID.LogAttr())
  1584. return config.FolderConfiguration{}, false
  1585. } else {
  1586. if slices.Contains(cfg.DeviceIDs(), deviceID) {
  1587. // Already shared nothing todo.
  1588. return config.FolderConfiguration{}, false
  1589. }
  1590. if cfg.Type == config.FolderTypeReceiveEncrypted {
  1591. if len(ccDeviceInfos.remote.EncryptionPasswordToken) == 0 && len(ccDeviceInfos.local.EncryptionPasswordToken) == 0 {
  1592. slog.Info("Failed to auto-accept device on existing folder as the remote wants to send us unencrypted data, but the folder type is receive-encrypted", folder.LogAttr(), deviceID.LogAttr())
  1593. return config.FolderConfiguration{}, false
  1594. }
  1595. } else {
  1596. if len(ccDeviceInfos.remote.EncryptionPasswordToken) > 0 || len(ccDeviceInfos.local.EncryptionPasswordToken) > 0 {
  1597. slog.Info("Failed to auto-accept device on existing folder as the remote wants to send us encrypted data, but the folder type is not receive-encrypted", folder.LogAttr(), deviceID.LogAttr())
  1598. return config.FolderConfiguration{}, false
  1599. }
  1600. }
  1601. cfg.Devices = append(cfg.Devices, config.FolderDeviceConfiguration{
  1602. DeviceID: deviceID,
  1603. })
  1604. slog.Info("Shared folder due to auto-accept", folder.LogAttr(), deviceID.LogAttr())
  1605. return cfg, true
  1606. }
  1607. }
  1608. func (m *model) introduceDevice(device protocol.Device, introducerCfg config.DeviceConfiguration) config.DeviceConfiguration {
  1609. addresses := []string{"dynamic"}
  1610. for _, addr := range device.Addresses {
  1611. if addr != "dynamic" {
  1612. addresses = append(addresses, addr)
  1613. }
  1614. }
  1615. slog.Info("Adding device to config (vouched for by introducer)", device.ID.LogAttr(), slog.Any("introducer", introducerCfg.DeviceID.Short()))
  1616. newDeviceCfg := m.cfg.DefaultDevice()
  1617. newDeviceCfg.DeviceID = device.ID
  1618. newDeviceCfg.Name = device.Name
  1619. newDeviceCfg.Compression = introducerCfg.Compression
  1620. newDeviceCfg.Addresses = addresses
  1621. newDeviceCfg.CertName = device.CertName
  1622. newDeviceCfg.IntroducedBy = introducerCfg.DeviceID
  1623. // The introducers' introducers are also our introducers.
  1624. if device.Introducer {
  1625. slog.Info("Device is now also an introducer", device.ID.LogAttr())
  1626. newDeviceCfg.Introducer = true
  1627. newDeviceCfg.SkipIntroductionRemovals = device.SkipIntroductionRemovals
  1628. }
  1629. return newDeviceCfg
  1630. }
  1631. // Closed is called when a connection has been closed
  1632. func (m *model) Closed(conn protocol.Connection, err error) {
  1633. connID := conn.ConnectionID()
  1634. deviceID := conn.DeviceID()
  1635. m.mut.Lock()
  1636. conn, ok := m.connections[connID]
  1637. if !ok {
  1638. m.mut.Unlock()
  1639. return
  1640. }
  1641. closed := m.closed[connID]
  1642. delete(m.closed, connID)
  1643. delete(m.connections, connID)
  1644. removedIsPrimary := m.promotedConnID[deviceID] == connID
  1645. remainingConns := without(m.deviceConnIDs[deviceID], connID)
  1646. var wait <-chan error
  1647. if removedIsPrimary {
  1648. m.progressEmitter.temporaryIndexUnsubscribe(conn)
  1649. if idxh, ok := m.indexHandlers.Get(deviceID); ok && idxh.conn.ConnectionID() == connID {
  1650. wait = m.indexHandlers.RemoveAndWaitChan(deviceID, 0)
  1651. }
  1652. m.scheduleConnectionPromotion()
  1653. }
  1654. if len(remainingConns) == 0 {
  1655. // All device connections closed
  1656. delete(m.deviceConnIDs, deviceID)
  1657. delete(m.promotedConnID, deviceID)
  1658. delete(m.connRequestLimiters, deviceID)
  1659. delete(m.helloMessages, deviceID)
  1660. delete(m.remoteFolderStates, deviceID)
  1661. delete(m.deviceDownloads, deviceID)
  1662. } else {
  1663. // Some connections remain
  1664. m.deviceConnIDs[deviceID] = remainingConns
  1665. }
  1666. m.mut.Unlock()
  1667. if wait != nil {
  1668. <-wait
  1669. }
  1670. m.mut.RLock()
  1671. m.deviceDidCloseRLocked(deviceID, time.Since(conn.EstablishedAt()))
  1672. m.mut.RUnlock()
  1673. k := map[bool]string{false: "secondary", true: "primary"}[removedIsPrimary]
  1674. slog.Info("Lost device connection", slog.String("kind", k), deviceID.LogAttr(), slog.Any("connection", conn), slogutil.Error(err), slog.Int("remaining", len(remainingConns)))
  1675. if len(remainingConns) == 0 {
  1676. slog.Info("Connection closed", deviceID.LogAttr(), slog.Any("connection", conn), slogutil.Error(err))
  1677. m.evLogger.Log(events.DeviceDisconnected, map[string]string{
  1678. "id": deviceID.String(),
  1679. "error": err.Error(),
  1680. })
  1681. }
  1682. close(closed)
  1683. }
  1684. // Implements protocol.RequestResponse
  1685. type requestResponse struct {
  1686. data []byte
  1687. closed chan struct{}
  1688. once sync.Once
  1689. }
  1690. func newRequestResponse(size int) *requestResponse {
  1691. return &requestResponse{
  1692. data: protocol.BufferPool.Get(size),
  1693. closed: make(chan struct{}),
  1694. }
  1695. }
  1696. func (r *requestResponse) Data() []byte {
  1697. return r.data
  1698. }
  1699. func (r *requestResponse) Close() {
  1700. r.once.Do(func() {
  1701. protocol.BufferPool.Put(r.data)
  1702. close(r.closed)
  1703. })
  1704. }
  1705. func (r *requestResponse) Wait() {
  1706. <-r.closed
  1707. }
  1708. // Request returns the specified data segment by reading it from local disk.
  1709. // Implements the protocol.Model interface.
  1710. func (m *model) Request(conn protocol.Connection, req *protocol.Request) (out protocol.RequestResponse, err error) {
  1711. if req.Size < 0 || req.Offset < 0 {
  1712. return nil, protocol.ErrInvalid
  1713. }
  1714. deviceID := conn.DeviceID()
  1715. m.mut.RLock()
  1716. folderCfg, ok := m.folderCfgs[req.Folder]
  1717. folderIgnores := m.folderIgnores[req.Folder]
  1718. m.mut.RUnlock()
  1719. if !ok {
  1720. // The folder might be already unpaused in the config, but not yet
  1721. // in the model.
  1722. l.Debugf("Request from %s for file %s in unstarted folder %q", deviceID.Short(), req.Name, req.Folder)
  1723. return nil, protocol.ErrGeneric
  1724. }
  1725. if !folderCfg.SharedWith(deviceID) {
  1726. slog.Warn("Request for file in unshared folder", slog.String("folder", req.Folder), deviceID.LogAttr(), slogutil.FilePath(req.Name))
  1727. return nil, protocol.ErrGeneric
  1728. }
  1729. if folderCfg.Paused {
  1730. l.Debugf("Request from %s for file %s in paused folder %q", deviceID.Short(), req.Name, req.Folder)
  1731. return nil, protocol.ErrGeneric
  1732. }
  1733. // Make sure the path is valid and in canonical form
  1734. if name, err := fs.Canonicalize(req.Name); err != nil {
  1735. l.Debugf("Request from %s in folder %q for invalid filename %s", deviceID.Short(), req.Folder, req.Name)
  1736. return nil, protocol.ErrGeneric
  1737. } else {
  1738. req.Name = name
  1739. }
  1740. if deviceID != protocol.LocalDeviceID {
  1741. l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d t=%v", m, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size, req.FromTemporary)
  1742. }
  1743. if fs.IsInternal(req.Name) {
  1744. l.Debugf("%v REQ(in) for internal file: %s: %q / %q o=%d s=%d", m, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1745. return nil, protocol.ErrInvalid
  1746. }
  1747. if folderIgnores.Match(req.Name).IsIgnored() {
  1748. l.Debugf("%v REQ(in) for ignored file: %s: %q / %q o=%d s=%d", m, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1749. return nil, protocol.ErrInvalid
  1750. }
  1751. // Restrict parallel requests by connection/device
  1752. m.mut.RLock()
  1753. limiter := m.connRequestLimiters[deviceID]
  1754. m.mut.RUnlock()
  1755. // The requestResponse releases the bytes to the buffer pool and the
  1756. // limiters when its Close method is called.
  1757. res := newLimitedRequestResponse(req.Size, limiter, m.globalRequestLimiter)
  1758. defer func() {
  1759. // Close it ourselves if it isn't returned due to an error
  1760. if err != nil {
  1761. res.Close()
  1762. }
  1763. }()
  1764. // Grab the FS after limiting, as it causes I/O and we want to minimize
  1765. // the race time between the symlink check and the read.
  1766. folderFs := folderCfg.Filesystem()
  1767. if err := osutil.TraversesSymlink(folderFs, filepath.Dir(req.Name)); err != nil {
  1768. l.Debugf("%v REQ(in) traversal check: %s - %s: %q / %q o=%d s=%d", m, err, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1769. return nil, protocol.ErrNoSuchFile
  1770. }
  1771. // Only check temp files if the flag is set, and if we are set to advertise
  1772. // the temp indexes.
  1773. if req.FromTemporary {
  1774. tempFn := fs.TempName(req.Name)
  1775. if info, err := folderFs.Lstat(tempFn); err != nil || !info.IsRegular() {
  1776. // Reject reads for anything that doesn't exist or is something
  1777. // other than a regular file.
  1778. l.Debugf("%v REQ(in) failed stating temp file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1779. return nil, protocol.ErrNoSuchFile
  1780. }
  1781. _, err := readOffsetIntoBuf(folderFs, tempFn, req.Offset, res.data)
  1782. if err == nil && scanner.Validate(res.data, req.Hash) {
  1783. return res, nil
  1784. }
  1785. // Fall through to reading from a non-temp file, just in case the temp
  1786. // file has finished downloading.
  1787. }
  1788. if info, err := folderFs.Lstat(req.Name); err != nil || !info.IsRegular() {
  1789. // Reject reads for anything that doesn't exist or is something
  1790. // other than a regular file.
  1791. l.Debugf("%v REQ(in) failed stating file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1792. return nil, protocol.ErrNoSuchFile
  1793. }
  1794. n, err := readOffsetIntoBuf(folderFs, req.Name, req.Offset, res.data)
  1795. switch {
  1796. case fs.IsNotExist(err):
  1797. l.Debugf("%v REQ(in) file doesn't exist: %s: %q / %q o=%d s=%d", m, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1798. return nil, protocol.ErrNoSuchFile
  1799. case errors.Is(err, io.EOF):
  1800. // Read beyond end of file. This might indicate a problem, or it
  1801. // might be a short block that gets padded when read for encrypted
  1802. // folders. We ignore the error and let the hash validation in the
  1803. // next step take care of it, by only hashing the part we actually
  1804. // managed to read.
  1805. case err != nil:
  1806. l.Debugf("%v REQ(in) failed reading file (%v): %s: %q / %q o=%d s=%d", m, err, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1807. return nil, protocol.ErrGeneric
  1808. }
  1809. if folderCfg.Type != config.FolderTypeReceiveEncrypted && len(req.Hash) > 0 && !scanner.Validate(res.data[:n], req.Hash) {
  1810. m.recheckFile(deviceID, req.Folder, req.Name, req.Offset, req.Hash)
  1811. l.Debugf("%v REQ(in) failed validating data: %s: %q / %q o=%d s=%d", m, deviceID.Short(), req.Folder, req.Name, req.Offset, req.Size)
  1812. return nil, protocol.ErrNoSuchFile
  1813. }
  1814. return res, nil
  1815. }
  1816. // newLimitedRequestResponse takes size bytes from the limiters in order,
  1817. // skipping nil limiters, then returns a requestResponse of the given size.
  1818. // When the requestResponse is closed the limiters are given back the bytes,
  1819. // in reverse order.
  1820. func newLimitedRequestResponse(size int, limiters ...*semaphore.Semaphore) *requestResponse {
  1821. multi := semaphore.MultiSemaphore(limiters)
  1822. multi.Take(size)
  1823. res := newRequestResponse(size)
  1824. go func() {
  1825. res.Wait()
  1826. multi.Give(size)
  1827. }()
  1828. return res
  1829. }
  1830. func (m *model) recheckFile(deviceID protocol.DeviceID, folder, name string, offset int64, hash []byte) {
  1831. cf, ok, err := m.CurrentFolderFile(folder, name)
  1832. if err != nil {
  1833. l.Debugf("%v recheckFile: %s: %q / %q: current file error: %v", m, deviceID, folder, name, err)
  1834. return
  1835. }
  1836. if !ok {
  1837. l.Debugf("%v recheckFile: %s: %q / %q: no current file", m, deviceID, folder, name)
  1838. return
  1839. }
  1840. if cf.IsDeleted() || cf.IsInvalid() || cf.IsSymlink() || cf.IsDirectory() {
  1841. l.Debugf("%v recheckFile: %s: %q / %q: not a regular file", m, deviceID, folder, name)
  1842. return
  1843. }
  1844. blockIndex := int(offset / int64(cf.BlockSize()))
  1845. if blockIndex >= len(cf.Blocks) {
  1846. l.Debugf("%v recheckFile: %s: %q / %q i=%d: block index too far", m, deviceID, folder, name, blockIndex)
  1847. return
  1848. }
  1849. block := cf.Blocks[blockIndex]
  1850. // Seems to want a different version of the file, whatever.
  1851. if !bytes.Equal(block.Hash, hash) {
  1852. l.Debugf("%v recheckFile: %s: %q / %q i=%d: hash mismatch %x != %x", m, deviceID, folder, name, blockIndex, block.Hash, hash)
  1853. return
  1854. }
  1855. // The hashes provided part of the request match what we expect to find according
  1856. // to what we have in the database, yet the content we've read off the filesystem doesn't
  1857. // Something is fishy, invalidate the file and rescan it.
  1858. // The file will temporarily become invalid, which is ok as the content is messed up.
  1859. m.mut.RLock()
  1860. runner, ok := m.folderRunners.Get(folder)
  1861. m.mut.RUnlock()
  1862. if !ok {
  1863. l.Debugf("%v recheckFile: %s: %q / %q: Folder stopped before rescan could be scheduled", m, deviceID, folder, name)
  1864. return
  1865. }
  1866. runner.ScheduleForceRescan(name)
  1867. l.Debugf("%v recheckFile: %s: %q / %q", m, deviceID, folder, name)
  1868. }
  1869. func (m *model) CurrentFolderFile(folder string, file string) (protocol.FileInfo, bool, error) {
  1870. return m.sdb.GetDeviceFile(folder, protocol.LocalDeviceID, file)
  1871. }
  1872. func (m *model) CurrentGlobalFile(folder string, file string) (protocol.FileInfo, bool, error) {
  1873. return m.sdb.GetGlobalFile(folder, file)
  1874. }
  1875. // Connection returns if we are connected to the given device.
  1876. func (m *model) ConnectedTo(deviceID protocol.DeviceID) bool {
  1877. m.mut.RLock()
  1878. _, ok := m.deviceConnIDs[deviceID]
  1879. m.mut.RUnlock()
  1880. return ok
  1881. }
  1882. // LoadIgnores loads or refreshes the ignore patterns from disk, if the
  1883. // folder is healthy, and returns the refreshed lines and patterns.
  1884. func (m *model) LoadIgnores(folder string) ([]string, []string, error) {
  1885. m.mut.RLock()
  1886. cfg, cfgOk := m.folderCfgs[folder]
  1887. ignores, ignoresOk := m.folderIgnores[folder]
  1888. m.mut.RUnlock()
  1889. if !cfgOk {
  1890. cfg, cfgOk = m.cfg.Folder(folder)
  1891. if !cfgOk {
  1892. return nil, nil, fmt.Errorf("folder %s does not exist", folder)
  1893. }
  1894. }
  1895. if cfg.Type == config.FolderTypeReceiveEncrypted {
  1896. return nil, nil, nil
  1897. }
  1898. if !ignoresOk {
  1899. ignores = ignore.New(cfg.Filesystem())
  1900. }
  1901. err := ignores.Load(".stignore")
  1902. if fs.IsNotExist(err) {
  1903. // Having no ignores is not an error.
  1904. return nil, nil, nil
  1905. }
  1906. // Return lines and patterns, which may have some meaning even when err
  1907. // != nil, depending on the specific error.
  1908. return ignores.Lines(), ignores.Patterns(), err
  1909. }
  1910. // CurrentIgnores returns the currently loaded set of ignore patterns,
  1911. // whichever it may be. No attempt is made to load or refresh ignore
  1912. // patterns from disk.
  1913. func (m *model) CurrentIgnores(folder string) ([]string, []string, error) {
  1914. m.mut.RLock()
  1915. _, cfgOk := m.folderCfgs[folder]
  1916. ignores, ignoresOk := m.folderIgnores[folder]
  1917. m.mut.RUnlock()
  1918. if !cfgOk {
  1919. return nil, nil, fmt.Errorf("folder %s does not exist", folder)
  1920. }
  1921. if !ignoresOk {
  1922. // Empty ignore patterns
  1923. return []string{}, []string{}, nil
  1924. }
  1925. return ignores.Lines(), ignores.Patterns(), nil
  1926. }
  1927. func (m *model) SetIgnores(folder string, content []string) error {
  1928. cfg, ok := m.cfg.Folder(folder)
  1929. if !ok {
  1930. return fmt.Errorf("folder %s does not exist", cfg.Description())
  1931. }
  1932. return m.setIgnores(cfg, content)
  1933. }
  1934. func (m *model) setIgnores(cfg config.FolderConfiguration, content []string) error {
  1935. err := cfg.CheckPath()
  1936. if errors.Is(err, config.ErrPathMissing) {
  1937. if err = cfg.CreateRoot(); err != nil {
  1938. return fmt.Errorf("failed to create folder root: %w", err)
  1939. }
  1940. err = cfg.CheckPath()
  1941. }
  1942. if err != nil && !errors.Is(err, config.ErrMarkerMissing) {
  1943. return err
  1944. }
  1945. if err := ignore.WriteIgnores(cfg.Filesystem(), ".stignore", content); err != nil {
  1946. slog.Error("Failed to save .stignore", slogutil.Error(err))
  1947. return err
  1948. }
  1949. m.mut.RLock()
  1950. runner, ok := m.folderRunners.Get(cfg.ID)
  1951. m.mut.RUnlock()
  1952. if ok {
  1953. runner.ScheduleScan()
  1954. }
  1955. return nil
  1956. }
  1957. // OnHello is called when an device connects to us.
  1958. // This allows us to extract some information from the Hello message
  1959. // and add it to a list of known devices ahead of any checks.
  1960. func (m *model) OnHello(remoteID protocol.DeviceID, addr net.Addr, hello protocol.Hello) error {
  1961. if _, ok := m.cfg.Device(remoteID); !ok {
  1962. if err := m.observed.AddOrUpdatePendingDevice(remoteID, hello.DeviceName, addr.String()); err != nil {
  1963. slog.Warn("Failed to persist pending device entry to database", slogutil.Error(err))
  1964. }
  1965. m.evLogger.Log(events.PendingDevicesChanged, map[string][]interface{}{
  1966. "added": {map[string]string{
  1967. "deviceID": remoteID.String(),
  1968. "name": hello.DeviceName,
  1969. "address": addr.String(),
  1970. }},
  1971. })
  1972. // DEPRECATED: Only for backwards compatibility, should be removed.
  1973. m.evLogger.Log(events.DeviceRejected, map[string]string{
  1974. "name": hello.DeviceName,
  1975. "device": remoteID.String(),
  1976. "address": addr.String(),
  1977. })
  1978. return errDeviceUnknown
  1979. }
  1980. return nil
  1981. }
  1982. // AddConnection adds a new peer connection to the model. An initial index will
  1983. // be sent to the connected peer, thereafter index updates whenever the local
  1984. // folder changes.
  1985. func (m *model) AddConnection(conn protocol.Connection, hello protocol.Hello) {
  1986. deviceID := conn.DeviceID()
  1987. deviceCfg, ok := m.cfg.Device(deviceID)
  1988. if !ok {
  1989. slog.Info("Trying to add connection to unknown device")
  1990. return
  1991. }
  1992. connID := conn.ConnectionID()
  1993. closed := make(chan struct{})
  1994. m.mut.Lock()
  1995. m.connections[connID] = conn
  1996. m.closed[connID] = closed
  1997. m.helloMessages[deviceID] = hello
  1998. m.deviceConnIDs[deviceID] = append(m.deviceConnIDs[deviceID], connID)
  1999. if m.deviceDownloads[deviceID] == nil {
  2000. m.deviceDownloads[deviceID] = newDeviceDownloadState()
  2001. }
  2002. event := map[string]string{
  2003. "id": deviceID.String(),
  2004. "deviceName": hello.DeviceName,
  2005. "clientName": hello.ClientName,
  2006. "clientVersion": hello.ClientVersion,
  2007. "type": conn.Type(),
  2008. }
  2009. addr := conn.RemoteAddr()
  2010. if addr != nil {
  2011. event["addr"] = addr.String()
  2012. }
  2013. m.evLogger.Log(events.DeviceConnected, event)
  2014. if len(m.deviceConnIDs[deviceID]) == 1 {
  2015. slog.Info("New device connection", deviceID.LogAttr(), slogutil.Address(conn.RemoteAddr()), slog.Group("remote", slog.String("name", hello.DeviceName), slog.String("client", hello.ClientName), slog.String("version", hello.ClientVersion)))
  2016. } else {
  2017. slog.Info("Additional device connection", deviceID.LogAttr(), slogutil.Address(conn.RemoteAddr()), slog.Int("count", len(m.deviceConnIDs[deviceID])-1))
  2018. }
  2019. m.mut.Unlock()
  2020. if (deviceCfg.Name == "" || m.cfg.Options().OverwriteRemoteDevNames) && hello.DeviceName != "" {
  2021. m.cfg.Modify(func(cfg *config.Configuration) {
  2022. for i := range cfg.Devices {
  2023. if cfg.Devices[i].DeviceID == deviceID {
  2024. if cfg.Devices[i].Name == "" || cfg.Options.OverwriteRemoteDevNames {
  2025. cfg.Devices[i].Name = hello.DeviceName
  2026. }
  2027. return
  2028. }
  2029. }
  2030. })
  2031. }
  2032. m.deviceWasSeen(deviceID)
  2033. m.scheduleConnectionPromotion()
  2034. }
  2035. func (m *model) scheduleConnectionPromotion() {
  2036. // Keeps deferring to prevent multiple executions in quick succession,
  2037. // e.g. if multiple connections to a single device are closed.
  2038. m.promotionTimer.Reset(time.Second)
  2039. }
  2040. // promoteConnections checks for devices that have connections, but where
  2041. // the primary connection hasn't started index handlers etc. yet, and
  2042. // promotes the primary connection to be the index handling one. This should
  2043. // be called after adding new connections, and after closing a primary
  2044. // device connection.
  2045. func (m *model) promoteConnections() {
  2046. // Slice of actions to take on connections after releasing the main
  2047. // mutex. We do this so that we do not perform blocking network actions
  2048. // inside the loop, and also to avoid a possible deadlock with calling
  2049. // Start() on connections that are already executing a Close() with a
  2050. // callback into the model...
  2051. var postLockActions []func()
  2052. m.mut.Lock()
  2053. for deviceID, connIDs := range m.deviceConnIDs {
  2054. cm, passwords := m.generateClusterConfigRLocked(deviceID)
  2055. if m.promotedConnID[deviceID] != connIDs[0] {
  2056. // The previously promoted connection is not the current
  2057. // primary; we should promote the primary connection to be the
  2058. // index handling one. We do this by sending a ClusterConfig on
  2059. // it, which will cause the other side to start sending us index
  2060. // messages there. (On our side, we manage index handlers based
  2061. // on where we get ClusterConfigs from the peer.)
  2062. conn := m.connections[connIDs[0]]
  2063. l.Debugf("Promoting connection to %s at %s", deviceID.Short(), conn)
  2064. postLockActions = append(postLockActions, func() {
  2065. if conn.Statistics().StartedAt.IsZero() {
  2066. conn.Start()
  2067. }
  2068. conn.ClusterConfig(cm, passwords)
  2069. })
  2070. m.promotedConnID[deviceID] = connIDs[0]
  2071. }
  2072. // Make sure any other new connections also get started, and that
  2073. // they get a secondary-marked ClusterConfig.
  2074. for _, connID := range connIDs[1:] {
  2075. conn := m.connections[connID]
  2076. if conn.Statistics().StartedAt.IsZero() {
  2077. postLockActions = append(postLockActions, func() {
  2078. conn.Start()
  2079. conn.ClusterConfig(&protocol.ClusterConfig{Secondary: true}, passwords)
  2080. })
  2081. }
  2082. }
  2083. }
  2084. m.mut.Unlock()
  2085. for _, action := range postLockActions {
  2086. action()
  2087. }
  2088. }
  2089. func (m *model) DownloadProgress(conn protocol.Connection, p *protocol.DownloadProgress) error {
  2090. deviceID := conn.DeviceID()
  2091. m.mut.RLock()
  2092. cfg, ok := m.folderCfgs[p.Folder]
  2093. m.mut.RUnlock()
  2094. if !ok || !cfg.SharedWith(deviceID) {
  2095. return nil
  2096. }
  2097. m.mut.RLock()
  2098. downloads := m.deviceDownloads[deviceID]
  2099. m.mut.RUnlock()
  2100. downloads.Update(p.Folder, p.Updates)
  2101. state := downloads.GetBlockCounts(p.Folder)
  2102. m.evLogger.Log(events.RemoteDownloadProgress, map[string]interface{}{
  2103. "device": deviceID.String(),
  2104. "folder": p.Folder,
  2105. "state": state,
  2106. })
  2107. return nil
  2108. }
  2109. func (m *model) deviceWasSeen(deviceID protocol.DeviceID) {
  2110. m.mut.RLock()
  2111. sr, ok := m.deviceStatRefs[deviceID]
  2112. m.mut.RUnlock()
  2113. if ok {
  2114. _ = sr.WasSeen()
  2115. }
  2116. }
  2117. func (m *model) deviceDidCloseRLocked(deviceID protocol.DeviceID, duration time.Duration) {
  2118. if sr, ok := m.deviceStatRefs[deviceID]; ok {
  2119. _ = sr.LastConnectionDuration(duration)
  2120. _ = sr.WasSeen()
  2121. }
  2122. }
  2123. func (m *model) RequestGlobal(ctx context.Context, deviceID protocol.DeviceID, folder, name string, blockNo int, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
  2124. conn, connOK := m.requestConnectionForDevice(deviceID)
  2125. if !connOK {
  2126. return nil, fmt.Errorf("requestGlobal: no connection to device: %s", deviceID.Short())
  2127. }
  2128. l.Debugf("%v REQ(out): %s (%s): %q / %q b=%d o=%d s=%d h=%x ft=%t", m, deviceID.Short(), conn, folder, name, blockNo, offset, size, hash, fromTemporary)
  2129. return conn.Request(ctx, &protocol.Request{Folder: folder, Name: name, BlockNo: blockNo, Offset: offset, Size: size, Hash: hash, FromTemporary: fromTemporary})
  2130. }
  2131. // requestConnectionForDevice returns a connection to the given device, to
  2132. // be used for sending a request. If there is only one device connection,
  2133. // this is the one to use. If there are multiple then we avoid the first
  2134. // ("primary") connection, which is dedicated to index data, and pick a
  2135. // random one of the others.
  2136. func (m *model) requestConnectionForDevice(deviceID protocol.DeviceID) (protocol.Connection, bool) {
  2137. m.mut.RLock()
  2138. defer m.mut.RUnlock()
  2139. connIDs, ok := m.deviceConnIDs[deviceID]
  2140. if !ok {
  2141. return nil, false
  2142. }
  2143. // If there is an entry in deviceConns, it always contains at least one
  2144. // connection.
  2145. connID := connIDs[0]
  2146. if len(connIDs) > 1 {
  2147. // Pick a random connection of the non-primary ones
  2148. idx := rand.Intn(len(connIDs)-1) + 1
  2149. connID = connIDs[idx]
  2150. }
  2151. conn, connOK := m.connections[connID]
  2152. return conn, connOK
  2153. }
  2154. func (m *model) ScanFolders() map[string]error {
  2155. m.mut.RLock()
  2156. folders := make([]string, 0, len(m.folderCfgs))
  2157. for folder := range m.folderCfgs {
  2158. folders = append(folders, folder)
  2159. }
  2160. m.mut.RUnlock()
  2161. errors := make(map[string]error, len(m.folderCfgs))
  2162. var errorsMut sync.Mutex
  2163. var wg sync.WaitGroup
  2164. wg.Add(len(folders))
  2165. for _, folder := range folders {
  2166. go func() {
  2167. err := m.ScanFolder(folder)
  2168. if err != nil {
  2169. errorsMut.Lock()
  2170. errors[folder] = err
  2171. errorsMut.Unlock()
  2172. }
  2173. wg.Done()
  2174. }()
  2175. }
  2176. wg.Wait()
  2177. return errors
  2178. }
  2179. func (m *model) ScanFolder(folder string) error {
  2180. return m.ScanFolderSubdirs(folder, nil)
  2181. }
  2182. func (m *model) ScanFolderSubdirs(folder string, subs []string) error {
  2183. m.mut.RLock()
  2184. err := m.checkFolderRunningRLocked(folder)
  2185. runner, _ := m.folderRunners.Get(folder)
  2186. m.mut.RUnlock()
  2187. if err != nil {
  2188. return err
  2189. }
  2190. return runner.Scan(subs)
  2191. }
  2192. func (m *model) DelayScan(folder string, next time.Duration) {
  2193. m.mut.RLock()
  2194. runner, ok := m.folderRunners.Get(folder)
  2195. m.mut.RUnlock()
  2196. if !ok {
  2197. return
  2198. }
  2199. runner.DelayScan(next)
  2200. }
  2201. // numHashers returns the number of hasher routines to use for a given folder,
  2202. // taking into account configuration and available CPU cores.
  2203. func (m *model) numHashers(folder string) int {
  2204. m.mut.RLock()
  2205. folderCfg := m.folderCfgs[folder]
  2206. numFolders := max(1, len(m.folderCfgs))
  2207. // MaxFolderConcurrency already limits the number of scanned folders, so
  2208. // prefer it over the overall number of folders to avoid limiting performance
  2209. // further for no reason.
  2210. if concurrency := m.cfg.Options().MaxFolderConcurrency(); concurrency > 0 {
  2211. numFolders = min(numFolders, concurrency)
  2212. }
  2213. m.mut.RUnlock()
  2214. if folderCfg.Hashers > 0 {
  2215. // Specific value set in the config, use that.
  2216. return folderCfg.Hashers
  2217. }
  2218. numCpus := runtime.GOMAXPROCS(-1)
  2219. if build.IsWindows || build.IsDarwin || build.IsIOS || build.IsAndroid {
  2220. // Interactive operating systems; don't load the system too heavily by
  2221. // default.
  2222. numCpus = max(1, numCpus/4)
  2223. }
  2224. // For other operating systems and architectures, lets try to get some
  2225. // work done... Divide the available CPU cores among the configured
  2226. // folders.
  2227. if perFolder := numCpus / numFolders; perFolder > 0 {
  2228. return perFolder
  2229. }
  2230. return 1
  2231. }
  2232. // generateClusterConfig returns a ClusterConfigMessage that is correct and the
  2233. // set of folder passwords for the given peer device
  2234. func (m *model) generateClusterConfig(device protocol.DeviceID) (*protocol.ClusterConfig, map[string]string) {
  2235. m.mut.RLock()
  2236. defer m.mut.RUnlock()
  2237. return m.generateClusterConfigRLocked(device)
  2238. }
  2239. func (m *model) generateClusterConfigRLocked(device protocol.DeviceID) (*protocol.ClusterConfig, map[string]string) {
  2240. message := &protocol.ClusterConfig{}
  2241. folders := m.cfg.FolderList()
  2242. passwords := make(map[string]string, len(folders))
  2243. for _, folderCfg := range folders {
  2244. if !folderCfg.SharedWith(device) {
  2245. continue
  2246. }
  2247. encryptionToken, hasEncryptionToken := m.folderEncryptionPasswordTokens[folderCfg.ID]
  2248. if folderCfg.Type == config.FolderTypeReceiveEncrypted && !hasEncryptionToken {
  2249. // We haven't gotten a token for us yet and without one the other
  2250. // side can't validate us - pretend we don't have the folder yet.
  2251. continue
  2252. }
  2253. protocolFolder := protocol.Folder{
  2254. ID: folderCfg.ID,
  2255. Label: folderCfg.Label,
  2256. }
  2257. // Even if we aren't paused, if we haven't started the folder yet
  2258. // pretend we are. Otherwise the remote might get confused about
  2259. // the missing index info (and drop all the info). We will send
  2260. // another cluster config once the folder is started.
  2261. if folderCfg.Paused {
  2262. protocolFolder.StopReason = protocol.FolderStopReasonPaused
  2263. }
  2264. nextDevice:
  2265. for _, folderDevice := range folderCfg.Devices {
  2266. deviceCfg, _ := m.cfg.Device(folderDevice.DeviceID)
  2267. protocolDevice := protocol.Device{
  2268. ID: deviceCfg.DeviceID,
  2269. Name: deviceCfg.Name,
  2270. Addresses: deviceCfg.Addresses,
  2271. Compression: deviceCfg.Compression.ToProtocol(),
  2272. CertName: deviceCfg.CertName,
  2273. Introducer: deviceCfg.Introducer,
  2274. }
  2275. if deviceCfg.DeviceID == m.id && hasEncryptionToken {
  2276. protocolDevice.EncryptionPasswordToken = encryptionToken
  2277. } else if folderDevice.EncryptionPassword != "" {
  2278. // For encrypted/untrusted devices we send the encryption
  2279. // token and prepare the password. We do not send any
  2280. // information about untrusted devices to _other_ devices,
  2281. // as they are not normal peers sharing the folder and we
  2282. // don't want things like the introducer features to kick in
  2283. // for them.
  2284. if folderDevice.DeviceID == device {
  2285. protocolDevice.EncryptionPasswordToken = protocol.PasswordToken(m.keyGen, folderCfg.ID, folderDevice.EncryptionPassword)
  2286. passwords[folderCfg.ID] = folderDevice.EncryptionPassword
  2287. } else {
  2288. continue nextDevice
  2289. }
  2290. }
  2291. if deviceCfg.DeviceID == m.id {
  2292. protocolDevice.IndexID, _ = m.sdb.GetIndexID(folderCfg.ID, protocol.LocalDeviceID)
  2293. protocolDevice.MaxSequence, _ = m.sdb.GetDeviceSequence(folderCfg.ID, protocol.LocalDeviceID)
  2294. } else {
  2295. protocolDevice.IndexID, _ = m.sdb.GetIndexID(folderCfg.ID, deviceCfg.DeviceID)
  2296. protocolDevice.MaxSequence, _ = m.sdb.GetDeviceSequence(folderCfg.ID, deviceCfg.DeviceID)
  2297. }
  2298. protocolFolder.Devices = append(protocolFolder.Devices, protocolDevice)
  2299. }
  2300. message.Folders = append(message.Folders, protocolFolder)
  2301. }
  2302. return message, passwords
  2303. }
  2304. func (m *model) State(folder string) (string, time.Time, error) {
  2305. m.mut.RLock()
  2306. runner, ok := m.folderRunners.Get(folder)
  2307. m.mut.RUnlock()
  2308. if !ok {
  2309. // The returned error should be an actual folder error, so returning
  2310. // errors.New("does not exist") or similar here would be
  2311. // inappropriate.
  2312. return "", time.Time{}, nil
  2313. }
  2314. state, changed, err := runner.getState()
  2315. return state.String(), changed, err
  2316. }
  2317. func (m *model) FolderErrors(folder string) ([]FileError, error) {
  2318. m.mut.RLock()
  2319. err := m.checkFolderRunningRLocked(folder)
  2320. runner, _ := m.folderRunners.Get(folder)
  2321. m.mut.RUnlock()
  2322. if err != nil {
  2323. return nil, err
  2324. }
  2325. return runner.Errors(), nil
  2326. }
  2327. func (m *model) WatchError(folder string) error {
  2328. m.mut.RLock()
  2329. err := m.checkFolderRunningRLocked(folder)
  2330. runner, _ := m.folderRunners.Get(folder)
  2331. m.mut.RUnlock()
  2332. if err != nil {
  2333. return nil //nolint:nilerr // If the folder isn't running, there's no error to report.
  2334. }
  2335. return runner.WatchError()
  2336. }
  2337. func (m *model) Override(folder string) {
  2338. // Grab the runner and the file set.
  2339. m.mut.RLock()
  2340. runner, ok := m.folderRunners.Get(folder)
  2341. m.mut.RUnlock()
  2342. if !ok {
  2343. return
  2344. }
  2345. // Run the override, taking updates as if they came from scanning.
  2346. runner.Override()
  2347. }
  2348. func (m *model) Revert(folder string) {
  2349. // Grab the runner and the file set.
  2350. m.mut.RLock()
  2351. runner, ok := m.folderRunners.Get(folder)
  2352. m.mut.RUnlock()
  2353. if !ok {
  2354. return
  2355. }
  2356. // Run the revert, taking updates as if they came from scanning.
  2357. runner.Revert()
  2358. }
  2359. type TreeEntry struct {
  2360. Name string `json:"name"`
  2361. ModTime time.Time `json:"modTime"`
  2362. Size int64 `json:"size"`
  2363. Type string `json:"type"`
  2364. Children []*TreeEntry `json:"children,omitempty"`
  2365. }
  2366. func findByName(slice []*TreeEntry, name string) *TreeEntry {
  2367. for _, child := range slice {
  2368. if child.Name == name {
  2369. return child
  2370. }
  2371. }
  2372. return nil
  2373. }
  2374. func (m *model) GlobalDirectoryTree(folder, prefix string, levels int, dirsOnly bool) ([]*TreeEntry, error) {
  2375. m.mut.RLock()
  2376. _, ok := m.folderCfgs[folder]
  2377. m.mut.RUnlock()
  2378. if !ok {
  2379. return nil, ErrFolderMissing
  2380. }
  2381. root := &TreeEntry{
  2382. Children: make([]*TreeEntry, 0),
  2383. }
  2384. sep := string(filepath.Separator)
  2385. prefix = osutil.NativeFilename(prefix)
  2386. if prefix != "" && !strings.HasSuffix(prefix, sep) {
  2387. prefix += sep
  2388. }
  2389. for f, err := range itererr.Zip(m.sdb.AllGlobalFilesPrefix(folder, prefix)) {
  2390. if err != nil {
  2391. return nil, err
  2392. }
  2393. // Don't include the prefix itself.
  2394. if f.IsInvalid() || f.Deleted || strings.HasPrefix(prefix, f.Name) {
  2395. continue
  2396. }
  2397. f.Name = strings.Replace(f.Name, prefix, "", 1)
  2398. dir := filepath.Dir(f.Name)
  2399. base := filepath.Base(f.Name)
  2400. if levels > -1 && strings.Count(f.Name, sep) > levels {
  2401. continue
  2402. }
  2403. parent := root
  2404. if dir != "." {
  2405. for _, path := range strings.Split(dir, sep) {
  2406. child := findByName(parent.Children, path)
  2407. if child == nil {
  2408. return nil, fmt.Errorf("could not find child '%s' for path '%s' in parent '%s'", path, f.Name, parent.Name)
  2409. }
  2410. parent = child
  2411. }
  2412. }
  2413. if dirsOnly && !f.IsDirectory() {
  2414. continue
  2415. }
  2416. parent.Children = append(parent.Children, &TreeEntry{
  2417. Name: base,
  2418. Type: f.Type.String(),
  2419. ModTime: f.ModTime(),
  2420. Size: f.Size,
  2421. })
  2422. }
  2423. return root.Children, nil
  2424. }
  2425. func (m *model) GetFolderVersions(folder string) (map[string][]versioner.FileVersion, error) {
  2426. m.mut.RLock()
  2427. err := m.checkFolderRunningRLocked(folder)
  2428. ver := m.folderVersioners[folder]
  2429. m.mut.RUnlock()
  2430. if err != nil {
  2431. return nil, err
  2432. }
  2433. if ver == nil {
  2434. return nil, errNoVersioner
  2435. }
  2436. return ver.GetVersions()
  2437. }
  2438. func (m *model) RestoreFolderVersions(folder string, versions map[string]time.Time) (map[string]error, error) {
  2439. m.mut.RLock()
  2440. err := m.checkFolderRunningRLocked(folder)
  2441. fcfg := m.folderCfgs[folder]
  2442. ver := m.folderVersioners[folder]
  2443. m.mut.RUnlock()
  2444. if err != nil {
  2445. return nil, err
  2446. }
  2447. if ver == nil {
  2448. return nil, errNoVersioner
  2449. }
  2450. restoreErrors := make(map[string]error)
  2451. for file, version := range versions {
  2452. if err := ver.Restore(file, version); err != nil {
  2453. restoreErrors[file] = err
  2454. }
  2455. }
  2456. // Trigger scan
  2457. if !fcfg.FSWatcherEnabled {
  2458. go func() { _ = m.ScanFolder(folder) }()
  2459. }
  2460. return restoreErrors, nil
  2461. }
  2462. func (m *model) Availability(folder string, file protocol.FileInfo, block protocol.BlockInfo) ([]Availability, error) {
  2463. m.mut.RLock()
  2464. defer m.mut.RUnlock()
  2465. cfg, ok := m.folderCfgs[folder]
  2466. if !ok {
  2467. return nil, ErrFolderMissing
  2468. }
  2469. return m.blockAvailabilityRLocked(cfg, file, block), nil
  2470. }
  2471. func (m *model) blockAvailability(cfg config.FolderConfiguration, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
  2472. m.mut.RLock()
  2473. defer m.mut.RUnlock()
  2474. return m.blockAvailabilityRLocked(cfg, file, block)
  2475. }
  2476. func (m *model) blockAvailabilityRLocked(cfg config.FolderConfiguration, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
  2477. var candidates []Availability
  2478. candidates = append(candidates, m.fileAvailabilityRLocked(cfg, file)...)
  2479. candidates = append(candidates, m.blockAvailabilityFromTemporaryRLocked(cfg, file, block)...)
  2480. return candidates
  2481. }
  2482. func (m *model) fileAvailability(cfg config.FolderConfiguration, file protocol.FileInfo) []Availability {
  2483. m.mut.RLock()
  2484. defer m.mut.RUnlock()
  2485. return m.fileAvailabilityRLocked(cfg, file)
  2486. }
  2487. func (m *model) fileAvailabilityRLocked(cfg config.FolderConfiguration, file protocol.FileInfo) []Availability {
  2488. var availabilities []Availability
  2489. devs, err := m.sdb.GetGlobalAvailability(cfg.ID, file.Name)
  2490. if err != nil {
  2491. return nil
  2492. }
  2493. for _, device := range devs {
  2494. if _, ok := m.remoteFolderStates[device]; !ok {
  2495. continue
  2496. }
  2497. if state := m.remoteFolderStates[device][cfg.ID]; state != remoteFolderValid {
  2498. continue
  2499. }
  2500. _, ok := m.deviceConnIDs[device]
  2501. if ok {
  2502. availabilities = append(availabilities, Availability{ID: device, FromTemporary: false})
  2503. }
  2504. }
  2505. return availabilities
  2506. }
  2507. func (m *model) blockAvailabilityFromTemporaryRLocked(cfg config.FolderConfiguration, file protocol.FileInfo, block protocol.BlockInfo) []Availability {
  2508. var availabilities []Availability
  2509. for _, device := range cfg.Devices {
  2510. if m.deviceDownloads[device.DeviceID].Has(cfg.ID, file.Name, file.Version, int(block.Offset/int64(file.BlockSize()))) {
  2511. availabilities = append(availabilities, Availability{ID: device.DeviceID, FromTemporary: true})
  2512. }
  2513. }
  2514. return availabilities
  2515. }
  2516. // BringToFront bumps the given files priority in the job queue.
  2517. func (m *model) BringToFront(folder, file string) {
  2518. m.mut.RLock()
  2519. runner, ok := m.folderRunners.Get(folder)
  2520. m.mut.RUnlock()
  2521. if ok {
  2522. runner.BringToFront(file)
  2523. }
  2524. }
  2525. func (m *model) ResetFolder(folder string) error {
  2526. m.mut.Lock()
  2527. defer m.mut.Unlock()
  2528. _, ok := m.folderRunners.Get(folder)
  2529. if ok {
  2530. return errors.New("folder must be paused when resetting")
  2531. }
  2532. slog.Info("Cleaning metadata for reset folder", "folder", folder)
  2533. return m.sdb.DropFolder(folder)
  2534. }
  2535. func (m *model) String() string {
  2536. return fmt.Sprintf("model@%p", m)
  2537. }
  2538. func (*model) VerifyConfiguration(from, to config.Configuration) error {
  2539. toFolders := to.FolderMap()
  2540. for _, from := range from.Folders {
  2541. to, ok := toFolders[from.ID]
  2542. if ok && from.Type != to.Type && (from.Type == config.FolderTypeReceiveEncrypted || to.Type == config.FolderTypeReceiveEncrypted) {
  2543. return errors.New("folder type must not be changed from/to receive-encrypted")
  2544. }
  2545. }
  2546. // Verify that any requested versioning is possible to construct, or we
  2547. // will panic later when starting the folder.
  2548. for _, to := range to.Folders {
  2549. if to.Versioning.Type != "" {
  2550. if _, err := versioner.New(to); err != nil {
  2551. return err
  2552. }
  2553. }
  2554. }
  2555. return nil
  2556. }
  2557. func (m *model) CommitConfiguration(from, to config.Configuration) bool {
  2558. // TODO: This should not use reflect, and should take more care to try to handle stuff without restart.
  2559. // Delay processing config changes until after the initial setup
  2560. <-m.started
  2561. // Go through the folder configs and figure out if we need to restart or not.
  2562. // Tracks devices affected by any configuration change to resend ClusterConfig.
  2563. clusterConfigDevices := make(deviceIDSet, len(from.Devices)+len(to.Devices))
  2564. closeDevices := make([]protocol.DeviceID, 0, len(to.Devices))
  2565. fromFolders := mapFolders(from.Folders)
  2566. toFolders := mapFolders(to.Folders)
  2567. for folderID, cfg := range toFolders {
  2568. if _, ok := fromFolders[folderID]; !ok {
  2569. // A folder was added.
  2570. if cfg.Paused {
  2571. slog.Info("Paused folder", cfg.LogAttr())
  2572. } else {
  2573. slog.Info("Adding folder", cfg.LogAttr())
  2574. if err := m.newFolder(cfg, to.Options.CacheIgnoredFiles); err != nil {
  2575. m.fatal(err)
  2576. return true
  2577. }
  2578. }
  2579. clusterConfigDevices.add(cfg.DeviceIDs())
  2580. }
  2581. }
  2582. removedFolders := make(map[string]struct{})
  2583. for folderID, fromCfg := range fromFolders {
  2584. toCfg, ok := toFolders[folderID]
  2585. if !ok {
  2586. // The folder was removed.
  2587. m.removeFolder(fromCfg)
  2588. clusterConfigDevices.add(fromCfg.DeviceIDs())
  2589. removedFolders[fromCfg.ID] = struct{}{}
  2590. continue
  2591. }
  2592. if fromCfg.Paused && toCfg.Paused {
  2593. continue
  2594. }
  2595. // This folder exists on both sides. Settings might have changed.
  2596. // Check if anything differs that requires a restart.
  2597. if !reflect.DeepEqual(fromCfg.RequiresRestartOnly(), toCfg.RequiresRestartOnly()) || from.Options.CacheIgnoredFiles != to.Options.CacheIgnoredFiles {
  2598. if err := m.restartFolder(fromCfg, toCfg, to.Options.CacheIgnoredFiles); err != nil {
  2599. m.fatal(err)
  2600. return true
  2601. }
  2602. clusterConfigDevices.add(fromCfg.DeviceIDs())
  2603. if toCfg.Type != config.FolderTypeReceiveEncrypted {
  2604. clusterConfigDevices.add(toCfg.DeviceIDs())
  2605. } else {
  2606. // If we don't have the encryption token yet, we need to drop
  2607. // the connection to make the remote re-send the cluster-config
  2608. // and with it the token.
  2609. m.mut.RLock()
  2610. _, ok := m.folderEncryptionPasswordTokens[toCfg.ID]
  2611. m.mut.RUnlock()
  2612. if !ok {
  2613. closeDevices = append(closeDevices, toCfg.DeviceIDs()...)
  2614. } else {
  2615. clusterConfigDevices.add(toCfg.DeviceIDs())
  2616. }
  2617. }
  2618. }
  2619. // Emit the folder pause/resume event
  2620. if fromCfg.Paused != toCfg.Paused {
  2621. eventType := events.FolderResumed
  2622. if toCfg.Paused {
  2623. eventType = events.FolderPaused
  2624. }
  2625. m.evLogger.Log(eventType, map[string]string{"id": toCfg.ID, "label": toCfg.Label})
  2626. }
  2627. }
  2628. // Pausing a device, unpausing is handled by the connection service.
  2629. fromDevices := from.DeviceMap()
  2630. toDevices := to.DeviceMap()
  2631. for deviceID, toCfg := range toDevices {
  2632. fromCfg, ok := fromDevices[deviceID]
  2633. if !ok {
  2634. sr := stats.NewDeviceStatisticsReference(db.NewTyped(m.sdb, "devicestats/"+deviceID.String()))
  2635. m.mut.Lock()
  2636. m.deviceStatRefs[deviceID] = sr
  2637. m.mut.Unlock()
  2638. continue
  2639. }
  2640. delete(fromDevices, deviceID)
  2641. if fromCfg.Paused == toCfg.Paused {
  2642. continue
  2643. }
  2644. if toCfg.Paused {
  2645. slog.Info("Pausing device", deviceID.LogAttr())
  2646. closeDevices = append(closeDevices, deviceID)
  2647. m.evLogger.Log(events.DevicePaused, map[string]string{"device": deviceID.String()})
  2648. } else {
  2649. // Ignored folder was removed, reconnect to retrigger the prompt.
  2650. if len(fromCfg.IgnoredFolders) > len(toCfg.IgnoredFolders) {
  2651. closeDevices = append(closeDevices, deviceID)
  2652. }
  2653. slog.Info("Resuming device", deviceID.LogAttr())
  2654. m.evLogger.Log(events.DeviceResumed, map[string]string{"device": deviceID.String()})
  2655. }
  2656. if toCfg.MaxRequestKiB != fromCfg.MaxRequestKiB {
  2657. m.mut.Lock()
  2658. m.setConnRequestLimitersLocked(toCfg)
  2659. m.mut.Unlock()
  2660. }
  2661. }
  2662. // Clean up after removed devices
  2663. removedDevices := make([]protocol.DeviceID, 0, len(fromDevices))
  2664. m.mut.Lock()
  2665. for deviceID := range fromDevices {
  2666. delete(m.deviceStatRefs, deviceID)
  2667. removedDevices = append(removedDevices, deviceID)
  2668. delete(clusterConfigDevices, deviceID)
  2669. }
  2670. m.mut.Unlock()
  2671. m.mut.RLock()
  2672. for _, id := range closeDevices {
  2673. delete(clusterConfigDevices, id)
  2674. if conns, ok := m.deviceConnIDs[id]; ok {
  2675. for _, connID := range conns {
  2676. go m.connections[connID].Close(errDevicePaused)
  2677. }
  2678. }
  2679. }
  2680. for _, id := range removedDevices {
  2681. delete(clusterConfigDevices, id)
  2682. if conns, ok := m.deviceConnIDs[id]; ok {
  2683. for _, connID := range conns {
  2684. go m.connections[connID].Close(errDevicePaused)
  2685. }
  2686. }
  2687. }
  2688. m.mut.RUnlock()
  2689. // Generating cluster-configs acquires the mutex.
  2690. m.sendClusterConfig(clusterConfigDevices.AsSlice())
  2691. ignoredDevices := observedDeviceSet(to.IgnoredDevices)
  2692. m.cleanPending(toDevices, toFolders, ignoredDevices, removedFolders)
  2693. m.globalRequestLimiter.SetCapacity(1024 * to.Options.MaxConcurrentIncomingRequestKiB())
  2694. m.folderIOLimiter.SetCapacity(to.Options.MaxFolderConcurrency())
  2695. // Some options don't require restart as those components handle it fine
  2696. // by themselves. Compare the options structs containing only the
  2697. // attributes that require restart and act appropriately.
  2698. if !reflect.DeepEqual(from.Options.RequiresRestartOnly(), to.Options.RequiresRestartOnly()) {
  2699. l.Debugln(m, "requires restart, options differ")
  2700. return false
  2701. }
  2702. return true
  2703. }
  2704. func (m *model) setConnRequestLimitersLocked(cfg config.DeviceConfiguration) {
  2705. // Touches connRequestLimiters which is protected by the mutex.
  2706. // 0: default, <0: no limiting
  2707. switch {
  2708. case cfg.MaxRequestKiB > 0:
  2709. m.connRequestLimiters[cfg.DeviceID] = semaphore.New(1024 * cfg.MaxRequestKiB)
  2710. case cfg.MaxRequestKiB == 0:
  2711. m.connRequestLimiters[cfg.DeviceID] = semaphore.New(1024 * defaultPullerPendingKiB)
  2712. }
  2713. }
  2714. func (m *model) cleanPending(existingDevices map[protocol.DeviceID]config.DeviceConfiguration, existingFolders map[string]config.FolderConfiguration, ignoredDevices deviceIDSet, removedFolders map[string]struct{}) {
  2715. var removedPendingFolders []map[string]string
  2716. pendingFolders, err := m.observed.PendingFolders()
  2717. if err != nil {
  2718. const msg = "Could not iterate through pending folder entries for cleanup"
  2719. slog.Warn(msg, slogutil.Error(err))
  2720. m.evLogger.Log(events.Failure, msg)
  2721. // Continue with pending devices below, loop is skipped.
  2722. }
  2723. for folderID, pf := range pendingFolders {
  2724. if _, ok := removedFolders[folderID]; ok {
  2725. // Forget pending folder device associations for recently removed
  2726. // folders as well, assuming the folder is no longer of interest
  2727. // at all (but might become pending again).
  2728. l.Debugf("Discarding pending removed folder %v from all devices", folderID)
  2729. if err := m.observed.RemovePendingFolder(folderID); err != nil {
  2730. const msg = "Failed to remove pending folder entry"
  2731. slog.Warn(msg, slog.String("folder", folderID), slogutil.Error(err))
  2732. m.evLogger.Log(events.Failure, msg)
  2733. } else {
  2734. removedPendingFolders = append(removedPendingFolders, map[string]string{
  2735. "folderID": folderID,
  2736. })
  2737. }
  2738. continue
  2739. }
  2740. for deviceID := range pf.OfferedBy {
  2741. if dev, ok := existingDevices[deviceID]; !ok {
  2742. l.Debugf("Discarding pending folder %v from unknown device %v", folderID, deviceID)
  2743. goto removeFolderForDevice
  2744. } else if dev.IgnoredFolder(folderID) {
  2745. l.Debugf("Discarding now ignored pending folder %v for device %v", folderID, deviceID)
  2746. goto removeFolderForDevice
  2747. }
  2748. if folderCfg, ok := existingFolders[folderID]; ok {
  2749. if folderCfg.SharedWith(deviceID) {
  2750. l.Debugf("Discarding now shared pending folder %v for device %v", folderID, deviceID)
  2751. goto removeFolderForDevice
  2752. }
  2753. }
  2754. continue
  2755. removeFolderForDevice:
  2756. if err := m.observed.RemovePendingFolderForDevice(folderID, deviceID); err != nil {
  2757. const msg = "Failed to remove pending folder-device entry"
  2758. slog.Warn(msg, slog.String("folder", folderID), deviceID.LogAttr(), slogutil.Error(err))
  2759. m.evLogger.Log(events.Failure, msg)
  2760. continue
  2761. }
  2762. removedPendingFolders = append(removedPendingFolders, map[string]string{
  2763. "folderID": folderID,
  2764. "deviceID": deviceID.String(),
  2765. })
  2766. }
  2767. }
  2768. if len(removedPendingFolders) > 0 {
  2769. m.evLogger.Log(events.PendingFoldersChanged, map[string]interface{}{
  2770. "removed": removedPendingFolders,
  2771. })
  2772. }
  2773. var removedPendingDevices []map[string]string
  2774. pendingDevices, err := m.observed.PendingDevices()
  2775. if err != nil {
  2776. const msg = "Could not iterate through pending device entries for cleanup"
  2777. slog.Warn(msg, slogutil.Error(err))
  2778. m.evLogger.Log(events.Failure, msg)
  2779. return
  2780. }
  2781. for deviceID := range pendingDevices {
  2782. if _, ok := ignoredDevices[deviceID]; ok {
  2783. l.Debugf("Discarding now ignored pending device %v", deviceID)
  2784. goto removeDevice
  2785. }
  2786. if _, ok := existingDevices[deviceID]; ok {
  2787. l.Debugf("Discarding now added pending device %v", deviceID)
  2788. goto removeDevice
  2789. }
  2790. continue
  2791. removeDevice:
  2792. if err := m.observed.RemovePendingDevice(deviceID); err != nil {
  2793. const msg = "Failed to remove pending device entry"
  2794. slog.Warn(msg, slogutil.Error(err))
  2795. m.evLogger.Log(events.Failure, msg)
  2796. continue
  2797. }
  2798. removedPendingDevices = append(removedPendingDevices, map[string]string{
  2799. "deviceID": deviceID.String(),
  2800. })
  2801. }
  2802. if len(removedPendingDevices) > 0 {
  2803. m.evLogger.Log(events.PendingDevicesChanged, map[string]interface{}{
  2804. "removed": removedPendingDevices,
  2805. })
  2806. }
  2807. }
  2808. // checkFolderRunningRLocked returns nil if the folder is up and running and a
  2809. // descriptive error if not.
  2810. // Need to hold (read) lock on m.mut when calling this.
  2811. func (m *model) checkFolderRunningRLocked(folder string) error {
  2812. _, ok := m.folderRunners.Get(folder)
  2813. if ok {
  2814. return nil
  2815. }
  2816. if cfg, ok := m.cfg.Folder(folder); !ok {
  2817. return ErrFolderMissing
  2818. } else if cfg.Paused {
  2819. return ErrFolderPaused
  2820. }
  2821. return ErrFolderNotRunning
  2822. }
  2823. // PendingDevices lists unknown devices that tried to connect.
  2824. func (m *model) PendingDevices() (map[protocol.DeviceID]db.ObservedDevice, error) {
  2825. return m.observed.PendingDevices()
  2826. }
  2827. // PendingFolders lists folders that we don't yet share with the offering devices. It
  2828. // returns the entries grouped by folder and filters for a given device unless the
  2829. // argument is specified as EmptyDeviceID.
  2830. func (m *model) PendingFolders(device protocol.DeviceID) (map[string]db.PendingFolder, error) {
  2831. return m.observed.PendingFoldersForDevice(device)
  2832. }
  2833. // DismissPendingDevices removes the record of a specific pending device.
  2834. func (m *model) DismissPendingDevice(device protocol.DeviceID) error {
  2835. l.Debugf("Discarding pending device %v", device)
  2836. err := m.observed.RemovePendingDevice(device)
  2837. if err != nil {
  2838. return err
  2839. }
  2840. removedPendingDevices := []map[string]string{
  2841. {"deviceID": device.String()},
  2842. }
  2843. m.evLogger.Log(events.PendingDevicesChanged, map[string]interface{}{
  2844. "removed": removedPendingDevices,
  2845. })
  2846. return nil
  2847. }
  2848. // DismissPendingFolders removes records of pending folders. Either a specific folder /
  2849. // device combination, or all matching a specific folder ID if the device argument is
  2850. // specified as EmptyDeviceID.
  2851. func (m *model) DismissPendingFolder(device protocol.DeviceID, folder string) error {
  2852. var removedPendingFolders []map[string]string
  2853. if device == protocol.EmptyDeviceID {
  2854. l.Debugf("Discarding pending removed folder %s from all devices", folder)
  2855. err := m.observed.RemovePendingFolder(folder)
  2856. if err != nil {
  2857. return err
  2858. }
  2859. removedPendingFolders = []map[string]string{
  2860. {"folderID": folder},
  2861. }
  2862. } else {
  2863. l.Debugf("Discarding pending folder %s from device %v", folder, device)
  2864. err := m.observed.RemovePendingFolderForDevice(folder, device)
  2865. if err != nil {
  2866. return err
  2867. }
  2868. removedPendingFolders = []map[string]string{
  2869. {
  2870. "folderID": folder,
  2871. "deviceID": device.String(),
  2872. },
  2873. }
  2874. }
  2875. if len(removedPendingFolders) > 0 {
  2876. m.evLogger.Log(events.PendingFoldersChanged, map[string]interface{}{
  2877. "removed": removedPendingFolders,
  2878. })
  2879. }
  2880. return nil
  2881. }
  2882. // mapFolders returns a map of folder ID to folder configuration for the given
  2883. // slice of folder configurations.
  2884. func mapFolders(folders []config.FolderConfiguration) map[string]config.FolderConfiguration {
  2885. m := make(map[string]config.FolderConfiguration, len(folders))
  2886. for _, cfg := range folders {
  2887. m[cfg.ID] = cfg
  2888. }
  2889. return m
  2890. }
  2891. // mapDevices returns a map of device ID to nothing for the given slice of
  2892. // device IDs.
  2893. func mapDevices(devices []protocol.DeviceID) map[protocol.DeviceID]struct{} {
  2894. m := make(map[protocol.DeviceID]struct{}, len(devices))
  2895. for _, dev := range devices {
  2896. m[dev] = struct{}{}
  2897. }
  2898. return m
  2899. }
  2900. func observedDeviceSet(devices []config.ObservedDevice) deviceIDSet {
  2901. res := make(deviceIDSet, len(devices))
  2902. for _, dev := range devices {
  2903. res[dev.ID] = struct{}{}
  2904. }
  2905. return res
  2906. }
  2907. func readOffsetIntoBuf(fs fs.Filesystem, file string, offset int64, buf []byte) (int, error) {
  2908. fd, err := fs.Open(file)
  2909. if err != nil {
  2910. l.Debugln("readOffsetIntoBuf.Open", file, err)
  2911. return 0, err
  2912. }
  2913. defer fd.Close()
  2914. n, err := fd.ReadAt(buf, offset)
  2915. if err != nil {
  2916. l.Debugln("readOffsetIntoBuf.ReadAt", file, err)
  2917. }
  2918. return n, err
  2919. }
  2920. // folderDeviceSet is a set of (folder, deviceID) pairs
  2921. type folderDeviceSet map[string]map[protocol.DeviceID]struct{}
  2922. // set adds the (dev, folder) pair to the set
  2923. func (s folderDeviceSet) set(dev protocol.DeviceID, folder string) {
  2924. devs, ok := s[folder]
  2925. if !ok {
  2926. devs = make(map[protocol.DeviceID]struct{})
  2927. s[folder] = devs
  2928. }
  2929. devs[dev] = struct{}{}
  2930. }
  2931. // has returns true if the (dev, folder) pair is in the set
  2932. func (s folderDeviceSet) has(dev protocol.DeviceID, folder string) bool {
  2933. _, ok := s[folder][dev]
  2934. return ok
  2935. }
  2936. // hasDevice returns true if the device is set on any folder
  2937. func (s folderDeviceSet) hasDevice(dev protocol.DeviceID) bool {
  2938. for _, devices := range s {
  2939. if _, ok := devices[dev]; ok {
  2940. return true
  2941. }
  2942. }
  2943. return false
  2944. }
  2945. // syncMutexMap is a type safe wrapper for a sync.Map that holds mutexes
  2946. type syncMutexMap struct {
  2947. inner sync.Map
  2948. }
  2949. func (m *syncMutexMap) Get(key string) *sync.Mutex {
  2950. v, _ := m.inner.LoadOrStore(key, new(sync.Mutex))
  2951. return v.(*sync.Mutex)
  2952. }
  2953. type deviceIDSet map[protocol.DeviceID]struct{}
  2954. func (s deviceIDSet) add(ids []protocol.DeviceID) {
  2955. for _, id := range ids {
  2956. if _, ok := s[id]; !ok {
  2957. s[id] = struct{}{}
  2958. }
  2959. }
  2960. }
  2961. func (s deviceIDSet) AsSlice() []protocol.DeviceID {
  2962. ids := make([]protocol.DeviceID, 0, len(s))
  2963. for id := range s {
  2964. ids = append(ids, id)
  2965. }
  2966. return ids
  2967. }
  2968. func encryptionTokenPath(cfg config.FolderConfiguration) string {
  2969. return filepath.Join(cfg.MarkerName, config.EncryptionTokenName)
  2970. }
  2971. type storedEncryptionToken struct {
  2972. FolderID string
  2973. Token []byte
  2974. }
  2975. func readEncryptionToken(cfg config.FolderConfiguration) ([]byte, error) {
  2976. fd, err := cfg.Filesystem().Open(encryptionTokenPath(cfg))
  2977. if err != nil {
  2978. return nil, err
  2979. }
  2980. defer fd.Close()
  2981. var stored storedEncryptionToken
  2982. if err := json.NewDecoder(fd).Decode(&stored); err != nil {
  2983. return nil, err
  2984. }
  2985. return stored.Token, nil
  2986. }
  2987. func writeEncryptionToken(token []byte, cfg config.FolderConfiguration) error {
  2988. tokenName := encryptionTokenPath(cfg)
  2989. fd, err := cfg.Filesystem().OpenFile(tokenName, fs.OptReadWrite|fs.OptCreate, 0o666)
  2990. if err != nil {
  2991. return err
  2992. }
  2993. defer fd.Close()
  2994. return json.NewEncoder(fd).Encode(storedEncryptionToken{
  2995. FolderID: cfg.ID,
  2996. Token: token,
  2997. })
  2998. }
  2999. func newFolderConfiguration(w config.Wrapper, id, label string, fsType config.FilesystemType, path string) config.FolderConfiguration {
  3000. fcfg := w.DefaultFolder()
  3001. fcfg.ID = id
  3002. fcfg.Label = label
  3003. fcfg.FilesystemType = fsType
  3004. fcfg.Path = path
  3005. return fcfg
  3006. }
  3007. type updatedPendingFolder struct {
  3008. FolderID string `json:"folderID"`
  3009. FolderLabel string `json:"folderLabel"`
  3010. DeviceID protocol.DeviceID `json:"deviceID"`
  3011. ReceiveEncrypted bool `json:"receiveEncrypted"`
  3012. RemoteEncrypted bool `json:"remoteEncrypted"`
  3013. }
  3014. // redactPathError checks if the error is actually a os.PathError, and if yes
  3015. // returns a redactedError with the path removed.
  3016. func redactPathError(err error) (error, bool) {
  3017. var perr *os.PathError
  3018. if !errors.As(err, &perr) {
  3019. return nil, false
  3020. }
  3021. return &redactedError{
  3022. error: err,
  3023. redacted: fmt.Errorf("%v: %w", perr.Op, perr.Err),
  3024. }, true
  3025. }
  3026. type redactedError struct {
  3027. error
  3028. redacted error
  3029. }
  3030. func without[E comparable, S ~[]E](s S, e E) S {
  3031. for i, x := range s {
  3032. if x == e {
  3033. return append(s[:i], s[i+1:]...)
  3034. }
  3035. }
  3036. return s
  3037. }