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