khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // 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 | |
| 14 | package fs |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "os" |
| 19 | "path/filepath" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 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" |
| 28 | ) |
| 29 | |
| 30 | // FS represents a pseudo-filesystem, normally /proc or /sys, which provides an |
| 31 | // interface to kernel data structures. |
| 32 | type FS string |
| 33 | |
| 34 | // NewFS returns a new FS mounted under the given mountPoint. It will error |
| 35 | // if the mount point can't be read. |
| 36 | func NewFS(mountPoint string) (FS, error) { |
| 37 | info, err := os.Stat(mountPoint) |
| 38 | if err != nil { |
| 39 | return "", fmt.Errorf("could not read %s: %s", mountPoint, err) |
| 40 | } |
| 41 | if !info.IsDir() { |
| 42 | return "", fmt.Errorf("mount point %s is not a directory", mountPoint) |
| 43 | } |
| 44 | |
| 45 | return FS(mountPoint), nil |
| 46 | } |
| 47 | |
| 48 | // Path appends the given path elements to the filesystem path, adding separators |
| 49 | // as necessary. |
| 50 | func (fs FS) Path(p ...string) string { |
| 51 | return filepath.Join(append([]string{string(fs)}, p...)...) |
| 52 | } |