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, "/") {
		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) {

Review comment (suggestion):
I think we can make this even simpler, we don't need the if. If it ends with "/", then path.Dir will just remove it (it will be the same path, without the terminating "/"), and if it doesn't end with "/", it will do what it does today.

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 {