| 38 | } |
| 39 | |
| 40 | func (h *mkdir) Transform(ctx context.Context, m mount.Mount, _ []mount.ActiveMount) (mount.Mount, error) { |
| 41 | |
| 42 | var options []string |
| 43 | for _, o := range m.Options { |
| 44 | if mkdirOption, isMkdir := strings.CutPrefix(o, prefixMkdir); isMkdir { |
| 45 | // Format is X-containerd.mkdir.path=value[:mode[:uid:gid]] |
| 46 | |
| 47 | value, isPath := strings.CutPrefix(mkdirOption, "path=") |
| 48 | if !isPath { |
| 49 | return mount.Mount{}, fmt.Errorf("invalid mkdir option %q: %w", o, errdefs.ErrInvalidArgument) |
| 50 | } |
| 51 | parts := strings.SplitN(value, ":", 4) |
| 52 | var ( |
| 53 | dir string |
| 54 | mode os.FileMode = 0700 |
| 55 | luid = os.Getuid() |
| 56 | lgid = os.Getgid() |
| 57 | uid, gid = luid, lgid |
| 58 | err error |
| 59 | ) |
| 60 | switch len(parts) { |
| 61 | case 4: |
| 62 | gid, err = strconv.Atoi(parts[3]) |
| 63 | if err != nil { |
| 64 | return mount.Mount{}, fmt.Errorf("invalid gid %q: %w", parts[3], errdefs.ErrInvalidArgument) |
| 65 | } |
| 66 | uid, err = strconv.Atoi(parts[2]) |
| 67 | if err != nil { |
| 68 | return mount.Mount{}, fmt.Errorf("invalid uid %q: %w", parts[2], errdefs.ErrInvalidArgument) |
| 69 | } |
| 70 | fallthrough |
| 71 | case 2: |
| 72 | var p uint64 |
| 73 | p, err = strconv.ParseUint(parts[1], 8, 32) |
| 74 | if err == nil { |
| 75 | mode = os.FileMode(p) |
| 76 | if mode != mode&os.ModePerm { |
| 77 | return mount.Mount{}, fmt.Errorf("invalid mode %o", p) |
| 78 | } |
| 79 | } else { |
| 80 | return mount.Mount{}, fmt.Errorf("invalid mode %s: %w", parts[1], err) |
| 81 | } |
| 82 | fallthrough |
| 83 | case 1: |
| 84 | dir = parts[0] |
| 85 | default: |
| 86 | return mount.Mount{}, fmt.Errorf("invalid mkdir option %q: %w", o, errdefs.ErrInvalidArgument) |
| 87 | } |
| 88 | |
| 89 | var r *os.Root |
| 90 | var subpath string |
| 91 | |
| 92 | for path, root := range h.rootMap { |
| 93 | if strings.HasPrefix(dir, path) { |
| 94 | r = root |
| 95 | subpath = strings.TrimPrefix(dir, path) |
| 96 | subpath, _ = filepath.Rel("/", subpath) |
| 97 | break |