github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/sidecar/permissions.go (about)

     1  package sidecar
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mutagen-io/mutagen/pkg/filesystem"
     7  )
     8  
     9  // SetVolumeOwnershipAndPermissionsIfEmpty will set the ownership and
    10  // permissions on a sidecar volume if (and only if) the volume is empty.
    11  func SetVolumeOwnershipAndPermissionsIfEmpty(name string, ownership *filesystem.OwnershipSpecification, mode filesystem.Mode) error {
    12  	// Open the volumes directory and defer its closure.
    13  	volumes, _, err := filesystem.OpenDirectory(volumeMountParent, false)
    14  	if err != nil {
    15  		return fmt.Errorf("unable to open volumes directory: %w", err)
    16  	}
    17  	defer volumes.Close()
    18  
    19  	// Open the volume mount point and defer its closure.
    20  	volume, err := volumes.OpenDirectory(name)
    21  	if err != nil {
    22  		return fmt.Errorf("unable to open volume root: %w", err)
    23  	}
    24  	defer volume.Close()
    25  
    26  	// Check if the volume is empty. If not, then we're done.
    27  	if contentNames, err := volume.ReadContentNames(); err != nil {
    28  		return fmt.Errorf("unable to read volume contents: %w", err)
    29  	} else if len(contentNames) != 0 {
    30  		return nil
    31  	}
    32  
    33  	// Set permissions on the volume.
    34  	return volumes.SetPermissions(name, ownership, mode)
    35  }