fs

package
v0.0.0-...-7fbf2ce Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicSwap

func AtomicSwap(sourcePath, destinationPath string) error

AtomicSwap atomically swaps two files or directories

Example:

err := fs.AtomicSwap("/tmp/file1", "/tmp/file2")
if err != nil {
	fmt.Printf("Error swapping files: %v", err)
	return
}

func CopyFile

func CopyFile(sourcePath, destinationPath string) error

CopyFile copies the contents of one file to another

Example:

err := fs.CopyFile("/tmp/batman", "/tmp/robin")
if err != nil {
	fmt.Printf("Error copying file: %v", err)
	return
}

func CreateDirectory

func CreateDirectory(directoryPath string) error

CreateDirectory creates a new directory

Example:

err := fs.CreateDirectory("/tmp/batman")
if err != nil {
	fmt.Printf("Error creating directory: %v", err)
	return
}

func DeleteDirectory

func DeleteDirectory(directoryPath string) error

DeleteDirectory deletes a directory and its contents

Example:

err := fs.DeleteDirectory("/tmp/batman")
if err != nil {
	fmt.Printf("Error deleting directory: %v", err)
	return
}

func DeleteFile

func DeleteFile(filePath string) error

DeleteFile deletes a file

Example:

err := fs.DeleteFile("/tmp/batman")
if err != nil {
	fmt.Printf("Error deleting file: %v", err)
	return
}

func DirectoryExists

func DirectoryExists(directoryPath string) bool

DirectoryExists checks if a directory exists

Example:

if fs.DirectoryExists("/tmp/batman") {
	fmt.Println("The directory exists!")
}

func FileExists

func FileExists(filePath string) bool

FileExists checks if a file exists

Example:

if fs.FileExists("/tmp/batman") {
	fmt.Println("The file exists!")
}

func GetDiskInfo

func GetDiskInfo(partitionPath string) (types.BaseInfo, error)

GetDiskInfo returns information about a specific disk partition

Example:

info, err := fs.GetDiskInfo("/dev/sda1")
if err != nil {
	fmt.Printf("Error getting partition info: %v", err)
	return
}
fmt.Printf("Path: %s\n", info.Path)
fmt.Printf("Size: %d\n", info.Size)
fmt.Printf("HumanSize: %s\n", info.HumanSize)

func GetDiskList

func GetDiskList() ([]types.DiskInfo, error)

GetDiskList returns a list of disks on the system

Example:

disks, err := fs.GetDiskList()
if err != nil {
	fmt.Printf("Error getting disk list: %v", err)
	return
}
for _, disk := range disks {
	fmt.Printf("Disk: %s\n", disk.Path)
	fmt.Printf("Size: %d\n", disk.Size)
	fmt.Printf("HumanSize: %s\n", disk.HumanSize)
	for _, partition := range disk.Partitions {
		fmt.Printf("Partition: %s\n", partition.Path)
		fmt.Printf("Size: %d\n", partition.Size)
		fmt.Printf("HumanSize: %s\n", partition.HumanSize)
		fmt.Printf("Filesystem: %s\n", partition.Filesystem)
		fmt.Printf("Mountpoint: %s\n", partition.Mountpoint)
		fmt.Printf("Label: %s\n", partition.Label)
		fmt.Printf("UUID: %s\n", partition.UUID)
		fmt.Printf("PARTUUID: %s\n", partition.PARTUUID)
		fmt.Printf("Flags: %v\n", partition.Flags)
	}
}

func GetFile

func GetFile(filePath string, fullPath bool) (types.FileInfo, error)

GetFile returns the file info of the specified file. If fullPath is true, the full path of the file will be returned instead of the relative path.

Example:

file, err := fs.GetFile("/batmans/cave/batmobile.txt", false)
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

fmt.Printf("Path: %s\n", file.Path)
fmt.Printf("Size: %d\n", file.Size)
fmt.Printf("Permissions: %s\n", file.Permissions.String())

func GetFileDiff

func GetFileDiff(firstFile, secondFile string) (types.FileDiffInfo, error)

GetFileDiff compares the content of two files and returns the changes

Example:

diff, err := fs.GetFileDiff("/tmp/batman", "/tmp/robin")
if err != nil {
	fmt.Printf("Error getting file diff: %v", err)
	return
}
fmt.Printf("Added lines: %v\n", diff.AddedLines)
fmt.Printf("Removed lines: %v\n", diff.RemovedLines)

func GetFileExtension

func GetFileExtension(filePath string) string

GetFileExtension returns the extension of the given file

Example:

extension := fs.GetFileExtension("/batmans/cave/batmobile.txt")
fmt.Printf("Extension: %s\n", extension)

func GetFileList

func GetFileList(directory string, recursive, fullPaths bool) ([]types.FileInfo, error)

GetFileList returns a list of files in the specified directory. If recursive is true, the function will recursively search for files, if fullPaths is true, the full path of the file will be returned instead of the relative path.

Example:

fileList, err := fs.GetFileList("/batmans/cave", true, false)
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

for _, file := range fileList {
	fmt.Printf("Path: %s\n", file.Path)
	fmt.Printf("Size: %d\n", file.Size)
	fmt.Printf("Permissions: %s\n", file.Permissions.String())
	fmt.Printf("Extension: %s\n", file.Extension)
}

func GetFileSize

func GetFileSize(filePath string) int64

GetFileSize returns the size of the specified file in bytes

Example:

