github.com/visualfc/goembed@v0.3.3/fs/fs.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fs
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  type (
    13  	FileInfo  = os.FileInfo
    14  	PathError = os.PathError
    15  	FileMode  = os.FileMode
    16  )
    17  
    18  const (
    19  	ModeIrregular = os.ModeIrregular
    20  	ModeDir       = os.ModeDir
    21  )
    22  
    23  var (
    24  	ErrNotExist = os.ErrNotExist
    25  	SkipDir     = filepath.SkipDir
    26  )
    27  
    28  // ValidPath reports whether the given path name
    29  // is valid for use in a call to Open.
    30  // Path names passed to open are unrooted, slash-separated
    31  // sequences of path elements, like “x/y/z”.
    32  // Path names must not contain a “.” or “..” or empty element,
    33  // except for the special case that the root directory is named “.”.
    34  //
    35  // Paths are slash-separated on all systems, even Windows.
    36  // Backslashes must not appear in path names.
    37  func ValidPath(name string) bool {
    38  	if name == "." {
    39  		// special case
    40  		return true
    41  	}
    42  
    43  	// Iterate over elements in name, checking each.
    44  	for {
    45  		i := 0
    46  		for i < len(name) && name[i] != '/' {
    47  			if name[i] == '\\' {
    48  				return false
    49  			}
    50  			i++
    51  		}
    52  		elem := name[:i]
    53  		if elem == "" || elem == "." || elem == ".." {
    54  			return false
    55  		}
    56  		if i == len(name) {
    57  			return true // reached clean ending
    58  		}
    59  		name = name[i+1:]
    60  	}
    61  }