// nodePathLock returns an etcd directory path used specifically for semaphore
// indices based on the given key.
1191
func (b *Etcd2Backend) nodePathLock(key string) string {
	return filepath.Join(b.path, filepath.Dir(key), Etcd2NodeLockPrefix+filepath.Base(key)+"/")
}
349869
func (l *FileLock) ExclusiveLock() error {
	return syscall.Flock(l.fd, syscall.LOCK_EX)
}
307189
func nodeKey(id ID) []byte {
	key := append([]byte(dbNodePrefix), id[:]...)
	key = append(key, ':')
	key = append(key, dbDiscoverRoot...)
	return key
}
627594
func (pe PathExpression) pushBackOneKeyLeg(key string) PathExpression {
	newPe := PathExpression{
		legs:  append(pe.legs, pathLeg{typ: pathLegKey, dotKey: key}),
		flags: pe.flags,
	}
	if key == "*" {
		newPe.flags |= pathExpressionContainsAsterisk
	}
	return newPe
}
600123
func (s *GlobalStore) lock(addr common.Address, key []byte) (unlock func(), err error) {
	start := time.Now()
	nodeLockKey := addr.Hex()
	for {
		_, loaded := s.nodesLocks.LoadOrStore(nodeLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	start = time.Now()
	keyLockKey := common.Bytes2Hex(key)
	for {
		_, loaded := s.keysLocks.LoadOrStore(keyLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	return func() {
		s.nodesLocks.Delete(nodeLockKey)
		s.keysLocks.Delete(keyLockKey)
	}, nil
}
786650
func (nm *defaultNodeManager) ExecuteOnNode(nodes []*Node, command Command, previous *Node) (bool, error) {
	if nodes == nil {
		panic("[defaultNodeManager] nil nodes argument")
	}
	if len(nodes) == 0 || nodes[0] == nil {
		return false, ErrDefaultNodeManagerRequiresNode
	}

	var err error
	executed := false

	nm.RLock()
	startingIndex := nm.nodeIndex
	nm.RUnlock()

	for {
		nm.Lock()
		if nm.nodeIndex >= len(nodes) {
			nm.nodeIndex = 0
		}
		node := nodes[nm.nodeIndex]
		nm.nodeIndex++
		nm.Unlock()

		// don't try the same node twice in a row if we have multiple nodes
		if len(nodes) > 1 && previous != nil && previous == node {
			continue
		}

		executed, err = node.execute(command)
		if executed == true {
			logDebug("[DefaultNodeManager]", "executed '%s' on node '%s', err '%v'", command.Name(), node, err)
			break
		}

		nm.RLock()
		if startingIndex == nm.nodeIndex {
			nm.RUnlock()
			// logDebug("[DefaultNodeManager]", "startingIndex %d nm.nodeIndex %d", startingIndex, nm.nodeIndex)
			break
		}
		nm.RUnlock()
	}

	return executed, err
}
578377
func TryExclusiveKeyLock(lockDir string, key string) (*KeyLock, error) {
	return createAndLock(lockDir, key, keyLockExclusive|keyLockNonBlocking)
}
357413
func (n *Node) WalkPath(path []byte, fn WalkFn) {
	search := path
	for {
		// Visit the leaf values if any
		if n.leaf != nil && fn(n.leaf.key, n.leaf.val) {
			return
		}

		// Check for key exhaution
		if len(search) == 0 {
			return
		}

		// Look for an edge
		_, n = n.getEdge(search[0])
		if n == nil {
			return
		}

		// Consume the search prefix
		if bytes.HasPrefix(search, n.prefix) {
			search = search[len(n.prefix):]
		} else {
			break
		}
	}
}
313988
func (tc *TreeCache) findNode(path string) (*TreeNode, error) {
	if !strings.HasPrefix(path, tc.root.path) {
		return nil, ErrRootNotMatch
	}

	path = strings.TrimPrefix(path, tc.root.path)
	current := tc.root
	for _, part := range strings.Split(path, "/") {
		if part == "" {
			continue
		}
		next, exists := current.FindChild(part)
		if !exists {
			return nil, ErrNodeNotFound
		}
		current = next
	}

	return current, nil
}
474748
func funlock(db *DB) error {
	return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
}
470297
func (db *FSDB) SetNoLock(key []byte, value []byte) {
	key = escapeKey(key)
	value = nonNilBytes(value)
	path := db.nameToPath(key)
	err := write(path, value)
	if err != nil {
		panic(errors.Wrapf(err, "Setting key %s (0x%X)", string(key), key))
	}
}
1166229
func (fbsk *folderBranchStatusKeeper) convertNodesToPathsLocked(
	m map[NodeID]Node) []string {
	var ret []string
	for _, n := range m {
		ret = append(ret, fbsk.nodeCache.PathFromNode(n).String())
	}
	return ret
}
890582
func (s *GlobalStore) KeyNodes(key []byte, startAddr *common.Address, limit int) (nodes mock.Nodes, err error) {
	return s.nodes(key, startAddr, limit)
}
576022
func (ncs *nodeCacheStandard) PathFromNode(node Node) (p data.Path) {
	ncs.lock.RLock()
	defer ncs.lock.RUnlock()

	ns, ok := node.Unwrap().(*nodeStandard)
	if !ok {
		p.Path = nil
		return
	}

	for ns != nil {
		core := ns.core
		if core.parent == nil && len(core.cachedPath.Path) > 0 {
			// The node was unlinked, but is still in use, so use its
			// cached path.  The path is already reversed, so append
			// it backwards one-by-one to the existing path.  If this
			// is the first node, we can just optimize by returning
			// the complete cached path.
			if len(p.Path) == 0 {
				return core.cachedPath
			}
			for i := len(core.cachedPath.Path) - 1; i >= 0; i-- {
				p.Path = append(p.Path, core.cachedPath.Path[i])
			}
			break
		}

		p.Path = append(p.Path, *core.pathNode)
		if core.parent != nil {
			ns = core.parent.Unwrap().(*nodeStandard)
		} else {
			break
		}
	}

	// need to reverse the path nodes
	for i := len(p.Path)/2 - 1; i >= 0; i-- {
		opp := len(p.Path) - 1 - i
		p.Path[i], p.Path[opp] = p.Path[opp], p.Path[i]
	}

	// TODO: would it make any sense to cache the constructed path?
	p.FolderBranch = ncs.folderBranch
	return
}
1257693
func (p *PolicyAuthorizer) NodeWrite(name string, scope sentinel.ScopeFn) bool {
	// Check for an exact rule or catch-all
	if rule, ok := getPolicy(name, p.nodeRules); ok {
		pr := rule.(RulePolicy)
		if allow, recurse := enforce(pr.aclPolicy, PolicyWrite); !recurse {
			return allow
		}
	}

	// No matching rule, use the parent.
	return p.parent.NodeWrite(name, scope)
}
516925
func (p Path) GetNode(ctx context.Context, config libkbfs.Config) (libkbfs.Node, data.EntryInfo, error) {
	if p.PathType != TLFPathType {
		entryInfo := data.EntryInfo{
			Type: data.Dir,
		}
		return nil, entryInfo, nil
	}

	tlfHandle, err := ParseTlfHandle(
		ctx, config.KBPKI(), config.MDOps(), config, p.TLFName, p.TLFType)
	if err != nil {
		return nil, data.EntryInfo{}, err
	}

	node, entryInfo, err := config.KBFSOps().GetOrCreateRootNode(ctx, tlfHandle, data.MasterBranch)
	if err != nil {
		return nil, data.EntryInfo{}, err
	}

	for _, component := range p.TLFComponents {
		lookupNode, lookupEntryInfo, lookupErr := config.KBFSOps().Lookup(ctx, node, component)
		if lookupErr != nil {
			return nil, data.EntryInfo{}, lookupErr
		}
		node = lookupNode
		entryInfo = lookupEntryInfo
	}

	return node, entryInfo, nil
}
798897
func (s *Simulation) NodesItems(key interface{}) (values map[enode.ID]interface{}) {
	s.mu.RLock()
	defer s.mu.RUnlock()

	ids := s.NodeIDs()
	values = make(map[enode.ID]interface{}, len(ids))
	for _, id := range ids {
		if _, ok := s.buckets[id]; !ok {
			continue
		}
		if v, ok := s.buckets[id].Load(key); ok {
			values[id] = v
		}
	}
	return values
}