size := fs.GetFileSize("/batmans/cave/batmobile.txt")
fmt.Printf("Size: %d\n", size)

func GetFilesystemInfo

func GetFilesystemInfo(path string) map[string]string

GetFilesystemInfo returns information about the filesystem of a file or partition by reading from /etc/mtab.

func GetHumanSize

func GetHumanSize(size int64) string

GetHumanSize converts the size from bytes to a human-readable format. For example, 1024 bytes would be converted to "1 kB".

Example:

fmt.Println(GetHumanSize(1024)) // 1 kB

func GetPartitionList

func GetPartitionList(diskPath string) ([]types.PartitionInfo, error)

GetPartitionList returns a list of disk partitions on the specified disk

Example:

partitions, err := fs.GetPartitionList("/dev/sda")
if err != nil {
	fmt.Printf("Error getting partition list: %v", err)
	return
}
for _, partition := range partitions {
	fmt.Printf("Partition: %s\n", partition.Path)
	fmt.Printf("Size: %d\n", partition.Size)
	fmt.Printf("HumanSize: %s\n", partition.HumanSize)
}

func IsDirectory

func IsDirectory(dirPath string) bool

IsDirectory checks whether the given path is a directory

Example:

if fs.IsDirectory("/batmans/cave") {
	fmt.Println("It's a directory!")
}

func IsFile

func IsFile(filePath string) bool

IsFile checks whether the given path is a regular file

Example:

if fs.IsFile("/batmans/cave/batmobile.txt") {
	fmt.Println("It's a file!")
}

func IsMounted

func IsMounted(source string, destination string) (bool, error)

IsMounted checks if the given source path is mounted in the given destination path. It does so by reading the /proc/mounts file.

Example:

mounted, err := fs.IsMounted("tmpfs", "/tmp")
if err != nil {
	fmt.Printf("Error checking if /tmp is mounted: %v", err)
	return
}
fmt.Printf("/tmp is mounted: %v", mounted)

func ListDirectories

func ListDirectories(directoryPath string) ([]string, error)

ListDirectories returns a list of directories in the specified directory

Example:

directories, err := fs.ListDirectories("/tmp")
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

for _, directory := range directories {
	fmt.Printf("Directory: %s\n", directory)
}

func Mount

func Mount(source, destination, fsType, data string, mode uintptr) error

Mount mounts the given source path in the given destination path. It also creates the destination path if it does not exist. An error is returned if the source path does not exist.

Notes (for developers):

Avoid mapping the fsType into a custom type, as it would require further maintenance once a new filesystem type is added to Vanilla OS.

Example:

err := fs.Mount("/dev/sda1", "/mnt", "ext4", "", syscall.MS_RDONLY)
if err != nil {
	fmt.Printf("Error mounting /dev/sda1: %v", err)
	return
}

func MountBind

func MountBind(src, dest string) error

MountBind mounts bind the given source path in the given destination path.

Notes:

This is just a wrapper of the Mount function, for convenience.

Example:

err := fs.MountBind("/bruce", "/batman")
if err != nil {
	fmt.Printf("Error bind mounting /batman: %v", err)
	return
}

func MountFuseOverlay

func MountFuseOverlay(targetDir, lowerDir, upperDir, workDir string) (err error)

MountFuseOverlay mounts the given lower, upper and work directories in the given destination path as an overlay filesystem using fuse-overlayfs.

Notes:

This implementation uses the fuse-overlayfs command-line tool, if that is not available in the system, this function will return an error.

Example:

err := fs.MountFuseOverlay("/batman", "/batman/lower", "/batman/upper", "/batman/work")
if err != nil {
	fmt.Printf("Error fuse-overlayfs mounting /batman: %v", err)
	return
}

func MountOverlay

func MountOverlay(lowerDir, upperDir, workDir string) error

MountOverlay mounts the given lower, upper and work directories in the given destination path as an overlay filesystem.

Notes:

This is just a wrapper of the Mount function, for convenience.

Example:

err := fs.MountOverlay("/batman/lower", "/batman/upper", "/batman/work")
if err != nil {
	fmt.Printf("Error overlay mounting /batman: %v", err)
	return
}

func MoveFile

func MoveFile(sourcePath, destinationPath string) error

MoveFile moves a file from one location to another

Example:

err := fs.MoveFile("/tmp/batman", "/tmp/robin")
if err != nil {
	fmt.Printf("Error moving file: %v", err)
	return
}

func Unmount

func Unmount(target string) error

Unmount unmounts the given path. An error is returned if the path is not mounted.

Example:

err := fs.Unmount("/mnt")
if err != nil {
	fmt.Printf("Error unmounting /mnt: %v", err)
	return
}

func UnmountFuseOverlay

func UnmountFuseOverlay(targetDir string) (err error)

UnmountFuseOverlay unmounts the given path using the fuse-overlayfs command- line tool. An error is returned if the path is not mounted.

Notes:

This implementation uses the fuse-overlayfs command-line tool, if that is not available in the system, this function will return an error.

Example:

err := fs.UnmountFuseOverlay("/batman")
if err != nil {
	fmt.Printf("Error fuse-overlayfs unmounting /batman: %v", err)
	return
}

func WriteFileContent

func WriteFileContent(filePath, content string) error

WriteFileContent writes content to the specified file

Example:

err := fs.WriteFileContent("/tmp/batman", "I'm Batman!")
if err != nil {
	fmt.Printf("Error writing file content: %v", err)
	return
}

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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