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"
19 UUIDMatch = regexp.MustCompile(`^[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}$`).MatchString
20 PDHMatch = regexp.MustCompile(`^[0-9a-f]{32}\+\d+$`).MatchString
23 // Collection is an arvados#collection resource.
24 type Collection struct {
25 UUID string `json:"uuid"`
26 Etag string `json:"etag"`
27 OwnerUUID string `json:"owner_uuid"`
28 TrashAt *time.Time `json:"trash_at"`
29 ManifestText string `json:"manifest_text"`
30 UnsignedManifestText string `json:"unsigned_manifest_text"`
31 Name string `json:"name"`
32 CreatedAt time.Time `json:"created_at"`
33 ModifiedAt time.Time `json:"modified_at"`
34 ModifiedByClientUUID string `json:"modified_by_client_uuid"`
35 ModifiedByUserUUID string `json:"modified_by_user_uuid"`
36 PortableDataHash string `json:"portable_data_hash"`
37 ReplicationConfirmed *int `json:"replication_confirmed"`
38 ReplicationConfirmedAt *time.Time `json:"replication_confirmed_at"`
39 ReplicationDesired *int `json:"replication_desired"`
40 StorageClassesDesired []string `json:"storage_classes_desired"`
41 StorageClassesConfirmed []string `json:"storage_classes_confirmed"`
42 StorageClassesConfirmedAt *time.Time `json:"storage_classes_confirmed_at"`
43 DeleteAt *time.Time `json:"delete_at"`
44 IsTrashed bool `json:"is_trashed"`
45 Properties map[string]interface{} `json:"properties"`
46 WritableBy []string `json:"writable_by,omitempty"`
47 FileCount int `json:"file_count"`
48 FileSizeTotal int64 `json:"file_size_total"`
49 Version int `json:"version"`
50 PreserveVersion bool `json:"preserve_version"`
51 CurrentVersionUUID string `json:"current_version_uuid"`
52 Description string `json:"description"`
55 func (c Collection) resourceName() string {
59 // SizedDigests returns the hash+size part of each data block
60 // referenced by the collection.
62 // Zero-length blocks are not included.
63 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
64 manifestText := []byte(c.ManifestText)
65 if len(manifestText) == 0 {
66 manifestText = []byte(c.UnsignedManifestText)
68 if len(manifestText) == 0 && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
69 // TODO: Check more subtle forms of corruption, too
70 return nil, fmt.Errorf("manifest is missing")
72 sds := make([]SizedDigest, 0, len(manifestText)/40)
73 for _, line := range bytes.Split(manifestText, []byte{'\n'}) {
77 tokens := bytes.Split(line, []byte{' '})
79 return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
81 for _, token := range tokens[1:] {
82 if !blockdigest.LocatorPattern.Match(token) {
83 // FIXME: ensure it's a file token
86 if bytes.HasPrefix(token, []byte("d41d8cd98f00b204e9800998ecf8427e+0")) {
87 // Exclude "empty block" placeholder
90 // FIXME: shouldn't assume 32 char hash
91 if i := bytes.IndexRune(token[33:], '+'); i >= 0 {
94 sds = append(sds, SizedDigest(string(token)))
100 type CollectionList struct {
101 Items []Collection `json:"items"`
102 ItemsAvailable int `json:"items_available"`
103 Offset int `json:"offset"`
104 Limit int `json:"limit"`
108 blkRe = regexp.MustCompile(`^ [0-9a-f]{32}\+\d+`)
109 tokRe = regexp.MustCompile(` ?[^ ]*`)
112 // PortableDataHash computes the portable data hash of the given
114 func PortableDataHash(mt string) string {
117 _ = tokRe.ReplaceAllFunc([]byte(mt), func(tok []byte) []byte {
118 if m := blkRe.Find(tok); m != nil {
119 // write hash+size, ignore remaining block hints
122 n, err := h.Write(tok)
129 return fmt.Sprintf("%x+%d", h.Sum(nil), size)
132 // CollectionIDFromDNSName returns a UUID or PDH if s begins with a
133 // UUID or URL-encoded PDH; otherwise "".
134 func CollectionIDFromDNSName(s string) string {
136 if i := strings.IndexRune(s, '.'); i >= 0 {
139 // Names like {uuid}--collections.example.com serve the same
140 // purpose as {uuid}.collections.example.com but can reduce
141 // cost/effort of using [additional] wildcard certificates.
142 if i := strings.Index(s, "--"); i >= 0 {
148 if pdh := strings.Replace(s, "-", "+", 1); PDHMatch(pdh) {