1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package pathdriver 18 19import ( 20 "path/filepath" 21) 22 23// PathDriver provides all of the path manipulation functions in a common 24// interface. The context should call these and never use the `filepath` 25// package or any other package to manipulate paths. 26type PathDriver interface { 27 Join(paths ...string) string 28 IsAbs(path string) bool 29 Rel(base, target string) (string, error) 30 Base(path string) string 31 Dir(path string) string 32 Clean(path string) string 33 Split(path string) (dir, file string) 34 Separator() byte 35 Abs(path string) (string, error) 36 Walk(string, filepath.WalkFunc) error 37 FromSlash(path string) string 38 ToSlash(path string) string 39 Match(pattern, name string) (matched bool, err error) 40} 41 42// pathDriver is a simple default implementation calls the filepath package. 43type pathDriver struct{} 44 45// LocalPathDriver is the exported pathDriver struct for convenience. 46var LocalPathDriver PathDriver = &pathDriver{} 47 48func (*pathDriver) Join(paths ...string) string { 49 return filepath.Join(paths...) 50} 51 52func (*pathDriver) IsAbs(path string) bool { 53 return filepath.IsAbs(path) 54} 55 56func (*pathDriver) Rel(base, target string) (string, error) { 57 return filepath.Rel(base, target) 58} 59 60func (*pathDriver) Base(path string) string { 61 return filepath.Base(path) 62} 63 64func (*pathDriver) Dir(path string) string { 65 return filepath.Dir(path) 66} 67 68func (*pathDriver) Clean(path string) string { 69 return filepath.Clean(path) 70} 71 72func (*pathDriver) Split(path string) (dir, file string) { 73 return filepath.Split(path) 74} 75 76func (*pathDriver) Separator() byte { 77 return filepath.Separator 78} 79 80func (*pathDriver) Abs(path string) (string, error) { 81 return filepath.Abs(path) 82} 83 84// Note that filepath.Walk calls os.Stat, so if the context wants to 85// to call Driver.Stat() for Walk, they need to create a new struct that 86// overrides this method. 87func (*pathDriver) Walk(root string, walkFn filepath.WalkFunc) error { 88 return filepath.Walk(root, walkFn) 89} 90 91func (*pathDriver) FromSlash(path string) string { 92 return filepath.FromSlash(path) 93} 94 95func (*pathDriver) ToSlash(path string) string { 96 return filepath.ToSlash(path) 97} 98 99func (*pathDriver) Match(pattern, name string) (bool, error) { 100 return filepath.Match(pattern, name) 101} 102