vfs.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (C) 2017 SUSE LLC. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package securejoin
  5. import "os"
  6. // In future this should be moved into a separate package, because now there
  7. // are several projects (umoci and go-mtree) that are using this sort of
  8. // interface.
  9. // VFS is the minimal interface necessary to use SecureJoinVFS. A nil VFS is
  10. // equivalent to using the standard os.* family of functions. This is mainly
  11. // used for the purposes of mock testing, but also can be used to otherwise use
  12. // SecureJoin with VFS-like system.
  13. type VFS interface {
  14. // Lstat returns a FileInfo describing the named file. If the file is a
  15. // symbolic link, the returned FileInfo describes the symbolic link. Lstat
  16. // makes no attempt to follow the link. These semantics are identical to
  17. // os.Lstat.
  18. Lstat(name string) (os.FileInfo, error)
  19. // Readlink returns the destination of the named symbolic link. These
  20. // semantics are identical to os.Readlink.
  21. Readlink(name string) (string, error)
  22. }
  23. // osVFS is the "nil" VFS, in that it just passes everything through to the os
  24. // module.
  25. type osVFS struct{}
  26. // Lstat returns a FileInfo describing the named file. If the file is a
  27. // symbolic link, the returned FileInfo describes the symbolic link. Lstat
  28. // makes no attempt to follow the link. These semantics are identical to
  29. // os.Lstat.
  30. func (o osVFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
  31. // Readlink returns the destination of the named symbolic link. These
  32. // semantics are identical to os.Readlink.
  33. func (o osVFS) Readlink(name string) (string, error) { return os.Readlink(name) }