1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 "git.arvados.org/arvados.git/sdk/go/blockdigest"
17 // Collection is an arvados#collection resource.
18 type Collection struct {
19 UUID string `json:"uuid"`
20 Etag string `json:"etag"`
21 OwnerUUID string `json:"owner_uuid"`
22 TrashAt *time.Time `json:"trash_at"`
23 ManifestText string `json:"manifest_text"`
24 UnsignedManifestText string `json:"unsigned_manifest_text"`
25 Name string `json:"name"`
26 CreatedAt time.Time `json:"created_at"`
27 ModifiedAt time.Time `json:"modified_at"`
28 ModifiedByClientUUID string `json:"modified_by_client_uuid"`
29 ModifiedByUserUUID string `json:"modified_by_user_uuid"`
30 PortableDataHash string `json:"portable_data_hash"`
31 ReplicationConfirmed *int `json:"replication_confirmed"`
32 ReplicationConfirmedAt *time.Time `json:"replication_confirmed_at"`
33 ReplicationDesired *int `json:"replication_desired"`
34 StorageClassesDesired []string `json:"storage_classes_desired"`
35 StorageClassesConfirmed []string `json:"storage_classes_confirmed"`
36 StorageClassesConfirmedAt *time.Time `json:"storage_classes_confirmed_at"`
37 DeleteAt *time.Time `json:"delete_at"`
38 IsTrashed bool `json:"is_trashed"`
39 Properties map[string]interface{} `json:"properties"`
40 WritableBy []string `json:"writable_by,omitempty"`
41 FileCount int `json:"file_count"`
42 FileSizeTotal int64 `json:"file_size_total"`
43 Version int `json:"version"`
44 PreserveVersion bool `json:"preserve_version"`
45 CurrentVersionUUID string `json:"current_version_uuid"`
46 Description string `json:"description"`
49 func (c Collection) resourceName() string {
53 // SizedDigests returns the hash+size part of each data block
54 // referenced by the collection.
56 // Zero-length blocks are not included.
57 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
58 manifestText := []byte(c.ManifestText)
59 if len(manifestText) == 0 {
60 manifestText = []byte(c.UnsignedManifestText)
62 if len(manifestText) == 0 && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
63 // TODO: Check more subtle forms of corruption, too
64 return nil, fmt.Errorf("manifest is missing")
66 sds := make([]SizedDigest, 0, len(manifestText)/40)
67 for _, line := range bytes.Split(manifestText, []byte{'\n'}) {
71 tokens := bytes.Split(line, []byte{' '})
73 return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
75 for _, token := range tokens[1:] {
76 if !blockdigest.LocatorPattern.Match(token) {
77 // FIXME: ensure it's a file token
80 if bytes.HasPrefix(token, []byte("d41d8cd98f00b204e9800998ecf8427e+0")) {
81 // Exclude "empty block" placeholder
84 // FIXME: shouldn't assume 32 char hash
85 if i := bytes.IndexRune(token[33:], '+'); i >= 0 {
88 sds = append(sds, SizedDigest(string(token)))
94 type CollectionList struct {
95 Items []Collection `json:"items"`
96 ItemsAvailable int `json:"items_available"`
97 Offset int `json:"offset"`
98 Limit int `json:"limit"`
102 blkRe = regexp.MustCompile(`^ [0-9a-f]{32}\+\d+`)
103 tokRe = regexp.MustCompile(` ?[^ ]*`)
106 // PortableDataHash computes the portable data hash of the given
108 func PortableDataHash(mt string) string {
111 _ = tokRe.ReplaceAllFunc([]byte(mt), func(tok []byte) []byte {
112 if m := blkRe.Find(tok); m != nil {
113 // write hash+size, ignore remaining block hints
116 n, err := h.Write(tok)
123 return fmt.Sprintf("%x+%d", h.Sum(nil), size)