model.go 19 KB

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