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

Code before:

// 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 {
	var tmpLowerDirs []string

	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
	}{
		"async":         {true, unix.MS_SYNCHRONOUS},
		"atime":         {true, unix.MS_NOATIME},
		"bind":          {false, unix.MS_BIND},
		"defaults":      {false, 0},
		"dev":           {true, unix.MS_NODEV},
		"diratime":      {true, unix.MS_NODIRATIME},
		"dirsync":       {false, unix.MS_DIRSYNC},
		"exec":          {true, unix.MS_NOEXEC},

Review comment (suggestion):
minor suggestion: `tmpLowerDirs := make([]string, 0, len(lowerDirs))`

Code after:

// 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
	}{
		"async":         {true, unix.MS_SYNCHRONOUS},
		"atime":         {true, unix.MS_NOATIME},
		"bind":          {false, unix.MS_BIND},
		"defaults":      {false, 0},
		"dev":           {true, unix.MS_NODEV},
		"diratime":      {true, unix.MS_NODIRATIME},
		"dirsync":       {false, unix.MS_DIRSYNC},
		"exec":          {true, unix.MS_NOEXEC},
		"mand":          {false, unix.MS_MANDLOCK},
		"noatime":       {false, unix.MS_NOATIME},