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