mem

package
v6.13.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

Package mem built-in mem lib VFS implementation. Usage Rely on github.com/c2fo/vfs/v6/backend

import(
    "github.com/c2fo/vfs/v6/backend"
    "github.com/c2fo/vfs/v6/backend/mem"
)
func UseFs() error {
    fs := backend.Backend(mem.Scheme)
    ...
}

Or call directly:

  import _mem "github.com/c2fo/vfs/v6/backend/mem"
  func DoSomething() {
	fs := _mem.NewFileSystem()
      ...
  }

Index

Constants

View Source
const Scheme = "mem"

Scheme defines the FileSystem type's underlying implementation.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

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

File implements vfs.File interface for the in-memory implementation of FileSystem. A file struct holds a pointer to a single memFile. Multiple threads will refer to the same memFile. Simultaneous reading is allowed, but writing and closing are protected by locks.

func (*File) Close

func (f *File) Close() error

Close imitates io.Closer by resetting the cursor and setting a boolean

func (*File) CopyToFile

func (f *File) CopyToFile(target vfs.File) (err error)

CopyToFile copies the receiver file into the target file. Additionally, after this is called, f's cursor will reset as if it had been closed.

func (*File) CopyToLocation

func (f *File) CopyToLocation(location vfs.Location) (vfs.File, error)

CopyToLocation copies the current file to the given location. If file exists at given location contents are simply overwritten using "CopyToFile", otherwise a newFile is made, takes the contents of the current file, and ends up at the given location

func (*File) Delete

func (f *File) Delete(_ ...options.DeleteOption) error

Delete removes the file from the FileSystem. Sets it path in the fsMap to nil, and also nils the file's members

func (*File) Exists

func (f *File) Exists() (bool, error)

Exists returns whether a file exists. Creating a file does not guarantee its existence, but creating one and writing to it does.

func (*File) LastModified

func (f *File) LastModified() (*time.Time, error)

LastModified simply returns the file's lastModified, if the file exists

func (*File) Location

func (f *File) Location() vfs.Location

Location simply returns the file's underlying location struct pointer

func (*File) MoveToFile

func (f *File) MoveToFile(file vfs.File) error

