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