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