github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/endpoint/local/paths.go (about)

     1  package local
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/mutagen-io/mutagen/pkg/filesystem"
     8  )
     9  
    10  const (
    11  	// alphaName is the name to use for alpha when distinguishing endpoints.
    12  	alphaName = "alpha"
    13  	// betaName is the name to use for beta when distinguishing endpoints.
    14  	betaName = "beta"
    15  
    16  	// stagingPrefixLength is the byte length to use for prefix directories when
    17  	// load-balancing staged files.
    18  	stagingPrefixLength = 1
    19  )
    20  
    21  // pathForCache computes the path to the serialized cache for the given session
    22  // identifier and endpoint role.
    23  func pathForCache(session string, alpha bool) (string, error) {
    24  	// Compute/create the caches directory.
    25  	cachesDirectoryPath, err := filesystem.Mutagen(true, filesystem.MutagenSynchronizationCachesDirectoryName)
    26  	if err != nil {
    27  		return "", fmt.Errorf("unable to compute/create caches directory: %w", err)
    28  	}
    29  
    30  	// Compute the endpoint name.
    31  	endpointName := alphaName
    32  	if !alpha {
    33  		endpointName = betaName
    34  	}
    35  
    36  	// Compute the cache name.
    37  	cacheName := fmt.Sprintf("%s_%s", session, endpointName)
    38  
    39  	// Success.
    40  	return filepath.Join(cachesDirectoryPath, cacheName), nil
    41  }
    42  
    43  // pathForMutagenStagingRoot computes the path to the staging root in the
    44  // Mutagen data directory for the given session identifier and endpoint. It
    45  // ensures that staging subdirectory of the Mutagen data directory exists, but
    46  // it does not create the staging root itself.
    47  func pathForMutagenStagingRoot(session string, alpha bool) (string, error) {
    48  	// Compute the path to the staging root parent (the global Mutagen data
    49  	// directory in which staging roots are stored) and ensure that it exists.
    50  	stagingDataPath, err := filesystem.Mutagen(true, filesystem.MutagenSynchronizationStagingDirectoryName)
    51  	if err != nil {
    52  		return "", fmt.Errorf("unable to create staging data directory: %w", err)
    53  	}
    54  
    55  	// Compute the endpoint name.
    56  	endpointName := alphaName
    57  	if !alpha {
    58  		endpointName = betaName
    59  	}
    60  
    61  	// Compute the staging root name.
    62  	stagingRootName := fmt.Sprintf("%s-%s", session, endpointName)
    63  
    64  	// Compute the combined path.
    65  	return filepath.Join(stagingDataPath, stagingRootName), nil
    66  }
    67  
    68  // pathForNeighboringStagingRoot computes the path to the staging root which
    69  // neighbors the synchronization root for the given root, session identifier,
    70  // and endpoint. It does not create the directory or any parent directories.
    71  func pathForNeighboringStagingRoot(root, session string, alpha bool) (string, error) {
    72  	// Compute the parent of the staging root.
    73  	parent := filepath.Dir(root)
    74  
    75  	// Compute the endpoint name.
    76  	endpointName := alphaName
    77  	if !alpha {
    78  		endpointName = betaName
    79  	}
    80  
    81  	// Compute the name of the staging directory.
    82  	stagingRootName := fmt.Sprintf(
    83  		"%sstaging-%s-%s",
    84  		filesystem.TemporaryNamePrefix,
    85  		session,
    86  		endpointName,
    87  	)
    88  
    89  	// Compute the path to the staging root.
    90  	return filepath.Join(parent, stagingRootName), nil
    91  }
    92  
    93  // pathForInternalStagingRoot computes the path to the staging root which is
    94  // internal to the synchronization root for the given root, session identifier,
    95  // and endpoint. It does not create the directory or any parent directories.
    96  func pathForInternalStagingRoot(root, session string, alpha bool) (string, error) {
    97  	// Compute the endpoint name.
    98  	endpointName := alphaName
    99  	if !alpha {
   100  		endpointName = betaName
   101  	}
   102  
   103  	// Compute the name of the staging directory.
   104  	stagingRootName := fmt.Sprintf(
   105  		"%sstaging-%s-%s",
   106  		filesystem.TemporaryNamePrefix,
   107  		session,
   108  		endpointName,
   109  	)
   110  
   111  	// Compute the path to the staging root.
   112  	return filepath.Join(root, stagingRootName), nil
   113  }