model.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. package main
  2. import (
  3. "crypto/sha1"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "path"
  10. "sync"
  11. "time"
  12. "github.com/calmh/syncthing/buffers"
  13. "github.com/calmh/syncthing/protocol"
  14. )
  15. type Model struct {
  16. dir string
  17. global map[string]File // the latest version of each file as it exists in the cluster
  18. gmut sync.RWMutex // protects global
  19. local map[string]File // the files we currently have locally on disk
  20. lmut sync.RWMutex // protects local
  21. remote map[string]map[string]File
  22. rmut sync.RWMutex // protects remote
  23. protoConn map[string]Connection
  24. rawConn map[string]io.Closer
  25. pmut sync.RWMutex // protects protoConn and rawConn
  26. // Queue for files to fetch. fq can call back into the model, so we must ensure
  27. // to hold no locks when calling methods on fq.
  28. fq *FileQueue
  29. dq chan File // queue for files to delete
  30. updatedLocal int64 // timestamp of last update to local
  31. updateGlobal int64 // timestamp of last update to remote
  32. lastIdxBcast time.Time
  33. lastIdxBcastRequest time.Time
  34. umut sync.RWMutex // provides updated* and lastIdx*
  35. rwRunning bool
  36. delete bool
  37. initmut sync.Mutex // protects rwRunning and delete
  38. trace map[string]bool
  39. sup suppressor
  40. parallelRequests int
  41. limitRequestRate chan struct{}
  42. imut sync.Mutex // protects Index
  43. }
  44. type Connection interface {
  45. ID() string
  46. Index([]protocol.FileInfo)
  47. Request(name string, offset int64, size uint32, hash []byte) ([]byte, error)
  48. Statistics() protocol.Statistics
  49. Option(key string) string
  50. }
  51. const (
  52. idxBcastHoldtime = 15 * time.Second // Wait at least this long after the last index modification
  53. idxBcastMaxDelay = 120 * time.Second // Unless we've already waited this long
  54. minFileHoldTimeS = 60 // Never allow file changes more often than this
  55. maxFileHoldTimeS = 600 // Always allow file changes at least this often
  56. )
  57. var (
  58. ErrNoSuchFile = errors.New("no such file")
  59. ErrInvalid = errors.New("file is invalid")
  60. )
  61. // NewModel creates and starts a new model. The model starts in read-only mode,
  62. // where it sends index information to connected peers and responds to requests
  63. // for file data without altering the local repository in any way.
  64. func NewModel(dir string, maxChangeBw int) *Model {
  65. m := &Model{
  66. dir: dir,
  67. global: make(map[string]File),
  68. local: make(map[string]File),
  69. remote: make(map[string]map[string]File),
  70. protoConn: make(map[string]Connection),
  71. rawConn: make(map[string]io.Closer),
  72. lastIdxBcast: time.Now(),
  73. trace: make(map[string]bool),
  74. sup: suppressor{threshold: int64(maxChangeBw)},
  75. fq: NewFileQueue(),
  76. dq: make(chan File),
  77. }
  78. go m.broadcastIndexLoop()
  79. return m
  80. }
  81. func (m *Model) LimitRate(kbps int) {
  82. m.limitRequestRate = make(chan struct{}, kbps)
  83. n := kbps/10 + 1
  84. go func() {
  85. for {
  86. time.Sleep(100 * time.Millisecond)
  87. for i := 0; i < n; i++ {
  88. select {
  89. case m.limitRequestRate <- struct{}{}:
  90. }
  91. }
  92. }
  93. }()
  94. }
  95. // Trace enables trace logging of the given facility. This is a debugging function; grep for m.trace.
  96. func (m *Model) Trace(t string) {
  97. m.trace[t] = true
  98. }
  99. // StartRW starts read/write processing on the current model. When in
  100. // read/write mode the model will attempt to keep in sync with the cluster by
  101. // pulling needed files from peer nodes.
  102. func (m *Model) StartRW(del bool, threads int) {
  103. m.initmut.Lock()
  104. defer m.initmut.Unlock()
  105. if m.rwRunning {
  106. panic("starting started model")
  107. }
  108. m.rwRunning = true
  109. m.delete = del
  110. m.parallelRequests = threads
  111. go m.cleanTempFiles()
  112. if del {
  113. go m.deleteLoop()
  114. }
  115. }
  116. // Generation returns an opaque integer that is guaranteed to increment on
  117. // every change to the local repository or global model.
  118. func (m *Model) Generation() int64 {
  119. m.umut.RLock()
  120. defer m.umut.RUnlock()
  121. return m.updatedLocal + m.updateGlobal
  122. }
  123. func (m *Model) LocalAge() float64 {
  124. m.umut.RLock()
  125. defer m.umut.RUnlock()
  126. return time.Since(time.Unix(m.updatedLocal, 0)).Seconds()
  127. }
  128. type ConnectionInfo struct {
  129. protocol.Statistics
  130. Address string
  131. ClientID string
  132. ClientVersion string
  133. }
  134. // ConnectionStats returns a map with connection statistics for each connected node.
  135. func (m *Model) ConnectionStats() map[string]ConnectionInfo {
  136. type remoteAddrer interface {
  137. RemoteAddr() net.Addr
  138. }
  139. m.pmut.RLock()
  140. var res = make(map[string]ConnectionInfo)
  141. for node, conn := range m.protoConn {
  142. ci := ConnectionInfo{
  143. Statistics: conn.Statistics(),
  144. ClientID: conn.Option("clientId"),
  145. ClientVersion: conn.Option("clientVersion"),
  146. }
  147. if nc, ok := m.rawConn[node].(remoteAddrer); ok {
  148. ci.Address = nc.RemoteAddr().String()
  149. }
  150. res[node] = ci
  151. }
  152. m.pmut.RUnlock()
  153. return res
  154. }
  155. // LocalSize returns the number of files, deleted files and total bytes for all
  156. // files in the global model.
  157. func (m *Model) GlobalSize() (files, deleted, bytes int) {
  158. m.gmut.RLock()
  159. for _, f := range m.global {
  160. if f.Flags&protocol.FlagDeleted == 0 {
  161. files++
  162. bytes += f.Size()
  163. } else {
  164. deleted++
  165. }
  166. }
  167. m.gmut.RUnlock()
  168. return
  169. }
  170. // LocalSize returns the number of files, deleted files and total bytes for all
  171. // files in the local repository.
  172. func (m *Model) LocalSize() (files, deleted, bytes int) {
  173. m.lmut.RLock()
  174. for _, f := range m.local {
  175. if f.Flags&protocol.FlagDeleted == 0 {
  176. files++
  177. bytes += f.Size()
  178. } else {
  179. deleted++
  180. }
  181. }
  182. m.lmut.RUnlock()
  183. return
  184. }
  185. // InSyncSize returns the number and total byte size of the local files that
  186. // are in sync with the global model.
  187. func (m *Model) InSyncSize() (files, bytes int) {
  188. m.gmut.RLock()
  189. m.lmut.RLock()
  190. for n, f := range m.local {
  191. if gf, ok := m.global[n]; ok && f.Equals(gf) {
  192. files++
  193. bytes += f.Size()
  194. }
  195. }
  196. m.lmut.RUnlock()
  197. m.gmut.RUnlock()
  198. return
  199. }
  200. // NeedFiles returns the list of currently needed files and the total size.
  201. func (m *Model) NeedFiles() (files []File, bytes int) {
  202. qf := m.fq.QueuedFiles()
  203. m.gmut.RLock()
  204. for _, n := range qf {
  205. f := m.global[n]
  206. files = append(files, f)
  207. bytes += f.Size()
  208. }
  209. m.gmut.RUnlock()
  210. return
  211. }
  212. // Index is called when a new node is connected and we receive their full index.
  213. // Implements the protocol.Model interface.
  214. func (m *Model) Index(nodeID string, fs []protocol.FileInfo) {
  215. var files = make([]File, len(fs))
  216. for i := range fs {
  217. files[i] = fileFromFileInfo(fs[i])
  218. }
  219. m.imut.Lock()
  220. defer m.imut.Unlock()
  221. if m.trace["net"] {
  222. debugf("NET IDX(in): %s: %d files", nodeID, len(fs))
  223. }
  224. repo := make(map[string]File)
  225. for _, f := range files {
  226. m.indexUpdate(repo, f)
  227. }
  228. m.rmut.Lock()
  229. m.remote[nodeID] = repo
  230. m.rmut.Unlock()
  231. m.recomputeGlobal()
  232. m.recomputeNeedForFiles(files)
  233. }
  234. // IndexUpdate is called for incremental updates to connected nodes' indexes.
  235. // Implements the protocol.Model interface.
  236. func (m *Model) IndexUpdate(nodeID string, fs []protocol.FileInfo) {
  237. var files = make([]File, len(fs))
  238. for i := range fs {
  239. files[i] = fileFromFileInfo(fs[i])
  240. }
  241. m.imut.Lock()
  242. defer m.imut.Unlock()
  243. if m.trace["net"] {
  244. debugf("NET IDXUP(in): %s: %d files", nodeID, len(files))
  245. }
  246. m.rmut.Lock()
  247. repo, ok := m.remote[nodeID]
  248. if !ok {
  249. warnf("Index update from node %s that does not have an index", nodeID)
  250. m.rmut.Unlock()
  251. return
  252. }
  253. for _, f := range files {
  254. m.indexUpdate(repo, f)
  255. }
  256. m.rmut.Unlock()
  257. m.recomputeGlobal()
  258. m.recomputeNeedForFiles(files)
  259. }
  260. func (m *Model) indexUpdate(repo map[string]File, f File) {
  261. if m.trace["idx"] {
  262. var flagComment string
  263. if f.Flags&protocol.FlagDeleted != 0 {
  264. flagComment = " (deleted)"
  265. }
  266. debugf("IDX(in): %q m=%d f=%o%s v=%d (%d blocks)", f.Name, f.Modified, f.Flags, flagComment, f.Version, len(f.Blocks))
  267. }
  268. if extraFlags := f.Flags &^ (protocol.FlagInvalid | protocol.FlagDeleted | 0xfff); extraFlags != 0 {
  269. warnf("IDX(in): Unknown flags 0x%x in index record %+v", extraFlags, f)
  270. return
  271. }
  272. repo[f.Name] = f
  273. }
  274. // Close removes the peer from the model and closes the underlying connection if possible.
  275. // Implements the protocol.Model interface.
  276. func (m *Model) Close(node string, err error) {
  277. if m.trace["net"] {
  278. debugf("NET: %s: %v", node, err)
  279. }
  280. if err == protocol.ErrClusterHash {
  281. warnf("Connection to %s closed due to mismatched cluster hash. Ensure that the configured cluster members are identical on both nodes.", node)
  282. }
  283. m.fq.RemoveAvailable(node)
  284. m.pmut.Lock()
  285. m.rmut.Lock()
  286. conn, ok := m.rawConn[node]
  287. if ok {
  288. conn.Close()
  289. }
  290. delete(m.remote, node)
  291. delete(m.protoConn, node)
  292. delete(m.rawConn, node)
  293. m.rmut.Unlock()
  294. m.pmut.Unlock()
  295. m.recomputeGlobal()
  296. m.recomputeNeedForGlobal()
  297. }
  298. // Request returns the specified data segment by reading it from local disk.
  299. // Implements the protocol.Model interface.
  300. func (m *Model) Request(nodeID, name string, offset int64, size uint32, hash []byte) ([]byte, error) {
  301. // Verify that the requested file exists in the local and global model.
  302. m.lmut.RLock()
  303. lf, localOk := m.local[name]
  304. m.lmut.RUnlock()
  305. m.gmut.RLock()
  306. _, globalOk := m.global[name]
  307. m.gmut.RUnlock()
  308. if !localOk || !globalOk {
  309. warnf("SECURITY (nonexistent file) REQ(in): %s: %q o=%d s=%d h=%x", nodeID, name, offset, size, hash)
  310. return nil, ErrNoSuchFile
  311. }
  312. if lf.Flags&protocol.FlagInvalid != 0 {
  313. return nil, ErrInvalid
  314. }
  315. if m.trace["net"] && nodeID != "<local>" {
  316. debugf("NET REQ(in): %s: %q o=%d s=%d h=%x", nodeID, name, offset, size, hash)
  317. }
  318. fn := path.Join(m.dir, name)
  319. fd, err := os.Open(fn) // XXX: Inefficient, should cache fd?
  320. if err != nil {
  321. return nil, err
  322. }
  323. defer fd.Close()
  324. buf := buffers.Get(int(size))
  325. _, err = fd.ReadAt(buf, offset)
  326. if err != nil {
  327. return nil, err
  328. }
  329. if m.limitRequestRate != nil {
  330. for s := 0; s < len(buf); s += 1024 {
  331. <-m.limitRequestRate
  332. }
  333. }
  334. return buf, nil
  335. }
  336. // ReplaceLocal replaces the local repository index with the given list of files.
  337. func (m *Model) ReplaceLocal(fs []File) {
  338. var updated bool
  339. var newLocal = make(map[string]File)
  340. m.lmut.RLock()
  341. for _, f := range fs {
  342. newLocal[f.Name] = f
  343. if ef := m.local[f.Name]; !ef.Equals(f) {
  344. updated = true
  345. }
  346. }
  347. m.lmut.RUnlock()
  348. if m.markDeletedLocals(newLocal) {
  349. updated = true
  350. }
  351. m.lmut.RLock()
  352. if len(newLocal) != len(m.local) {
  353. updated = true
  354. }
  355. m.lmut.RUnlock()
  356. if updated {
  357. m.lmut.Lock()
  358. m.local = newLocal
  359. m.lmut.Unlock()
  360. m.recomputeGlobal()
  361. m.recomputeNeedForGlobal()
  362. m.umut.Lock()
  363. m.updatedLocal = time.Now().Unix()
  364. m.lastIdxBcastRequest = time.Now()
  365. m.umut.Unlock()
  366. }
  367. }
  368. // SeedLocal replaces the local repository index with the given list of files,
  369. // in protocol data types. Does not track deletes, should only be used to seed
  370. // the local index from a cache file at startup.
  371. func (m *Model) SeedLocal(fs []protocol.FileInfo) {
  372. m.lmut.Lock()
  373. m.local = make(map[string]File)
  374. for _, f := range fs {
  375. m.local[f.Name] = fileFromFileInfo(f)
  376. }
  377. m.lmut.Unlock()
  378. m.recomputeGlobal()
  379. m.recomputeNeedForGlobal()
  380. }
  381. // ConnectedTo returns true if we are connected to the named node.
  382. func (m *Model) ConnectedTo(nodeID string) bool {
  383. m.pmut.RLock()
  384. _, ok := m.protoConn[nodeID]
  385. m.pmut.RUnlock()
  386. return ok
  387. }
  388. // RepoID returns a unique ID representing the current repository location.
  389. func (m *Model) RepoID() string {
  390. return fmt.Sprintf("%x", sha1.Sum([]byte(m.dir)))
  391. }
  392. // AddConnection adds a new peer connection to the model. An initial index will
  393. // be sent to the connected peer, thereafter index updates whenever the local
  394. // repository changes.
  395. func (m *Model) AddConnection(rawConn io.Closer, protoConn Connection) {
  396. nodeID := protoConn.ID()
  397. m.pmut.Lock()
  398. m.protoConn[nodeID] = protoConn
  399. m.rawConn[nodeID] = rawConn
  400. m.pmut.Unlock()
  401. go func() {
  402. idx := m.ProtocolIndex()
  403. protoConn.Index(idx)
  404. }()
  405. m.initmut.Lock()
  406. rw := m.rwRunning
  407. m.initmut.Unlock()
  408. if !rw {
  409. return
  410. }
  411. for i := 0; i < m.parallelRequests; i++ {
  412. i := i
  413. go func() {
  414. if m.trace["pull"] {
  415. debugln("PULL: Starting", nodeID, i)
  416. }
  417. for {
  418. m.pmut.RLock()
  419. if _, ok := m.protoConn[nodeID]; !ok {
  420. if m.trace["pull"] {
  421. debugln("PULL: Exiting", nodeID, i)
  422. }
  423. m.pmut.RUnlock()
  424. return
  425. }
  426. m.pmut.RUnlock()
  427. qb, ok := m.fq.Get(nodeID)
  428. if ok {
  429. if m.trace["pull"] {
  430. debugln("PULL: Request", nodeID, i, qb.name, qb.block.Offset)
  431. }
  432. data, _ := protoConn.Request(qb.name, qb.block.Offset, qb.block.Size, qb.block.Hash)
  433. m.fq.Done(qb.name, qb.block.Offset, data)
  434. } else {
  435. time.Sleep(1 * time.Second)
  436. }
  437. }
  438. }()
  439. }
  440. }
  441. // ProtocolIndex returns the current local index in protocol data types.
  442. // Must be called with the read lock held.
  443. func (m *Model) ProtocolIndex() []protocol.FileInfo {
  444. var index []protocol.FileInfo
  445. m.lmut.RLock()
  446. for _, f := range m.local {
  447. mf := fileInfoFromFile(f)
  448. if m.trace["idx"] {
  449. var flagComment string
  450. if mf.Flags&protocol.FlagDeleted != 0 {
  451. flagComment = " (deleted)"
  452. }
  453. debugf("IDX(out): %q m=%d f=%o%s v=%d (%d blocks)", mf.Name, mf.Modified, mf.Flags, flagComment, mf.Version, len(mf.Blocks))
  454. }
  455. index = append(index, mf)
  456. }
  457. m.lmut.RUnlock()
  458. return index
  459. }
  460. func (m *Model) requestGlobal(nodeID, name string, offset int64, size uint32, hash []byte) ([]byte, error) {
  461. m.pmut.RLock()
  462. nc, ok := m.protoConn[nodeID]
  463. m.pmut.RUnlock()
  464. if !ok {
  465. return nil, fmt.Errorf("requestGlobal: no such node: %s", nodeID)
  466. }
  467. if m.trace["net"] {
  468. debugf("NET REQ(out): %s: %q o=%d s=%d h=%x", nodeID, name, offset, size, hash)
  469. }
  470. return nc.Request(name, offset, size, hash)
  471. }
  472. func (m *Model) broadcastIndexLoop() {
  473. for {
  474. m.umut.RLock()
  475. bcastRequested := m.lastIdxBcastRequest.After(m.lastIdxBcast)
  476. holdtimeExceeded := time.Since(m.lastIdxBcastRequest) > idxBcastHoldtime
  477. m.umut.RUnlock()
  478. maxDelayExceeded := time.Since(m.lastIdxBcast) > idxBcastMaxDelay
  479. if bcastRequested && (holdtimeExceeded || maxDelayExceeded) {
  480. idx := m.ProtocolIndex()
  481. var indexWg sync.WaitGroup
  482. indexWg.Add(len(m.protoConn))
  483. m.umut.Lock()
  484. m.lastIdxBcast = time.Now()
  485. m.umut.Unlock()
  486. m.pmut.RLock()
  487. for _, node := range m.protoConn {
  488. node := node
  489. if m.trace["net"] {
  490. debugf("NET IDX(out/loop): %s: %d files", node.ID(), len(idx))
  491. }
  492. go func() {
  493. node.Index(idx)
  494. indexWg.Done()
  495. }()
  496. }
  497. m.pmut.RUnlock()
  498. indexWg.Wait()
  499. }
  500. time.Sleep(idxBcastHoldtime)
  501. }
  502. }
  503. // markDeletedLocals sets the deleted flag on files that have gone missing locally.
  504. func (m *Model) markDeletedLocals(newLocal map[string]File) bool {
  505. // For every file in the existing local table, check if they are also
  506. // present in the new local table. If they are not, check that we already
  507. // had the newest version available according to the global table and if so
  508. // note the file as having been deleted.
  509. var updated bool
  510. m.gmut.RLock()
  511. m.lmut.RLock()
  512. for n, f := range m.local {
  513. if _, ok := newLocal[n]; !ok {
  514. if gf := m.global[n]; !gf.NewerThan(f) {
  515. if f.Flags&protocol.FlagDeleted == 0 {
  516. f.Flags = protocol.FlagDeleted
  517. f.Version++
  518. f.Blocks = nil
  519. updated = true
  520. }
  521. newLocal[n] = f
  522. }
  523. }
  524. }
  525. m.lmut.RUnlock()
  526. m.gmut.RUnlock()
  527. return updated
  528. }
  529. func (m *Model) updateLocal(f File) {
  530. var updated bool
  531. m.lmut.Lock()
  532. if ef, ok := m.local[f.Name]; !ok || !ef.Equals(f) {
  533. m.local[f.Name] = f
  534. updated = true
  535. }
  536. m.lmut.Unlock()
  537. if updated {
  538. m.recomputeGlobal()
  539. // We don't recomputeNeed here for two reasons:
  540. // - a need shouldn't have arisen due to having a newer local file
  541. // - recomputeNeed might call into fq.Add but we might have been called by
  542. // fq which would be a deadlock on fq
  543. m.umut.Lock()
  544. m.updatedLocal = time.Now().Unix()
  545. m.lastIdxBcastRequest = time.Now()
  546. m.umut.Unlock()
  547. }
  548. }
  549. /*
  550. XXX: Not done, needs elegant handling of availability
  551. func (m *Model) recomputeGlobalFor(files []File) bool {
  552. m.gmut.Lock()
  553. defer m.gmut.Unlock()
  554. var updated bool
  555. for _, f := range files {
  556. if gf, ok := m.global[f.Name]; !ok || f.NewerThan(gf) {
  557. m.global[f.Name] = f
  558. updated = true
  559. // Fix availability
  560. }
  561. }
  562. return updated
  563. }
  564. */
  565. func (m *Model) recomputeGlobal() {
  566. var newGlobal = make(map[string]File)
  567. m.lmut.RLock()
  568. for n, f := range m.local {
  569. newGlobal[n] = f
  570. }
  571. m.lmut.RUnlock()
  572. var available = make(map[string][]string)
  573. m.rmut.RLock()
  574. var highestMod int64
  575. for nodeID, fs := range m.remote {
  576. for n, nf := range fs {
  577. if lf, ok := newGlobal[n]; !ok || nf.NewerThan(lf) {
  578. newGlobal[n] = nf
  579. available[n] = []string{nodeID}
  580. if nf.Modified > highestMod {
  581. highestMod = nf.Modified
  582. }
  583. } else if lf.Equals(nf) {
  584. available[n] = append(available[n], nodeID)
  585. }
  586. }
  587. }
  588. m.rmut.RUnlock()
  589. for f, ns := range available {
  590. m.fq.SetAvailable(f, ns)
  591. }
  592. // Figure out if anything actually changed
  593. m.gmut.RLock()
  594. var updated bool
  595. if highestMod > m.updateGlobal || len(newGlobal) != len(m.global) {
  596. updated = true
  597. } else {
  598. for n, f0 := range newGlobal {
  599. if f1, ok := m.global[n]; !ok || !f0.Equals(f1) {
  600. updated = true
  601. break
  602. }
  603. }
  604. }
  605. m.gmut.RUnlock()
  606. if updated {
  607. m.gmut.Lock()
  608. m.umut.Lock()
  609. m.global = newGlobal
  610. m.updateGlobal = time.Now().Unix()
  611. m.umut.Unlock()
  612. m.gmut.Unlock()
  613. }
  614. }
  615. type addOrder struct {
  616. n string
  617. remote []Block
  618. fm *fileMonitor
  619. }
  620. func (m *Model) recomputeNeedForGlobal() {
  621. var toDelete []File
  622. var toAdd []addOrder
  623. m.gmut.RLock()
  624. for _, gf := range m.global {
  625. toAdd, toDelete = m.recomputeNeedForFile(gf, toAdd, toDelete)
  626. }
  627. m.gmut.RUnlock()
  628. for _, ao := range toAdd {
  629. m.fq.Add(ao.n, ao.remote, ao.fm)
  630. }
  631. for _, gf := range toDelete {
  632. m.dq <- gf
  633. }
  634. }
  635. func (m *Model) recomputeNeedForFiles(files []File) {
  636. var toDelete []File
  637. var toAdd []addOrder
  638. m.gmut.RLock()
  639. for _, gf := range files {
  640. toAdd, toDelete = m.recomputeNeedForFile(gf, toAdd, toDelete)
  641. }
  642. m.gmut.RUnlock()
  643. for _, ao := range toAdd {
  644. m.fq.Add(ao.n, ao.remote, ao.fm)
  645. }
  646. for _, gf := range toDelete {
  647. m.dq <- gf
  648. }
  649. }
  650. func (m *Model) recomputeNeedForFile(gf File, toAdd []addOrder, toDelete []File) ([]addOrder, []File) {
  651. m.lmut.RLock()
  652. lf, ok := m.local[gf.Name]
  653. m.lmut.RUnlock()
  654. if !ok || gf.NewerThan(lf) {
  655. if gf.Flags&protocol.FlagInvalid != 0 {
  656. // Never attempt to sync invalid files
  657. return toAdd, toDelete
  658. }
  659. if gf.Flags&protocol.FlagDeleted != 0 && !m.delete {
  660. // Don't want to delete files, so forget this need
  661. return toAdd, toDelete
  662. }
  663. if gf.Flags&protocol.FlagDeleted != 0 && !ok {
  664. // Don't have the file, so don't need to delete it
  665. return toAdd, toDelete
  666. }
  667. if m.trace["need"] {
  668. debugf("NEED: lf:%v gf:%v", lf, gf)
  669. }
  670. if gf.Flags&protocol.FlagDeleted != 0 {
  671. toDelete = append(toDelete, gf)
  672. } else {
  673. local, remote := BlockDiff(lf.Blocks, gf.Blocks)
  674. fm := fileMonitor{
  675. name: gf.Name,
  676. path: path.Clean(path.Join(m.dir, gf.Name)),
  677. global: gf,
  678. model: m,
  679. localBlocks: local,
  680. }
  681. toAdd = append(toAdd, addOrder{gf.Name, remote, &fm})
  682. }
  683. }
  684. return toAdd, toDelete
  685. }
  686. func (m *Model) WhoHas(name string) []string {
  687. var remote []string
  688. m.gmut.RLock()
  689. m.rmut.RLock()
  690. gf := m.global[name]
  691. for node, files := range m.remote {
  692. if file, ok := files[name]; ok && file.Equals(gf) {
  693. remote = append(remote, node)
  694. }
  695. }
  696. m.rmut.RUnlock()
  697. m.gmut.RUnlock()
  698. return remote
  699. }
  700. func (m *Model) deleteLoop() {
  701. for file := range m.dq {
  702. if m.trace["file"] {
  703. debugln("FILE: Delete", file.Name)
  704. }
  705. path := path.Clean(path.Join(m.dir, file.Name))
  706. err := os.Remove(path)
  707. if err != nil {
  708. warnf("%s: %v", file.Name, err)
  709. }
  710. m.updateLocal(file)
  711. }
  712. }
  713. func fileFromFileInfo(f protocol.FileInfo) File {
  714. var blocks = make([]Block, len(f.Blocks))
  715. var offset int64
  716. for i, b := range f.Blocks {
  717. blocks[i] = Block{
  718. Offset: offset,
  719. Size: b.Size,
  720. Hash: b.Hash,
  721. }
  722. offset += int64(b.Size)
  723. }
  724. return File{
  725. Name: f.Name,
  726. Flags: f.Flags,
  727. Modified: f.Modified,
  728. Version: f.Version,
  729. Blocks: blocks,
  730. }
  731. }
  732. func fileInfoFromFile(f File) protocol.FileInfo {
  733. var blocks = make([]protocol.BlockInfo, len(f.Blocks))
  734. for i, b := range f.Blocks {
  735. blocks[i] = protocol.BlockInfo{
  736. Size: b.Size,
  737. Hash: b.Hash,
  738. }
  739. }
  740. return protocol.FileInfo{
  741. Name: f.Name,
  742. Flags: f.Flags,
  743. Modified: f.Modified,
  744. Version: f.Version,
  745. Blocks: blocks,
  746. }
  747. }