filesystem

package module
v0.64.0-rc.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: Apache-2.0 Imports: 15 Imported by: 5

Documentation

Overview

Package filesystem provides functions and types to interact with the filesystem

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDir added in v0.63.0

func CopyDir(src, dst string) error

CopyDir copies directory recursively

func CopyFile added in v0.63.0

func CopyFile(src, dst string) error

CopyFile atomically copies file path `src“ to file path `dst`.

func CopyFileAll added in v0.63.0

func CopyFileAll(src, dst string) error

CopyFileAll calls CopyFile, but will create necessary directories for `dst`.

func EnsureParentDirsExist added in v0.63.0

func EnsureParentDirsExist(p string) error

EnsureParentDirsExist makes a path immediately available for writing by creating the necessary parent directories.

func FetchArtifact added in v0.64.0

func FetchArtifact[T any](ctx context.Context, location string, factory ArtifactBuilder[T]) (T, error)

FetchArtifact attempts to fetch an artifact from the specified location using the provided factory. This function is blocking and will keep retrying until either the artifact is successfully retrieved or the provided context is done. If the context is done before the artifact is retrieved, it returns an error indicating that the artifact could not be read in the given time.

func FetchOrCreateArtifact added in v0.64.0

func FetchOrCreateArtifact[T any](ctx context.Context, location string, factory ArtifactBuilder[T]) (T, error)

FetchOrCreateArtifact attempts to load an artifact using the provided factory. If the artifact does not exist, it generates a new one, stores it, and returns it.

The function first tries to load the artifact using the provided location. If loading fails, it generates a temporary artifact and attempts to acquire a file lock. When the lock is acquired, the function checks if another process has already created the artifact. If not, it moves the temporary artifact to its final location.

The function will repeatedly try to acquire the lock until the context is canceled or the lock is acquired.

This function is thread-safe and non-blocking.

func FileExists

func FileExists(path string) bool

FileExists returns true if a file exists and is accessible, false otherwise

func GetFileModTime added in v0.52.0

func GetFileModTime(path string) (time.Time, error)

GetFileModTime gets the modification time

func GetFileSize added in v0.52.0

func GetFileSize(path string) (int64, error)

GetFileSize gets the file size

func OpenShared

func OpenShared(path string) (*os.File, error)

OpenShared reimplements the os.Open function for Windows because the default implementation opens files without the FILE_SHARE_DELETE flag. cf: https://github.com/golang/go/blob/release-branch.go1.11/src/syscall/syscall_windows.go#L271 Without FILE_SHARE_DELETE, other users cannot rename/remove the file while this handle is open. Adding this flag allows the agent to have the file open, while not preventing it from being rotated/deleted.

On non-Windows platforms, this calls through to os.Open directly.

func ReadLines

func ReadLines(filename string) ([]string, error)

ReadLines reads a file line by line

func TryFetchArtifact added in v0.64.0

func TryFetchArtifact[T any](location string, factory ArtifactBuilder[T]) (T, error)

TryFetchArtifact attempts to load an artifact using the provided factory. If the artifact does not exist, it return an error.

Types

type ArtifactBuilder added in v0.64.0

type ArtifactBuilder[T any] interface {
	// Generate creates a new artifact and returns it along with its serialized form.
	Generate() (T, []byte, error)
	// Deserialize converts a serialized artifact into an in-memory representation.
	Deserialize([]byte) (T, error)
}

ArtifactBuilder is a generic interface for building, serializing, and deserializing artifacts. The type parameter T represents the in-memory type of the artifact.

type Disk

type Disk struct{}

Disk gets information about the disk

func NewDisk

func NewDisk() Disk

NewDisk creates a new instance of Disk

func (Disk) GetUsage

func (Disk) GetUsage(path string) (*DiskUsage, error)

GetUsage gets the disk usage

type DiskUsage

type DiskUsage struct {
	Total     uint64
	Available uint64
}

DiskUsage is the disk usage

type Permission

type Permission struct{}

Permission handles permissions for Unix and Windows

func NewPermission

func NewPermission() (*Permission, error)

NewPermission creates a new instance of `Permission`

func (*Permission) RemoveAccessToOtherUsers

func (p *Permission) RemoveAccessToOtherUsers(path string) error

RemoveAccessToOtherUsers on Unix this calls RestrictAccessToUser and then removes all access to the file for 'group' and 'other'

func (*Permission) RestrictAccessToUser

func (p *Permission) RestrictAccessToUser(path string) error

RestrictAccessToUser sets the file user and group to the same as 'dd-agent' user. If the function fails to lookup "dd-agent" user it return nil immediately.

Jump to

Keyboard shortcuts

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