utils

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chtimes

func Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access time and modified time of a file at the given path. If the modified time is prior to the Unix Epoch (unixMinTime), or after the end of Unix Time (unixEpochTime), os.Chtimes has undefined behavior. In this case, Chtimes defaults to Unix Epoch, just in case.

func CombineURL

func CombineURL(parts ...string) string

func Copy

func Copy(src, dstDir string, chownRoot bool) error

func Download

func Download(rawurl string, directory string, filename string) (savedname string, err error)

func DownloadDisabledTLS

func DownloadDisabledTLS(rawurl string, directory string, filename string) (savedname string, err error)

func DownloadWithHeader

func DownloadWithHeader(rawurl string, directory string, filename string, header map[string]string) (savedname string, err error)

func EnsureDir

func EnsureDir(path string, perm fs.FileMode) error

ensureDir checks if a path exists and is a directory. If the path does not exist, it tries to create the directory. If the path is a file, it returns an error.

func HttpGet

func HttpGet(url string) (body []byte, statusCode int, err error)

func HttpGetPanic added in v0.2.2

func HttpGetPanic(url string) string

func IsDir added in v0.2.2

func IsDir(path string) bool

func IsNotExist

func IsNotExist(err error) bool

IsNotExist tells you if err is an error that implies that either the path accessed does not exist (or path components don't exist). This is effectively a more broad version of os.IsNotExist.

func IsURL

func IsURL(str string) bool

func LUtimesNano

func LUtimesNano(path string, ts []syscall.Timespec) error

LUtimesNano is used to change access and modification time of the specified path. It's used for symbol link file because unix.UtimesNano doesn't support a NOFOLLOW flag atm.

func Max

func Max[T Comparable](a, b T) T

func Min

func Min[T Comparable](a, b T) T

func PathExists

func PathExists(path string) bool

func SafeFilename added in v0.2.2

func SafeFilename(dstDir string, ext string, parts ...string) (filename string)

generate a safe filename for target directory, by only preserve the letters and numbers in `parts` and add a number if a file with same name already exists in the target directory. TODO: safe for parallel calling?

func SecureJoin

func SecureJoin(root, unsafePath string) (string, error)

SecureJoinVFS joins the two given path components (similar to Join) except that the returned path is guaranteed to be scoped inside the provided root path (when evaluated). Any symbolic links in the path are evaluated with the given root treated as the root of the filesystem, similar to a chroot. The filesystem state is evaluated through the given VFS interface (if nil, the standard os.* family of functions are used).

Note that the guarantees provided by this function only apply if the path components in the returned string are not modified (in other words are not replaced with symlinks on the filesystem) after this function has returned. Such a symlink race is necessarily out-of-scope of SecureJoin.

NOTE: Due to the above limitation, Linux users are strongly encouraged to use OpenInRoot instead, which does safely protect against these kinds of attacks. There is no way to solve this problem with SecureJoinVFS because the API is fundamentally wrong (you cannot return a "safe" path string and guarantee it won't be modified afterwards).

Volume names in unsafePath are always discarded, regardless if they are provided via direct input or when evaluating symlinks. Therefore:

"C:\Temp" + "D:\path\to\file.txt" results in "C:\Temp\path\to\file.txt"

func URLtoFilename

func URLtoFilename(url string) (filename string)

func UnescapeURL

func UnescapeURL(encodedURL string) (string, error)

Types

type Comparable

type Comparable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | string
}

Jump to

Keyboard shortcuts

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