utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 32 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

ctx.go

Index

Constants

View Source
const NbPinNumbers = 4

Variables

View Source
var ErrUnzipSizeLimit = errors.New("zip extraction size limit exceeded")

ErrUnzipSizeLimit indicates that a zip archive would extract beyond the configured output-size limit.

Functions

func ByteCountDecimal

func ByteCountDecimal(b int64) string

ByteCountDecimal converts bytes to human readable byte string

func ChunkRangesBytes

func ChunkRangesBytes(chunkRanges []int64, fileSize, defaultChunkSize int64) int64

ChunkRangesBytes returns the exact number of file bytes represented by ranges, including a possibly-short final chunk.

func ChunkRangesContain

func ChunkRangesContain(chunkRanges []int64, position int64) bool

ChunkRangesContain reports whether the chunk at position is requested. An empty range list requests every chunk.

func ChunkRangesCount

func ChunkRangesCount(chunkRanges []int64, fileSize, defaultChunkSize int64) int

ChunkRangesCount returns the number of chunks represented by ranges. An empty range list means the whole file, matching the transfer protocol.

func ChunkRangesToChunks

func ChunkRangesToChunks(chunkRanges []int64) (chunks []int64)

ChunkRangesToChunks converts chunk ranges to list

func Exists

func Exists(name string) bool

Exists reports whether the named file or directory exists.

func FindOpenPorts

func FindOpenPorts(host string, portNumStart, numPorts int) (openPorts []int)

func GenerateRandomPin

func GenerateRandomPin() string

GenerateRandomPin returns a randomly generated pin with set length

func GetConfigDir

func GetConfigDir(requireValidPath bool) (homedir string, err error)

Get or create home directory

func GetInput

func GetInput(prompt string) (string, error)

GetInput returns one line of input from stdin with a given prompt, with surrounding whitespace trimmed. On read error (e.g. closed or exhausted stdin) the returned string is empty, so callers that treat an empty answer as consent must check the error.

func GetInputContext

func GetInputContext(ctx context.Context, prompt string) (string, error)

GetInputContext returns one line of input from stdin, or the context error when the caller is canceled while waiting for input. The read runs in a goroutine because terminals and arbitrary pipes do not provide a portable context-aware read operation. A canceled CLI process exits without waiting for that goroutine.

func GetLocalIPs

func GetLocalIPs() (ips []string, err error)

GetLocalIPs returns all local ips

func GetRandomName

func GetRandomName() string

GetRandomName returns a random three-word croc code.

It retains its historical signature for callers outside croc. New code that needs to handle a random-source failure should call codephrase.Generate.

func HashFile

func HashFile(fname string, algorithm string, showProgress ...bool) (hash256 []byte, err error)

HashFile returns the hash of a file or, in case of a symlink, the SHA256 hash of its target. Takes an argument to specify the algorithm to use.

func HashFileCtx

func HashFileCtx(ctx context.Context, fname string, algorithm string, showProgress ...bool) ([]byte, error)

HashFileCtx returns the hash of a file with context cancellation support.

func HighwayHashFile

func HighwayHashFile(fname string, doShowProgress bool) (hashHighway []byte, err error)

HighwayHashFile returns highwayhash of a file

func HighwayHashReader

func HighwayHashReader(sr *io.SectionReader, bar *progressbar.ProgressBar) ([]byte, error)

HighwayHashReader returns highwayhash for a SectionReader.

func IMOHashFile

func IMOHashFile(fname string) (hash []byte, err error)

IMOHashFile returns imohash

func IMOHashFileFull

func IMOHashFileFull(fname string) (hash []byte, err error)

IMOHashFileFull returns imohash of full file

func IMOHashReader

func IMOHashReader(sr *io.SectionReader, bar *progressbar.ProgressBar) ([]byte, error)

IMOHashReader returns imohash for a SectionReader. Uses spinner progress bar for indeterminate progress.

func IMOHashReaderFull

func IMOHashReaderFull(sr *io.SectionReader, bar *progressbar.ProgressBar) ([]byte, error)

IMOHashReaderFull returns full imohash (no sampling) for a SectionReader.

func IsLocalIP

func IsLocalIP(ipaddress string) bool

func LocalIP

func LocalIP() string

LocalIP returns local ip address

func MD5HashFile

func MD5HashFile(fname string, doShowProgress bool) (hash256 []byte, err error)

MD5HashFile returns MD5 hash

func MD5HashReader

func MD5HashReader(sr *io.SectionReader, bar *progressbar.ProgressBar) ([]byte, error)

MD5HashReader returns MD5 hash for a SectionReader.

func MarkFileForRemoval

func MarkFileForRemoval(fname string)

func MissingChunks

func MissingChunks(fname string, fsize int64, chunkSize int) (chunkRanges []int64)

MissingChunks returns the positions of missing chunks. If file doesn't exist, it returns an empty chunk list (all chunks). If the file size is not the same as requested, it returns an empty chunk list (all chunks).

