path_driver.go 2.7 KB

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