42 #include <sys/types.h>
49 #include <boost/filesystem/operations.hpp>
50 #include <boost/filesystem/convenience.hpp>
51 #include <boost/filesystem/exception.hpp>
54 #include <boost/regex.hpp>
56 #include <boost/format.hpp>
59 #include "tce_config.h"
65 string(DIR_SEPARATOR);
69 using namespace boost::filesystem;
81 Path origString(fileName);
82 string file = origString.string();
83 Path DS(DIRECTORY_SEPARATOR);
84 string::size_type lastPathPos = file.rfind(
DS.string());
85 if (lastPathPos == string::npos) {
86 return CURRENT_DIRECTORY;
89 string dirPath = file.substr(0, lastPathPos);
102 if (!isPath(pathName)) {
105 Path origString(pathName);
106 string path = origString.string();
107 Path DS(DIRECTORY_SEPARATOR);
108 unsigned int index = path.find_last_of(DIRECTORY_SEPARATOR);
109 return path.substr(index + 1);
125 if (fileName ==
"") {
129 std::string destPath = directoryOfPath(fileName);
131 return !fileExists(fileName) && fileExists(destPath) &&
132 fileIsWritable(destPath);
146 const int increment = 100;
148 char* buf =
new char[bufSize];
151 char* retValue = getcwd(buf, bufSize);
154 while (retValue == NULL && errno == ERANGE) {
156 bufSize += increment;
157 buf =
new char[bufSize];
158 retValue = getcwd(buf, bufSize);
161 std::string dirName =
"";
162 if (retValue == NULL) {
164 std::string procName =
"FileSystem::currentWorkingDir";
165 std::string errorMsg =
"Current working directory cannot be read.";
175 return path.string();
186 return (chdir(path.c_str()) == 0);
198 const std::string& pattern,
199 std::vector<std::string>& filenames) {
202 glob(pattern.c_str(), 0, NULL, &globbuf);
203 for (
size_t i = 0; i < globbuf.gl_pathc; i++) {
204 filenames.push_back(globbuf.gl_pathv[i]);
218 string withoutTilde = stringWithTilde;
219 if (withoutTilde ==
"~" || withoutTilde.substr(0, 2) ==
"~/") {
220 withoutTilde.erase(0, 1);
236 string pathString = path.string();
237 Path DS(DIRECTORY_SEPARATOR);
238 return pathString.substr(0, 1) ==
239 DS.string() ? true :
false;
254 string pathString = path.string();
255 return !isAbsolutePath(pathString) && !pathString.empty();
266 return (!pathName.empty() &&
267 (isRelativePath(pathName) || isAbsolutePath(pathName)));
280 const Path path(fileName);
281 return boost::filesystem::extension(path);
292 const Path path(fileName);
293 return boost::filesystem::basename(path);
304 string absolutePath(
"");
305 if (isAbsolutePath(pathName)) {
306 absolutePath = pathName;
308 absolutePath = currentWorkingDir() + DIRECTORY_SEPARATOR + pathName;
310 Path path(absolutePath);
312 return path.string();
323 if (fileIsWritable(fileName) && fileIsReadable(fileName)) {
324 if (chmod(fileName.c_str(), S_IXUSR | S_IRUSR | S_IWUSR) != 0) {
327 }
else if (fileIsWritable(fileName)) {
328 if (chmod(fileName.c_str(), S_IXUSR | S_IWUSR) != 0) {
331 }
else if (fileIsReadable(fileName)) {
332 if (chmod(fileName.c_str(), S_IXUSR | S_IRUSR) != 0) {
336 if (chmod(fileName.c_str(), S_IXUSR) != 0) {
353 if (!isAbsolutePath(filePath) || !fileExists(filePath)) {
354 return std::time_t(-1);
357 std::time_t lastModTime;
359 lastModTime = boost::filesystem::last_write_time(filePath);
361 lastModTime = std::time_t(-1);
377 if (!isAbsolutePath(filePath) || !fileExists(filePath)) {
378 return static_cast<uintmax_t
>(-1);
383 fileSize = boost::filesystem::file_size(filePath);
385 fileSize =
static_cast<uintmax_t
>(-1);
401 std::string p = path;
402 if (!isAbsolutePath(p)) {
403 p = absolutePathOf(p);
405 Path DS(DIRECTORY_SEPARATOR);
406 Path orPath(p.substr(1));
407 string origPath = orPath.string();
408 string currentPath =
DS.string();
409 while (origPath.size() > 0) {
410 string::size_type pos = origPath.find(
DS.string());
411 if (pos == string::npos) {
412 currentPath += origPath;
415 currentPath += origPath.substr(0, pos);
416 origPath.replace(0, pos + 1,
"");
419 Path dirPath(currentPath);
420 if (!boost::filesystem::exists(dirPath)) {
421 boost::filesystem::create_directory(dirPath);
427 currentPath +=
DS.string();
442 const std::string& path,
443 const std::string& tempDirPrefix) {
444 const int RANDOM_CHARS = 10;
445 const string DS(DIRECTORY_SEPARATOR);
446 string tempDir = path +
DS + tempDirPrefix;
448 for (
int i = 0; i < RANDOM_CHARS || fileExists(tempDir); ++i) {
454 if (!createDirectory (tempDir)) {
471 string fileName = filePath.string();
473 if (fileExists(file)) {
477 if (fileIsCreatable(fileName)) {
478 FILE* f = fopen(fileName.c_str(),
"w");
494 if (fileExists(path)) {
497 boost::filesystem::remove_all(tcePath);
525 namespace fs = boost::filesystem;
526 Path sourcePath(source);
527 Path targetPath(target);
530 if (fs::exists(targetPath)) {
531 if(!fileIsDirectory(target)) {
532 fs::remove(targetPath);
534 fs::path::iterator lastIt = --(sourcePath.end());
535 targetPath /= *lastIt;
537 if(!fileIsDirectory(targetPath.string())) {
538 fs::remove(targetPath);
542 fs::copy_file(sourcePath, targetPath);
543 }
catch (boost::filesystem::filesystem_error e) {
547 "Unable to copy '%s' to '%s'") %
548 sourcePath.string() % targetPath.string()).str());
563 const std::vector<std::string>& searchPaths,
const std::string& file) {
566 if (isAbsolutePath(file)) {
567 if (fileExists(file)) {
570 string errorMsg =
"File " + file +
" not found.";
575 for (vector<string>::const_iterator iter = searchPaths.begin();
576 iter != searchPaths.end(); iter++) {
578 string pathToFile = path +
DS + file;
579 if (fileExists(pathToFile)) {
580 return absolutePathOf(pathToFile);
584 string errorMsg =
"File " + file +
" not found in any search path.";
585 errorMsg +=
"Searched paths:\n";
586 for (
unsigned int i = 0; i < searchPaths.size(); i++) {
587 errorMsg += searchPaths.at(i) +
"\n";
599 std::vector<std::string>
601 const std::string& directory,
const bool absolutePaths) {
603 std::vector<std::string> contents;
605 boost::filesystem::directory_iterator end_iter;
607 if (directory !=
"") {
608 Path path(directory);
609 for (boost::filesystem::directory_iterator iter(path);
610 iter != end_iter; iter++) {
612 contents.push_back(absolutePathOf(iter->path().string()));
614 contents.push_back(iter->path().string());
619 for (boost::filesystem::directory_iterator iter(
620 boost::filesystem::current_path());
621 iter != end_iter; iter++) {
623 contents.push_back(absolutePathOf(iter->path().string()));
625 contents.push_back(iter->path().string());
632 }
catch (
const boost::filesystem::filesystem_error& e) {
646 std::vector<std::string>
648 std::vector<std::string> subTrees;
651 directory_iterator end_itr;
653 for (directory_iterator itr(directory); itr != end_itr; ++itr) {
654 if (is_directory(*itr) && exists(*itr) &&
655 (*itr).path().string().find(
".") == string::npos) {
656 subTrees.push_back((*itr).path().string());
658 std::vector<std::string> subSubTrees =
659 directorySubTrees((*itr).path().string());
661 for (
size_t i = 0; i < subSubTrees.size(); ++i) {
662 subTrees.push_back(subSubTrees.at(i));
666 }
catch (
const boost::filesystem::filesystem_error& e) {
683 const Path& startDirectory,
684 const std::string& fileName,
687 if (!exists(startDirectory)) {
690 directory_iterator end_itr;
692 for (directory_iterator itr(startDirectory); itr != end_itr; ++itr) {
693 if (is_directory(*itr)) {
694 Path p((*itr).path().string());
695 if (findFileInDirectoryTree(p, fileName, pathFound))
698 #if BOOST_VERSION >= 103600
699 else if (itr->path().filename() == fileName)
701 else if (itr->leaf() == fileName)
704 pathFound =
Path((*itr).path().string());
725 const std::string& first,
726 const std::string& second,
727 const std::string& rootDirectory) {
729 std::string testRootDir = rootDirectory;
730 if (rootDirectory.substr(rootDirectory.size()-1) != DIRECTORY_SEPARATOR) {
731 testRootDir += DIRECTORY_SEPARATOR;
734 if (first == second) {
742 return ((testRootDir + first) == second) ? true :
false;
744 return ((testRootDir + second) == first) ? true :
false;
764 namespace fs = boost::filesystem;
766 Path basePath(baseDir);
767 Path toRelPath(toRelDir);
769 fs::path::iterator dstIt = basePath.begin();
770 fs::path::iterator POIt = toRelPath.begin();
772 fs::path::iterator dstEndIt = basePath.end();
773 fs::path::iterator POEndIt = toRelPath.end();
775 unsigned int sameCount = 0;
776 for (; dstIt != dstEndIt && POIt != POEndIt && *dstIt == *POIt;
777 ++dstIt, ++POIt, ++sameCount) {}
786 if (dstIt == dstEndIt) {
788 while (POIt != POEndIt) {
789 #if BOOST_FILESYSTEM_VERSION < 3
790 toRelDir.append(*POIt++);
792 std::string
const tmp = POIt->string();
793 toRelDir.append(tmp);
796 if (POIt != POEndIt) {
797 toRelDir.append(DIRECTORY_SEPARATOR);
803 while (POIt != POEndIt) {
804 #if BOOST_FILESYSTEM_VERSION < 3
805 temp.append(*POIt++);
807 std::string
const tmp = POIt->string();
811 if (POIt != POEndIt) {
812 temp.append(DIRECTORY_SEPARATOR);
816 unsigned int diffCount = 0;
817 for (; dstIt != dstEndIt; ++dstIt, ++diffCount) {
821 for (
unsigned int i = 0; diffCount > i; ++i) {
822 toRelDir.append(
"..");
823 toRelDir.append(DIRECTORY_SEPARATOR);
825 toRelDir.append(temp);
845 const std::vector<std::string>& searchPaths,
846 const std::string& basePath,
847 std::string& toRelPath) {
849 if (!isAbsolutePath(basePath)) {
853 for (
unsigned int i = 0; i < searchPaths.size(); ++i) {
854 string searchPath = searchPaths.at(i);
855 string relativePath = basePath;
858 if (relativeDir(searchPath, relativePath)) {
859 string fullPath = searchPath + DIRECTORY_SEPARATOR + relativePath;
862 if (relativePath ==
"") {
863 toRelPath = relativePath;
865 }
else if (isRelativePath(relativePath) && fileExists(fullPath)) {
866 if (fullPath.length() == basePath.length()) {
867 toRelPath = relativePath;
895 const std::string& sourceFile,
896 const std::string& blockStartRE,
897 const std::string& blockEndRE,
898 std::string& readBlock,
899 const bool includeMatchingLines) {
901 const int LINESIZE = 256;
904 const boost::regex reStart(blockStartRE,
905 boost::regex::perl|boost::regex::icase);
906 const boost::regex reEnd(blockEndRE,
907 boost::regex::perl|boost::regex::icase);
909 string::const_iterator begin;
910 string::const_iterator end;
912 std::ifstream ifs(sourceFile.c_str(), std::ifstream::in);
913 boost::match_results<string::const_iterator> matches;
914 bool outsideBlock =
true;
917 ifs.getline(line, LINESIZE-1);
918 stemp = string(line);
919 begin = stemp.begin();
923 if (boost::regex_search(begin, end, outsideBlock ? reStart : reEnd)) {
924 if (includeMatchingLines) {
925 readBlock.append(stemp +
"\n");
930 outsideBlock =
false;
932 }
else if (!outsideBlock) {
933 readBlock.append(stemp +
"\n");
959 const std::string& targetFile,
960 const std::string& ARStartRE,
961 const std::string& writeToFile,
962 const std::string& AREndRE,
963 const bool discardBlockBorder) {
965 const int LINESIZE = 1000;
968 const boost::regex reStart(ARStartRE,
969 boost::regex::perl|boost::regex::icase);
971 if (!AREndRE.empty()) {
972 reEnd = boost::regex(AREndRE, boost::regex::perl|boost::regex::icase);
974 boost::regex re = reStart;
976 string::const_iterator begin;
977 string::const_iterator end;
980 int blockStartPos = -1;
981 std::fstream fs(targetFile.c_str(), std::fstream::in | std::fstream::out);
982 boost::match_results<string::const_iterator> matches;
984 bool beforeBlock =
true;
985 bool afterBlock =
false;
988 fs.getline(line, LINESIZE-1);
991 if (blockStartPos != -1) {
993 fs.seekp(blockStartPos);
994 fs.seekg(blockStartPos);
1007 fs.write(writeToFile.c_str(), writeToFile.length());
1008 fs.write(endFile.c_str(), endFile.length());
1010 if (truncate(targetFile.c_str(), fs.tellp()) != 0) {
1017 stemp = string(line);
1018 begin = stemp.begin();
1022 if (!afterBlock && boost::regex_search(begin, end, re)) {
1025 beforeBlock =
false;
1027 afterBlock = AREndRE.empty() ? true : afterBlock;
1029 if (discardBlockBorder) {
1030 blockStartPos =
static_cast<int>(fs.tellg()) -
1031 static_cast<int>(fs.gcount());
1033 blockStartPos = fs.tellg();
1038 if (!discardBlockBorder) {
1039 endFile.append(stemp +
"\n");
1043 }
else if (afterBlock) {
1045 endFile.append(stemp +
"\n");
1061 std::ifstream fileStream(filepath);
1062 if (!fileStream.is_open()) {
1066 std::string dummyLine;
1067 while (std::getline(fileStream, dummyLine)) {
1087 boost::filesystem::path(path) {
1101 if (
this == &pathName) {
1105 boost::filesystem::path::assign(
1106 pathName.string().begin(), pathName.string().end());
1113 Path::operator std::string()
const {
1114 return this->string();
1121 return this->string();
1129 return this->string().c_str();
1136 return Path(boost::filesystem::operator/(path, fileOrDir));