Skip to content

Commit

Permalink
[XrdPfc] xrdpfc_print add indent opt and cleanup
Browse files Browse the repository at this point in the history
- add -indent option for JSON output

- remove redundant plain print of filename in JSON output

- improve commandline option parsing

- fix manpage and replace -- with - for long options
  • Loading branch information
osschar authored and gganis committed Nov 23, 2021
1 parent b59a61b commit 2e6a8eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
59 changes: 33 additions & 26 deletions src/XrdPfc/XrdPfcPrint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

using namespace XrdPfc;

Print::Print(XrdOss* oss, bool v, bool j, const char* path) : m_oss(oss), m_verbose(v), m_json(j), m_ossUser("nobody")
Print::Print(XrdOss* oss, bool v, bool j, int i, const char* path) :
m_oss(oss), m_verbose(v), m_json(j), m_indent(i), m_ossUser("nobody")
{
if (isInfoFile(path))
{
Expand All @@ -44,7 +45,7 @@ Print::Print(XrdOss* oss, bool v, bool j, const char* path) : m_oss(oss), m_verb
XrdOssDF* dh = m_oss->newDir(m_ossUser);
if ( dh->Opendir(path, m_env) >= 0 )
{
printDir(dh, path, m_json);
printDir(dh, path);
}
delete dh;
}
Expand All @@ -54,7 +55,7 @@ bool Print::isInfoFile(const char* path)
{
if (strncmp(&path[strlen(path)-6], ".cinfo", 6))
{
printf("%s is not cinfo file.\n\n", path);
// printf("%s is not cinfo file.\n\n", path);
return false;
}
return true;
Expand All @@ -63,8 +64,6 @@ bool Print::isInfoFile(const char* path)

void Print::printFileJson(const std::string& path)
{
printf("FILE: %s\n", path.c_str());

XrdOssDF* fh = m_oss->newFile(m_ossUser);
fh->Open((path).c_str(),O_RDONLY, 0600, m_env);

Expand Down Expand Up @@ -149,7 +148,7 @@ void Print::printFileJson(const std::string& path)
}
jobj["accesses"] = acc_arr;

std::cout << jobj.dump() << "\n";
std::cout << jobj.dump(m_indent) << "\n";

delete fh;
}
Expand Down Expand Up @@ -241,7 +240,7 @@ void Print::printFile(const std::string& path)
delete fh;
}

void Print::printDir(XrdOssDF* iOssDF, const std::string& path, bool m_json)
void Print::printDir(XrdOssDF* iOssDF, const std::string& path)
{
// printf("---------> print dir %s \n", path.c_str());

Expand All @@ -261,19 +260,22 @@ void Print::printDir(XrdOssDF* iOssDF, const std::string& path, bool m_json)
std::string np = path + "/" + std::string(&buff[0]);
if (isInfoFile(buff))
{
if (first) first = false;
else printf("\n");
if (m_json) printFileJson(np);
else printFile(np);
if (m_json) {
printFileJson(np);
} else {
if (first) first = false;
else printf("\n");
printFile(np);
}
}
else
{
XrdOssDF* dh = m_oss->newDir(m_ossUser);
if (dh->Opendir(np.c_str(), m_env) >= 0)
{
printDir(dh, np, m_json);
printDir(dh, np);
}
delete dh; dh = 0;
delete dh;
}
}
}
Expand All @@ -284,9 +286,10 @@ void Print::printDir(XrdOssDF* iOssDF, const std::string& path, bool m_json)

int main(int argc, char *argv[])
{
static const char* usage = "Usage: pfc_print [-c config_file] [-v] [-j] path\n\n";
static const char* usage = "Usage: pfc_print [-h] [-c config_file] [-v] [-j] [-i indent] path\n\n";
bool verbose = false;
bool json = false;
int indent = -1;
const char* cfgn = 0;

XrdOucEnv myEnv;
Expand All @@ -296,37 +299,41 @@ int main(int argc, char *argv[])

XrdOucStream Config(&err, getenv("XRDINSTANCE"), &myEnv, "=====> ");
XrdOucArgs Spec(&err, "xrdpfc_print: ", "",
"help", 1, "h",
"verbose", 1, "v",
"config", 1, "c",
"config", 1, "c:",
"json", 1, "j",
"indent", 1, "i:",
(const char *) 0);

Spec.Set(argc-1, &argv[1]);
char theOpt;

while ((theOpt = Spec.getopt()) != (char)-1)
{
// printf("GETOPT %c -- arg=%s\n", theOpt, Spec.argval);
switch (theOpt)
{
case 'c':
{
cfgn = Spec.getarg();
case 'c': {
cfgn = Spec.argval;
int fd = open(cfgn, O_RDONLY, 0);
Config.Attach(fd);
break;
}
case 'v':
{
case 'v': {
verbose = true;
break;
}
case 'j':
{
case 'j': {
json = true;
break;
}
default:
{
case 'i': {
indent = std::stoi(Spec.argval);
break;
}
case 'h':
default: {
printf("%s", usage);
exit(1);
}
Expand Down Expand Up @@ -373,13 +380,13 @@ int main(int argc, char *argv[])
std::string tmp = Config.GetWord();
tmp += &path[6];
// printf("Absolute path %s \n", tmp.c_str());
XrdPfc::Print p(oss, verbose, json, tmp.c_str());
XrdPfc::Print p(oss, verbose, json, indent, tmp.c_str());
}
}
}
else
{
XrdPfc::Print p(oss, verbose, json, path);
XrdPfc::Print p(oss, verbose, json, indent, path);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/XrdPfc/XrdPfcPrint.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public:
//------------------------------------------------------------------------
//! Constructor.
//------------------------------------------------------------------------
Print(XrdOss* oss, bool v, bool j, const char* path);
Print(XrdOss* oss, bool v, bool j, int i, const char* path);

private:
XrdOss* m_oss; //! file system
XrdOucEnv m_env; //! env used by file system
bool m_verbose; //! print each block
bool m_json; //! print in json format
int m_indent; //! indent for json dump
const char* m_ossUser; //! file system user

//---------------------------------------------------------------------
Expand All @@ -57,7 +58,7 @@ private:
//---------------------------------------------------------------------
//! Print information in meta-data file recursivly
//---------------------------------------------------------------------
void printDir(XrdOssDF* iOssDF, const std::string& path, bool m_json);
void printDir(XrdOssDF* iOssDF, const std::string& path);
};
}

Expand Down

0 comments on commit 2e6a8eb

Please sign in to comment.