github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/internal/iohelpers/filemode.go (about)

     1  package iohelpers
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  )
     7  
     8  // NormalizeFileMode converts the given mode to a mode that will work as
     9  // expected on the given OS. A no-op on non-Windows OSes, but on Windows modes
    10  // work differently - only the owner read/write bits are honoured (i.e. the 0200
    11  // mask).
    12  func NormalizeFileMode(mode os.FileMode) os.FileMode {
    13  	if runtime.GOOS == "windows" {
    14  		return windowsFileMode(mode)
    15  	}
    16  	return mode
    17  }
    18  
    19  func windowsFileMode(mode os.FileMode) os.FileMode {
    20  	// non-owner and execute bits are stripped on files
    21  	if !mode.IsDir() {
    22  		mode &^= 0o177
    23  	}
    24  
    25  	if mode&0o200 != 0 {
    26  		// writeable implies read/write on Windows
    27  		mode |= 0o666
    28  	} else if mode&0o400 != 0 {
    29  		mode |= 0o444
    30  	}
    31  
    32  	return mode
    33  }