github.com/MetalBlockchain/metalgo@v1.11.9/utils/perms/chmod.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  	"path/filepath"
    10  )
    11  
    12  // ChmodR sets the permissions of all directories and optionally files to [perm]
    13  // permissions.
    14  func ChmodR(dir string, dirOnly bool, perm os.FileMode) error {
    15  	if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
    16  		return nil
    17  	}
    18  	return filepath.Walk(dir, func(name string, info os.FileInfo, err error) error {
    19  		if err != nil || (dirOnly && !info.IsDir()) {
    20  			return err
    21  		}
    22  		return os.Chmod(name, perm)
    23  	})
    24  }