11453: Merge branch 'master' into 11453-federated-tokens
[arvados.git] / sdk / go / arvados / collection.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "bufio"
9         "fmt"
10         "strings"
11         "time"
12
13         "git.curoverse.com/arvados.git/sdk/go/blockdigest"
14 )
15
16 // Collection is an arvados#collection resource.
17 type Collection struct {
18         UUID                   string     `json:"uuid,omitempty"`
19         TrashAt                *time.Time `json:"trash_at,omitempty"`
20         ManifestText           string     `json:"manifest_text,omitempty"`
21         UnsignedManifestText   string     `json:"unsigned_manifest_text,omitempty"`
22         Name                   string     `json:"name,omitempty"`
23         CreatedAt              *time.Time `json:"created_at,omitempty"`
24         ModifiedAt             *time.Time `json:"modified_at,omitempty"`
25         PortableDataHash       string     `json:"portable_data_hash,omitempty"`
26         ReplicationConfirmed   *int       `json:"replication_confirmed,omitempty"`
27         ReplicationConfirmedAt *time.Time `json:"replication_confirmed_at,omitempty"`
28         ReplicationDesired     *int       `json:"replication_desired,omitempty"`
29         DeleteAt               *time.Time `json:"delete_at,omitempty"`
30         IsTrashed              bool       `json:"is_trashed,omitempty"`
31 }
32
33 func (c Collection) resourceName() string {
34         return "collection"
35 }
36
37 // SizedDigests returns the hash+size part of each data block
38 // referenced by the collection.
39 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
40         manifestText := c.ManifestText
41         if manifestText == "" {
42                 manifestText = c.UnsignedManifestText
43         }
44         if manifestText == "" && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
45                 // TODO: Check more subtle forms of corruption, too
46                 return nil, fmt.Errorf("manifest is missing")
47         }
48         var sds []SizedDigest
49         scanner := bufio.NewScanner(strings.NewReader(manifestText))
50         scanner.Buffer(make([]byte, 1048576), len(manifestText))
51         for scanner.Scan() {
52                 line := scanner.Text()
53                 tokens := strings.Split(line, " ")
54                 if len(tokens) < 3 {
55                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
56                 }
57                 for _, token := range tokens[1:] {
58                         if !blockdigest.LocatorPattern.MatchString(token) {
59                                 // FIXME: ensure it's a file token
60                                 break
61                         }
62                         // FIXME: shouldn't assume 32 char hash
63                         if i := strings.IndexRune(token[33:], '+'); i >= 0 {
64                                 token = token[:33+i]
65                         }
66                         sds = append(sds, SizedDigest(token))
67                 }
68         }
69         return sds, scanner.Err()
70 }
71
72 // CollectionList is an arvados#collectionList resource.
73 type CollectionList struct {
74         Items          []Collection `json:"items"`
75         ItemsAvailable int          `json:"items_available"`
76         Offset         int          `json:"offset"`
77         Limit          int          `json:"limit"`
78 }