osutil

package
v0.0.0-...-129a086 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package osutil implements utilities for native OS support.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed  = errors.New("write to closed writer")
	TempPrefix = ".syncthing.tmp."
)

Functions

func Copy

func Copy(method fs.CopyRangeMethod, src, dst fs.Filesystem, from, to string) (err error)

Copy copies the file content from source to destination. Tries hard to succeed on various systems by temporarily tweaking directory permissions and removing the destination file when necessary.

func GetLans

func GetLans() ([]*net.IPNet, error)

func GetLatencyForURL

func GetLatencyForURL(ctx context.Context, addr string) (time.Duration, error)

GetLatencyForURL parses the given URL, tries opening a TCP connection to it and returns the time it took to establish the connection.

func HideConsole

func HideConsole()

func IsDeleted

func IsDeleted(ffs fs.Filesystem, name string) bool

func LineEndingsWriter

func LineEndingsWriter(w io.Writer) io.Writer

LineEndingsWriter returns a writer that writes platform-appropriate line endings. (This is a no-op on non-Windows platforms.)

func MaximizeOpenFileLimit

func MaximizeOpenFileLimit() (int, error)

MaximizeOpenFileLimit tries to set the resource limit RLIMIT_NOFILE (number of open file descriptors) to the max (hard limit), if the current (soft limit) is below the max. Returns the new (though possibly unchanged) limit, or an error if it could not be changed.

func NativeFilename

func NativeFilename(s string) string

func NormalizedFilename

func NormalizedFilename(s string) string

func RenameOrCopy

func RenameOrCopy(method fs.CopyRangeMethod, src, dst fs.Filesystem, from, to string) error

RenameOrCopy renames a file, leaving source file intact in case of failure. Tries hard to succeed on various systems by temporarily tweaking directory permissions and removing the destination file when necessary.

func SetLowPriority

func SetLowPriority() error

SetLowPriority lowers the process CPU scheduling priority, and possibly I/O priority depending on the platform and OS.

func TCPPing

func TCPPing(ctx context.Context, address string) (time.Duration, error)

TCPPing returns the duration required to establish a TCP connection to the given host. ICMP packets require root privileges, hence why we use tcp.

func TempFile

func TempFile(filesystem fs.Filesystem, dir, prefix string) (f fs.File, err error)

TempFile creates a new temporary file in the directory dir with a name beginning with prefix, opens the file for reading and writing, and returns the resulting *os.File. If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.

func TraversesSymlink(filesystem fs.Filesystem, name string) error

TraversesSymlink returns an error if any path component of name (including name itself) traverses a symlink.

Types

type AtomicWriter

type AtomicWriter struct {
	// contains filtered or unexported fields
}

An AtomicWriter is an *os.File that writes to a temporary file in the same directory as the final path. On successful Close the file is renamed to its final path. Any error on Write or during Close is accumulated and returned on Close, so a lazy user can ignore errors until Close.

func CreateAtomic

func CreateAtomic(path string) (*AtomicWriter, error)

CreateAtomic is like os.Create, except a temporary file name is used instead of the given name. The file is created with secure (0600) permissions.

func CreateAtomicFilesystem

func CreateAtomicFilesystem(filesystem fs.Filesystem, path string) (*AtomicWriter, error)

CreateAtomicFilesystem is like os.Create, except a temporary file name is used instead of the given name. The file is created with secure (0600) permissions.

func (*AtomicWriter) Close

func (w *AtomicWriter) Close() error

Close closes the temporary file and renames it to the final path. It is invalid to call Write() or Close() after Close().

func (*AtomicWriter) Write

func (w *AtomicWriter) Write(bs []byte) (int, error)

Write is like io.Writer, but is a no-op on an already failed AtomicWriter.

type NotADirectoryError

type NotADirectoryError struct {
	// contains filtered or unexported fields
}

NotADirectoryError is an error indicating an expected path is not a directory

func (*NotADirectoryError) Error

func (e *NotADirectoryError) Error() string

type ReplacingWriter

type ReplacingWriter struct {
	Writer io.Writer
	From   byte
	To     []byte
}

func (ReplacingWriter) Write

func (w ReplacingWriter) Write(bs []byte) (int, error)

type TraversesSymlinkError

type TraversesSymlinkError struct {
	// contains filtered or unexported fields
}

TraversesSymlinkError is an error indicating symlink traversal

func (*TraversesSymlinkError) Error

func (e *TraversesSymlinkError) Error() string

Jump to

Keyboard shortcuts

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