validate.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package container
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/docker/swarmkit/api"
  7. )
  8. func validateMounts(mounts []api.Mount) error {
  9. for _, mount := range mounts {
  10. // Target must always be absolute
  11. if !filepath.IsAbs(mount.Target) {
  12. return fmt.Errorf("invalid mount target, must be an absolute path: %s", mount.Target)
  13. }
  14. switch mount.Type {
  15. // The checks on abs paths are required due to the container API confusing
  16. // volume mounts as bind mounts when the source is absolute (and vice-versa)
  17. // See #25253
  18. // TODO: This is probably not necessary once #22373 is merged
  19. case api.MountTypeBind:
  20. if !filepath.IsAbs(mount.Source) {
  21. return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
  22. }
  23. case api.MountTypeVolume:
  24. if filepath.IsAbs(mount.Source) {
  25. return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
  26. }
  27. case api.MountTypeTmpfs:
  28. if mount.Source != "" {
  29. return errors.New("invalid tmpfs source, source must be empty")
  30. }
  31. default:
  32. return fmt.Errorf("invalid mount type: %s", mount.Type)
  33. }
  34. }
  35. return nil
  36. }