Source file src/path/filepath/symlink_windows.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package filepath
     6  
     7  import (
     8  	"strings"
     9  	"syscall"
    10  )
    11  
    12  // normVolumeName is like VolumeName, but makes drive letter upper case.
    13  // result of EvalSymlinks must be unique, so we have
    14  // EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`).
    15  func normVolumeName(path string) string {
    16  	volume := VolumeName(path)
    17  
    18  	if len(volume) > 2 { // isUNC
    19  		return volume
    20  	}
    21  
    22  	return strings.ToUpper(volume)
    23  }
    24  
    25  // normBase returns the last element of path with correct case.
    26  func normBase(path string) (string, error) {
    27  	p, err := syscall.UTF16PtrFromString(path)
    28  	if err != nil {
    29  		return "", err
    30  	}
    31  
    32  	var data syscall.Win32finddata
    33  
    34  	h, err := syscall.FindFirstFile(p, &data)
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  	syscall.FindClose(h)
    39  
    40  	return syscall.UTF16ToString(data.FileName[:]), nil
    41  }
    42  
    43  // baseIsDotDot reports whether the last element of path is "..".
    44  // The given path should be 'Clean'-ed in advance.
    45  func baseIsDotDot(path string) bool {
    46  	i := strings.LastIndexByte(path, Separator)
    47  	return path[i+1:] == ".."
    48  }
    49  
    50  // toNorm returns the normalized path that is guaranteed to be unique.
    51  // It should accept the following formats:
    52  //   - UNC paths                              (e.g \\server\share\foo\bar)
    53  //   - absolute paths                         (e.g C:\foo\bar)
    54  //   - relative paths begin with drive letter (e.g C:foo\bar, C:..\foo\bar, C:.., C:.)
    55  //   - relative paths begin with '\'          (e.g \foo\bar)
    56  //   - relative paths begin without '\'       (e.g foo\bar, ..\foo\bar, .., .)
    57  //
    58  // The returned normalized path will be in the same form (of 5 listed above) as the input path.
    59  // If two paths A and B are indicating the same file with the same format, toNorm(A) should be equal to toNorm(B).
    60  // The normBase parameter should be equal to the normBase func, except for in tests.  See docs on the normBase func.
    61  func toNorm(path string, normBase func(string) (string, error)) (string, error) {
    62  	if path == "" {
    63  		return path, nil
    64  	}
    65  
    66  	path = Clean(path)
    67  
    68  	volume := normVolumeName(path)
    69  	path = path[len(volume):]
    70  
    71  	// skip special cases
    72  	if path == "" || path == "." || path == `\` {
    73  		return volume + path, nil
    74  	}
    75  
    76  	var normPath string
    77  
    78  	for {
    79  		if baseIsDotDot(path) {
    80  			normPath = path + `\` + normPath
    81  
    82  			break
    83  		}
    84  
    85  		name, err := normBase(volume + path)
    86  		if err != nil {
    87  			return "", err
    88  		}
    89  
    90  		normPath = name + `\` + normPath
    91  
    92  		i := strings.LastIndexByte(path, Separator)
    93  		if i == -1 {
    94  			break
    95  		}
    96  		if i == 0 { // `\Go` or `C:\Go`
    97  			normPath = `\` + normPath
    98  
    99  			break
   100  		}
   101  
   102  		path = path[:i]
   103  	}
   104  
   105  	normPath = normPath[:len(normPath)-1] // remove trailing '\'
   106  
   107  	return volume + normPath, nil
   108  }
   109  
   110  func evalSymlinks(path string) (string, error) {
   111  	newpath, err := walkSymlinks(path)
   112  	if err != nil {
   113  		return "", err
   114  	}
   115  	newpath, err = toNorm(newpath, normBase)
   116  	if err != nil {
   117  		return "", err
   118  	}
   119  	return newpath, nil
   120  }
   121  

View as plain text