51 #include <sys/param.h> 59 #include <sys/types.h> 94 const char* lastSlash = strrchr(fName,
'/');
97 baseFileName = lastSlash + 1;
101 baseFileName = fName;
110 size_t in = path.find_last_of(
"/");
111 if (in != path.npos && path.length() > 1)
112 return path.substr(in + 1);
123 string baseFileName = fName;
125 size_t pos = baseFileName.find_last_of(
'.');
126 if (pos != string::npos) {
127 baseFileName = baseFileName.substr(0, pos);
136 const char* lastSlash = strrchr(fName,
'/');
137 string pathComponent =
".";
139 pathComponent = fName;
140 pathComponent.resize(lastSlash - fName);
142 return pathComponent;
147 fnmatch(
const std::vector<std::string>& patternVec,
148 const char*
string,
int flags)
150 for (
uint i = 0; i < patternVec.size(); ++i) {
151 const std::string& pat = patternVec[i];
174 if (stat(path, &sbuf) == 0) {
185 if (stat(path, &sbuf) == 0) {
186 return (S_ISDIR(sbuf.st_mode)
196 int srcFd = open(path, O_RDONLY);
203 while ((nRead =
read(srcFd, buf, 256)) > 0) {
204 for (
int i = 0; i < nRead; i++) {
205 if (buf[i] == c) count++;
221 static const int bufSz = 4096;
224 while ((nRead =
read(srcFd, buf, bufSz)) > 0) {
225 write(dstFd, buf, nRead);
236 va_start(srcFnmList, dst);
238 DIAG_MsgIf(0,
"FileUtil::copy: ... -> " << dst);
240 int dstFd = open(dst, O_WRONLY | O_CREAT | O_TRUNC,
241 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
243 DIAG_Throw(
"[FileUtil::copy] could not open destination file '" 244 << dst <<
"' (" << strerror(errno) <<
")");
250 while ( (srcFnm = va_arg(srcFnmList,
char*)) ) {
251 int srcFd = open(srcFnm, O_RDONLY);
252 if ((srcFd < 0) || (dstFd < 0)) {
253 errorMsg += (string(
"could not open '") + srcFnm +
"' (" 254 + strerror(errno) +
")");
265 if (!errorMsg.empty()) {
266 DIAG_Throw(
"[FileUtil::copy] could not open source files: " << errorMsg);
272 move(
const char* dst,
const char* src)
274 int ret = rename(src, dst);
276 DIAG_Throw(
"[FileUtil::move] '" << src <<
"' -> '" << dst <<
"'");
282 remove(
const char* file)
292 DIAG_Throw(
"Invalid mkdir argument: (NULL)");
295 string pathStr = dir;
296 bool isAbsPath = (dir[0] ==
'/');
309 std::vector<string> pathVec;
325 size_t endIdx = pathVec.size() - 1;
327 size_t curIdx = endIdx;
328 for ( ; curIdx >= begIdx; --curIdx) {
344 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
346 for ( ; curIdx <= endIdx; ++curIdx) {
352 int ret =
::mkdir(x.c_str(), mode);
354 DIAG_Throw(
"[FileUtil::mkdir] '" << pathStr <<
"': Could not mkdir '" 355 << x <<
"' (" << strerror(errno) <<
")");
363 std::pair<string, bool>
366 string dirnm_new = dirnm;
367 bool is_done =
false;
369 mode_t mkmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
371 int ret =
::mkdir(dirnm, mkmode);
373 if (errno == EEXIST) {
375 std::vector<string> dirnmVec;
380 string dirnm1 = string(dirnm) +
"-" + string(jobid_cstr);
381 dirnmVec.push_back(dirnm1);
387 string dirnm2 = string(dirnm) +
"-" + pid_str;
388 dirnmVec.push_back(dirnm2);
391 for (
uint i = 0; i < dirnmVec.size(); ++i) {
392 dirnm_new = dirnmVec[i];
393 DIAG_Msg(1,
"Directory '" << dirnm <<
"' already exists. Trying '" << dirnm_new <<
"'");
394 ret =
::mkdir(dirnm_new.c_str(), mkmode);
402 DIAG_Msg(1,
"Created directory: " << dirnm_new);
405 DIAG_Die(
"Could not create an alternative to directory " << dirnm);
409 DIAG_Die(
"Could not create database directory " << dirnm);
413 return make_pair(dirnm_new, is_done);
433 static char tmpfilename[MAXPATHLEN];
438 strcpy(tmpfilename,
"/tmp/hpcviewtmpXXXXXX");
439 close(mkstemp(tmpfilename));
ssize_t MONITOR_EXT_WRAP_NAME() write(int fd, const void *buf, size_t count)
static const char * getFileName(Struct::ANode *strct)
string toStr(const int x, int base)
void copy(const char *dst,...)
string dirname(const char *fName)
bool isReadable(const char *path)
bool fnmatch(const std::vector< std::string > &patternVec, const char *string, int flags)
void move(const char *dst, const char *src)
#define DIAG_MsgIf(ifexpr,...)
string join(const std::vector< string > &tokenvec, const char *delim, size_t begIdx, size_t endIdx)
bool isDir(const char *path)
void tokenize_char(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)
std::pair< string, bool > mkdirUnique(const char *dirnm)
const char * DIAG_UnexpectedInput
#define DIAG_Msg(level,...)
int countChar(const char *path, char c)
int mkdir(const char *dir)
ssize_t MONITOR_EXT_WRAP_NAME() read(int fd, void *buf, size_t count)
const char * OSUtil_jobid()
static void cpy(int srcFd, int dstFd)
string basename(const char *fName)
string rmSuffix(const char *fName)