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