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