github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/osutil/core_windows.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build windows
    16  
    17  package osutil
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"golang.org/x/sys/windows"
    24  )
    25  
    26  const (
    27  	IsWindows     = true
    28  	PathDelimiter = string(byte(filepath.Separator))
    29  )
    30  
    31  var (
    32  	SystemVolume   = filepath.VolumeName(os.Getenv("SYSTEMROOT"))
    33  	FileSystemRoot = SystemVolume + PathDelimiter
    34  )
    35  
    36  // PathToNative will convert a Unix path into the Windows-native variant (only if running on a Windows machine)
    37  func PathToNative(p string) string {
    38  	if len(p) == 0 {
    39  		return p
    40  	}
    41  	p = filepath.FromSlash(p)
    42  	if !StartsWithWindowsVolume(p) {
    43  		if p[0] == PathDelimiter[0] {
    44  			p = SystemVolume + p
    45  		} else {
    46  			p = FileSystemRoot + p
    47  		}
    48  	}
    49  	return p
    50  }
    51  
    52  // IsWindowsSharingViolation returns if the error is a Windows sharing violation
    53  func IsWindowsSharingViolation(err error) bool {
    54  	if pathErr, ok := err.(*os.PathError); ok && pathErr != nil && pathErr.Err == windows.ERROR_SHARING_VIOLATION {
    55  		return true
    56  	}
    57  	return false
    58  }