main
1// Copyright (C) 2017-2024 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
5package securejoin
6
7import "os"
8
9// In future this should be moved into a separate package, because now there
10// are several projects (umoci and go-mtree) that are using this sort of
11// interface.
12
13// VFS is the minimal interface necessary to use [SecureJoinVFS]. A nil VFS is
14// equivalent to using the standard [os].* family of functions. This is mainly
15// used for the purposes of mock testing, but also can be used to otherwise use
16// [SecureJoinVFS] with VFS-like system.
17type VFS interface {
18 // Lstat returns an [os.FileInfo] describing the named file. If the
19 // file is a symbolic link, the returned [os.FileInfo] describes the
20 // symbolic link. Lstat makes no attempt to follow the link.
21 // The semantics are identical to [os.Lstat].
22 Lstat(name string) (os.FileInfo, error)
23
24 // Readlink returns the destination of the named symbolic link.
25 // The semantics are identical to [os.Readlink].
26 Readlink(name string) (string, error)
27}
28
29// osVFS is the "nil" VFS, in that it just passes everything through to the os
30// module.
31type osVFS struct{}
32
33func (o osVFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
34
35func (o osVFS) Readlink(name string) (string, error) { return os.Readlink(name) }