github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/storage/fileset/dir_inserter.go (about)

     1  package fileset
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/pachyderm/pachyderm/src/server/pkg/storage/fileset/index"
    10  )
    11  
    12  type dirInserter struct {
    13  	x FileSet
    14  }
    15  
    16  // NewDirInserter creates a file set that inserts directory entries.
    17  func NewDirInserter(x FileSet) FileSet {
    18  	return &dirInserter{x: x}
    19  }
    20  
    21  // Iterate calls cb once for every file in lexicographical order by path
    22  func (s *dirInserter) Iterate(ctx context.Context, cb func(File) error, _ ...bool) error {
    23  	lastPath := ""
    24  	var emit func(p string, f File) error
    25  	emit = func(p string, f File) error {
    26  		parent := Clean(parentOf(p), true)
    27  		if p == "/" || lastPath >= parent {
    28  			if err := cb(f); err != nil {
    29  				return err
    30  			}
    31  			lastPath = p
    32  			return nil
    33  		}
    34  		// need to create entry for parent
    35  		df := dirFile{
    36  			path: parent,
    37  		}
    38  		if err := emit(parent, df); err != nil {
    39  			return err
    40  		}
    41  		return emit(p, f)
    42  	}
    43  	return s.x.Iterate(ctx, func(f File) error {
    44  		return emit(f.Index().Path, f)
    45  	})
    46  }
    47  
    48  func parentOf(x string) string {
    49  	x = strings.TrimRight(x, "/")
    50  	y := path.Dir(x)
    51  	if y == x {
    52  		return "/"
    53  	}
    54  	if !IsDir(y) {
    55  		y += "/"
    56  	}
    57  	return y
    58  }
    59  
    60  type dirFile struct {
    61  	path string
    62  }
    63  
    64  func (d dirFile) Index() *index.Index {
    65  	return &index.Index{
    66  		Path: d.path,
    67  	}
    68  }
    69  
    70  func (d dirFile) Content(w io.Writer) error {
    71  	return nil
    72  }