github.com/rawahars/moby@v24.0.4+incompatible/pkg/longpath/longpath.go (about)

     1  // Package longpath introduces some constants and helper functions for handling
     2  // long paths in Windows.
     3  //
     4  // Long paths are expected to be prepended with "\\?\" and followed by either a
     5  // drive letter, a UNC server\share, or a volume identifier.
     6  package longpath // import "github.com/docker/docker/pkg/longpath"
     7  
     8  import (
     9  	"os"
    10  	"runtime"
    11  	"strings"
    12  )
    13  
    14  // Prefix is the longpath prefix for Windows file paths.
    15  const Prefix = `\\?\`
    16  
    17  // AddPrefix adds the Windows long path prefix to the path provided if
    18  // it does not already have it.
    19  func AddPrefix(path string) string {
    20  	if !strings.HasPrefix(path, Prefix) {
    21  		if strings.HasPrefix(path, `\\`) {
    22  			// This is a UNC path, so we need to add 'UNC' to the path as well.
    23  			path = Prefix + `UNC` + path[1:]
    24  		} else {
    25  			path = Prefix + path
    26  		}
    27  	}
    28  	return path
    29  }
    30  
    31  // MkdirTemp is the equivalent of [os.MkdirTemp], except that on Windows
    32  // the result is in Windows longpath format. On Unix systems it is
    33  // equivalent to [os.MkdirTemp].
    34  func MkdirTemp(dir, prefix string) (string, error) {
    35  	tempDir, err := os.MkdirTemp(dir, prefix)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  	if runtime.GOOS != "windows" {
    40  		return tempDir, nil
    41  	}
    42  	return AddPrefix(tempDir), nil
    43  }