785c18d4a75090a9847be18122b418b392c45474
[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         "bytes"
9         "crypto/md5"
10         "fmt"
11         "regexp"
12         "time"
13
14         "git.arvados.org/arvados.git/sdk/go/blockdigest"
15 )
16
17 // Collection is an arvados#collection resource.
18 type Collection struct {
19         UUID                      string                 `json:"uuid"`
20         Etag                      string                 `json:"etag"`
21         OwnerUUID                 string                 `json:"owner_uuid"`
22         TrashAt                   *time.Time             `json:"trash_at"`
23         ManifestText              string                 `json:"manifest_text"`
24         UnsignedManifestText      string                 `json:"unsigned_manifest_text"`
25         Name                      string                 `json:"name"`
26         CreatedAt                 time.Time              `json:"created_at"`
27         ModifiedAt                time.Time              `json:"modified_at"`
28         ModifiedByClientUUID      string                 `json:"modified_by_client_uuid"`
29         ModifiedByUserUUID        string                 `json:"modified_by_user_uuid"`
30         PortableDataHash          string                 `json:"portable_data_hash"`
31         ReplicationConfirmed      *int                   `json:"replication_confirmed"`
32         ReplicationConfirmedAt    *time.Time             `json:"replication_confirmed_at"`
33         ReplicationDesired        *int                   `json:"replication_desired"`
34         StorageClassesDesired     []string               `json:"storage_classes_desired"`
35         StorageClassesConfirmed   []string               `json:"storage_classes_confirmed"`
36         StorageClassesConfirmedAt *time.Time             `json:"storage_classes_confirmed_at"`
37         DeleteAt                  *time.Time             `json:"delete_at"`
38         IsTrashed                 bool                   `json:"is_trashed"`
39         Properties                map[string]interface{} `json:"properties"`
40         WritableBy                []string               `json:"writable_by,omitempty"`
41         FileCount                 int                    `json:"file_count"`
42         FileSizeTotal             int64                  `json:"file_size_total"`
43         Version                   int                    `json:"version"`
44         PreserveVersion           bool                   `json:"preserve_version"`
45         CurrentVersionUUID        string                 `json:"current_version_uuid"`
46         Description               string                 `json:"description"`
47 }
48
49 func (c Collection) resourceName() string {
50         return "collection"
51 }
52
53 // SizedDigests returns the hash+size part of each data block
54 // referenced by the collection.
55 //
56 // Zero-length blocks are not included.
57 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
58         manifestText := []byte(c.ManifestText)
59         if len(manifestText) == 0 {
60                 manifestText = []byte(c.UnsignedManifestText)
61         }
62         if len(manifestText) == 0 && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
63                 // TODO: Check more subtle forms of corruption, too
64                 return nil, fmt.Errorf("manifest is missing")
65         }
66         sds := make([]SizedDigest, 0, len(manifestText)/40)
67         for _, line := range bytes.Split(manifestText, []byte{'\n'}) {
68                 if len(line) == 0 {
69                         continue
70                 }
71                 tokens := bytes.Split(line, []byte{' '})
72                 if len(tokens) < 3 {
73                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
74                 }
75                 for _, token := range tokens[1:] {
76                         if !blockdigest.LocatorPattern.Match(token) {
77                                 // FIXME: ensure it's a file token
78                                 break
79                         }
80                         if bytes.HasPrefix(token, []byte("d41d8cd98f00b204e9800998ecf8427e+0")) {
81                                 // Exclude "empty block" placeholder
82                                 continue
83                         }
84                         // FIXME: shouldn't assume 32 char hash
85                         if i := bytes.IndexRune(token[33:], '+'); i >= 0 {
86                                 token = token[:33+i]
87                         }
88                         sds = append(sds, SizedDigest(string(token)))
89                 }
90         }
91         return sds, nil
92 }
93
94 type CollectionList struct {
95         Items          []Collection `json:"items"`
96         ItemsAvailable int          `json:"items_available"`
97         Offset         int          `json:"offset"`
98         Limit          int          `json:"limit"`
99 }
100
101 var (
102         blkRe = regexp.MustCompile(`^ [0-9a-f]{32}\+\d+`)
103         tokRe = regexp.MustCompile(` ?[^ ]*`)
104 )
105
106 // PortableDataHash computes the portable data hash of the given
107 // manifest.
108 func PortableDataHash(mt string) string {
109         h := md5.New()
110         size := 0
111         _ = tokRe.ReplaceAllFunc([]byte(mt), func(tok []byte) []byte {
112                 if m := blkRe.Find(tok); m != nil {
113                         // write hash+size, ignore remaining block hints
114                         tok = m
115                 }
116                 n, err := h.Write(tok)
117                 if err != nil {
118                         panic(err)
119                 }
120                 size += n
121                 return nil
122         })
123         return fmt.Sprintf("%x+%d", h.Sum(nil), size)
124 }