filesystem

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

  • Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License

Package filesystem implements functions that use file system operations. It is created to test functionality on a real operating system and to mock its usage in handlers package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileWatcher

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

FileWatcher observes file and notifies when observed type of change occurs (e.g. write). It always provides latest event that has occurred.

func (*FileWatcher) GetEvent

func (f *FileWatcher) GetEvent() *WatcherEvent

GetEvent returns the latest WatcherEvent that was observed. Nil will be returned if there were no new events between GetEvent calls.

func (*FileWatcher) GetNotificationChannel

func (f *FileWatcher) GetNotificationChannel() <-chan struct{}

GetNotificationChannel returns channel on which a notification that an event was observed is sent. To find out the latest event GetEvent must be called. There may be false positives. In such case GetEvent will return nil.

func (*FileWatcher) Stop

func (f *FileWatcher) Stop()

Stop ceases FileWatcher operations.

type Filesystem

type Filesystem interface {
	// DoesExist returns true if a status from path returns no error.
	DoesExist(path string) bool
	// Hardlink creates a hardlink of filePath to hardlinkPath. If hardlinkPath already exists then it is deleted.
	Hardlink(filePath, hardlinkPath string) error
	// DeleteFile deletes a filePath.
	DeleteFile(filePath string) error
	// ClearDir deletes all files from a dirPath.
	ClearDir(filePath string) error
	// MoveFile moves a fromPath file to a toPath.
	MoveFile(fromPath, toPath string) error
	// Copy copies a fromPath file content to a toPath file.
	Copy(fromPath, toPath string) error
	// ListFileNamesInDir returns a list with file names (not paths) from dirPath.
	ListFileNamesInDir(dirPath string) ([]string, error)
	// NewFileWatcher creates file watcher based on fsnotify library (inotify).
	NewFileWatcher(watchedFile string, watchedOps fsnotify.Op) (Watcher, error)
	// Extract extracts all files from a tarball to a toDir directory.
	Extract(tarball, toDir string) error
	// AreFilesDifferent checks if two files has different contents or modes.
	AreFilesDifferent(firstFilePath, secondFilePath string) (bool, error)
}

Filesystem provides multiple file system utilities. It is also used to separate file operations from rest of the code.

func New

func New(logger *slog.Logger) Filesystem

New returns a Filesystem implementation that works on underlying filesystem.

type Watcher

type Watcher interface {
	// GetEvent returns file event that occurred in the system.
	GetEvent() *WatcherEvent
	// GetNotificationChannel returns a channel that sends notifications when a new event is available.
	GetNotificationChannel() <-chan struct{}
	// Stop causes Watcher to cease its operation.
	Stop()
}

Watcher describes types that are source of information about events (e.g. file change, errors, etc.). Watcher also notifies about such event via a channel.

type WatcherEvent

type WatcherEvent struct {
	// Operation denotes which action (e.g. write, read) was observed on the watched file.
	Operation fsnotify.Op
	// Error denotes that error has occurred while watching.
	Error error
}

WatcherEvent is an event that a watcher pushes to a channel. It contains operation that was observed on a watched file or an error if it occurred. If the Error is not nil, Operation won't be set.

Jump to

Keyboard shortcuts

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