MoveToFile creates a newFile, and moves it to "file". The receiver is always deleted (since it's being "moved")

func (*File) MoveToLocation

func (f *File) MoveToLocation(location vfs.Location) (vfs.File, error)

MoveToLocation moves the receiver file to the passed in location. It does so by creating a copy of 'f' in "location". 'f' is subsequently deleted

func (*File) Name

func (f *File) Name() string

Name returns the basename of the file

func (*File) Path

func (f *File) Path() string

Path returns the absolute path to the file

func (*File) Read

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

Read implements the io.Reader interface. Returns number of bytes read and potential errors

func (*File) Seek

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

Seek implements the io.Seeker interface. Returns the current position of the cursor and errors if any

func (*File) Size

func (f *File) Size() (uint64, error)

Size returns the size of the file contents. In our case, the length of the file's byte slice

func (*File) String

func (f *File) String() string

String implements the io.Stringer interface. It returns a string representation of the file's URI

func (*File) Touch

func (f *File) Touch() error

Touch takes a in-memory vfs.File, makes it existent, and updates the lastModified

func (*File) URI

func (f *File) URI() string

URI returns the file's URI, if it exists

func (*File) Write

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

Write implements the io.Writer interface. Returns number of bytes written and any errors. Unless Seek or Read is called first, Write's should overwrite any existing file. Otherwise, it should edit the file in place.

type FileSystem

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

FileSystem implements vfs.FileSystem for an in-memory file system.

func NewFileSystem

func NewFileSystem() *FileSystem

NewFileSystem is used to initialize the file system struct for an in-memory FileSystem.

func (*FileSystem) Name

func (fs *FileSystem) Name() string

Name returns the name of the underlying FileSystem

func (*FileSystem) NewFile

func (fs *FileSystem) NewFile(volume, absFilePath string) (vfs.File, error)

NewFile function returns the in-memory implementation of vfs.File. Since this is inside FileSystem, we assume that the caller knows that the CWD is the root. If a non-absolute path is given, an error is thrown. Additionally, a file does not technically exist until a call to "Touch()" is made on it. The "Touch" call links the file with FileSystem's map and brings it into existence. If a file is written to before a touch call, Write() will take care of that call. This is true for other functions as well and existence only poses a problem in the context of deletion or copying FROM a non-existent file.

func (*FileSystem) NewLocation

func (fs *FileSystem) NewLocation(volume, absLocPath string) (vfs.Location, error)

NewLocation function returns the in-memory implementation of vfs.Location. A location always exists. If a file is created on a location that has not yet been made in the fsMap, then the location will be created with the file

func (*FileSystem) Retry

func (fs *FileSystem) Retry() vfs.Retry

Retry will return a retrier provided via options, or a no-op if none is provided.

func (*FileSystem) Scheme

func (fs *FileSystem) Scheme() string

Scheme returns the scheme of the underlying FileSystem

type Location

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

Location implements the vfs.Location interface specific to in-memory FileSystem.

func (*Location) ChangeDir

func (l *Location) ChangeDir(relLocPath string) error

ChangeDir simply changes the directory of the location

func (*Location) DeleteFile

func (l *Location) DeleteFile(relFilePath string, _ ...options.DeleteOption) error

DeleteFile locates the file given the fileName and calls delete on it

func (*Location) Exists

func (l *Location) Exists() (bool, error)

Exists always returns true on locations

func (*Location) FileSystem

func (l *Location) FileSystem() vfs.FileSystem

FileSystem returns the type of file system location exists on, if it exists at all

func (*Location) List

func (l *Location) List() ([]string, error)

List finds all of the files living at the current location and returns them in a slice of strings. If there are no files at location, then an empty slice will be returned

func (*Location) ListByPrefix

func (l *Location) ListByPrefix(prefix string) ([]string, error)

ListByPrefix tags a prefix onto the current path and in a slice, returns all file base names whose full paths contain that substring Returns empty slice if nothing found

func (*Location) ListByRegex

func (l *Location) ListByRegex(regex *regexp.Regexp) ([]string, error)

ListByRegex takes a regular expression and returns a slice of strings containing the base names of files found that matched the regular expression. Returns an empty slice upon nothing found

func (*Location) NewFile

func (l *Location) NewFile(relFilePath string) (vfs.File, error)

NewFile creates a vfs.File given its relative path and tags it onto "l's" path

func (*Location) NewLocation

func (l *Location) NewLocation(relLocPath string) (vfs.Location, error)

NewLocation creates a new location at the given relative path, which is tagged onto the current locations absolute path

func (*Location) Path

func (l *Location) Path() string

Path returns the full, absolute path of the location with leading and trailing slashes

func (*Location) String

func (l *Location) String() string

String implements io.Stringer by returning the location's URI as a string

func (*Location) URI

func (l *Location) URI() string

URI returns the URI of the location if the location exists

func (*Location) Volume

func (l *Location) Volume() string

Volume returns the volume of the current FileSystem.

type ReadWriteSeeker added in v6.12.0

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

ReadWriteSeeker is a custom type that implements io.ReadWriteSeeker.

func NewReadWriteSeeker added in v6.12.0

func NewReadWriteSeeker() *ReadWriteSeeker

NewReadWriteSeeker creates a new ReadWriteSeeker.

func NewReadWriteSeekerWithData added in v6.12.0

func NewReadWriteSeekerWithData(data []byte) *ReadWriteSeeker

NewReadWriteSeekerWithData creates a new ReadWriteSeeker with the provided data.

func (*ReadWriteSeeker) Bytes added in v6.12.0

func (rws *ReadWriteSeeker) Bytes() []byte

Bytes returns a byte slice of the data.

func (*ReadWriteSeeker) Read added in v6.12.0

func (rws *ReadWriteSeeker) Read(p []byte) (n int, err error)

Read reads data from the current cursor position and advances the cursor.

func (*ReadWriteSeeker) Seek added in v6.12.0

func (rws *ReadWriteSeeker) Seek(offset int64, whence int) (int64, error)

Seek sets the cursor position.

func (*ReadWriteSeeker) Write added in v6.12.0

func (rws *ReadWriteSeeker) Write(p []byte) (n int, err error)

Write writes data to the current cursor position and advances the cursor.

Jump to

Keyboard shortcuts

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