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