Skip to content

Commit

Permalink
lib: Add path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Sep 10, 2024
1 parent 01bc37d commit ff8b3f3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
74 changes: 74 additions & 0 deletions pkg/lib/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of Cockpit.
*
* Copyright (C) 2024 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <https://www.gnu.org/licenses/>.
*/

//

/* Drop all trailing slashes, but never drop the first character.
foo/// -> foo
//// -> /
*/

function drop_slashes(path : string): string {
let pos = path.length;
while (pos > 1 && path[pos - 1] == "/")
pos -= 1;
return pos == path.length ? path : path.substr(0, pos);
}

/*
foo -> .
/ -> /
foo/bar -> foo
/foo -> /
foo/// -> .
/foo/// -> /
//// -> /
//foo/// -> /
///foo///bar/// -> ///foo
*/

export function dirname(path : string): string {
const norm = drop_slashes(path);
const pos = norm.lastIndexOf("/");
if (pos < 0)
return ".";
else if (pos == 0)
return "/";
else
return drop_slashes(norm.substr(0, pos));
}

/*
foo -> foo
bar/foo/ -> foo
//bar//foo/// -> foo
*/

export function basename(path : string): string {
const norm = drop_slashes(path);
const pos = norm.lastIndexOf("/");
if (pos < 0)
return norm;
else
return norm.substr(pos + 1);
}
5 changes: 3 additions & 2 deletions pkg/sosreport/sosreport.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { EyeIcon, EyeSlashIcon } from '@patternfly/react-icons';

import { EmptyStatePanel } from "cockpit-components-empty-state.jsx";
import { ListingTable } from "cockpit-components-table.jsx";
import { basename as path_basename } from "path";

import cockpit from "cockpit";
import { superuser } from "superuser";
Expand Down Expand Up @@ -74,7 +75,7 @@ function sosLister() {
}

function parse_report_name(path) {
const basename = path.replace(/.*\//, "");
const basename = path_basename(path);
const archive_rx = /^(secured-)?sosreport-(.*)\.tar\.[^.]+(\.gpg)?$/;
const m = basename.match(archive_rx);
if (m) {
Expand Down Expand Up @@ -197,7 +198,7 @@ function sosCreate(args, setProgress, setError, setErrorDetail) {
}

function sosDownload(path) {
const basename = path.replace(/.*\//, "");
const basename = path_basename(path);
const query = window.btoa(JSON.stringify({
payload: "fsread1",
binary: "raw",
Expand Down
10 changes: 2 additions & 8 deletions pkg/storaged/btrfs/subvolume.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.
import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js";
import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";

import { dirname } from "path";

import {
PageTable, StorageCard, StorageDescription, ChildrenTable,
new_card, new_page, navigate_away_from_card, register_crossref, get_crossrefs,
Expand Down Expand Up @@ -249,14 +251,6 @@ function subvolume_delete(volume, subvol, mount_point_in_parent, card) {
});
}

function dirname(path) {
const i = path.lastIndexOf("/");
if (i < 0)
return null;
else
return path.substr(0, i);
}

export function make_btrfs_subvolume_pages(parent, volume) {
let subvols = client.uuids_btrfs_subvols[volume.data.uuid];
if (!subvols) {
Expand Down

0 comments on commit ff8b3f3

Please sign in to comment.