#include "HBANPIVPort.h"
#include "Exceptions.h"
#include "Trace.h"
#include <iostream>
#include <iomanip>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <dirent.h>
#include <libdevinfo.h>
using namespace std;
HBANPIVPort::HBANPIVPort() {
}
bool HBANPIVPort::operator==(HBANPIVPort &comp) {
return (this->getPortWWN() == comp.getPortWWN() &&
this->getNodeWWN() == comp.getNodeWWN());
}
string HBANPIVPort::lookupControllerPath(string path) {
Trace log("lookupControllerPath");
DIR *dp;
char buf[MAXPATHLEN];
char node[MAXPATHLEN];
struct dirent **dirpp, *dirp;
const char dir[] = "/dev/cfg";
ssize_t count;
uchar_t *dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
if ((dp = opendir(dir)) == NULL) {
string tmp = "Unable to open ";
tmp += dir;
tmp += "to find controller number.";
delete[] (dir_buf);
throw IOError(tmp);
}
dirp = (struct dirent *) dir_buf;
dirpp = &dirp;
while ((readdir_r(dp, dirp, dirpp)) == 0 && dirp != NULL) {
if (strcmp(dirp->d_name, ".") == 0 ||
strcmp(dirp->d_name, "..") == 0) {
continue;
}
sprintf(node, "%s/%s", dir, dirp->d_name);
if ((count = readlink(node,buf,sizeof(buf)))) {
buf[count] = '\0';
if (strstr(buf, path.c_str())) {
string cfg_path = dir;
cfg_path += "/";
cfg_path += dirp->d_name;
closedir(dp);
delete[] (dir_buf);
return (cfg_path);
}
}
}
closedir(dp);
delete[] (dir_buf);
throw InternalError("Unable to find controller path");
}