fssafe

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 3 Imported by: 2

README

github.com/zostay/fssafe

GoDoc

This is a small go package that provides a tools for saving files atomically. It also provides tooling for testing loading and saving of files using the same interface.

Installation

There's nothing to install, but to add it to your project:

go get github.com/zostay/fssafe

Usage

Then to use it in your code:

package main

import (
	"fmt"
	"io"
	
	"github.com/zostay/fssafe"
)

func main() {
    loaderSaver := fssafe.NewFileSystemLoaderSaver("file.txt")

    // save a file, this will create a file named file.txt.new temporarily
    w, err := loaderSaver.Saver()
    if err != nil {
        panic(err)
    }

    fmt.Fprintln(w, "Hello, World!")
    w.Close()

    // If file.txt already exists, it will be renamed to file.txt.old.  The temporary
    // file.txt.new will be renamed to file.txt

    // load a file
    r, err := loaderSaver.Loader()
    if err != nil {
        panic(err)
    }

    data, err := io.ReadAll(r)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(data))
    // Output: Hello, World!
}

Testing

Another loader/saver is provided that can be created using NewTestingLoaderSaver(). The saver just writes to a memory buffer and the loader reads from that memory buffer.

package main

import (
	"fmt"
	"io"
	
	"github.com/zostay/fssafe"
)

func main() {
    loaderSaver := fssafe.NewTestingLoaderSaver()

    // save to a memory buffer
    w, err := loaderSaver.Saver()
    if err != nil {
        panic(err)
    }

    fmt.Fprintln(w, "Hello, World!")
    w.Close()

    // load from the shared memory buffer
    r, err := loaderSaver.Loader()
    if err != nil {
        panic(err)
    }
    
    data, err := io.ReadAll(r)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(data))
    // Output: Hello, World!
}

Copyright 2020 Andrew Sterling Hanenkamp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicLoaderSaver

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

BasicLoaderSaver provides the minimum functionality for a LoaderSaver.

func NewFileSystemLoaderSaver

func NewFileSystemLoaderSaver(path string) *BasicLoaderSaver

NewFileSystemLoaderSaver builds a file system loader/saver that creates single file backups prior to writing and attempts to make saves atomic by writing data to a temporary file, which is then moved in to replace the save file.

This returns a LoaderSaver.

func (*BasicLoaderSaver) Loader

func (ls *BasicLoaderSaver) Loader() (io.ReadCloser, error)

Loader provides a io.ReadCloser for reading a save file.

func (*BasicLoaderSaver) LoaderFunc

func (ls *BasicLoaderSaver) LoaderFunc() Loader

LoaderFunc returns the Loader function used.

func (*BasicLoaderSaver) Saver

func (ls *BasicLoaderSaver) Saver() (io.WriteCloser, error)

Saver provides an io.WriteCloser for writing a save file.

func (*BasicLoaderSaver) SaverFunc

func (ls *BasicLoaderSaver) SaverFunc() Saver

SaverFunc returns the Saver function used.

type Loader

type Loader func() (io.ReadCloser, error)

Loader is a function that returns a reader for a save file or database we want to safely read from.

type LoaderSaver

type LoaderSaver interface {
	Loader() (io.ReadCloser, error)
	Saver() (io.WriteCloser, error)
	LoaderFunc() Loader
	SaverFunc() Saver
}

LoaderSaver is the interface that pairs a Loader with a Saver.

type Saver

type Saver func() (io.WriteCloser, error)

Saver is a function that returns a writer to a save file or database we want to safely write to.

type TestingLoaderSaver

type TestingLoaderSaver struct {
	BasicLoaderSaver
	// contains filtered or unexported fields
}

TestingLoaderSaver provides an in-memory loader and saver for testing.

func NewTestingLoaderSaver

func NewTestingLoaderSaver() *TestingLoaderSaver

NewTestingLoaderSaver returns a new, blank TestingLoaderSaver.

func (*TestingLoaderSaver) Buffers added in v0.1.0

func (t *TestingLoaderSaver) Buffers() []*bytes.Buffer

Buffers returns each buffer written by the saver in the order it occurred.

func (*TestingLoaderSaver) CurrentReader added in v0.1.1

func (t *TestingLoaderSaver) CurrentReader() int

CurrentReader returns the index of the current reader and can be used as the key into ReadersClosed to determine which was or was not closed.

func (*TestingLoaderSaver) CurrentWriter added in v0.1.1

func (t *TestingLoaderSaver) CurrentWriter() int

CurrentWriter returns the index of the current writer and can be used as the key into WritersClosed to determine which was or was not closed.

func (*TestingLoaderSaver) ReadersClosed added in v0.1.1

func (t *TestingLoaderSaver) ReadersClosed() []bool

ReadersClosed returns a slice of booleans indicating whether each reader created was closed.

func (*TestingLoaderSaver) WritersClosed added in v0.1.1

func (t *TestingLoaderSaver) WritersClosed() []bool

WritersClosed returns a slice of booleans indicating whether each writer created was closed.

type TestingReader

type TestingReader struct {
	*bytes.Reader      // the in-memory reader
	Closed        bool // whether the reader has been closed
}

TestingReader provides an in-memory reader for testing.

func (*TestingReader) Close

func (t *TestingReader) Close() error

Close marks the reader as closed.

type TestingWriter

type TestingWriter struct {
	*bytes.Buffer      // the in-memory writer
	Closed        bool // whether the writer has been closed
	// contains filtered or unexported fields
}

TestingWriter provides an in-memory writer for testing.

func (*TestingWriter) Close

func (t *TestingWriter) Close() error

Close marks the writer as closed.

Jump to

Keyboard shortcuts

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