blob: 269a56880963cebe554cdcb39a221f725aa423a2 [file] [edit]
#!/bin/bash
# Copyright 2026 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Source
### Clean up the stale CartFS directories.
## usage: fx cog-clean
##
## This is a script to clean up the Cog workspace. When the workspace is deleted,
## the files are not removed from CartFS. This script will remove the files
## from CartFS that are no longer in the workspace.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
set -e
readonly CARTFS_MOUNT_POINT="/google/cartfs/mount"
readonly GIT_CITC_PATH="$(which git-citc)"
# Collect the list of cog workspaces.
cog_workspaces=$(git-citc list)
# Collect the stale dirs from CartFS.
stale_dirs=()
if [[ ! -d "$CARTFS_MOUNT_POINT" ]]; then
fx-info "CartFS mount point not found: $CARTFS_MOUNT_POINT. Nothing to clean."
exit 0
fi
fx-info "Processing CartFS mount point: $CARTFS_MOUNT_POINT"
while IFS= read -r cartfs_dir; do
if [[ -z "$cartfs_dir" ]]; then
continue
fi
# Skip the shared jiri root directory.
if [[ "$cartfs_dir" == ".jiri_root" ]]; then
continue
fi
cartfs_abs_path="$CARTFS_MOUNT_POINT/$cartfs_dir"
cog_json_path="$cartfs_abs_path/.cog.json"
# If the .cog.json file is not present, skip it.
if [[ ! -f "$cog_json_path" ]]; then
fx-warn "Skipping unrecognized CartFS directory '$cartfs_abs_path' (no .cog.json found)"
continue
fi
workspace_name=$(fx-command-run jq -r .workspace_name "$cog_json_path")
metadata_workspace_id=$(fx-command-run jq -r .workspace_id "$cog_json_path")
if [[ "$metadata_workspace_id" == "null" ]]; then
metadata_workspace_id=""
fi
# If the workspace name is not in the list of cog workspaces, remove it from CartFS.
if ! echo "${cog_workspaces}" | grep -Fxq "${workspace_name}"; then
fx-info "Found stale directory '$cartfs_abs_path' (workspace no longer exists)"
stale_dirs+=("$cartfs_abs_path")
continue
fi
# Verify the workspace_id matches the current workspace instance.
actual_workspace_id_path="/google/cog/cloud/$(id -un)/$workspace_name/.citc/workspace_id"
actual_workspace_id=""
if [[ -f "$actual_workspace_id_path" ]]; then
actual_workspace_id=$(cat "$actual_workspace_id_path")
fi
if [[ "$metadata_workspace_id" != "$actual_workspace_id" ]]; then
fx-info "Found stale directory '$cartfs_abs_path' (workspace ID mismatch: '$metadata_workspace_id' != '$actual_workspace_id')"
stale_dirs+=("$cartfs_abs_path")
fi
done <<< "$(ls -A "$CARTFS_MOUNT_POINT")"
# Prompt the user to confirm the stale dirs to remove.
if [[ "${#stale_dirs[@]}" -gt 0 ]]; then
fx-info
fx-info "The following stale dirs will be removed:"
fx-info
for stale_dir in "${stale_dirs[@]}"; do
fx-info " $stale_dir"
done
echo
read -p "Are you sure you want to remove these stale dirs? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
fx-info "Removing stale dirs..."
for stale_dir in "${stale_dirs[@]}"; do
fx-info "Removing $stale_dir"
rm -rf "$stale_dir"
done
else
fx-info "No stale dirs removed."
fi
else
fx-info "No stale dirs found."
fi