compat

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2025 License: MIT Imports: 6 Imported by: 0

README

compat

Keep a Changelog Go Reference go.mod LICENSE Go Report Card

Overview

compat is a pure-Go library providing unified access to file and device metadata, atomic file operations, process priority, etc. on all major operating systems, include Windows, Linux, macOS, Android, iOS, and many others.

Usage

The documentation is available at https://pkg.go.dev/github.com/rasa/compat

Stat

Here's an example of calling compat.Stat():

package main

import (
   "fmt"
   "github.com/rasa/compat"
)

func main() {
	fi, err := compat.Stat(os.Executable())
	if err != nil {
		fmt.Println(err)
		return
	}
        // Same functions as os.Stat() and os.Lstat():
	fmt.Printf("Name()    =%v\n", fi.Name())     // base name of the file
	fmt.Printf("Size()    =%v\n", fi.Size())     // length in bytes
	fmt.Printf("Mode()    =0o%o\n", fi.Mode())   // file mode bits
	fmt.Printf("ModTime() =%v\n", fi.ModTime())  // last modified time
	fmt.Printf("IsDir()   =%v\n", fi.IsDir())    // is a directory
	fmt.Printf("Sys()     =%+v\n", fi.Sys())     // underlying data source
        // New functions provided by this compat library:
	fmt.Printf("DeviceID()=%v\n", fi.DeviceID()) // device ID
	fmt.Printf("FileID()  =%v\n", fi.FileID())   // inode/file ID
	fmt.Printf("Links()   =%v\n", fi.Links())    // number of hard links
	fmt.Printf("ATime()   =%v\n", fi.ATime())    // last access time
	fmt.Printf("BTime()   =%v\n", fi.BTime())    // birth/created time
	fmt.Printf("CTime()   =%v\n", fi.CTime())    // metadata changed time
	fmt.Printf("MTime()   =%v\n", fi.MTime())    // alias for ModTime
	fmt.Printf("UID()     =%v\n", fi.UID())      // user ID
	fmt.Printf("GID()     =%v\n", fi.GID())      // group ID
}

which, on Linux, produced:

Name()    =cmd
Size()    =1597624
Mode()    =0o775
ModTime() =2025-05-08 22:11:01.353744514 -0700 PDT
IsDir()   =false
Sys()     =&{Dev:64512 Ino:56893266 Nlink:1 Mode:33277 Uid:1000 Gid:1000 X__pad0:0 Rdev:0 Size:1597624 Blksize:4096 Blocks:3128 Atim:{Sec:1746767461 Nsec:354744521} Mtim:{Sec:1746767461 Nsec:353744514} Ctim:{Sec:1746767461 Nsec:353744514} X__unused:[0 0 0]}
DeviceID()=64512
FileID()  =56893266
Links()   =1
ATime()   =2025-05-08 22:11:01.354744521 -0700 PDT
BTime()   =0001-01-01 00:00:00 +0000 UTC
CTime()   =2025-05-08 22:11:01.353744514 -0700 PDT
MTime()   =2025-05-08 22:11:01.353744514 -0700 PDT
UID()     =1000
GID()     =1000

Installing

To install compat, use go get:

go get github.com/rasa/compat

FileInfo Functions

The Stat() and Lstat() functions return a FileInfo object. The table below lists the operating system support for each of the FileInfo functions:

OS DeviceID() FileID()* Links()* ATime()* BTime()* CTime()* UID()* GID()*
AIX ✖️
Android ✖️
Dragonfly ✖️
FreeBSD ✖️
Illumos ✖️
iOS
Linux ✖️
macOS
NetBSD ✖️
OpenBSD ✖️
Plan9 ✖️ ☑️ ☑️
Solaris ✖️
WebAssembly ✖️
Windows ✖️ 🚧 🚧

* Support will depend on the underlying file system. See Comparison of file systems for details.

✅ fully supported.
☑️ the UID() and GID() values are 64-bit hashes of the user and group names.
🚧 planned to be implemented.
✖️ not implemented (but potentially could be if supported by the operating/file system).
❌ not implemented (as it is not supported by the operating system).

Other Functions

All other functions provided by this library are fully supported by all the above operating systems.

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

License

This project is MIT licensed.

Documentation

Index

Constants

View Source
const (
	// MaxNice is the maximum value returned by Nice().
	MaxNice = 19
	// MinNice is the minimum value returned by Nice().
	MinNice = -20
)

Variables

This section is empty.

Functions

func Nice added in v0.2.0

func Nice() (int, error)

