github.com/amundsenjunior/helm@v2.8.0-rc.1.0.20180119233529-2b92431476e1+incompatible/pkg/sympath/walk.go (about)

     1  /*
     2  Copyright (c) for portions of walk.go are held by The Go Authors, 2009 and are provided under
     3  the BSD license.
     4  
     5  https://github.com/golang/go/blob/master/LICENSE
     6  
     7  Copyright 2017 The Kubernetes Authors All rights reserved.
     8  Licensed under the Apache License, Version 2.0 (the "License");
     9  you may not use this file except in compliance with the License.
    10  You may obtain a copy of the License at
    11  
    12  http://www.apache.org/licenses/LICENSE-2.0
    13  
    14  Unless required by applicable law or agreed to in writing, software
    15  distributed under the License is distributed on an "AS IS" BASIS,
    16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  See the License for the specific language governing permissions and
    18  limitations under the License.
    19  */
    20  
    21  package sympath
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  	"sort"
    28  )
    29  
    30  // Walk walks the file tree rooted at root, calling walkFn for each file or directory
    31  // in the tree, including root. All errors that arise visiting files and directories
    32  // are filtered by walkFn. The files are walked in lexical order, which makes the
    33  // output deterministic but means that for very large directories Walk can be
    34  // inefficient. Walk follows symbolic links.
    35  func Walk(root string, walkFn filepath.WalkFunc) error {
    36  	info, err := os.Lstat(root)
    37  	if err != nil {
    38  		err = walkFn(root, nil, err)
    39  	} else {
    40  		err = symwalk(root, info, walkFn)
    41  	}
    42  	if err == filepath.SkipDir {
    43  		return nil
    44  	}
    45  	return err
    46  }
    47  
    48  // readDirNames reads the directory named by dirname and returns
    49  // a sorted list of directory entries.
    50  func readDirNames(dirname string) ([]string, error) {
    51  	f, err := os.Open(dirname)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	names, err := f.Readdirnames(-1)
    56  	f.Close()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	sort.Strings(names)
    61  	return names, nil
    62  }
    63  
    64  // symwalk recursively descends path, calling walkFn.
    65  func symwalk(path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
    66  	// Recursively walk symlinked directories.
    67  	if IsSymlink(info) {
    68  		resolved, err := filepath.EvalSymlinks(path)
    69  		if err != nil {
    70  			return fmt.Errorf("error evaluating symlink %s: %s", path, err)
    71  		}
    72  		if info, err = os.Lstat(resolved); err != nil {
    73  			return err
    74  		}
    75  		if err := symwalk(resolved, info, walkFn); err != nil && err != filepath.SkipDir {
    76  			return err
    77  		}
    78  	}
    79  
    80  	if err := walkFn(path, info, nil); err != nil {
    81  		return err
    82  	}
    83  
    84  	if !info.IsDir() {
    85  		return nil
    86  	}
    87  
    88  	names, err := readDirNames(path)
    89  	if err != nil {
    90  		return walkFn(path, info, err)
    91  	}
    92  
    93  	for _, name := range names {
    94  		filename := filepath.Join(path, name)
    95  		fileInfo, err := os.Lstat(filename)
    96  		if err != nil {
    97  			if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
    98  				return err
    99  			}
   100  		} else {
   101  			err = symwalk(filename, fileInfo, walkFn)
   102  			if err != nil {
   103  				if (!fileInfo.IsDir() && !IsSymlink(fileInfo)) || err != filepath.SkipDir {
   104  					return err
   105  				}
   106  			}
   107  		}
   108  	}
   109  	return nil
   110  }
   111  
   112  // IsSymlink is used to determine if the fileinfo is a symbolic link.
   113  func IsSymlink(fi os.FileInfo) bool {
   114  	return fi.Mode()&os.ModeSymlink != 0
   115  }