snd package - github.com/fclairamb/afero-snd - Go Packages

snd

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 8 Imported by: 4

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)

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 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
}

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

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