mirror of
https://github.com/luxfi/filesystem.git
synced 2026-10-30 16:55:58 +00:00
25 lines
649 B
Go
25 lines
649 B
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package perms
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
)
|
|
|
|
// Create a file at [filename] that has [perm] permissions.
|
|
func Create(filename string, perm os.FileMode) (*os.File, error) {
|
|
if info, err := os.Stat(filename); err == nil {
|
|
if info.Mode() != perm {
|
|
// The file currently has the wrong permissions, so update them.
|
|
if err := os.Chmod(filename, perm); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
|
|
}
|