Skip to content

Commit

Permalink
code clean-up
Browse files Browse the repository at this point in the history
get rid of the basename() mess
  • Loading branch information
lyonel committed Jan 14, 2021
1 parent d3c66a6 commit 0140f7f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/core/hw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ void hwNode::setLogicalName(const string & name)
This->logicalnames.push_back("/dev/" + n);
}
else
This->logicalnames.push_back((n[0]=='/')?n:basename(n.c_str()));
This->logicalnames.push_back((n[0]=='/')?n:shortname(n));

if(This->dev == "")
This->dev = get_devid(n);
Expand Down
10 changes: 10 additions & 0 deletions src/core/osutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ string dirname(const string & path)
return result;
}

string shortname(const string & path)
{
size_t len = path.length();
char *buffer = new char[len + 1];
path.copy(buffer, len);
buffer[len] = '\0';
string result = basename(buffer);
delete[] buffer;
return result;
}

string spaces(unsigned int count, const string & space)
{
Expand Down
1 change: 1 addition & 0 deletions src/core/osutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bool samefile(const std::string & path1, const std::string & path2);
std::string readlink(const std::string & path);
std::string realpath(const std::string & path);
std::string dirname(const std::string & path);
std::string shortname(const std::string & path);
bool loadfile(const std::string & file, std::vector < std::string > &lines);

size_t splitlines(const std::string & s,
Expand Down
5 changes: 2 additions & 3 deletions src/core/pci.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -1172,9 +1171,9 @@ bool scan_pci(hwNode & n)
string drivername = readlink(string(devices[i]->d_name)+"/driver");
string modulename = readlink(string(devices[i]->d_name)+"/driver/module");

device->setConfig("driver", basename(const_cast<char *>(drivername.c_str())));
device->setConfig("driver", shortname(drivername));
if(exists(modulename))
device->setConfig("module", basename(const_cast<char *>(modulename.c_str())));
device->setConfig("module", shortname(modulename));

if(exists(string(devices[i]->d_name)+"/rom"))
{
Expand Down
21 changes: 10 additions & 11 deletions src/core/sysfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <libgen.h>


__ID("@(#) $Id$");
Expand Down Expand Up @@ -104,7 +103,7 @@ static string sysfs_getbustype(const string & path)
{
string devname =
string(fs.path + "/bus/") + string(namelist[i]->d_name) +
"/devices/" + basename(const_cast<char*>(path.c_str()));
"/devices/" + shortname(path);

if (samefile(devname, path))
{
Expand Down Expand Up @@ -152,7 +151,7 @@ static string sysfstobusinfo(const string & path)

if (bustype == "usb")
{
string name = basename(const_cast<char*>(path.c_str()));
string name = shortname(path);
if (matches(name, "^[0-9]+-[0-9]+(\\.[0-9]+)*:[0-9]+\\.[0-9]+$"))
{
size_t colon = name.rfind(":");
Expand All @@ -163,18 +162,18 @@ static string sysfstobusinfo(const string & path)

if (bustype == "virtio")
{
string name = basename(const_cast<char*>(path.c_str()));
string name = shortname(path);
if (name.compare(0, 6, "virtio") == 0)
return "virtio@" + name.substr(6);
else
return "virtio@" + name;
}

if (bustype == "vio")
return string("vio@") + basename(const_cast<char*>(path.c_str()));
return string("vio@") + shortname(path);

if (bustype == "ccw")
return string("ccw@") + basename(const_cast<char*>(path.c_str()));
return string("ccw@") + shortname(path);

if (bustype == "ccwgroup")
{
Expand Down Expand Up @@ -252,7 +251,7 @@ string entry::driver() const
string driverlink = This->devpath + "/driver";
if (!exists(driverlink))
return "";
return basename(const_cast<char*>(readlink(driverlink).c_str()));
return shortname(readlink(driverlink));
}


Expand Down Expand Up @@ -340,7 +339,7 @@ string entry::name_in_class(const string & classname) const

string entry::name() const
{
return basename(const_cast<char*>(This->devpath.c_str()));
return shortname(This->devpath);
}


Expand All @@ -352,17 +351,17 @@ entry entry::parent() const

string entry::classname() const
{
return basename(const_cast<char*>(dirname(This->devpath).c_str()));
return shortname(dirname(This->devpath));
}

string entry::subsystem() const
{
return basename(const_cast<char*>(realpath(This->devpath+"/subsystem").c_str()));
return shortname(realpath(This->devpath+"/subsystem"));
}

bool entry::isvirtual() const
{
return string(basename(const_cast<char*>(dirname(dirname(This->devpath)).c_str()))) == "virtual";
return shortname(dirname(dirname(This->devpath))) == "virtual";
}

string entry::string_attr(const string & name, const string & def) const
Expand Down

0 comments on commit 0140f7f

Please sign in to comment.