filesystems

package
v0.65.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ModeUserR   = modeRead << modeUserShift
	ModeUserW   = modeWrite << modeUserShift
	ModeUserX   = modeExec << modeUserShift
	ModeUserRW  = ModeUserR | ModeUserW
	ModeUserRWX = ModeUserRW | ModeUserX

	ModeGroupR   = modeRead << modeGroupShift
	ModeGroupW   = modeWrite << modeGroupShift
	ModeGroupX   = modeExec << modeGroupShift
	ModeGroupRW  = ModeGroupR | ModeGroupW
	ModeGroupRWX = ModeGroupRW | ModeGroupX

	ModeOtherR   = modeRead << modeOtherShift
	ModeOtherW   = modeWrite << modeOtherShift
	ModeOtherX   = modeExec << modeOtherShift
	ModeOtherRW  = ModeOtherR | ModeOtherW
	ModeOtherRWX = ModeOtherRW | ModeOtherX

	ModeAllR   = ModeUserR | ModeGroupR | ModeOtherR
	ModeAllW   = ModeUserW | ModeGroupW | ModeOtherW
	ModeAllX   = ModeUserX | ModeGroupX | ModeOtherX
	ModeAllRW  = ModeAllR | ModeAllW
	ModeAllRWX = ModeAllRW | ModeGroupX
)

Variables

This section is empty.

Functions

func ReadDir

func ReadDir(fsys FileSystem, name string) ([]fs.DirEntry, error)

ReadDir reads the named directory, returning all its directory entries sorted by filename. If an error occurs reading the directory, ReadDir returns the entries it was able to read before the error, along with the error.

func WalkDir

func WalkDir(fsys FileSystem, root string, fn fs.WalkDirFunc) error

WalkDir walks the file tree rooted at root, calling fn for each file or directory in the tree, including root.

All errors that arise visiting files and directories are filtered by fn: see the fs.WalkDirFunc documentation for details.

The files are walked in lexical order, which makes the output deterministic but requires WalkDir to read an entire directory into memory before proceeding to walk that directory.

WalkDir does not follow symbolic links.

Types

type File

type File = frameless.File

func Create

func Create(fsys FileSystem, name string) (File, error)

Create creates or truncates the named file. If the file already exists, it is truncated. If the file does not exist, it is created with mode 0666 (before umask). If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.

func Open

func Open(fsys FileSystem, name string) (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. If there is an error, it will be of type *PathError.

type FileSystem

type FileSystem = frameless.FileSystem

type Local added in v0.65.0

type Local struct {
	// RootPath is an optional parameter to jail the file system access for file access.
	RootPath string
}

Local provides local file system access through the frameless.FileSystem interface.

Example
package main

import (
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"

	"github.com/adamluzsi/frameless/resources/filesystems"
)

func main() {
	fsys := filesystems.Local{}

	file, err := fsys.OpenFile("test", os.O_RDWR|os.O_CREATE|os.O_EXCL, filesystems.ModeUserRWX)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	file.Write([]byte("Hello world!"))
	file.Seek(0, io.SeekStart)

	bs, err := io.ReadAll(file)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(bs)) // "Hello world!"

	file.Close()
	fsys.Remove("test")

	fsys.Mkdir("a", filesystems.ModeUserRWX)

	file2Name := filepath.Join("a", "test.txt")
	file2, err := filesystems.Create(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	file2, err = filesystems.Open(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	filesystems.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
		return fs.SkipDir
	})
}
Output:

func (Local) Mkdir added in v0.65.0

func (fs Local) Mkdir(name string, perm fs.FileMode) error

func (Local) OpenFile added in v0.65.0

func (fs Local) OpenFile(name string, flag int, perm fs.FileMode) (frameless.File, error)

func (Local) Remove added in v0.65.0

func (fs Local) Remove(name string) error

func (Local) Stat added in v0.65.0

func (fs Local) Stat(name string) (fs.FileInfo, error)

type Memory

type Memory struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"

	"github.com/adamluzsi/frameless/resources/filesystems"
)

func main() {
	fsys := &filesystems.Memory{}

	file, err := fsys.OpenFile("test", os.O_RDWR|os.O_CREATE|os.O_EXCL, filesystems.ModeUserRWX)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	file.Write([]byte("Hello world!"))
	file.Seek(0, io.SeekStart)

	bs, err := io.ReadAll(file)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(bs)) // "Hello world!"

	file.Close()
	fsys.Remove("test")

	fsys.Mkdir("a", filesystems.ModeUserRWX)

	file2Name := filepath.Join("a", "test.txt")
	file2, err := filesystems.Create(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	file2, err = filesystems.Open(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	filesystems.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
		return fs.SkipDir
	})
}
Output:

func (*Memory) Mkdir

func (mfs *Memory) Mkdir(name string, perm fs.FileMode) error

func (*Memory) OpenFile

func (mfs *Memory) OpenFile(name string, flag int, perm fs.FileMode) (frameless.File, error)

func (*Memory) Remove

func (mfs *Memory) Remove(name string) error

func (*Memory) Stat

func (mfs *Memory) Stat(name string) (fs.FileInfo, error)

type MemoryFile

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

func (*MemoryFile) Close

func (f *MemoryFile) Close() error

func (*MemoryFile) Read

func (f *MemoryFile) Read(bytes []byte) (int, error)

func (*MemoryFile) ReadDir

func (f *MemoryFile) ReadDir(n int) ([]fs.DirEntry, error)

func (*MemoryFile) Seek

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

func (*MemoryFile) Stat

func (f *MemoryFile) Stat() (fs.FileInfo, error)

func (*MemoryFile) Sync

func (f *MemoryFile) Sync() error

func (*MemoryFile) Write

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

Jump to

Keyboard shortcuts

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