| 2956 | const CONTAINER_NAME_PREFIX: &str = "openshell-"; |
| 2957 | |
| 2958 | fn container_name_for_sandbox(sandbox: &DriverSandbox) -> String { |
| 2959 | let id_suffix = sanitize_docker_name(&sandbox.id); |
| 2960 | let name = sanitize_docker_name(&sandbox.name); |
| 2961 | if name.is_empty() { |
| 2962 | let mut base = format!("{CONTAINER_NAME_PREFIX}{id_suffix}"); |
| 2963 | // The prefix is always < MAX_CONTAINER_NAME_LEN. Truncate the id |
| 2964 | // suffix only if the sandbox id itself is pathologically long. |
| 2965 | if base.len() > MAX_CONTAINER_NAME_LEN { |
| 2966 | base.truncate(MAX_CONTAINER_NAME_LEN); |
| 2967 | } |
| 2968 | return base; |
| 2969 | } |
| 2970 | |
| 2971 | // Reserve space for the prefix and the `-<id_suffix>` tail so the id |
| 2972 | // suffix — which is what makes the name unique between sandboxes that |
| 2973 | // share a human-readable prefix — is never truncated away. |
| 2974 | let reserved = CONTAINER_NAME_PREFIX.len() + 1 + id_suffix.len(); |
| 2975 | if reserved >= MAX_CONTAINER_NAME_LEN { |
| 2976 | // Pathological sandbox id. Fall back to `<prefix><id>` and truncate. |
| 2977 | let mut base = format!("{CONTAINER_NAME_PREFIX}{id_suffix}"); |
| 2978 | base.truncate(MAX_CONTAINER_NAME_LEN); |
| 2979 | return trim_container_name_tail(base); |
| 2980 | } |
| 2981 | |
| 2982 | let name_budget = MAX_CONTAINER_NAME_LEN - reserved; |
| 2983 | let truncated_name = if name.len() > name_budget { |
| 2984 | trim_container_name_tail(name[..name_budget].to_string()) |
| 2985 | } else { |
| 2986 | name |
| 2987 | }; |
| 2988 | format!("{CONTAINER_NAME_PREFIX}{truncated_name}-{id_suffix}") |
| 2989 | } |
| 2990 | |
| 2991 | /// Docker container names may not end with `-`, `.`, or `_`. Truncation can |
| 2992 | /// leave one of those trailing, so strip them before returning. |