vitess.io/vitess@v0.16.2/go/vt/topo/memorytopo/directory.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package memorytopo
    18  
    19  import (
    20  	"context"
    21  
    22  	"vitess.io/vitess/go/vt/proto/vtrpc"
    23  	"vitess.io/vitess/go/vt/vterrors"
    24  
    25  	"vitess.io/vitess/go/vt/topo"
    26  )
    27  
    28  // ListDir is part of the topo.Conn interface.
    29  func (c *Conn) ListDir(ctx context.Context, dirPath string, full bool) ([]topo.DirEntry, error) {
    30  	if err := c.dial(ctx); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	c.factory.mu.Lock()
    35  	defer c.factory.mu.Unlock()
    36  
    37  	if c.factory.err != nil {
    38  		return nil, c.factory.err
    39  	}
    40  
    41  	isRoot := false
    42  	if dirPath == "" || dirPath == "/" {
    43  		isRoot = true
    44  	}
    45  
    46  	// Get the node to list.
    47  	n := c.factory.nodeByPath(c.cell, dirPath)
    48  	if n == nil {
    49  		return nil, topo.NewError(topo.NoNode, dirPath)
    50  	}
    51  
    52  	// Check it's a directory.
    53  	if !n.isDirectory() {
    54  		return nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "node %v in cell %v is not a directory", dirPath, c.cell)
    55  	}
    56  
    57  	result := make([]topo.DirEntry, 0, len(n.children))
    58  	for name, child := range n.children {
    59  		e := topo.DirEntry{
    60  			Name: name,
    61  		}
    62  		if full {
    63  			e.Type = topo.TypeFile
    64  			if child.isDirectory() {
    65  				e.Type = topo.TypeDirectory
    66  			}
    67  			if isRoot && name == electionsPath {
    68  				e.Ephemeral = true
    69  			}
    70  		}
    71  		result = append(result, e)
    72  	}
    73  	topo.DirEntriesSortByName(result)
    74  	return result, nil
    75  }