types.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import "sync"
  8. type FilesystemType string
  9. // Option modifies a filesystem at creation. An option might be specific
  10. // to a filesystem-type.
  11. //
  12. // String is used to detect options with the same effect, i.e. must be different
  13. // for options with different effects. Meaning if an option has parameters, a
  14. // representation of those must be part of the returned string.
  15. type Option interface {
  16. String() string
  17. apply(Filesystem) Filesystem
  18. }
  19. // Factory function type for constructing a custom file system. It takes the URI
  20. // and options as its parameters.
  21. type FilesystemFactory func(string, ...Option) (Filesystem, error)
  22. // For each registered file system type, a function to construct a file system.
  23. var filesystemFactories map[FilesystemType]FilesystemFactory = make(map[FilesystemType]FilesystemFactory)
  24. var filesystemFactoriesMutex sync.Mutex = sync.Mutex{}
  25. // Register a function to be called when a filesystem is to be constructed with
  26. // the specified fsType. The function will receive the URI for the file system as well
  27. // as all options.
  28. func RegisterFilesystemType(fsType FilesystemType, fn FilesystemFactory) {
  29. filesystemFactoriesMutex.Lock()
  30. defer filesystemFactoriesMutex.Unlock()
  31. filesystemFactories[fsType] = fn
  32. }