SetFileWriteFlag changes write permissions on a file Used to make a file read-only or not. When writeEnabled = false, the write bit is removed for all roles. When writeEnabled = true, the behaviour is different per platform: On Mac & Linux, the write bit is set only on the owner as per default umask
(path string, writeEnabled bool)
| 433 | // All other bits are unaffected. |
| 434 | // On Windows, all the write bits are set since Windows doesn't support Unix permissions. |
| 435 | func SetFileWriteFlag(path string, writeEnabled bool) error { |
| 436 | stat, err := os.Stat(path) |
| 437 | if err != nil { |
| 438 | return err |
| 439 | } |
| 440 | mode := uint32(stat.Mode()) |
| 441 | |
| 442 | if (writeEnabled && (mode&0200) > 0) || |
| 443 | (!writeEnabled && (mode&0222) == 0) { |
| 444 | // no change needed |
| 445 | return nil |
| 446 | } |
| 447 | |
| 448 | if writeEnabled { |
| 449 | mode = mode | 0200 // set owner write only |
| 450 | // Go's own Chmod makes Windows set all though |
| 451 | } else { |
| 452 | mode = mode &^ 0222 // disable all write |
| 453 | } |
| 454 | return os.Chmod(path, os.FileMode(mode)) |
| 455 | } |
| 456 | |
| 457 | // TempFile creates a temporary file in specified directory with proper permissions for the repository. |
| 458 | // On success, it returns an open, non-nil *os.File, and the caller is responsible |