...

Source file src/github.com/containerd/continuity/testutil_test.go

Documentation: github.com/containerd/continuity

     1  /*
     2     Copyright The containerd 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  //nolint:unused,deadcode
    18  package continuity
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"path/filepath"
    25  )
    26  
    27  func tree(w io.Writer, dir string) error {
    28  	fmt.Fprintf(w, "%s\n", dir)
    29  	return _tree(w, dir, "")
    30  }
    31  
    32  func _tree(w io.Writer, dir string, indent string) error {
    33  	entries, err := os.ReadDir(dir)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	for i, entry := range entries {
    38  		fPath := filepath.Join(dir, entry.Name())
    39  		fIndent := indent
    40  		if i < len(entries)-1 {
    41  			fIndent += "|-- "
    42  		} else {
    43  			fIndent += "`-- "
    44  		}
    45  		target := ""
    46  		fileInfo, err := entry.Info()
    47  		if err != nil {
    48  			return fmt.Errorf("file info not found for %s: %w", entry.Name(), err)
    49  		}
    50  		if (fileInfo.Mode() & os.ModeSymlink) == os.ModeSymlink {
    51  			realPath, err := os.Readlink(fPath)
    52  			if err != nil {
    53  				target += fmt.Sprintf(" -> unknown (error: %v)", err)
    54  			} else {
    55  				target += " -> " + realPath
    56  			}
    57  		}
    58  		fmt.Fprintf(w, "%s%s%s\n", fIndent, entry.Name(), target)
    59  		if entry.IsDir() {
    60  			dIndent := indent
    61  			if i < len(entries)-1 {
    62  				dIndent += "|   "
    63  			} else {
    64  				dIndent += "    "
    65  			}
    66  			if err := _tree(w, fPath, dIndent); err != nil {
    67  				return err
    68  			}
    69  		}
    70  	}
    71  	return nil
    72  }
    73  

View as plain text