Debian Codegraph @

pkg/storaged/btrfs/utils.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 import cockpit from "cockpit";
6
7 import { decode_filename } from "../utils.js";
8
9 const _ = cockpit.gettext;
10
11 /*
12 * Calculate the usage based on the data from `btrfs filesystem show` which has
13 * been made available to client.uuids_btrfs_usage. The size/usage is provided
14 * per block device.
15 */
16 export function btrfs_device_usage(client, uuid, path) {
17 const block = client.blocks[path];
18 const device = block && block.Device;
19 const uuid_usage = client.uuids_btrfs_usage[uuid];
20 if (uuid_usage && device) {
21 const usage = uuid_usage[decode_filename(device)];
22 if (usage) {
23 return [usage, block.Size];
24 }
25 }
26 return [0, block.Size];
27 }
28
29 /**
30 * Calculate the overall btrfs "volume" usage. UDisks only knows the usage per block.
31 */
32 export function btrfs_usage(client, volume) {
33 const block_fsys = client.blocks_fsys[volume.path];
34 const mount_point = block_fsys && block_fsys.MountPoints[0];
35 let use = mount_point && client.fsys_sizes.data[decode_filename(mount_point)];
36 if (!use)
37 use = [volume.data.used, client.uuids_btrfs_blocks[volume.data.uuid].reduce((sum, b) => sum + b.Size, 0)];
38 return use;
39 }
40
41 /**
42 * Is the btrfs volume mounted anywhere
43 */
44 export function btrfs_is_volume_mounted(client, block_devices) {
45 for (const block_device of block_devices) {
46 const block_fs = client.blocks_fsys[block_device.path];
47 if (block_fs && block_fs.MountPoints.length > 0) {
48 return true;
49 }
50 }
51 return false;
52 }
53
54 export function parse_subvol_from_options(options) {
55 const subvol = { };
56 const subvolid_match = options.match(/subvolid=(?<subvolid>\d+)/);
57 const subvol_match = options.match(/subvol=(?<subvol>[^\s,]+)/);
58 if (subvolid_match)
59 subvol.id = subvolid_match.groups.subvolid;
60 if (subvol_match)
61 subvol.pathname = subvol_match.groups.subvol;
62
63 if (subvolid_match || subvol_match)
64 return subvol;
65 else
66 return null;
67 }
68
69 export function validate_subvolume_name(name) {
70 if (name === "")
71 return _("Name cannot be empty.");
72 if (name.length > 255)
73 return _("Name cannot be longer than 255 characters.");
74 if (name.includes('/'))
75 return cockpit.format(_("Name cannot contain the character '/'."));
76 }