blob: 0040753b1c180f6aa2d459758e5af8b2cbf30d62 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// Copyright 2019 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package fs
15
16import (
17 "fmt"
18 "os"
19 "path/filepath"
20)
21
22const (
23 // DefaultProcMountPoint is the common mount point of the proc filesystem.
24 DefaultProcMountPoint = "/proc"
25
26 // DefaultSysMountPoint is the common mount point of the sys filesystem.
27 DefaultSysMountPoint = "/sys"
khenaidoo26721882021-08-11 17:42:52 -040028
29 // DefaultConfigfsMountPoint is the common mount point of the configfs
30 DefaultConfigfsMountPoint = "/sys/kernel/config"
khenaidoo59ce9dd2019-11-11 13:05:32 -050031)
32
33// FS represents a pseudo-filesystem, normally /proc or /sys, which provides an
34// interface to kernel data structures.
35type FS string
36
37// NewFS returns a new FS mounted under the given mountPoint. It will error
38// if the mount point can't be read.
39func NewFS(mountPoint string) (FS, error) {
40 info, err := os.Stat(mountPoint)
41 if err != nil {
khenaidoo26721882021-08-11 17:42:52 -040042 return "", fmt.Errorf("could not read %q: %w", mountPoint, err)
khenaidoo59ce9dd2019-11-11 13:05:32 -050043 }
44 if !info.IsDir() {
khenaidoo26721882021-08-11 17:42:52 -040045 return "", fmt.Errorf("mount point %q is not a directory", mountPoint)
khenaidoo59ce9dd2019-11-11 13:05:32 -050046 }
47
48 return FS(mountPoint), nil
49}
50
51// Path appends the given path elements to the filesystem path, adding separators
52// as necessary.
53func (fs FS) Path(p ...string) string {
54 return filepath.Join(append([]string{string(fs)}, p...)...)
55}