SearchExistingFileSchema does a search query looking for an existing file with entire contents of wholeRef, then does a HEAD request to verify the file still exists on the server. If so, it returns that file schema's blobref. If multiple wholeRef values are provided, any may match. This is used for
(ctx context.Context, wholeRef ...blob.Ref)
| 811 | // It returns (zero, nil) if not found. A non-nil error is only returned |
| 812 | // if there were problems searching. |
| 813 | func (c *Client) SearchExistingFileSchema(ctx context.Context, wholeRef ...blob.Ref) (blob.Ref, error) { |
| 814 | sr, err := c.SearchRoot() |
| 815 | if err != nil { |
| 816 | return blob.Ref{}, err |
| 817 | } |
| 818 | if len(wholeRef) == 0 { |
| 819 | return blob.Ref{}, nil |
| 820 | } |
| 821 | url := sr + "camli/search/files" |
| 822 | for i, ref := range wholeRef { |
| 823 | if i == 0 { |
| 824 | url += "?wholedigest=" + ref.String() |
| 825 | } else { |
| 826 | url += "&wholedigest=" + ref.String() |
| 827 | } |
| 828 | } |
| 829 | req := c.newRequest(ctx, "GET", url) |
| 830 | res, err := c.doReqGated(req) |
| 831 | if err != nil { |
| 832 | return blob.Ref{}, err |
| 833 | } |
| 834 | if res.StatusCode != 200 { |
| 835 | body, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20)) |
| 836 | res.Body.Close() |
| 837 | return blob.Ref{}, fmt.Errorf("client: got status code %d from URL %s; body %s", res.StatusCode, url, body) |
| 838 | } |
| 839 | var ress camtypes.FileSearchResponse |
| 840 | if err := httputil.DecodeJSON(res, &ress); err != nil { |
| 841 | // Check that we're not just hitting the change introduced in 2018-01-13-6e8a5930c9fee81640c6c75a9a549fec98064186 |
| 842 | mismatch, err := c.versionMismatch(ctx) |
| 843 | if err != nil { |
| 844 | log.Printf("Could not verify whether client is too recent or server is too old: %v", err) |
| 845 | } else if mismatch { |
| 846 | return blob.Ref{}, fmt.Errorf("Client is too recent for this server. Use a client built before 2018-01-13-6e8a5930c9, or upgrade the server to after that revision.") |
| 847 | } |
| 848 | return blob.Ref{}, fmt.Errorf("client: error parsing JSON from URL %s: %v", url, err) |
| 849 | } |
| 850 | if len(ress.Files) == 0 { |
| 851 | return blob.Ref{}, nil |
| 852 | } |
| 853 | for wholeRef, files := range ress.Files { |
| 854 | for _, f := range files { |
| 855 | if c.FileHasContents(ctx, f, blob.MustParse(wholeRef)) { |
| 856 | return f, nil |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | return blob.Ref{}, nil |
| 861 | } |
| 862 | |
| 863 | // versionMismatch returns true if the server was built before 2018-01-13 and |
| 864 | // the client was built at or after 2018-01-13. |
no test coverage detected