Debian Codegraph @

pkg/storaged/filesystem/filesystem.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8 import client from "../client";
9
10 import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
11 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
12 import { useEvent } from "hooks";
13
14 import {
15 block_name, fmt_size, validate_fsys_label
16 } from "../utils.js";
17 import {
18 dialog_open, TextInput,
19 } from "../dialog.jsx";
20 import { StorageLink, StorageUsageBar, StorageSize } from "../storage-controls.jsx";
21 import { StorageCard, StorageDescription, new_card, useIsNarrow } from "../pages.jsx";
22
23 import { std_format_action } from "../block/actions.jsx";
24 import { is_mounted, MountPoint, mount_point_text, edit_mount_point } from "./utils.jsx";
25 import { mounting_dialog } from "./mounting-dialog.jsx";
26 import { check_mismounted_fsys, MismountAlert } from "./mismounting.jsx";
27
28 const _ = cockpit.gettext;
29
30 /* This page is used in a variety of cases, which can be distinguished
31 * by looking at the "backing_block" and "content_block" parameters:
32 *
33 * not-encrypted: backing_block == content_block,
34 * content_block != null
35 *
36 * encrypted and unlocked: backing_block != content_block,
37 * backing_block != null,
38 * content_block != null
39 *
40 * encrypted and locked: backing_block != null,
41 * content_block == null
42 *
43 * "backing_block" is always non-null and always refers to the block
44 * device that we want to talk about in the UI. "content_block" (when
45 * non-null) is the block that we need to use for filesystem related
46 * actions, such as mounting. It's the one with the
47 * "o.fd.UDisks2.Filesystem" interface.
48 *
49 * When "content_block" is null, then "backing_block" is a locked LUKS
50 * device, but we could figure out the fstab entry for the filesystem
51 * that's on it.
52 */
53
54 const MountPointUsageBar = ({ mount_point, block, short }) => {
55 useEvent(client.fsys_sizes, "changed");
56 const narrow = useIsNarrow();
57
58 const stats = client.fsys_sizes.data[mount_point];
59 if (stats)
60 return <StorageUsageBar stats={stats} critical={0.95} block={block_name(block)} short={short} />;
61 else if (short && !narrow)
62 return <StorageSize size={block.Size} />;
63 else
64 return fmt_size(block.Size);
65 };
66
67 export function make_filesystem_card(next, backing_block, content_block, fstab_config) {
68 const [, mount_point] = fstab_config;
69 const mismount_warning = check_mismounted_fsys(backing_block, content_block, fstab_config);
70 const mounted = content_block && is_mounted(client, content_block);
71
72 const mp_text = mount_point_text(mount_point, mounted);
73 if (mp_text == null)
74 return null;
75
76 return new_card({
77 title: content_block ? cockpit.format(_("$0 filesystem"), content_block.IdType) : _("Filesystem"),
78 location: mp_text,
79 next,
80 page_size: <MountPointUsageBar mount_point={mount_point} block={backing_block} short />,
81 has_warning: !!mismount_warning,
82 component: FilesystemCard,
83 props: { backing_block, content_block, fstab_config, mismount_warning },
84 actions: [
85 client.in_anaconda_mode() &&
86 { title: _("Edit mount point"), action: () => edit_mount_point(content_block || backing_block) },
87 content_block && mounted
88 ? { title: _("Unmount"), action: () => mounting_dialog(client, content_block, "unmount") }
89 : null,
90 !(content_block && mounted) && !client.in_anaconda_mode()
91 ? { title: _("Mount"), action: () => mounting_dialog(client, content_block || backing_block, "mount") }
92 : null,
93 std_format_action(backing_block, content_block),
94 ]
95 });
96 }
97
98 export const FilesystemCard = ({
99 card, backing_block, content_block, fstab_config, mismount_warning
100 }) => {
101 function rename_dialog() {
102 // assert(content_block)
103 const block_fsys = client.blocks_fsys[content_block.path];
104
105 dialog_open({
106 Title: _("Filesystem name"),
107 Fields: [
108 TextInput("name", _("Name"),
109 {
110 validate: name => validate_fsys_label(name, content_block.IdType),
111 value: content_block.IdLabel
112 })
113 ],
114 Action: {
115 Title: _("Save"),
116 action: function (vals) {
117 return block_fsys.SetLabel(vals.name, {});
118 }
119 }
120 });
121 }
122
123 const [, mount_point] = fstab_config;
124 const mounted = content_block && is_mounted(client, content_block);
125
126 return (
127 <StorageCard card={card}>
128 <CardBody>
129 <DescriptionList className="pf-m-horizontal-on-sm">
130 <StorageDescription title={_("Name")}
131 value={content_block?.IdLabel || "-"}
132 action={<StorageLink onClick={rename_dialog}
133 excuse={!content_block ? _("Filesystem is locked") : null}>
134 {_("edit")}
135 </StorageLink>} />
136 <StorageDescription title={_("Mount point")}>
137 <MountPoint fstab_config={fstab_config}
138 backing_block={backing_block} content_block={content_block} />
139 </StorageDescription>
140 { mounted &&
141 <StorageDescription title={_("Usage")}>
142 <MountPointUsageBar mount_point={mount_point} block={backing_block} />
143 </StorageDescription>
144 }
145 </DescriptionList>
146 </CardBody>
147 { mismount_warning &&
148 <CardBody>
149 <MismountAlert warning={mismount_warning}
150 fstab_config={fstab_config}
151 backing_block={backing_block} content_block={content_block} />
152 </CardBody>
153 }
154 </StorageCard>);
155 };