Documentation
¶
Overview ¶
Package filesystem provides functions and types to interact with the filesystem
Index ¶
- func CopyDir(src, dst string) error
- func CopyFile(src, dst string) error
- func CopyFileAll(src, dst string) error
- func EnsureParentDirsExist(p string) error
- func FetchArtifact[T any](ctx context.Context, location string, factory ArtifactBuilder[T]) (T, error)
- func FetchOrCreateArtifact[T any](ctx context.Context, location string, factory ArtifactBuilder[T]) (T, error)
- func FileExists(path string) bool
- func GetFileModTime(path string) (time.Time, error)
- func GetFileSize(path string) (int64, error)
- func OpenShared(path string) (*os.File, error)
- func ReadLines(filename string) ([]string, error)
- func TryFetchArtifact[T any](location string, factory ArtifactBuilder[T]) (T, error)
- type ArtifactBuilder
- type Disk
- type DiskUsage
- type Permission
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyFileAll ¶ added in v0.63.0
CopyFileAll calls CopyFile, but will create necessary directories for `dst`.
func EnsureParentDirsExist ¶ added in v0.63.0
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 ¶
FileExists returns true if a file exists and is accessible, false otherwise
func GetFileModTime ¶ added in v0.52.0
GetFileModTime gets the modification time
func GetFileSize ¶ added in v0.52.0
GetFileSize gets the file size
func OpenShared ¶
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 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 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.