github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/rsync/transmission.go (about)

     1  package rsync
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // resetToZeroMaintainingCapacity resets a Transmission to its zero value, with
     8  // the exception that it will leave the Operation member allocated if it's
     9  // already set will simply call resetToZeroMaintainingCapacity on the Operation.
    10  // This allows some decoders to re-use the Operation data slice capacity when
    11  // decoding.
    12  func (t *Transmission) resetToZeroMaintainingCapacity() {
    13  	// Reset the expected file size.
    14  	t.ExpectedSize = 0
    15  
    16  	// Reset the Done parameter.
    17  	t.Done = false
    18  
    19  	// Reset the operation to its zero value if non-nil.
    20  	if t.Operation != nil {
    21  		t.Operation.resetToZeroMaintainingCapacity()
    22  	}
    23  
    24  	// Reset the error parameter.
    25  	t.Error = ""
    26  }
    27  
    28  // EnsureValid ensures that the Transmission's invariants are respected.
    29  func (t *Transmission) EnsureValid() error {
    30  	// A nil transmission is not valid.
    31  	if t == nil {
    32  		return errors.New("nil transmission")
    33  	}
    34  
    35  	// Handle validation based on whether or not the transmission is marked as
    36  	// being the end of a file.
    37  	if t.Done {
    38  		if t.ExpectedSize != 0 {
    39  			return errors.New("non-zero expected file size at end of stream")
    40  		} else if t.Operation != nil && !t.Operation.isZeroValue() {
    41  			return errors.New("operation present at end of stream")
    42  		}
    43  	} else {
    44  		if t.Operation == nil {
    45  			return errors.New("operation missing from middle of stream")
    46  		} else if err := t.Operation.EnsureValid(); err != nil {
    47  			return errors.New("invalid operation in stream")
    48  		} else if t.Error != "" {
    49  			return errors.New("error in middle of stream")
    50  		}
    51  	}
    52  
    53  	// Success.
    54  	return nil
    55  }