Android-cuttlefish cvd tool
url_namespace.h
Go to the documentation of this file.
1//
2// Copyright (C) 2026 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <optional>
19#include <string>
20#include <string_view>
21#include <vector>
22
23#include "absl/strings/string_view.h"
24
26
27namespace cuttlefish {
28
29// Returns the "<scheme>" of a "<scheme>://..." string, or nullopt when it
30// does not begin with one. A scheme starts with a letter and continues with
31// letters, digits, '+', '-' and '.'.
32std::optional<absl::string_view> UrlScheme(absl::string_view url);
33
34struct ParsedUrl {
35 std::string authority; // bucket or host
36 std::string path; // no leading '/', ends with '/' in directory form
37 std::string query; // without the '?'
38
39 // Returns whether the URL names a prefix rather than a single object. A URL
40 // naming just the bucket or the host has an empty path and names a prefix.
41 bool IsDirectoryForm() const { return path.empty() || path.ends_with('/'); }
42};
43
44Result<ParsedUrl> ParseUrl(absl::string_view url);
45
46// Returns the product named by a "<product>-img-<id>.zip" or
47// "<product>-img.zip" artifact, or nullopt when the name does not follow the
48// Android Build convention.
49std::optional<std::string> DeriveProduct(absl::string_view basename);
50
51// Returns true when the object form names one archive, so that `{selector}`
52// names a member of it rather than a second artifact of the build.
53bool IsArchiveMember(const std::optional<std::string>& object,
54 const std::optional<std::string>& filepath,
55 const std::string& artifact_name);
56
57// The kinds of zip archive a fetch resolves by name.
58enum class BuildZipKind {
59 kImages,
62};
63
64// Returns how `kind` is spelled inside an artifact name, as in the "img" of
65// "<product>-img-<id>.zip".
66std::string_view format_as(BuildZipKind kind);
67
68// Returns the name of the `kind` zip of a URL build that holds `object` and
69// nothing else.
70Result<std::string> ResolveUrlZipName(absl::string_view object,
71 BuildZipKind kind);
72
73// Returns the name of the `kind` zip of a URL build whose complete object
74// listing is `names`. A name states its kind either as Android Build's
75// "-<kind>-" infix or as a "-<kind>.zip" suffix. `{selector}` never names a
76// zip, so it is not an input.
77Result<std::string> ResolveUrlZipName(const std::vector<std::string>& names,
78 BuildZipKind kind);
79
80} // namespace cuttlefish
Definition: alloc_driver.h:21
Result< std::string > ResolveUrlZipName(absl::string_view object, BuildZipKind kind)
Definition: url_namespace.cpp:118
std::optional< std::string > DeriveProduct(absl::string_view basename)
Definition: url_namespace.cpp:93
BuildZipKind
Definition: url_namespace.h:58
Result< ParsedUrl > ParseUrl(absl::string_view url)
Definition: url_namespace.cpp:67
std::optional< absl::string_view > UrlScheme(absl::string_view url)
Definition: url_namespace.cpp:51
bool IsArchiveMember(const std::optional< std::string > &object, const std::optional< std::string > &filepath, const std::string &artifact_name)
Definition: url_namespace.cpp:133
std::string_view format_as(DeviceType device_type)
Definition: device_type.cpp:77
Definition: url_namespace.h:34
std::string query
Definition: url_namespace.h:37
std::string authority
Definition: url_namespace.h:35
bool IsDirectoryForm() const
Definition: url_namespace.h:41
std::string path
Definition: url_namespace.h:36