filepath

package
v0.0.0-...-3e27772 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2024 License: BSD-3-Clause Imports: 6 Imported by: 52

Documentation

Overview

Package filepath implements utility routines for manipulating filename paths in a way compatible with the target operating system-defined file paths. It is a modification of the original GO package path/filepath solving severe errors in handling symbolic links.

The original package defines a function Clean that formally normalizes a file path by eliminating .. and . entries. This is done WITHOUT observing the actual file system. Although this is no problem for the function itself, because it is designed to do so, it becomes a severe problem for the whole package, because nearly all functions internally use Clean to clean the path. As a consequence even functions like Join deliver corrupted invalid results for valid inputs if the path incorporates symbolic links to directories. Especially EvalSymlinks cannot be used to evaluate paths to existing files, because Clean is internally used to normalize content of symbolic links.

This package provides a set of functions that do not hamper the meaning of path names keeping the rest of the original specification as far as possible. Additionally some new functions (like Canonical) or alternate versions of existing functions (like Split2) are provided that offer a more useful specification than the original one.

Index

Constants

View Source
const PathSeparatorString = string(os.PathSeparator)

Variables

View Source
var ErrBadPattern = orig.ErrBadPattern
View Source
var SkipDir = orig.SkipDir

SkipDir is used as a return value from WalkFuncs to indicate that the directory named in the call is to be skipped. It is not returned as an error by any function.

Functions

func Abs

func Abs(path string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Symbolic links in the given path will be resolved, but not in the current working directory, if used to make the path absolute. The denoted file may not exist. Abs never calls Clean on the result, so the resulting path will denote the same file as the argument.

func Base

func Base(path string) string

func Canonical

func Canonical(path string, exist bool) (string, error)

Canonical returns the canonical absolute path of a file. If exist=false the denoted file must not exist, but then the part of the initial path refering to a not existing directoy structure is lexically resolved (like Clean) and does not consider potential symbolic links that might occur if the file is finally created in the future.

func Clean

func Clean(path string) string

func Dir

func Dir(path string) string

Dir acts like filepath.Dir, but does not clean the path Like the original Dir function this is NOT the counterpart of Base if the path ends with a trailing Separator. Base("a/b/")="b" and Dir("a/b/") = "a/b".

func Dir2

func Dir2(path string) string

Dir2 returns the path's directory dropping the final element after removing trailing Separators, Dir2 goes not call Clean on the path. If the path is empty, Dir2 returns ".". If the path consists entirely of Separators, Dir2 returns a single Separator. The returned path does not end in a Separator unless it is the root directory. This function is the counterpart of Base Base("a/b/")="b" and Dir("a/b/") = "a". In general Trim(Join(Dir2(path),Base(path))) should be Trim(path)

func EvalSymlinks(path string) (string, error)

EvalSymLinks resolves all symbolic links in a path and returns a path not containing any symbolic link anymore. It does not call Clean on a non-canonical path, so the result always denotes the same file than the original path. If the given path is a relative one, a reLative one is returned as long as there is no absolute symbolic link and the relative path does not goes up the current working diretory. If a relative path is returned, symbolic links up the current working directory are not resolved.

func Exists

func Exists(path string) bool

Exists checks whether a file exists.

func Ext

func Ext(path string) string

func FromSlash

func FromSlash(path string) string

func Glob

func Glob(pattern string) (matches []string, err error)

func IsAbs

func IsAbs(path string) bool

func IsRoot

func IsRoot(path string) bool

IsRoot determines whether a given path is a root path. This might be the Separator or the Separator preceded by a volume name under Windows. This function is directly taken from the original filepath package.

func Join

func Join(elems ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. Join never calls Clean on the result to assure the result denotes the same file as the input. On Windows, the result is a UNC path if and only if the first path element is a UNC path.

func Match

func Match(pattern, name string) (matched bool, err error)

func Rel

func Rel(basepath, targpath string) (string, error)

func Split

func Split(path string) (string, string)

func Split2

func Split2(path string) (dir, file string)

Split2 splits path immediately following the final Separator, separating it into a directory and file name component. If there is no Separator in path, Split returns an empty dir and file set to path. In contrast to Split the directory path does not end with a trailing Separator, so Split2 can subsequently called for the directory part, again.

func SplitList

func SplitList(path string) []string

func SplitPath

func SplitPath(path string) (string, []string, bool)

SplitPath splits a path into a volume, an array of the path segments and a rooted flag. The rooted flag is true, if the given path is an absolute one. In this case the segment array does not contain a root segment.

func SplitVolume

func SplitVolume(path string) (string, string)

SplitVolume splits a path into a volume and a path part.

func ToSlash

func ToSlash(path string) string

func Trim

func Trim(path string) string

Trim eleminates additional slashes and dot segments from a path name. An empty path is unchanged.

func VolumeName

func VolumeName(path string) string

func Walk

func Walk(root string, walkFn WalkFunc) error

Types

type WalkFunc

type WalkFunc orig.WalkFunc

WalkFunc is the type of the function called for each file or directory visited by Walk. The path argument contains the argument to Walk as a prefix; that is, if Walk is called with "dir", which is a directory containing the file "a", the walk function will be called with argument "dir/a". The info argument is the os.FileInfo for the named path.

If there was a problem walking to the file or directory named by path, the incoming error will describe the problem and the function can decide how to handle that error (and Walk will not descend into that directory). In the case of an error, the info argument will be nil. If an error is returned, processing stops. The sole exception is when the function returns the special value SkipDir. If the function returns SkipDir when invoked on a directory, Walk skips the directory's contents entirely. If the function returns SkipDir when invoked on a non-directory file, Walk skips the remaining files in the containing directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL