Documentation
¶
Overview ¶
Package providersync syncs provider credential files to/from the DB.
On startup: DB → filesystem (restore). Background ticker: filesystem → DB (backup), skipping unchanged files via SHA-256 hash comparison. Retention job: purges expired file rows on each tick.
Index ¶
- func SourceToInstance(src entity.ProviderStorageSource) provider.Instance
- type Manager
- func (m *Manager) CheckSource(mode, syncPath string) ([]string, error)
- func (m *Manager) DeleteByAbsPath(ctx context.Context, abs string) (int64, error)
- func (m *Manager) DeleteByID(ctx context.Context, id uint) error
- func (m *Manager) DeleteByInstance(ctx context.Context, providerType, instanceName string) (int64, error)
- func (m *Manager) DeleteSource(ctx context.Context, id uint) error
- func (m *Manager) EnsureWatcher(ctx context.Context, debounceMs int) error
- func (m *Manager) GetByID(ctx context.Context, id uint) (entity.ProviderStorage, error)
- func (m *Manager) GetSource(ctx context.Context, id uint) (entity.ProviderStorageSource, error)
- func (m *Manager) ListAll(ctx context.Context) ([]entity.ProviderStorage, error)
- func (m *Manager) ListChildren(ctx context.Context, providerType, instanceName string, parentID uint) ([]entity.ProviderStorage, error)
- func (m *Manager) ListRoots(ctx context.Context) ([]entity.ProviderStorage, error)
- func (m *Manager) ListSources(ctx context.Context) ([]entity.ProviderStorageSource, error)
- func (m *Manager) PurgeExcluded(ctx context.Context, providerType, instanceName string) (int, error)
- func (m *Manager) RecomputeRetention(ctx context.Context, providerType, instanceName string) (int, error)
- func (m *Manager) Reload(ctx context.Context) error
- func (m *Manager) RepairTree(ctx context.Context) (int, error)
- func (m *Manager) RestoreAll(ctx context.Context, verbose ...bool) error
- func (m *Manager) RestoreAllForce(ctx context.Context, verbose ...bool) error
- func (m *Manager) RestoreSelected(ctx context.Context, ids []uint, srcsByInstance map[string][]SrcInfo) (int, error)
- func (m *Manager) RunRetention(ctx context.Context)
- func (m *Manager) SaveSource(ctx context.Context, src entity.ProviderStorageSource) (entity.ProviderStorageSource, error)
- func (m *Manager) SetRetention(ctx context.Context, id uint, days int) error
- func (m *Manager) StopWatcher()
- func (m *Manager) SyncAll(ctx context.Context) (int, error)
- func (m *Manager) SyncFile(ctx context.Context, ins provider.Instance, abs string) (int, int, error)
- func (m *Manager) SyncOne(ctx context.Context, ins provider.Instance, verbose ...bool) (int, int, error)
- func (m *Manager) Upload(ctx context.Context, providerType, instanceName, relPath string, ...) error
- type SrcInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SourceToInstance ¶
func SourceToInstance(src entity.ProviderStorageSource) provider.Instance
SourceToInstance converts a ProviderStorageSource to a provider.Instance suitable for SyncOne / backup calls. Excludes are read separately at the store layer since provider.Instance has no slot for them.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager drives syncing for all provider instances that have Storage configured. One Manager per app lifetime; call Start once.
func (*Manager) CheckSource ¶
CheckSource lists file paths under syncPath without reading content.
func (*Manager) DeleteByAbsPath ¶ added in v0.14.4
DeleteByAbsPath hard-deletes the file row at the given absolute path. Used by the realtime watcher in response to fsnotify Remove/Rename events — when a file disappears from disk it must disappear from DB immediately, bypassing the retention job. Returns rows affected.
func (*Manager) DeleteByID ¶
DeleteByID removes one file row from DB.
func (*Manager) DeleteByInstance ¶
func (m *Manager) DeleteByInstance(ctx context.Context, providerType, instanceName string) (int64, error)
DeleteByInstance removes all rows for a provider instance.
func (*Manager) DeleteSource ¶
DeleteSource removes a sync source by ID and recomputes file retention so rows that previously inherited this source's retention drop back to the next matching source (or 0 / lifetime if none).
func (*Manager) EnsureWatcher ¶ added in v0.14.4
EnsureWatcher starts the realtime fsnotify watcher if it isn't already running, or hot-reloads its source set if it is. Idempotent — safe to call every job tick. debounceMs <= 0 falls back to 1000.
Boot order matters: the caller (server.go) MUST run RestoreAllForce to completion before calling EnsureWatcher, otherwise the watcher races the restore writes and reports them as "disk changes" back to itself.
func (*Manager) ListChildren ¶
func (m *Manager) ListChildren(ctx context.Context, providerType, instanceName string, parentID uint) ([]entity.ProviderStorage, error)
ListChildren returns direct children (files + folders) under parentID for an instance. parentID=0 means instance root.
func (*Manager) ListSources ¶
ListSources returns all configured sync sources.
func (*Manager) PurgeExcluded ¶ added in v0.13.0
func (m *Manager) PurgeExcluded(ctx context.Context, providerType, instanceName string) (int, error)
PurgeExcluded walks every row for (providerType, instanceName) and deletes rows whose absolute rel_path matches any current enabled-source exclude pattern. Empty folder rows left behind are also pruned so the tree doesn't show ghost directories. Returns the number of file rows deleted.
func (*Manager) RecomputeRetention ¶ added in v0.13.0
func (m *Manager) RecomputeRetention(ctx context.Context, providerType, instanceName string) (int, error)
RecomputeRetention walks every file row for (providerType, instanceName) and rewrites retention_days based on the deepest currently-enabled source whose path covers the row. Returns the number of rows updated. Cheap to call on every source change since pickRetention is in-memory.
func (*Manager) Reload ¶ added in v0.14.4
Reload tells the realtime watcher to re-read sources from DB and adjust its kernel watch set accordingly. No-op when the watcher is not running. Called after SaveSource / DeleteSource so toggling a row in the Settings UI propagates without a job tick.
func (*Manager) RepairTree ¶ added in v0.13.0
RepairTree rewires parent_id values for every row from its rel_path so listChildren / explorer drill-down works even when a parent row was previously deleted (orphan recovery). Returns the number of rows fixed. Cheap; intended for boot-time and after destructive operations.
func (*Manager) RestoreAll ¶
RestoreAll writes all DB file rows back to filesystem with the disk-wins guard: missing → write, same hash → skip, diverged → keep disk + log. Used by the cron tick and the /restore-now UI button.
func (*Manager) RestoreAllForce ¶ added in v0.13.2
RestoreAllForce overwrites every covered file on disk with the DB copy, no hash check. Used at server boot so DB is the source of truth on the first restore after a container restart (no-volume env).
func (*Manager) RestoreSelected ¶
func (m *Manager) RestoreSelected(ctx context.Context, ids []uint, srcsByInstance map[string][]SrcInfo) (int, error)
RestoreSelected writes specific DB rows (by ID) back to filesystem. srcsByInstance is kept for API compatibility but ignored: rel_path is now an absolute filesystem path, written directly. Returns count of files written.
func (*Manager) RunRetention ¶
RunRetention purges expired file rows.
func (*Manager) SaveSource ¶
func (m *Manager) SaveSource(ctx context.Context, src entity.ProviderStorageSource) (entity.ProviderStorageSource, error)
SaveSource creates or updates a sync source and immediately runs a backup pass. Also recomputes per-file retention for the instance so existing rows reflect the new (or changed) source retention without waiting for the next sync tick.
func (*Manager) SetRetention ¶
SetRetention updates retention_days for one file row.
func (*Manager) StopWatcher ¶ added in v0.14.4
func (m *Manager) StopWatcher()
StopWatcher tears down the realtime watcher. No-op when not running. Pending debounced events are dropped — the next cron tick will pick them up via the normal walk path.
func (*Manager) SyncAll ¶ added in v0.13.0
SyncAll iterates all enabled sources and runs a backup pass for each. Returns number of sources that synced cleanly; per-source errors are logged but do not abort the loop (one bad path shouldn't stop the others). Safe to call on startup so users see populated rows immediately without having to wait for the cron tick.
func (*Manager) SyncFile ¶ added in v0.14.4
func (m *Manager) SyncFile(ctx context.Context, ins provider.Instance, abs string) (int, int, error)
SyncFile syncs one absolute path on disk into DB. Used by the realtime watcher to react to a single Write/Create event without re-walking the whole source. Returns (changed, skipped, err) where skipped=1 means the content hash matched and no DB write happened.
func (*Manager) SyncOne ¶
func (m *Manager) SyncOne(ctx context.Context, ins provider.Instance, verbose ...bool) (int, int, error)
SyncOne runs a single backup pass for one instance. SyncOne backs up one provider instance from disk to DB. Returns (changed, skipped, error) — changed is the number of files written/updated; skipped is files whose content hash was unchanged.
func (*Manager) Upload ¶
func (m *Manager) Upload(ctx context.Context, providerType, instanceName, relPath string, content []byte) error
Upload stores file content directly into DB (manual upload).
If relPath is already covered by an enabled include source whose provider/instance differs from the caller's values, the upload is re-tagged to that source. This stops a single physical path from living under two (provider, instance) pairs — which would leave one copy uncovered by RestoreAll and effectively unrestorable.