Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Add path functions #20991

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
2 changes: 2 additions & 0 deletions files.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const info = {
"base1/test-user.js",
"base1/test-websocket.js",

"lib/test-path.ts",

"kdump/test-config-client.js",

"networkmanager/test-utils.js",
Expand Down
46 changes: 46 additions & 0 deletions pkg/lib/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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/>.
*/

function drop_slashes(path : string): string {
// Drop all trailing slashes, but never drop the first character.
let pos = path.length;
while (pos > 1 && path[pos - 1] == "/")
pos -= 1;
return pos == path.length ? path : path.substr(0, pos);
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 added lines are not executed by any test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So hmm this coverage does not account for the qunit coverage right? Feature, Bug ;-)

Would be awesome to merge them but sounds tedious so back to reviewing :)

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This added line is not executed by any test.

else
return drop_slashes(norm.substr(0, pos));
}

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);
}
57 changes: 57 additions & 0 deletions pkg/lib/test-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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/>.
*/

import { dirname, basename } from "./path";
import QUnit from "qunit-tests";

QUnit.test("dirname", function (assert) {
const checks = [
["foo", "."],
["/", "/"],
["foo/bar", "foo"],
["/foo", "/"],
["foo///", "."],
["/foo///", "/"],
["////", "/"],
["//foo///", "/"],
["///foo///bar///", "///foo"],
];

assert.expect(checks.length);
for (let i = 0; i < checks.length; i++) {
assert.strictEqual(dirname(checks[i][0]), checks[i][1],
"dirname(" + checks[i][0] + ") = " + checks[i][1]);
}
});

QUnit.test("basename", function (assert) {
const checks = [
["foo", "foo"],
["bar/foo/", "foo"],
["//bar//foo///", "foo"],
];

assert.expect(checks.length);
for (let i = 0; i < checks.length; i++) {
assert.strictEqual(basename(checks[i][0]), checks[i][1],
"basename(" + checks[i][0] + ") = " + checks[i][1]);
}
});

QUnit.start();
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
12 changes: 3 additions & 9 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 Expand Up @@ -297,7 +291,7 @@ export function make_btrfs_subvolume_pages(parent, volume) {
let dn = pn;
while (true) {
dn = dirname(dn);
if (!dn) {
if (dn == "." || dn == "/") {
subvols_by_pathname[pn].parent = 5;
break;
} else if (subvols_by_pathname[dn]) {
Expand Down