snd

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 10 Imported by: 4 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Afero Sync & Delete fs

Go Reference Go Report Card

The S&D file system relies on:

  • A temporary file system to store files as fast as possible
  • A permanent file system for the destination files

It allows to quickly accept new files and synchronize them on a slower file system.

The rationale is that when using some cloud-based file systems like S3, Google Drive or Dropbox on an FTP server, some devices consider the slowness as an error.

Requirements

  • Go 1.24 or later

Features

  • Asynchronous file synchronization from temporary to destination filesystem
  • Automatic garbage collection of old files
  • Built-in logging using Go's standard log/slog package
  • Configurable cleanup behavior (file age, minimum file count, cleanup period)

Closing

Close waits for the operations queued before it (file copies, renames, deletions) to reach the destination, at most Behavior.CloseTimeout (1 minute by default). CloseContext takes a context instead. Operations still pending when the wait ends keep running in the background. Close can be called more than once. Once closed, the file system refuses new operations with ErrClosed.

When no Temporary file system is given, NewFs creates a temporary directory: it is removed once the queue is drained. A Temporary file system given in the config is never removed.

Installation

go get github.com/fclairamb/afero-snd

Usage

package main

import (
    "log/slog"
    "os"

    "github.com/fclairamb/afero-snd"
    "github.com/spf13/afero"
)

func main() {
    // Create a new sync & delete filesystem
    fs, err := snd.NewFs(&snd.Config{
        Destination: afero.NewOsFs(),
        Temporary:   afero.NewMemMapFs(),
        Logger:      slog.New(slog.NewTextHandler(os.Stdout, nil)),
    })
    if err != nil {
        panic(err)
    }
    defer fs.Close()

    // Use the filesystem
    file, err := fs.Create("test.txt")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // Write to file - changes are queued for sync
    file.WriteString("Hello, World!")
}

Documentation

Overview

Package snd is an afero sync & delete file system

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("snd file system is closed")

ErrClosed is returned when an operation can't be synced because the file system is closed

View Source
var ErrNoDestination = errors.New("destination FS needs to be specified")

ErrNoDestination is returned when the destination is nil

Functions

This section is empty.

Types

type Behavior

type Behavior struct {
	FileNbMin           *int          // Minimum number of files to keep
	FileAgeMin          time.Duration // Minimum age of a file to be deleted
	OperationsQueueSize int           // Queue of operations to perform
	CleanupPeriod       time.Duration // Period of time between cleanups
	CloseTimeout        time.Duration // Maximum time Close waits for the queued operations (default: 1 minute)
}

Behavior defines the GC logic

type Config

type Config struct {
	Destination afero.Fs
	Temporary   afero.Fs
	Behavior    *Behavior
	Logger      *slog.Logger
}

Config defines the SND file system configuration

type File

type File struct {
	afero.File // Underlying temporary file
	// contains filtered or unexported fields
}

File is the internal representation of a file

func (*File) Close

func (f *File) Close() error

Close the file

func (*File) Sync

func (f *File) Sync() error

Sync triggers a sync call on the local file system and copy on the distant one

func (*File) Truncate

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

Truncate truncates the file to size bytes

type Fs

type Fs struct {
	afero.Fs
	// contains filtered or unexported fields
}

Fs is the SND file system

func NewFs

func NewFs(config *Config) (*Fs, error)

NewFs instantiates a new file system

func (*Fs) Chmod

func (fs *Fs) Chmod(name string, mode os.FileMode) error

Chmod changes the mode of the named file to mode.

func (*Fs) Chown

func (fs *Fs) Chown(name string, uid, gid int) error

Chown changes the numeric uid and gid of the named file.

func (*Fs) Chtimes

func (fs *Fs) Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times of the named

func (*Fs) Close

func (fs *Fs) Close() error

Close the file system. It waits for the operations queued before it to run, at most Behavior.CloseTimeout. It can be called more than once.

func (*Fs) CloseContext added in v0.3.0

func (fs *Fs) CloseContext(ctx context.Context) error

CloseContext closes the file system. It waits for the operations queued before it to run, until the context is done. The operations still queued then keep running in the background. The temporary directory created by NewFs (never one given in the Config) is removed once they have all run.

func (*Fs) MkDir

func (fs *Fs) MkDir(name string, perm os.FileMode) error

MkDir creates a new directory with the specified name and permission bits.

func (*Fs) MkdirAll

func (fs *Fs) MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory andall its parents

func (*Fs) Name

func (fs *Fs) Name() string

Name returns the name of the file system

func (*Fs) OpenFile

func (fs *Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error)

OpenFile opens a file using the given flags and the given mode.

func (*Fs) Remove

func (fs *Fs) Remove(name string) error

Remove removes a file identified by name, returning an error, if any happens.

func (*Fs) RemoveAll

func (fs *Fs) RemoveAll(path string) error

RemoveAll removes a path and any children it contains.

func (*Fs) Rename

func (fs *Fs) Rename(oldName, newName string) error

Rename renames (moves) a file.

func (*Fs) Sync

func (fs *Fs) Sync() error

Sync syncs the file system, i.e. makes sure all the waiting operations have been done.

Jump to

Keyboard shortcuts

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