github.com/MetalBlockchain/metalgo@v1.11.9/utils/perms/create.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package perms
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  )
    10  
    11  // Create a file at [filename] that has [perm] permissions.
    12  func Create(filename string, perm os.FileMode) (*os.File, error) {
    13  	if info, err := os.Stat(filename); err == nil {
    14  		if info.Mode() != perm {
    15  			// The file currently has the wrong permissions, so update them.
    16  			if err := os.Chmod(filename, perm); err != nil {
    17  				return nil, err
    18  			}
    19  		}
    20  	} else if !errors.Is(err, os.ErrNotExist) {
    21  		return nil, err
    22  	}
    23  	return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
    24  }