Repository: containerd/containerd
File: core/mount/mount_linux.go (Go)

Code before:
	// IDMapMount the directory containing all the layers
	if err := IDMapMountWithAttrs(commonDir, tempRemountsLocation, usernsFd, unix.MOUNT_ATTR_RDONLY, 0); err != nil {
		return nil, cleanUp, err
	}

	// Build new lower dir paths through the idmapped directory
	tmpLowerDirs := buildIDMappedPaths(lowerDirs, commonDir, tempRemountsLocation)

	return tmpLowerDirs, cleanUp, nil
}

// getCommonDirectory finds the common directory among the lowerDirs passed in.
// "/" and "." are considered invalid common directories and are treated as error
func getCommonDirectory(lowerDirs []string) (string, error) {
	commonPrefix := longestCommonPrefix(lowerDirs)
	if commonPrefix == "" {
		return "", fmt.Errorf("no common prefix found")
	}

	// Ensure the common prefix ends at a directory boundary
	if !strings.HasSuffix(commonPrefix, "/") {
		lastSlash := strings.LastIndex(commonPrefix, "/")
		if lastSlash == -1 {
			return "", fmt.Errorf("no valid directory separator found in common prefix")
		}
		commonPrefix = commonPrefix[:lastSlash]
	} else {
		commonPrefix = strings.TrimSuffix(commonPrefix, "/")
	}

	if commonPrefix == "." || commonPrefix == "" {
		return "", fmt.Errorf("invalid common directory: %s", commonPrefix)
	}

	return commonPrefix, nil
}

// buildIDMappedPaths constructs new lower directory paths through an idmapped mount of the commonDir.
// It takes the original lowerDirs, the commonDir of those dirs, and rewrites the paths
// to go through the idMappedDir directory to achieve idmapped lowerdirs ready for overlayfs
func buildIDMappedPaths(lowerDirs []string, commonDir, idMappedDir string) []string {
	var tmpLowerDirs []string

	for _, lowerDir := range lowerDirs {
		relativePath := strings.TrimPrefix(lowerDir, commonDir)
		tmpLowerDirs = append(tmpLowerDirs, filepath.Join(idMappedDir, relativePath))
	}

	return tmpLowerDirs
}


Review comment (suggestion):
If it doesn't end in "/", isn't it simpler to use [filepath.Dir()][link] here? I think this is doing the same


[link]: https://pkg.go.dev/path/filepath#Dir

Code after:
	// IDMapMount the directory containing all the layers
	if err := IDMapMountWithAttrs(commonDir, tempRemountsLocation, usernsFd, unix.MOUNT_ATTR_RDONLY, 0); err != nil {
		return nil, cleanUp, err
	}

	// Build new lower dir paths through the idmapped directory
	tmpLowerDirs := buildIDMappedPaths(lowerDirs, commonDir, tempRemountsLocation)

	return tmpLowerDirs, cleanUp, nil
}

// getCommonDirectory finds the common directory among the lowerDirs passed in.
// "/" and "." are considered invalid common directories and are treated as error
func getCommonDirectory(lowerDirs []string) (string, error) {
	commonPrefix := longestCommonPrefix(lowerDirs)
	if commonPrefix == "" {
		return "", fmt.Errorf("no common prefix found")
	}

	// Ensure the common prefix ends at a directory boundary
	commonPrefix = path.Dir(commonPrefix)

	if commonPrefix == "." || commonPrefix == "/" {
		return "", fmt.Errorf("invalid common directory: %s", commonPrefix)
	}

	return commonPrefix, nil
}

// buildIDMappedPaths constructs new lower directory paths through an idmapped mount of the commonDir.
// It takes the original lowerDirs, the commonDir of those dirs, and rewrites the paths
// to go through the idMappedDir directory to achieve idmapped lowerdirs ready for overlayfs
func buildIDMappedPaths(lowerDirs []string, commonDir, idMappedDir string) []string {
	tmpLowerDirs := make([]string, 0, len(lowerDirs))

	for _, lowerDir := range lowerDirs {
		relativePath := strings.TrimPrefix(lowerDir, commonDir)
		tmpLowerDirs = append(tmpLowerDirs, filepath.Join(idMappedDir, relativePath))
	}

	return tmpLowerDirs
}

// parseMountOptions takes fstab style mount options and parses them for
// use with a standard mount() syscall
func parseMountOptions(options []string) (opt mountOpt) {
	loopOpt := "loop"
	flagsMap := map[string]struct {
		clear bool
		flag  int
	}{