From 41dd74650180d0930072f1715239addf56c22e89 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Fri, 27 Feb 2015 01:10:39 +0100 Subject: [PATCH] ReDirWhich, fix: owner in ReDirList --- base/ReByteBuffer.hpp | 25 +++++++-- cunit/cuReDirTools.cpp | 3 +- os/ReDirTools.cpp | 112 +++++++++++++++++++++++++++++++++++++++-- os/ReDirTools.hpp | 12 ++++- os/ReTraverser.cpp | 28 +++++------ 5 files changed, 156 insertions(+), 24 deletions(-) diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index 849c3f7..ac2fd52 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -121,11 +121,28 @@ public: return *this; } /** @brief Tests whether another instance is equal to this instance. - * @param buffer the buffer to compare - * @return true: the buffer's contents are equal + * @param data string to compare + * @param length length of data. If -1: strlen(data) + * @param ignoreCase true: case insensitive comparison + * @return true: the buffer's contents are equal */ - inline bool equals(const ReByteBuffer& buffer) const{ - bool rc = buffer.length() == m_length && _memcmp(buffer.str(), m_buffer, m_length) == 0; + inline bool equals(const char* data, size_t length, bool ignoreCase = false) const{ + if (length == (size_t) -1) + length = strlen(data); + bool rc = length == m_length + && (! ignoreCase && _memcmp(data, m_buffer, length) == 0 + || ignoreCase && _strnicmp(data, m_buffer, length) == 0); + return rc; + } + /** @brief Tests whether another instance is equal to this instance. + * @param buffer the buffer to compare + * @param ignoreCase true: case insensitive comparison + * @return true: the buffer's contents are equal + */ + inline bool equals(const ReByteBuffer& buffer, bool ignoreCase = false) const{ + bool rc = buffer.length() == m_length + && (! ignoreCase && _memcmp(buffer.str(), m_buffer, m_length) == 0 + || ignoreCase && _strnicmp(buffer.str(), m_buffer, m_length) == 0); return rc; } ReByteBuffer& fill(Byte filler = 0, int start = 0, int end = -1); diff --git a/cunit/cuReDirTools.cpp b/cunit/cuReDirTools.cpp index 6f00be6..b846d32 100644 --- a/cunit/cuReDirTools.cpp +++ b/cunit/cuReDirTools.cpp @@ -73,9 +73,8 @@ private: } void run(){ initTree(); - testToolSync(); - testList2(); if (m_testAll){ + testList2(); testToolStatistic(); testBasic(); testDirOptions(); diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 478852b..a2e92da 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -41,6 +41,7 @@ static const char* s_helpSummary[] = { "synchronize copies only modified or new files from", " from a source directory tre to a target", "touch sets the filetimes", + "which finds a file in a path list (like PATH)", NULL }; @@ -128,6 +129,16 @@ const char* s_touchExamples[] = { NULL }; +const char* s_whichUsage[] = { + ": w(hicht) [] [ ...]", + " finds a file in a path list (like PATH)", + NULL +}; +const char* s_whichExamples[] = { + "dirtool which --list=JAVA_CLASSPATH org.jdom.jar *.jdom*.jar", + "dirtool which find.exe", + NULL +}; /** * Constructor. * @@ -656,6 +667,7 @@ ReTool::ReTool(const char* usage[], const char* example[], m_reservedFirst(reservedFirst), m_reservedLast(reservedLast), m_addCurrentDirIfNoArguments(addCurrentDirIfNoArguments), + m_hasStandardArgs(true), m_traverser(NULL, this, logger), m_filter(), m_start(time(NULL)), @@ -700,8 +712,14 @@ void ReTool::run(int argc, const char** argv){ m_programArgs.init(argc, argv); if (m_programArgs.getArgCount() < m_minArguments) m_programArgs.help(i18n("too few arguments"), false, stdout); - setFilterFromProgramArgs(m_filter); + if (m_hasStandardArgs) + setFilterFromProgramArgs(m_filter); doIt(); + if (m_output != stdout){ + fclose(m_output); + m_output = stdout; + } + } catch (ReOptionException& exc) { m_programArgs.help(exc.getMessage(), false, stdout); } @@ -1902,6 +1920,83 @@ void ReDirSync::doIt(){ } } +/** + * Constructor. + * + * @param logger logger for error handling + */ +ReDirWhich::ReDirWhich(ReLogger* logger) : + ReTool(s_batchUsage, s_batchExamples, 0, 0, 0, true, logger) +{ + // no standard options: + m_programArgs.addBool("all", + i18n("all files will be found, not only the first"), + 'a', "all", false); + m_programArgs.addString("list", + i18n("a path list (separator see option 'separator'"), + 'l', "list", false, NULL); + m_programArgs.addString("separator", + i18n("separator between the path elements"), + 's', "separator", false, +#if defined __linux__ + ":" +#elif defined __WIN32__ + ";" +#endif + ); + m_programArgs.addString("variable", + i18n("variable with the path list"), + 'v', "variable", false, "PATH"); + m_hasStandardArgs = false; +} + +/** + * Creates the batch file. + */ +void ReDirWhich::doIt(){ + ReByteBuffer value, path; + bool all = false; + ReStringList items; + char sep = 0; + m_programArgs.getString("list", path); + if (path.length() == 0){ + m_programArgs.getString("variable", value); + if (getenv(value.str()) == NULL) + help("Umgebungsvariable nicht definiert: ", value.str()); + else + path = getenv(value.str()); + m_programArgs.getString("separator", value); + sep = value.at(0); + } + items.split(path.str(), sep); + struct stat info; + ReByteBuffer full; + for (int ix = 0; ix < m_programArgs.getArgCount(); ix++){ + bool found = false; + + ReByteBuffer arg(m_programArgs.getArg(ix)); + for (size_t ixItem = 0; ixItem < items.count(); ixItem++){ + full.set(items.strOf(ixItem)); + if (arg.indexOf('*') < 0){ + full.ensureLastChar(OS_SEPARATOR_CHAR); + full.append(arg); + found = stat(full.str(), &info) == 0; + if (found) + printf("%s\n", full.str()); + } else { + ReDirectory dir(full.str()); + if (dir.findFirst(arg.str(), false)){ + do { + printf("%s%c%s\n", full.str(), OS_SEPARATOR_CHAR, dir.currentFile()); + } while(dir.findNext()); + } + } + if (found && ! all) + break; + } + } +} + /** * Tests whether a abrevation of an argument is given. * @param full the full name @@ -1933,14 +2028,23 @@ void ReDirTools::help(int argc, const char* argv[]){ } else if (isArg("list", arg0)){ ReDirList list(m_logger); list.help(NULL); - } else if (isArg("help", arg0)) + } else if (isArg("help", arg0)){ printField(s_helpSummary); - else if (isArg("statistic", arg0)){ + } else if (isArg("md5", arg0)){ + ReDirMD5 md5(m_logger); + md5.help(NULL); + } else if (isArg("statistic", arg0)){ ReDirStatistic stat(m_logger); stat.help(NULL); } else if (isArg("test", arg0)){ void testAll(); testAll(); + } else if (isArg("touch", arg0)){ + ReDirTouch touch(m_logger); + touch.help(NULL); + } else if (isArg("which", arg0)){ + ReDirWhich which(m_logger); + which.help(NULL); } else printf("+++ unknown sub command: %s\n", arg0); } @@ -1987,6 +2091,8 @@ int ReDirTools::main(int argc, char* orgArgv[]){ ReDirSync(m_logger).run(argc, argv); else if (isArg("touch", arg0)) ReDirTouch(m_logger).run(argc, argv); + else if (isArg("which", arg0)) + ReDirWhich(m_logger).run(argc, argv); else if (isArg("test", arg0)){ void testAll(); testAll(); diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index da857e2..0f1a5d8 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -120,6 +120,7 @@ protected: int m_reservedFirst; int m_reservedLast; bool m_addCurrentDirIfNoArguments; + bool m_hasStandardArgs; ReTraverser m_traverser; ReDirEntryFilter_t m_filter; int64_t m_start; @@ -160,7 +161,7 @@ protected: }; /** - * List file attributes of files. + * Calculates the MD5 checksum of files. */ class ReDirMD5 : public ReTool { public: @@ -250,6 +251,15 @@ protected: ReFileTime_t m_accessed; }; +/** + * Find files in a list (like PATH). + */ +class ReDirWhich : public ReTool { +public: + ReDirWhich(ReLogger* logger); +protected: + virtual void doIt(); +}; class ReDirTools { diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 655468b..be3bf87 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -15,16 +15,16 @@ #pragma comment(lib, "advapi32.lib") #endif -enum RELOC_TRAVERSER { - LC_RIGHTS_AS_STRING_1 = LC_TRAVERSER + 1, // 50301 - LC_RIGHTS_AS_STRING_2, // 50302 - LC_RIGHTS_AS_STRING_3, // 50303 - LC_GET_PRIVILEGE_1, // 50304 - LC_GET_PRIVILEGE_2, // 50305 - LC_GET_PRIVILEGE_3, // 50306 - LC_GET_FILE_OWNER_1, // 50307 - LC_GET_FILE_OWNER_2, // 50308 -}; +enum RELOC_TRAVERSER { + LC_RIGHTS_AS_STRING_1 = LC_TRAVERSER + 1, // 50401 + LC_RIGHTS_AS_STRING_2, // 50402 + LC_RIGHTS_AS_STRING_3, // 50403 + LC_GET_PRIVILEGE_1, // 50404 + LC_GET_PRIVILEGE_2, // 50405 + LC_GET_PRIVILEGE_3, // 50406 + LC_GET_FILE_OWNER_1, // 50407 + LC_GET_FILE_OWNER_2, // 50408 +}; /** * Constructor. @@ -242,8 +242,8 @@ bool ReDirStatus_t::getFileOwner(HANDLE handle, const char* file, * @param privilege the name of the privilege, e.g. "SeBackup" * @param logger logger for error logging */ -bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){ - bool rc = false; +bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){ + bool rc = false; LUID luidPrivilege; HANDLE hAccessToken; if (! OpenProcessToken (GetCurrentProcess(), @@ -271,7 +271,7 @@ bool ReDirStatus_t::getPrivilege(const char* privilege, ReLogger* logger){ } } return rc; -} +} #endif /* __WIN32__ */ /** @@ -441,7 +441,7 @@ const char* ReDirStatus_t::rightsAsString(ReByteBuffer& buffer, bool numerical, HANDLE handle = INVALID_HANDLE_VALUE; if (! isDirectory()){ if ( (handle = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) m_logger->sayF(LOG_ERROR | CAT_FILE, LC_RIGHTS_AS_STRING_1, "CreateFile($1): $2").arg(name).arg((int) GetLastError()).end(); } else if (m_getPrivilege){ -- 2.39.5