pathvalidate

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2020 License: MIT Imports: 10 Imported by: 0

README

pathvalidate

PkgGoDev Go Report Card

Path santization based on pathvalidate from Python https://pypi.org/project/pathvalidate/

import path: github.com/lordwelch/pathvalidate

Example:

# Validate Path
err := pathvalidate.ValidateFilepath("Simple/Name", '_')
sanitized, err := pathvalidate.SanitizeFilepath("Simple/Name", '_')

# Validate Filename
err := pathvalidate.ValidateFilename("Simple/Name")
sanitized, err := pathvalidate.SanitizeFilename("Simple/Name")

Output:

# Validate Path
err: <nil>
sanitized: Simple/Name err: <nil>

# Validate Filename
err: pathvalidate: invalid character: '/' (0x2f)
sanitized: Simple_Name err: <nil>

defaults

Windows

Invalid Path: Unicode categories: Cc, Cf, Z excluding space + :*?"<>|

Invalid Filename: Invalid Path + / + \

Max Path Length: 260

Reserved words

NTFS Reserved Names: $MFT, $MFTMIRR, $LOGFILE $VOLUME, $ATTRDEF, $BITMAP, $BOOT, $BADCLUS, $SECURE, $UPCASE, $EXTEND, $QUOTA, $OBJID, $REPARSE

Windows Reserved Names: CON, PRN, AUX, CLOCK$, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, COM10, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, LPT10

Linux

Invalid Path: Unicode categories: Cc, Cf, Z excluding space +

Invalid Filename: Invalid Path + /

Max Path Length: 4096

Reserved words

None

Darwin

Invalid Path: Unicode categories: Cc, Cf, Z excluding space +

Invalid Filename: Invalid Path + /

Max Path Length: 4096

Reserved words

:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NTFSReserved = []string{
		"$MFT",
		"$MFTMIRR",
		"$LOGFILE",
		"$VOLUME",
		"$ATTRDEF",
		"$BITMAP",
		"$BOOT",
		"$BADCLUS",
		"$SECURE",
		"$UPCASE",
		"$EXTEND",
		"$QUOTA",
		"$OBJID",
		"$REPARSE",
	} // Only in root directory

	WindowsReserved = []string{
		"CON", "PRN", "AUX", "CLOCK$", "NUL",
		"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10",
		"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "LPT10",
	}

	DarwinReserved = []string{":"} // Is this needed?
)
View Source
var (
	InvalidPath              = rangetable.Merge(unicode.Cc, unicode.Cf, unicode.Z)
	InvalidFilename          = rangetable.Merge(InvalidPath, rangetable.New('/'))
	InvalidWindowsPath       = rangetable.Merge(InvalidPath, rangetable.New(':', '*', '?', '"', '<', '>', '|'))
	InvalidWindowsFilename   = rangetable.Merge(InvalidFilename, InvalidWindowsPath, rangetable.New('\\'))
	DefaultMaxFilenameLength = 255
)
View Source
var (
	ErrInvalidChar  = errors.New("pathvalidate: invalid character")
	ErrMaxLength    = errors.New("pathvalidate: max length exceeded")
	ErrMinLength    = errors.New("pathvalidate: min length not met")
	ErrReservedWord = errors.New("pathvalidate: reserved word found")
)
View Source
var (
	DefaultFilenameSanitizer = FilenameSanitizer{}
	DefaultFilepathSanitizer = FilepathSanitizer{}
)
View Source
var DefaultBaseFile = BaseFile{
	MaxLength:        getDefaultMaxLength(runtime.GOOS),
	ReservedKeywords: getDefaultKeywords(runtime.GOOS),
	MinLength:        1,
}

Functions

func SanitizeFilename

func SanitizeFilename(path string, replacement rune) (string, error)

func SanitizeFilepath

func SanitizeFilepath(path string, replacement rune) (string, error)

func ValidateFilename

func ValidateFilename(path string) error

func ValidateFilepath

func ValidateFilepath(path string) error

Types

type BaseFile

type BaseFile struct {
	ReservedKeywords []string
	MinLength        int
	MaxLength        int
}

func (BaseFile) IsReservedKeyword

func (bf BaseFile) IsReservedKeyword(name string) bool

func (BaseFile) UpdateReservedKeywords

func (bf BaseFile) UpdateReservedKeywords(name, suffix string) string

type FilenameSanitizer

type FilenameSanitizer struct {
	BaseFile
}

func (FilenameSanitizer) Sanitize

func (f FilenameSanitizer) Sanitize(path string, replacement rune) (string, error)

func (FilenameSanitizer) Validate

func (f FilenameSanitizer) Validate(path string) error

type FilepathSanitizer

type FilepathSanitizer struct {
	FilenameSanitizer
}

func (FilepathSanitizer) Sanitize

func (fps FilepathSanitizer) Sanitize(path string, replacement rune) (string, error)

func (FilepathSanitizer) Validate

func (fps FilepathSanitizer) Validate(path string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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