billyfs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 9 Imported by: 0

README

billyfs - Abstract File System Adapter

The billyfs package enables seamless conversion of any absfs.SymlinkFileSystem into a file system compatible with github.com/go-git/go-billy/v5. This integration allows developers to utilize the robust features of the go-git package across a variety of file systems that implement the absfs.SymlinkFileSystem interface. By bridging these technologies, billyfs facilitates more flexible and efficient management of file systems within Go applications, especially for those requiring git functionality.

See the absfs package for more information about the absfs.SymlinkFileSystem interface and the absfs abstract file system API.

Installation

Install billyfs using the typical go get command:

$ go get github.com/absfs/billyfs

Example Usage

The following example demonstrates how to use billyfs to create a UNIX-like folder structure and manipulate files within a custom file system setup. This example provides a practical insight into integrating billyfs into your projects.

package main

import (
    "fmt"
    slash "path"
    "sort"

    "github.com/absfs/absfs"
    "github.com/absfs/billyfs"
    "github.com/absfs/memfs"
    "github.com/absfs/osfs"
)

// Choose the file system type: "memfs" for in-memory or "osfs" for OS file
// system, or add any other file system that implements
// `the absfs.SymlinkFileSystem` interface.
const FS = "memfs"

// List of UNIX-like directories to create in the file system.
var UNIXLikeDirs = []string{
    "/bin", "/etc", "/tmp", "/var", "/opt", "/home", "/root", "/mnt", "/media",
    "/srv", "/lib", "/usr/local/src", "/usr/bin", "/usr/sbin", "/usr/lib",
    "/usr/include", "/usr/share", "/usr/src",
}

func main() {
    var fs absfs.SymlinkFileSystem
    var err error

    // Initialize the file system based on the FS constant.
    switch FS {
    case "memfs":
        fs, err = memfs.NewFS()
    case "osfs":
        fs, err = osfs.NewFS()
    }
    if err != nil {
        panic(err)
    }

    // Create a UNIX-like folder structure as a demonstration.
    for _, dir := range UNIXLikeDirs {
        err := fs.MkdirAll(dir, 0755)
        if err != nil {
            panic(err)
        }
    }

    // Use billyfs to adapt the absfs file system.
    bfs, err := billyfs.NewFS(fs, "/usr/local/src")
    if err != nil {
        panic(err)
    }

    // Create and remove a test file to demonstrate file manipulation.
    f, _ := bfs.Create("/test.txt")
    defer func() {
        err := f.Close()
        if err != nil {
            panic(err)
        }
        err = bfs.Remove("/test.txt")
        if err != nil {
            panic(err)
        }
    }()
    bfsdata := []byte("This demonstrates file manipulation within the adapted file system.\n")
    _, err = f.Write(bfsdata)
    if err != nil {
        panic(err)
    }

    // Additional code to demonstrate listing files and directories...
}

This example showcases basic operations like creating directories, files, and listing contents within a file system adapted by billyfs. Remember to adapt the constants and variable values to fit your specific use case.

Contributing

We warmly welcome contributions to the billyfs project. Whether you're fixing bugs, adding new features, or improving the documentation, your help is greatly appreciated. Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or fix.
  3. Commit your changes with clear, descriptive messages.
  4. Push your changes and submit a pull request.

We will review your pull request as soon as possible. Thank you for your contributing to the absfs ecosystem!

LICENSE

This project is governed by the MIT License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

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

File implements the billy.File interface by using the absfs.File interface.

func (*File) Close

func (f *File) Close() error

io.Closer interface

func (*File) Lock

func (f *File) Lock() error

func (*File) Name

func (f *File) Name() string

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

io.Reader interface

func (*File) ReadAt

func (f *File) ReadAt(p []byte, off int64) (n int, err error)

io.ReaderAt interface

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

io.Seeker interface

func (*File) Truncate

func (f *File) Truncate(size int64) error

Truncate the file.

func (*File) Unlock

func (f *File) Unlock() error

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

io.Writer interface

func (*File) WriteAt

func (f *File) WriteAt(p []byte, off int64) (n int, err error)

io.WriterAt interface

type Filesystem

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

Filesystem implements all functions of the go-billy Filesystem interface by using the absfs.FileSystem interface.

func NewFS

func NewFS(fs absfs.SymlinkFileSystem, dir string) (*Filesystem, error)

NewFS wraps a absfs.FileSystem go-billy from a `absfs.FileSystem` compatible object and a path. The path must be an absolute path and must already exist in the fs provided otherwise an error is returned.

func (*Filesystem) Capabilities

func (f *Filesystem) Capabilities() billy.Capability

Capabilities returns the features supported by a filesystem. Absfs supports all capabilities.

func (*Filesystem) Chmod

func (f *Filesystem) Chmod(name string, mode os.FileMode) error

Chmod changes the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target.

func (*Filesystem) Chown

func (f *Filesystem) Chown(name string, uid, gid int) error

Chown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target.

func (*Filesystem) Chroot

func (f *Filesystem) Chroot(path string) (billy.Filesystem, error)

Chroot returns a new filesystem from the same type where the new root is the given path. Files outside of the designated directory tree cannot be accessed.

func (*Filesystem) Chtimes

func (f *Filesystem) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.

The underlying filesystem may truncate or round the values to a less precise time unit.

func (*Filesystem) Create

func (f *Filesystem) Create(filename string) (billy.File, error)

Create creates the named file with mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.

func (*Filesystem) Join

func (f *Filesystem) Join(elem ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. Join calls filepath.Clean on the result; in particular, all empty strings are ignored. On Windows, the result is a UNC path if and only if the first path element is a UNC path.

func (*Filesystem) Lchown

func (f *Filesystem) Lchown(name string, uid, gid int) error

Lchown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link itself.

func (*Filesystem) Lstat

func (f *Filesystem) Lstat(filename string) (os.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.

func (*Filesystem) MkdirAll

func (f *Filesystem) MkdirAll(filename string, perm os.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is/ already a directory, MkdirAll does nothing and returns nil.

func (*Filesystem) Open

func (f *Filesystem) Open(filename string) (billy.File, error)

Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.

func (*Filesystem) OpenFile

func (f *Filesystem) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error)

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O.

func (*Filesystem) ReadDir

func (f *Filesystem) ReadDir(path string) ([]os.FileInfo, error)

ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename.

func (f *Filesystem) Readlink(link string) (string, error)

Readlink returns the target path of link.

func (*Filesystem) Remove

func (f *Filesystem) Remove(filename string) error

Remove removes the named file or directory.

func (*Filesystem) Rename

func (f *Filesystem) Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories.

func (*Filesystem) Root

func (f *Filesystem) Root() string

Root returns the root path of the filesystem.

func (*Filesystem) Stat

func (f *Filesystem) Stat(filename string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file.

func (f *Filesystem) Symlink(target, link string) error

Symlink creates a symbolic-link from link to target. target may be an absolute or relative path, and need not refer to an existing node. Parent directories of link are created as necessary.

func (*Filesystem) TempFile

func (f *Filesystem) TempFile(dir string, prefix string) (billy.File, error)

TempFile creates a new temporary file in the directory dir with a name beginning with prefix, opens the file for reading and writing, and returns the resulting *os.File. If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.

Jump to

Keyboard shortcuts

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