Nice gets the CPU process priority. The return value is in a range from -20 (least nice), to 19 (most nice), even on non-Unix systems such as Windows, plan9, etc. If not supported by the operating system, -1 is returned.

func Renice added in v0.2.0

func Renice(_ int) error

Renice sets the CPU process priority. The nice parameter can range from -20 (least nice), to 19 (most nice), even on non-Unix systems such as Windows, plan9, etc.

func SameDevice

func SameDevice(fi1, fi2 FileInfo) bool

SameDevice reports whether fi1 and fi2 describe files on the same device. For example, on Unix this means that the device fields of the two underlying structures are identical; on other systems the decision may be based on the path names. SameDevice only applies to results returned by this package's Stat. It returns false in other cases.

func SameDevices

func SameDevices(name1, name2 string) bool

SameDevices reports whether name1 and name2 are files on the same device. The function follow symlinks.

func SameFile

func SameFile(fi1, fi2 FileInfo) bool

SameFile reports whether fi1 and fi2 describe the same file. For example, on Unix this means that the device and inode fields of the two underlying structures are identical; on other systems the decision may be based on the path names. SameDevice only applies to results returned by this package's Stat. It returns false in other cases.

func SameFiles

func SameFiles(name1, name2 string) bool

SameFiles reports whether name1 and name2 are the same file. The function follow symlinks.

func Supports

func Supports(function SupportedType) bool

Supports returns whether function is supported by the operating system.

Types

type FileInfo

type FileInfo interface {
	Name() string       // base name of the file
	Size() int64        // length in bytes for regular files; system-dependent for others
	Mode() os.FileMode  // file mode bits
	ModTime() time.Time // last modified time
	IsDir() bool        // abbreviation for Mode().IsDir()
	Sys() any           // underlying data source
	DeviceID() uint64   // device ID
	FileID() uint64     // file ID
	Links() uint64      // number of hard links, or 0 if unsupported
	ATime() time.Time   // last accessed time, or 0 if unsupported
	BTime() time.Time   // birth (created) time, or 0 if unsupported
	CTime() time.Time   // last changed time, or 0 if unsupported
	MTime() time.Time   // last modified time (alias)
	UID() uint64        // user ID, or 0 if unsupported
	GID() uint64        // group ID, or 0 if unsupported
}

A FileInfo describes a file and is returned by Stat. See https://github.com/golang/go/blob/ad7a6f81/src/io/fs/fs.go#L158

func Lstat

func Lstat(name string) (FileInfo, error)

Lstat returns a FileInfo describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If there is an error, it will be of type [*PathError].

On Windows, if the file is a reparse point that is a surrogate for another named entity (such as a symbolic link or mounted folder), the returned FileInfo describes the reparse point, and makes no attempt to resolve it.

func Stat

func Stat(name string) (FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type [*PathError].

type InvalidNiceError added in v0.2.0

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

InvalidNiceError is returned when the niceness value passed by the user is invalid.

func (*InvalidNiceError) Error added in v0.2.0

func (e *InvalidNiceError) Error() string

type NiceError added in v0.2.0

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

NiceError is returned when system call failed.

func (*NiceError) Error added in v0.2.0

func (e *NiceError) Error() string

type ReniceError added in v0.2.0

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

ReniceError is returned when the system failed to set the OS's niceness level.

func (*ReniceError) Error added in v0.2.0

func (e *ReniceError) Error() string

type SupportedType added in v0.2.0

type SupportedType uint

SupportedType defines a bitmask that identifies if the OS supports specific fields, or not.

const (
	// Links defines if FileInfo's Links() function is supported by the OS.
	// Links() returns the number of hard links to the file.
	Links SupportedType = 1 << iota
	// ATime defines if FileInfo's ATime() function is supported by the OS.
	// ATime() returns the time the file was last accessed.
	ATime
	// BTime defines if FileInfo's BTime() function is supported by the OS.
	// BTime() returns the time the file was created (or "birthed").
	BTime
	// CTime defines if FileInfo's CTime() function is supported by the OS.
	// CTime() returns the time the file's metadata was last changed.
	CTime
	// UID defines if FileInfo's UID() function is supported by the OS.
	// UID() returns the user ID of the file's owner.
	UID
	// GID defines if FileInfo's GID() function is supported by the OS.
	// GID() returns the group ID of the file's group.
	GID
)

Directories

Path Synopsis
main is a sample application that runs functions provided by this library.
main is a sample application that runs functions provided by this library.

Jump to

Keyboard shortcuts

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