Merge branch '2411-check-copyright'
[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 // SizedDigests returns the hash+size part of each data block
34 // referenced by the collection.
35 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
36         manifestText := c.ManifestText
37         if manifestText == "" {
38                 manifestText = c.UnsignedManifestText
39         }
40         if manifestText == "" && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
41                 // TODO: Check more subtle forms of corruption, too
42                 return nil, fmt.Errorf("manifest is missing")
43         }
44         var sds []SizedDigest
45         scanner := bufio.NewScanner(strings.NewReader(manifestText))
46         scanner.Buffer(make([]byte, 1048576), len(manifestText))
47         for scanner.Scan() {
48                 line := scanner.Text()
49                 tokens := strings.Split(line, " ")
50                 if len(tokens) < 3 {
51                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
52                 }
53                 for _, token := range tokens[1:] {
54                         if !blockdigest.LocatorPattern.MatchString(token) {
55                                 // FIXME: ensure it's a file token
56                                 break
57                         }
58                         // FIXME: shouldn't assume 32 char hash
59                         if i := strings.IndexRune(token[33:], '+'); i >= 0 {
60                                 token = token[:33+i]
61                         }
62                         sds = append(sds, SizedDigest(token))
63                 }
64         }
65         return sds, scanner.Err()
66 }
67
68 // CollectionList is an arvados#collectionList resource.
69 type CollectionList struct {
70         Items          []Collection `json:"items"`
71         ItemsAvailable int          `json:"items_available"`
72         Offset         int          `json:"offset"`
73         Limit          int          `json:"limit"`
74 }