github.com/mckael/restic@v0.8.3/internal/restic/blob.go (about)

     1  package restic
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/restic/restic/internal/errors"
     7  )
     8  
     9  // Blob is one part of a file or a tree.
    10  type Blob struct {
    11  	Type   BlobType
    12  	Length uint
    13  	ID     ID
    14  	Offset uint
    15  }
    16  
    17  func (b Blob) String() string {
    18  	return fmt.Sprintf("<Blob (%v) %v, offset %v, length %v>",
    19  		b.Type, b.ID.Str(), b.Offset, b.Length)
    20  }
    21  
    22  // PackedBlob is a blob stored within a file.
    23  type PackedBlob struct {
    24  	Blob
    25  	PackID ID
    26  }
    27  
    28  // BlobHandle identifies a blob of a given type.
    29  type BlobHandle struct {
    30  	ID   ID
    31  	Type BlobType
    32  }
    33  
    34  func (h BlobHandle) String() string {
    35  	return fmt.Sprintf("<%s/%s>", h.Type, h.ID.Str())
    36  }
    37  
    38  // BlobType specifies what a blob stored in a pack is.
    39  type BlobType uint8
    40  
    41  // These are the blob types that can be stored in a pack.
    42  const (
    43  	InvalidBlob BlobType = iota
    44  	DataBlob
    45  	TreeBlob
    46  )
    47  
    48  func (t BlobType) String() string {
    49  	switch t {
    50  	case DataBlob:
    51  		return "data"
    52  	case TreeBlob:
    53  		return "tree"
    54  	case InvalidBlob:
    55  		return "invalid"
    56  	}
    57  
    58  	return fmt.Sprintf("<BlobType %d>", t)
    59  }
    60  
    61  // MarshalJSON encodes the BlobType into JSON.
    62  func (t BlobType) MarshalJSON() ([]byte, error) {
    63  	switch t {
    64  	case DataBlob:
    65  		return []byte(`"data"`), nil
    66  	case TreeBlob:
    67  		return []byte(`"tree"`), nil
    68  	}
    69  
    70  	return nil, errors.New("unknown blob type")
    71  }
    72  
    73  // UnmarshalJSON decodes the BlobType from JSON.
    74  func (t *BlobType) UnmarshalJSON(buf []byte) error {
    75  	switch string(buf) {
    76  	case `"data"`:
    77  		*t = DataBlob
    78  	case `"tree"`:
    79  		*t = TreeBlob
    80  	default:
    81  		return errors.New("unknown blob type")
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  // BlobHandles is an ordered list of BlobHandles that implements sort.Interface.
    88  type BlobHandles []BlobHandle
    89  
    90  func (h BlobHandles) Len() int {
    91  	return len(h)
    92  }
    93  
    94  func (h BlobHandles) Less(i, j int) bool {
    95  	for k, b := range h[i].ID {
    96  		if b == h[j].ID[k] {
    97  			continue
    98  		}
    99  
   100  		if b < h[j].ID[k] {
   101  			return true
   102  		}
   103  
   104  		return false
   105  	}
   106  
   107  	return h[i].Type < h[j].Type
   108  }
   109  
   110  func (h BlobHandles) Swap(i, j int) {
   111  	h[i], h[j] = h[j], h[i]
   112  }
   113  
   114  func (h BlobHandles) String() string {
   115  	elements := make([]string, 0, len(h))
   116  	for _, e := range h {
   117  		elements = append(elements, e.String())
   118  	}
   119  	return fmt.Sprintf("%v", elements)
   120  }