Giter VIP home page Giter VIP logo

memoryfs's Introduction

memoryfs

An in-memory filesystem implementation of io/fs.FS.

memoryfs implements all of the currently defined io/fs interfaces:

It also allows the creation of files and directories.

Example

package main

import (
    "fmt"
    "io/fs"

    "github.com/liamg/memoryfs"
)

func main() {

    memfs := memoryfs.New()

    if err := memfs.MkdirAll("my/dir", 0o700); err != nil {
        panic(err)
    }

    if err := memfs.WriteFile("my/dir/file.txt", []byte("hello world"), 0o600); err != nil {
        panic(err)
    }

    data, err := fs.ReadFile(memfs, "my/dir/file.txt")
    if err != nil {
        panic(err)
    }

    fmt.Println(string(data))
}

Lazy Loading

If you are mirroring a disk file-system in memory, it can become very inefficient when large files are in use. For this scenario, the WriteLazyFile method is recommended. It allows you to add a file whose content will be provided on-demand by calling the LazyOpener function.

memoryfs's People

Contributors

liamg avatar owenrumney avatar yktakaha4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

memoryfs's Issues

Any more examples?

Hello,

I recently came across this project and was wondering if you had any more examples that might use some of the other finctions?

fs.GlobFS
fs.ReadDirFS as well as to how you fs.WriteDirFS
fs.StatFS
fs.SubFS

I am also particularly interested in reading/writing directories and putting files within those directories as wells as getting statfs on the filesystem total, fora directory, and file so that I can learn how it is done.

Thanks

feat: add TimeProvider

Thanks for a great library.

In my use case, I needed to inject an any value into ModTime, so I made the following modifications.
The default value is set to time.Now(), so backward compatibility is not broken.
yktakaha4@2fcb18e

// SetTimeProvider set function to mock generation of current time (default: time.Now())
func (m *FS) SetTimeProvider(provider func() time.Time) {
	m.context.provideTime = provider
}

// Usage
mfs := New()

t1 := time.Date(2112, 9, 3, 12, 34, 56, 321, time.UTC)
mfs.SetTimeProvider(func() time.Time {
	return t1
})

err = mfs.WriteFile("test.txt", []byte("content"), 0o644)
assert.NoError(t, err)

stat, err = mfs.Stat("test.txt")
assert.NoError(t, err)
assert.Equal(t, t1, stat.ModTime())

Would this change be appropriate for this library?
If no problem, I would like to create a pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.