44package ipnlocal
55
66import (
7- "cmp"
87 "fmt"
98 "os"
109 "slices"
@@ -26,26 +25,14 @@ const (
2625// enabled. This is currently based on checking for the drive:share node
2726// attribute.
2827func (b * LocalBackend ) DriveSharingEnabled () bool {
29- b .mu .Lock ()
30- defer b .mu .Unlock ()
31- return b .driveSharingEnabledLocked ()
32- }
33-
34- func (b * LocalBackend ) driveSharingEnabledLocked () bool {
35- return b .netMap != nil && b .netMap .SelfNode .HasCap (tailcfg .NodeAttrsTaildriveShare )
28+ return b .currentNode ().SelfHasCap (tailcfg .NodeAttrsTaildriveShare )
3629}
3730
3831// DriveAccessEnabled reports whether accessing Taildrive shares on remote nodes
3932// is enabled. This is currently based on checking for the drive:access node
4033// attribute.
4134func (b * LocalBackend ) DriveAccessEnabled () bool {
42- b .mu .Lock ()
43- defer b .mu .Unlock ()
44- return b .driveAccessEnabledLocked ()
45- }
46-
47- func (b * LocalBackend ) driveAccessEnabledLocked () bool {
48- return b .netMap != nil && b .netMap .SelfNode .HasCap (tailcfg .NodeAttrsTaildriveAccess )
35+ return b .currentNode ().SelfHasCap (tailcfg .NodeAttrsTaildriveAccess )
4936}
5037
5138// DriveSetServerAddr tells Taildrive to use the given address for connecting
@@ -266,7 +253,7 @@ func (b *LocalBackend) driveNotifyShares(shares views.SliceView[*drive.Share, dr
266253// shares has changed since the last notification.
267254func (b * LocalBackend ) driveNotifyCurrentSharesLocked () {
268255 var shares views.SliceView [* drive.Share , drive.ShareView ]
269- if b .driveSharingEnabledLocked () {
256+ if b .DriveSharingEnabled () {
270257 // Only populate shares if sharing is enabled.
271258 shares = b .pm .prefs .DriveShares ()
272259 }
@@ -310,12 +297,12 @@ func (b *LocalBackend) updateDrivePeersLocked(nm *netmap.NetworkMap) {
310297 }
311298
312299 var driveRemotes []* drive.Remote
313- if b .driveAccessEnabledLocked () {
300+ if b .DriveAccessEnabled () {
314301 // Only populate peers if access is enabled, otherwise leave blank.
315302 driveRemotes = b .driveRemotesFromPeers (nm )
316303 }
317304
318- fs .SetRemotes (b . netMap .Domain , driveRemotes , b .newDriveTransport ())
305+ fs .SetRemotes (nm .Domain , driveRemotes , b .newDriveTransport ())
319306}
320307
321308func (b * LocalBackend ) driveRemotesFromPeers (nm * netmap.NetworkMap ) []* drive.Remote {
@@ -330,32 +317,28 @@ func (b *LocalBackend) driveRemotesFromPeers(nm *netmap.NetworkMap) []*drive.Rem
330317 // Peers are available to Taildrive if:
331318 // - They are online
332319 // - They are allowed to share at least one folder with us
333- b .mu .Lock ()
334- latestNetMap := b .netMap
335- b .mu .Unlock ()
336-
337- idx , found := slices .BinarySearchFunc (latestNetMap .Peers , peerID , func (candidate tailcfg.NodeView , id tailcfg.NodeID ) int {
338- return cmp .Compare (candidate .ID (), id )
339- })
340- if ! found {
320+ cn := b .currentNode ()
321+ peer , ok := cn .PeerByID (peerID )
322+ if ! ok {
341323 return false
342324 }
343325
344- peer := latestNetMap .Peers [idx ]
345-
346326 // Exclude offline peers.
347327 // TODO(oxtoacart): for some reason, this correctly
348328 // catches when a node goes from offline to online,
349329 // but not the other way around...
330+ // TODO(oxtoacart,nickkhyl): the reason was probably
331+ // that we were using netmap.Peers instead of b.peers.
332+ // The netmap.Peers slice is not updated in all cases.
333+ // It should be fixed now that we use PeerByIDOk.
350334 if ! peer .Online ().Get () {
351335 return false
352336 }
353337
354338 // Check that the peer is allowed to share with us.
355339 addresses := peer .Addresses ()
356340 for _ , p := range addresses .All () {
357- capsMap := b .PeerCaps (p .Addr ())
358- if capsMap .HasCapability (tailcfg .PeerCapabilityTaildriveSharer ) {
341+ if cn .PeerHasCap (p .Addr (), tailcfg .PeerCapabilityTaildriveSharer ) {
359342 return true
360343 }
361344 }
0 commit comments