Skip to content

Commit

Permalink
Merge pull request #1010 from aschnell/master
Browse files Browse the repository at this point in the history
- extended parser for df command
  • Loading branch information
aschnell authored Jan 30, 2025
2 parents 51fd849 + cbffd56 commit b395457
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
6 changes: 0 additions & 6 deletions storage/EtcFstab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ namespace storage
}


FstabEntry::FstabEntry()
: fs_type(FsType::UNKNOWN)
{
}


FstabEntry::FstabEntry(const string& spec, const string& mount_point, FsType fs_type)
: spec(spec), fs_type(fs_type)
{
Expand Down
7 changes: 2 additions & 5 deletions storage/EtcFstab.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,7 @@ namespace storage
{
public:

/**
* Constructor.
**/
FstabEntry();
FstabEntry() = default;

FstabEntry(const string & spec, const string& mount_point, FsType fs_type);

Expand Down Expand Up @@ -273,7 +270,7 @@ namespace storage

string spec; // including UUID=, LABEL=, PARTUUID=, ...
string mount_point; // always use set_mount_point()
FsType fs_type; // see Filesystems/Filesystem.h
FsType fs_type = FsType::UNKNOWN; // see Filesystems/Filesystem.h
MountOpts mount_opts;
int dump_pass = 0; // historic
int fsck_pass = 0;
Expand Down
18 changes: 14 additions & 4 deletions storage/SystemInfo/CmdDf.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2017-2023] SUSE LLC
* Copyright (c) [2017-2025] SUSE LLC
*
* All Rights Reserved.
*
Expand All @@ -22,10 +22,11 @@

#include "storage/Utils/SystemCmd.h"
#include "storage/Utils/StorageDefines.h"
#include "storage/Utils/StorageTmpl.h"
#include "storage/Utils/LoggerImpl.h"
#include "storage/SystemInfo/CmdDf.h"
#include "storage/Utils/ExceptionImpl.h"
#include "storage/Filesystems/FilesystemImpl.h"
#include "storage/Utils/AppUtil.h"


namespace storage
Expand All @@ -51,7 +52,15 @@ namespace storage
std::istringstream data(lines[1]);
classic(data);

data >> size >> used;
data >> size >> used >> available;

if (size < used + available)
y2war("implausible df values");

string tmp;
data >> tmp;
if (!toValue(tmp, fs_type))
y2war("unknown fs-type '" << tmp << "'");

if (data.fail())
ST_THROW(Exception("parse error"));
Expand All @@ -63,7 +72,8 @@ namespace storage
std::ostream&
operator<<(std::ostream& s, const CmdDf& cmd_df)
{
s << "path:" << cmd_df.path << " size:" << cmd_df.size << " used:" << cmd_df.used << '\n';
s << "path:" << cmd_df.path << " size:" << cmd_df.size << " used:" << cmd_df.used
<< " available:" << cmd_df.available << " fs-type:" << toString(cmd_df.fs_type) << '\n';

return s;
}
Expand Down
9 changes: 8 additions & 1 deletion storage/SystemInfo/CmdDf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 SUSE LLC
* Copyright (c) [2017-2025] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -28,6 +28,7 @@
#include <vector>

#include "storage/FreeInfo.h"
#include "storage/Filesystems/Filesystem.h"


namespace storage
Expand All @@ -44,6 +45,9 @@ namespace storage

unsigned long long get_size() const { return size; }
unsigned long long get_used() const { return used; }
unsigned long long get_available() const { return available; }

FsType get_fs_type() const { return fs_type; }

SpaceInfo get_space_info() const { return SpaceInfo(size, used); }

Expand All @@ -57,6 +61,9 @@ namespace storage

unsigned long long size = 0;
unsigned long long used = 0;
unsigned long long available = 0;

FsType fs_type = FsType::UNKNOWN;

};

Expand Down
4 changes: 2 additions & 2 deletions testsuite/SystemInfo/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ check_PROGRAMS = \
btrfs-subvolume-get-default.test btrfs-subvolume-list.test \
btrfs-subvolume-show.test btrfs-qgroup-show-60.test \
btrfs-qgroup-show-602.test btrfs-qgroup-show-62.test \
cryptsetup-status.test \
cryptsetup-bitlk-dump.test cryptsetup-luks-dump.test dasdview.test \
cryptsetup-status.test cryptsetup-bitlk-dump.test \
cryptsetup-luks-dump.test dasdview.test df.test \
dir.test dmraid.test dumpe2fs.test resize2fs.test ntfsresize.test \
dmsetup-info.test dmsetup-table.test lsattr.test lsscsi.test lvs.test \
mdadm-detail.test mdlinks.test \
Expand Down
63 changes: 63 additions & 0 deletions testsuite/SystemInfo/df.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE libstorage

#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>

#include "storage/SystemInfo/CmdDf.h"
#include "storage/Utils/Mockup.h"
#include "storage/Utils/StorageDefines.h"


using namespace std;
using namespace storage;


void
check(const vector<string>& input, const vector<string>& output)
{
Mockup::set_mode(Mockup::Mode::PLAYBACK);
Mockup::set_command(DF_BIN " --block-size=1 --output=size,used,avail,fstype /test", input);

CmdDf cmd_df("/test");

ostringstream parsed;
parsed.setf(std::ios::boolalpha);
parsed << cmd_df;

string lhs = parsed.str();
string rhs = boost::join(output, "\n") + "\n";

BOOST_CHECK_EQUAL(lhs, rhs);
}


BOOST_AUTO_TEST_CASE(parse1)
{
vector<string> input = {
" 1B-blocks Used Avail Type",
"10738466816 7864320 10183770112 btrfs"
};

vector<string> output = {
"path:/test size:10738466816 used:7864320 available:10183770112 fs-type:btrfs"
};

check(input, output);
}


BOOST_AUTO_TEST_CASE(parse2)
{
vector<string> input = {
" 1B-blocks Used Avail Type",
"983348543488 61565304832 871756464128 nfs4"
};

vector<string> output = {
"path:/test size:983348543488 used:61565304832 available:871756464128 fs-type:nfs4"
};

check(input, output);
}

0 comments on commit b395457

Please sign in to comment.