func NewCtxFile

func NewCtxFile(ctx context.Context, f *os.File) *ctxFile

NewCtxFile creates a new context-aware file wrapper.

func NormalizeRelativePath

func NormalizeRelativePath(p string) string

NormalizeRelativePath converts a path to a portable, clean relative-path representation suitable for exact comparisons.

func PublicIP

func PublicIP() (ip string, err error)

PublicIP returns public ip address

func PublicIPContext

func PublicIPContext(ctx context.Context) (ip string, err error)

PublicIPContext returns the public IPv4 address while allowing the caller to cancel the lookup.

func RandomFileName

func RandomFileName() (fname string, err error)

func RejectSymlinkPath

func RejectSymlinkPath(root, target string) error

RejectSymlinkPath rejects existing symlinks in target and its path components below root. This prevents writes through a pre-existing symlink from escaping the intended destination directory.

func RemoveMarkedFiles

func RemoveMarkedFiles() (err error)

func SHA256

func SHA256(s string) string

SHA256 returns sha256 sum

func UnusedFilename

func UnusedFilename(folder, name string) string

UnusedFilename returns a filename in folder that does not exist yet, appending " (1)", " (2)", etc. before the extension of name until an unused one is found.

func UnzipDirectory

func UnzipDirectory(destination string, source string) error

UnzipDirectory extracts source into destination. By default, extraction is limited to the archive's on-disk size. Croc-created zip archives use deflate without compression, so their extracted contents fit within this limit.

func UnzipDirectoryContext

func UnzipDirectoryContext(ctx context.Context, destination string, source string) error

UnzipDirectoryContext is UnzipDirectory with cancellation support.

func UnzipDirectoryFromFileAtRootWithLimit

func UnzipDirectoryFromFileAtRootWithLimit(root *receivefs.Root, source *os.File, maxExtractedBytes int64) error

UnzipDirectoryFromFileAtRootWithLimit extracts through the caller's already opened receive root, so a renamed or replaced working-directory path cannot switch the extraction destination after the transfer began.

func UnzipDirectoryFromFileAtRootWithLimitContext

func UnzipDirectoryFromFileAtRootWithLimitContext(ctx context.Context, root *receivefs.Root, source *os.File, maxExtractedBytes int64) error

UnzipDirectoryFromFileAtRootWithLimitContext is UnzipDirectoryFromFileAtRootWithLimit with cancellation support.

func UnzipDirectoryWithLimit

func UnzipDirectoryWithLimit(destination string, source string, maxExtractedBytes int64) error

UnzipDirectoryWithLimit extracts source into destination while allowing at most maxExtractedBytes bytes of regular-file output in total.

func UnzipDirectoryWithLimitContext

func UnzipDirectoryWithLimitContext(ctx context.Context, destination string, source string, maxExtractedBytes int64) error

UnzipDirectoryWithLimitContext is UnzipDirectoryWithLimit with cancellation support.

func ValidFileName

func ValidFileName(fname string) (err error)

ValidFileName checks if a filename is valid by making sure it has no invisible characters

func XXHashFile

func XXHashFile(fname string, doShowProgress bool) (hash256 []byte, err error)

XXHashFile returns the xxhash of a file

func XXHashReader

func XXHashReader(sr *io.SectionReader, bar *progressbar.ProgressBar) ([]byte, error)

XXHashReader returns xxhash for a SectionReader.

func ZipDirectory

func ZipDirectory(destination string, source string, ignoredPaths map[string]bool, exclusions []string) (err error)

ZipDirectory writes the contents of source into a zip at destination. Files whose absolute path is present in ignoredPaths are skipped (used by the --git flag to honour .gitignore rules). Files whose zip path contains any string in exclusions (case-insensitive) are also skipped, mirroring the post-walk filter in cli.go for non-zip transfers.

func ZipDirectoryContext

func ZipDirectoryContext(ctx context.Context, destination string, source string, ignoredPaths map[string]bool, exclusions []string) (err error)

ZipDirectoryContext is ZipDirectory with cancellation support.

func ZipDirectoryWithExactExclusions

func ZipDirectoryWithExactExclusions(destination string, source string, ignoredPaths map[string]bool, exclusions, exactExclusions []string) (err error)

ZipDirectoryWithExactExclusions is ZipDirectory with support for exact paths relative to source. Legacy exclusions remain case-insensitive substring matches.

func ZipDirectoryWithExactExclusionsContext

func ZipDirectoryWithExactExclusionsContext(ctx context.Context, destination string, source string, ignoredPaths map[string]bool, exclusions, exactExclusions []string) (err error)

ZipDirectoryWithExactExclusionsContext is ZipDirectoryWithExactExclusions with cancellation support. A canceled or otherwise failed operation removes the incomplete destination archive.

Types

This section is empty.

Jump to

Keyboard shortcuts

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