Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions app/client/components/__test__/restore-form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ const originalEventSource = globalThis.EventSource;

beforeEach(() => {
globalThis.EventSource = MockEventSource as unknown as typeof EventSource;
server.use(
http.get("/api/v1/system/info", () =>
HttpResponse.json({
runtime: "server",
capabilities: {
rclone: false,
sysAdmin: false,
volumeBackends: ["directory"],
repositoryBackends: ["local", "s3", "r2", "gcs", "azure", "sftp", "rest"],
},
}),
),
);
});

afterEach(() => {
Expand Down Expand Up @@ -62,7 +75,7 @@ describe("RestoreForm", () => {
repository={fromAny({ shortId: "repo-1", name: "Repo 1" })}
snapshotId="snap-1"
returnPath="/repositories/repo-1/snap-1"
queryBasePath="/mnt/project/subdir"
snapshotSourcePathPlan={{ queryBasePath: "/mnt/project/subdir", requiresCustomTarget: false }}
displayBasePath="/mnt"
/>,
);
Expand All @@ -81,7 +94,7 @@ describe("RestoreForm", () => {
});
});

test("restores the selected full path when the display root is unrelated", async () => {
test("requires a custom target when the display root is unrelated", async () => {
let restoreRequestBody: unknown;

server.use(
Expand Down Expand Up @@ -115,7 +128,7 @@ describe("RestoreForm", () => {
repository={fromAny({ shortId: "repo-1", name: "Repo 1" })}
snapshotId="snap-1"
returnPath="/repositories/repo-1/snap-1"
queryBasePath="/mnt/project"
snapshotSourcePathPlan={{ queryBasePath: "/mnt/project", requiresCustomTarget: false }}
displayBasePath="/other/root"
/>,
);
Expand Down Expand Up @@ -152,4 +165,25 @@ describe("RestoreForm", () => {
});
});
});

test("allows original restore when Windows snapshot paths match a Windows host", () => {
server.use(
http.get("/api/v1/repositories/:shortId/snapshots/:snapshotId/files", () =>
HttpResponse.json({ files: [] }),
),
);

render(
<RestoreForm
repository={fromAny({ shortId: "repo-1", name: "Repo 1" })}
snapshotId="snap-1"
returnPath="/repositories/repo-1/snap-1"
snapshotSourcePathPlan={{ queryBasePath: "/C/Users/nicolas", requiresCustomTarget: false }}
displayBasePath="C:\\Users\\Nicolas"
/>,
);

expect(screen.queryByText("Source paths do not match")).toBeNull();
expect(screen.getByRole("button", { name: "Original location" }).hasAttribute("disabled")).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,33 @@ describe("SnapshotTreeBrowser", () => {
expect(selectedKind === "dir").toBe(true);
});

test("maps Windows display roots to restic snapshot tree paths", async () => {
mockListSnapshotFiles({
files: [
{ name: "Downloads", path: "/C/Users/nicolas/Downloads", type: "dir" },
{ name: "a.txt", path: "/C/Users/nicolas/Downloads/a.txt", type: "file" },
],
});

let selectedPaths: Set<string> | undefined;

renderSnapshotTreeBrowser({
queryBasePath: "/C/Users/nicolas",
displayBasePath: "C:\\Users\\Nicolas",
withCheckboxes: true,
onSelectionChange: (paths) => {
selectedPaths = paths;
},
});

const row = await screen.findByRole("button", { name: "Downloads" });
const checkbox = within(row).getByRole("checkbox");

await userEvent.click(checkbox);

expect(selectedPaths ? Array.from(selectedPaths) : []).toEqual(["/C/Users/nicolas/Downloads"]);
});

test("uses the query base path for the initial request when display base path is broader", async () => {
const requests = mockListSnapshotFiles();

Expand Down
36 changes: 13 additions & 23 deletions app/client/components/file-browsers/snapshot-tree-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,7 @@
import { FileBrowser, type FileBrowserUiProps } from "~/client/components/file-browsers/file-browser";
import { useFileBrowser } from "~/client/hooks/use-file-browser";
import { parseError } from "~/client/lib/errors";
import { isPathWithin, normalizeAbsolutePath } from "@zerobyte/core/utils";

function createPathPrefixFns(basePath: string) {
return {
strip(path: string) {
if (basePath === "/") return path;
if (path === basePath) return "/";
if (path.startsWith(`${basePath}/`)) return path.slice(basePath.length);
return path;
},
add(displayPath: string) {
if (basePath === "/") return displayPath;
if (displayPath === "/") return basePath;
return `${basePath}${displayPath}`;
},
};
}
import { createSnapshotPathContext } from "@zerobyte/core/restic";

type SnapshotTreeBrowserProps = FileBrowserUiProps & {
repositoryId: string;
Expand All @@ -45,11 +29,11 @@

const { selectedPaths, onSelectionChange, onSingleSelectionKindChange, ...fileBrowserUiProps } = uiProps;
const queryClient = useQueryClient();
const normalizedQueryBasePath = normalizeAbsolutePath(queryBasePath);
const normalizedDisplayBasePath = normalizeAbsolutePath(displayBasePath ?? "/");
const effectiveDisplayBasePath = isPathWithin(normalizedDisplayBasePath, normalizedQueryBasePath)
? normalizedDisplayBasePath
: "/";
const snapshotPathContext = useMemo(
() => createSnapshotPathContext({ snapshotPaths: [queryBasePath], displayBasePath }),
[displayBasePath, queryBasePath],
);
const normalizedQueryBasePath = snapshotPathContext.browser.initialQueryPath();

const { data, isLoading, error } = useQuery({
...listSnapshotFilesOptions({
Expand All @@ -59,7 +43,13 @@
enabled,
});

const displayPathFns = useMemo(() => createPathPrefixFns(effectiveDisplayBasePath), [effectiveDisplayBasePath]);
const displayPathFns = useMemo(
() => ({
strip: snapshotPathContext.browser.toDisplayPath,

Check warning on line 48 in app/client/components/file-browsers/snapshot-tree-browser.tsx

View workflow job for this annotation

GitHub Actions / lint

typescript(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.
add: snapshotPathContext.browser.toSnapshotPath,

Check warning on line 49 in app/client/components/file-browsers/snapshot-tree-browser.tsx

View workflow job for this annotation

GitHub Actions / lint

typescript(unbound-method)

Avoid referencing unbound methods which may cause unintentional scoping of `this`.
}),
[snapshotPathContext],
);

const displaySelectedPaths = useMemo(() => {
if (!selectedPaths) return undefined;
Expand Down
20 changes: 11 additions & 9 deletions app/client/components/restore-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,40 @@ import { SnapshotTreeBrowser } from "~/client/components/file-browsers/snapshot-
import { RestoreProgress } from "~/client/components/restore-progress";
import { restoreSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen";
import { type RestoreCompletedEvent, useServerEvents } from "~/client/hooks/use-server-events";
import { OVERWRITE_MODES, type OverwriteMode } from "@zerobyte/core/restic";
import { isPathWithin } from "@zerobyte/core/utils";
import { createSnapshotPathContext, OVERWRITE_MODES, type OverwriteMode } from "@zerobyte/core/restic";
import type { Repository } from "~/client/lib/types";
import { handleRepositoryError } from "~/client/lib/errors";
import { useNavigate } from "@tanstack/react-router";
import { cn } from "~/client/lib/utils";

type RestoreLocation = "original" | "custom";
type SnapshotRestorePlan = { queryBasePath: string; requiresCustomTarget: boolean };

interface RestoreFormProps {
repository: Repository;
snapshotId: string;
returnPath: string;
queryBasePath?: string;
snapshotSourcePathPlan: SnapshotRestorePlan;
displayBasePath?: string;
hasNonPosixSnapshotPaths?: boolean;
}

export function RestoreForm({
repository,
snapshotId,
returnPath,
queryBasePath,
snapshotSourcePathPlan,
displayBasePath,
hasNonPosixSnapshotPaths = false,
}: RestoreFormProps) {
const navigate = useNavigate();
const { addEventListener } = useServerEvents();

const snapshotBasePath = queryBasePath ?? "/";
const hasMismatchedDisplayBasePath = displayBasePath && !isPathWithin(displayBasePath, snapshotBasePath);
const restoreRequiresCustomTarget = hasNonPosixSnapshotPaths || hasMismatchedDisplayBasePath;
const snapshotBasePath = snapshotSourcePathPlan.queryBasePath;
const snapshotPathContext = createSnapshotPathContext({
snapshotPaths: [snapshotBasePath],
displayBasePath,
});
const hasMismatchedDisplayBasePath = displayBasePath && !snapshotPathContext.browser.isDisplayBaseCompatible();
const restoreRequiresCustomTarget = snapshotSourcePathPlan.requiresCustomTarget || !!hasMismatchedDisplayBasePath;

const [restoreLocation, setRestoreLocation] = useState<RestoreLocation>(
restoreRequiresCustomTarget ? "custom" : "original",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTimeFormat } from "~/client/lib/datetime";
import { cn } from "~/client/lib/utils";
import { Link } from "@tanstack/react-router";
import { SnapshotTreeBrowser } from "~/client/components/file-browsers/snapshot-tree-browser";
import { findCommonAncestor } from "@zerobyte/core/utils";
import { createSnapshotPathContext } from "@zerobyte/core/restic";

interface Props {
snapshot: Snapshot;
Expand All @@ -30,8 +30,7 @@ export const SnapshotFileBrowser = (props: Props) => {
const { snapshot, repositoryId, backupId, displayBasePath, onDeleteSnapshot, isDeletingSnapshot } = props;
const { formatDateTime } = useTimeFormat();

const hasNonPosixSnapshotPaths = snapshot.paths.some((path) => !path.startsWith("/"));
const queryBasePath = hasNonPosixSnapshotPaths ? "/" : findCommonAncestor(snapshot.paths);
const snapshotPathContext = createSnapshotPathContext({ snapshotPaths: snapshot.paths, displayBasePath });

return (
<div className="space-y-4">
Expand Down Expand Up @@ -80,7 +79,7 @@ export const SnapshotFileBrowser = (props: Props) => {
<SnapshotTreeBrowser
repositoryId={repositoryId}
snapshotId={snapshot.short_id}
queryBasePath={queryBasePath}
queryBasePath={snapshotPathContext.browser.initialQueryPath()}
displayBasePath={displayBasePath}
{...treeProps}
/>
Expand Down
10 changes: 5 additions & 5 deletions app/client/modules/repositories/routes/restore-snapshot.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { RestoreForm } from "~/client/components/restore-form";
import type { Repository } from "~/client/lib/types";

type SnapshotRestorePlan = { queryBasePath: string; requiresCustomTarget: boolean };

type Props = {
repository: Repository;
snapshotId: string;
returnPath: string;
queryBasePath?: string;
snapshotSourcePathPlan: SnapshotRestorePlan;
displayBasePath?: string;
hasNonPosixSnapshotPaths?: boolean;
};

export function RestoreSnapshotPage(props: Props) {
const { returnPath, snapshotId, repository, queryBasePath, displayBasePath, hasNonPosixSnapshotPaths } = props;
const { returnPath, snapshotId, repository, snapshotSourcePathPlan, displayBasePath } = props;

return (
<RestoreForm
repository={repository}
snapshotId={snapshotId}
returnPath={returnPath}
queryBasePath={queryBasePath}
snapshotSourcePathPlan={snapshotSourcePathPlan}
displayBasePath={displayBasePath}
hasNonPosixSnapshotPaths={hasNonPosixSnapshotPaths}
/>
);
}
26 changes: 16 additions & 10 deletions app/routes/(dashboard)/backups/$backupId/$snapshotId.restore.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createFileRoute } from "@tanstack/react-router";
import { getBackupSchedule } from "~/client/api-client";
import { getRepositoryOptions, getSnapshotDetailsOptions } from "~/client/api-client/@tanstack/react-query.gen";
import {
getRepositoryOptions,
getSnapshotDetailsOptions,
getSnapshotRestorePlanOptions,
} from "~/client/api-client/@tanstack/react-query.gen";
import { RestoreSnapshotPage } from "~/client/modules/repositories/routes/restore-snapshot";
import { getVolumeMountPath } from "~/client/lib/volume-path";
import { findCommonAncestor } from "@zerobyte/core/utils";

export const Route = createFileRoute("/(dashboard)/backups/$backupId/$snapshotId/restore")({
component: RouteComponent,
Expand All @@ -15,7 +18,7 @@ export const Route = createFileRoute("/(dashboard)/backups/$backupId/$snapshotId
throw new Response("Not Found", { status: 404 });
}

const [snapshot, repository] = await Promise.all([
const [snapshot, repository, restorePlan] = await Promise.all([
context.queryClient.ensureQueryData({
...getSnapshotDetailsOptions({
path: { shortId: schedule.data.repository.shortId, snapshotId: params.snapshotId },
Expand All @@ -24,17 +27,21 @@ export const Route = createFileRoute("/(dashboard)/backups/$backupId/$snapshotId
context.queryClient.ensureQueryData({
...getRepositoryOptions({ path: { shortId: schedule.data.repository.shortId } }),
}),
context.queryClient.ensureQueryData({
...getSnapshotRestorePlanOptions({
path: { shortId: schedule.data.repository.shortId, snapshotId: params.snapshotId },
}),
}),
]);

const hasNonPosixSnapshotPaths = snapshot.paths.some((path) => !path.startsWith("/"));
const displayBasePath = getVolumeMountPath(schedule.data.volume);

return {
snapshot,
repository,
schedule: schedule.data,
queryBasePath: hasNonPosixSnapshotPaths ? "/" : findCommonAncestor(snapshot.paths),
displayBasePath: getVolumeMountPath(schedule.data.volume),
hasNonPosixSnapshotPaths,
displayBasePath,
snapshotSourcePathPlan: restorePlan,
};
},
head: ({ params }) => ({
Expand All @@ -58,16 +65,15 @@ export const Route = createFileRoute("/(dashboard)/backups/$backupId/$snapshotId

function RouteComponent() {
const { backupId, snapshotId } = Route.useParams();
const { repository, queryBasePath, displayBasePath, hasNonPosixSnapshotPaths } = Route.useLoaderData();
const { repository, displayBasePath, snapshotSourcePathPlan } = Route.useLoaderData();

return (
<RestoreSnapshotPage
returnPath={`/backups/${backupId}`}
snapshotId={snapshotId}
repository={repository}
queryBasePath={queryBasePath}
snapshotSourcePathPlan={snapshotSourcePathPlan}
displayBasePath={displayBasePath}
hasNonPosixSnapshotPaths={hasNonPosixSnapshotPaths}
/>
);
}
Loading
Loading