LCOV - code coverage report
Current view: top level - pkg/storaged/btrfs - utils.jsx Coverage Total Hit
Test: cockpit Lines: 97.6 % 42 41
Test Date: 2026-07-17 12:03:54

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2023 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5          113 : import cockpit from "cockpit";
       6              : 
       7              : import { decode_filename } from "../utils.js";
       8              : 
       9          113 : 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            9 : export function btrfs_device_usage(client, uuid, path) {
      17            9 :     const block = client.blocks[path];
      18            9 :     const device = block && block.Device;
      19            9 :     const uuid_usage = client.uuids_btrfs_usage[uuid];
      20            8 :     if (uuid_usage && device) {
      21            8 :         const usage = uuid_usage[decode_filename(device)];
      22            8 :         if (usage) {
      23            8 :             return [usage, block.Size];
      24            8 :         }
      25            8 :     }
      26            2 :     return [0, block.Size];
      27            9 : }
      28              : 
      29              : /**
      30              :  * Calculate the overall btrfs "volume" usage. UDisks only knows the usage per block.
      31              :  */
      32          112 : export function btrfs_usage(client, volume) {
      33          112 :     const block_fsys = client.blocks_fsys[volume.path];
      34          112 :     const mount_point = block_fsys && block_fsys.MountPoints[0];
      35          112 :     let use = mount_point && client.fsys_sizes.data[decode_filename(mount_point)];
      36          112 :     if (!use)
      37           13 :         use = [volume.data.used, client.uuids_btrfs_blocks[volume.data.uuid].reduce((sum, b) => sum + b.Size, 0)];
      38          112 :     return use;
      39          112 : }
      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           49 : export function parse_subvol_from_options(options) {
      55           49 :     const subvol = { };
      56           49 :     const subvolid_match = options.match(/subvolid=(?<subvolid>\d+)/);
      57           49 :     const subvol_match = options.match(/subvol=(?<subvol>[^\s,]+)/);
      58           49 :     if (subvolid_match)
      59            0 :         subvol.id = subvolid_match.groups.subvolid;
      60           49 :     if (subvol_match)
      61           49 :         subvol.pathname = subvol_match.groups.subvol;
      62              : 
      63           49 :     if (subvolid_match || subvol_match)
      64           48 :         return subvol;
      65              :     else
      66           48 :         return null;
      67           49 : }
      68              : 
      69            4 : export function validate_subvolume_name(name) {
      70            4 :     if (name === "")
      71            1 :         return _("Name cannot be empty.");
      72            4 :     if (name.length > 255)
      73            1 :         return _("Name cannot be longer than 255 characters.");
      74            4 :     if (name.includes('/'))
      75            1 :         return cockpit.format(_("Name cannot contain the character '/'."));
      76            4 : }
        

Generated by: LCOV version 2.0-1