osfs

package module
v0.0.0-...-e4b1ff7 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2017 License: BSD-2-Clause Imports: 8 Imported by: 5

README

Request filesystem capabilities

This is a small package that can determine on what kind of filesystem a file resides. On Linux, it reads this information from /proc/self/mountinfo.

Example:

package main

import (
	"log"
	"os"

	"github.com/aykevl/osfs"
)

func main() {
	path := "/some/file"

	// Get the os.FileInfo of any file or directory.
	st, err := os.Stat(path)
	if err != nil {
		log.Fatal("could not stat file:", err)
	}

	// Ignoring the error is valid and will not crash your program.
	// But you can, for example, print a warning that something went wrong.
	info, _ := osfs.Read()
	mount := info.Get(path, st)

	if mount != nil {
		log.Print("Filesystem root:", mount.Root)
	}

	// It is valid to call Filesystem() on a nil mount: it will return the
	// capabilities of the default or most common filesystem on your OS.
	if mount.Filesystem().Hardlink {
		log.Print("This filesystem supports hardlinks!")
	}
}

Documentation: GoDoc

The package is designed to be easy to use and resilient to unknown systems. It does what looks like the best possible action.

Supported systems:

  • For the time being, only Linux is supported. It will probably work on other systems, but will act like filesystems support nothing.

Documentation

Overview

Package osfs determines filesystem capabilities based on os.FileInfo.

On Linux, it parses /proc/self/mountinfo. Then it can find the mount point based on the st_dev field of the stat result (os.FileInfo.Sys()), and determine filesystem capabilities from the filesystem type.

Index

Constants

View Source
const MOUNTINFO_PATH = "/proc/self/mountinfo"

MOUNTINFO_PATH is the common path for the mountinfo file on Linux.

Variables

View Source
var Default = defaultFilesystem()

Default has the filesystem capabilities of the common filesystem(s) on the current OS.

Functions

This section is empty.

Types

type Filesystem

type Filesystem struct {
	Permissions os.FileMode // supported permissions are set (e.g. 0777 on Linux)
	Symlink     bool
	Hardlink    bool
	Inode       bool
	Memory      bool // true if this is an in-memory filesystem (tmpfs)
	Special     bool // true if this is a filesystem like /proc
}

Filesystem contains capabilities of one filesystem. The zero value means it has no special features (and is not a typical POSIX filesystem).

type Info

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

Info lists all filesystems on the current system. A specific filesystem can be fetched using Get().

func Read

func Read() (*Info, error)

Read retuns a list of all mountpoints and their filesystem types. It always returns a valid Info object, but may also return an error on failures. Errors are worked around as much as possible. Thus, you can safely ignore Read() errors while still having reasonable defaults.

func (*Info) Get

func (info *Info) Get(path string, fileInfo os.FileInfo) (*MountPoint, error)

Get returns the mount point based on a path and stat result. The path does not have to be absolute and may contain symlinks.

func (*Info) GetPath

func (info *Info) GetPath(path string) (*MountPoint, error)

GetPath returns the mount point based on a path. It does a os.Stat and Info.Get on the file. It is a shorthand for Get(path, stat).

func (*Info) GetReal

func (info *Info) GetReal(filePath string, fileInfo os.FileInfo) *MountPoint

MountPoint returns the mountpoint associated with the FileInfo, or nil if the mount point wasn't found. When nil is returned, you can still safely call Filesystem() on the returned pointer (it will return the default filesystem). Calling GetReal() on a nil *Info is not allowed.

The filePath must be absolute (pass filepath.IsAbs), otherwise this function will panic. If you aren't sure that the path is absolute, use filepath.Abs().

func (*Info) Len

func (info *Info) Len() int

Len returns the number of mount points found.

type MountPoint

type MountPoint struct {
	FSRoot string // root of the mount within the filesystem
	Root   string // mount point relative to the process's root
	Type   string // filesystem type, e.g. "ext4"
	// contains filtered or unexported fields
}

MountPoint contains information on one mount point, for example one line of /proc/self/mountinfo.

All fields have the zero value if they are unknown (e.g. ints are 0). Only Root is required to be set (when Type is empty, it is the default filesystem).

func (*MountPoint) DevNumber

func (p *MountPoint) DevNumber() (uint64, bool)

Return the device number for this mount point (the st_dev field for files on it).

func (*MountPoint) Filesystem

func (p *MountPoint) Filesystem() Filesystem

Filesystem returns capabilities of the filesystem for this mount point. The results are more like an educated guess, but should give correct results for the vast majority of detected filesystems. It has a reasonable default (e.g. on Linux a standard POSIX filesystem with hardlinks and inodes).

Jump to

Keyboard shortcuts

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