From: Hamatoma Date: Mon, 23 Feb 2015 23:29:01 +0000 (+0100) Subject: win corrections. cunit clean X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=a4bc3a2e4b8e423ae24e16964001e777cb890605;p=crepublib win corrections. cunit clean --- diff --git a/base/ReByteBuffer.hpp b/base/ReByteBuffer.hpp index 590dbce..0769f06 100644 --- a/base/ReByteBuffer.hpp +++ b/base/ReByteBuffer.hpp @@ -170,7 +170,7 @@ public: */ inline ReByteBuffer& reduceLength(int decrement = 1){ if (decrement > 0 && m_length > 0){ - if (decrement > m_length) + if (decrement > (int) m_length) decrement = m_length; setLength(m_length - decrement); } diff --git a/base/ReTestUnit.cpp b/base/ReTestUnit.cpp index 7797d2f..b78bcb4 100644 --- a/base/ReTestUnit.cpp +++ b/base/ReTestUnit.cpp @@ -7,6 +7,9 @@ * The latest sources: https://github.com/republib */ #include "base/rebase.hpp" + +ReByteBuffer ReTestUnit::m_tempDir; + /** @brief Constructor. * * @param name the name of the test class @@ -217,31 +220,25 @@ const char* ReTestUnit::colMarker(int col){ */ void ReTestUnit::createTestDir(){ if (m_tempDir.length() == 0){ - char name[512]; if (getenv("TMP") != NULL){ - strcpy(name, getenv("TMP")); + m_tempDir = getenv("TMP"); } else if (getenv("TEMP")){ - strcpy(name, getenv("TEMP")); + m_tempDir = getenv("TEMP"); } else { - strcpy(name, "/tmp/"); + m_tempDir = "/tmp/"; } - char* ptr = name + strlen(name) - 1; - if (*ptr != OS_SEPARATOR_CHAR) - strcpy(ptr + 1, OS_SEPARATOR); - strcat(ptr, "retestunit"); - strcat(ptr, OS_SEPARATOR); + m_tempDir.ensureLastChar(OS_SEPARATOR_CHAR); + m_tempDir.append("retestunit", -1); struct stat info; #ifdef __WIN32__ #define ALLPERMS 0 #endif - if (lstat(name, &info) != 0) - _mkdir(name, ALLPERMS); - else{ - char cmd[512 + 128]; - _snprintf(cmd, sizeof cmd, "rm -Rf %s*", name); - system(cmd); - } - m_tempDir.set(name, -1); + int error = 0; + if (lstat(m_tempDir.str(), &info) != 0) + if (_mkdir(m_tempDir.str(), ALLPERMS) != 0) + error = GetLastError(); + ReDirectory::deleteTree(m_tempDir.str(), false); + m_tempDir.ensureLastChar(OS_SEPARATOR_CHAR); } } @@ -305,7 +302,10 @@ const char* ReTestUnit::testDir(){ */ void ReTestUnit::createFile(const char* filename, const char* content){ FILE* fp = fopen(filename, "w"); - if (fp != NULL && content != NULL){ + if (fp == NULL){ + int error = GetLastError(); + logF(true, "createFile(%d): %s", error, filename); + } else if (content != NULL){ fwrite(content, strlen(content), 1, fp); fclose(fp); } diff --git a/base/ReTestUnit.hpp b/base/ReTestUnit.hpp index 87df8c4..e515d8a 100644 --- a/base/ReTestUnit.hpp +++ b/base/ReTestUnit.hpp @@ -47,8 +47,9 @@ protected: int m_errorCount; ReByteBuffer m_name; ReByteBuffer m_sourceFile; - ReByteBuffer m_tempDir; ReByteBuffer m_buffer; +protected: + static ReByteBuffer m_tempDir; }; #define checkT(cond) assertTrue(cond, __LINE__) #define checkF(cond) assertFalse(cond, __LINE__) diff --git a/base/rebase.hpp b/base/rebase.hpp index d9bfbbe..61f67fc 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -61,7 +61,8 @@ typedef FILETIME ReFileTime_t; # define S_ISDIR(mode) (((mode) & _S_IFDIR) != 0) # define ALLPERMS 0 -# define _mkdir(name, mode) CreateDirectory(name, NULL) +//# define _mkdir(name, mode) (!CreateDirectory(name, NULL)) +# define _mkdir(name, mode) _mkdir(name) #endif #define RE_TESTUNIT diff --git a/cunit/cuReByteBuffer.cpp b/cunit/cuReByteBuffer.cpp index 3bb497a..1315480 100644 --- a/cunit/cuReByteBuffer.cpp +++ b/cunit/cuReByteBuffer.cpp @@ -48,10 +48,10 @@ private: void testEnsureLastChar(){ ReByteBuffer buffer("1/"); buffer.ensureLastChar('/'); - checkEqu(2, buffer.length()); + checkEqu(2U, buffer.length()); checkEqu('/', buffer.lastChar()); buffer.ensureLastChar('x'); - checkEqu(3, buffer.length()); + checkEqu(3U, buffer.length()); checkEqu('x', buffer.lastChar()); } void testLastChar(){ @@ -63,46 +63,46 @@ private: void testReduceLength(){ ReByteBuffer buffer("12345"); buffer.reduceLength(); - checkEqu(4, buffer.length()); + checkEqu(4U, buffer.length()); checkEqu("1234", buffer.str()); buffer.reduceLength(2); - checkEqu(2, buffer.length()); + checkEqu(2U, buffer.length()); checkEqu("12", buffer.str()); buffer.reduceLength(0).reduceLength(-1); - checkEqu(2, buffer.length()); + checkEqu(2U, buffer.length()); checkEqu("12", buffer.str()); buffer.reduceLength(99); - checkEqu(0, buffer.length()); + checkEqu(0U, buffer.length()); } void testRemoveLastChar(){ ReByteBuffer buffer("123"); buffer.removeLastChar('x').removeLastChar('3'); - checkEqu(2, buffer.length()); + checkEqu(2U, buffer.length()); checkEqu("12", buffer.str()); } void testAppendChar(){ ReByteBuffer buffer; buffer.appendChar('1'); - checkEqu(1, buffer.length()); + checkEqu(1U, buffer.length()); checkEqu("1", buffer.str()); buffer.appendChar('2'); - checkEqu(2, buffer.length()); + checkEqu(2U, buffer.length()); checkEqu("12", buffer.str()); buffer.appendChar('x', 1); - checkEqu(3, buffer.length()); + checkEqu(3U, buffer.length()); checkEqu("12x", buffer.str()); buffer.appendChar('y', 3); - checkEqu(6, buffer.length()); + checkEqu(6U, buffer.length()); checkEqu("12xyyy", buffer.str()); buffer.appendChar('x', 0); - checkEqu(6, buffer.length()); + checkEqu(6U, buffer.length()); checkEqu("12xyyy", buffer.str()); buffer.appendChar('x', -1); - checkEqu(6, buffer.length()); + checkEqu(6U, buffer.length()); checkEqu("12xyyy", buffer.str()); } diff --git a/cunit/cuReDirTools.cpp b/cunit/cuReDirTools.cpp index 16ffc99..6c5da85 100644 --- a/cunit/cuReDirTools.cpp +++ b/cunit/cuReDirTools.cpp @@ -1,343 +1,356 @@ -/* - * cuReTraverser.cpp - * - * License: Public domain - * Do what you want. - * No warranties and disclaimer of any damages. - * The latest sources: https://github.com/republib - */ - -#include "base/rebase.hpp" -#include "os/reos.hpp" - -static const char* s_empty[] = { NULL }; -// count of seconds from 1.1.1970 until 1.1.1980: -const int tenYear = (365 * 10 + 2) * 24 * 3600; - -class TestReDirTools : public ReTestUnit { -public: - TestReDirTools() : ReTestUnit("ReDirTools", __FILE__){ - m_base = testDir(); - ReDirectory::deleteTree(m_base.str()); - m_base.append("dirtool"); - _mkdir(m_base.str(), ALLPERMS); - m_base.append(OS_SEPARATOR, -1); - run(); - ReDirectory::deleteTree(m_base.str(), true); - } -private: - ReByteBuffer m_base; - ReByteBuffer m_buffer; -private: - const char* makeDir(const char* relPath){ - m_buffer = m_base; - m_buffer.append(relPath); - m_buffer.replaceAll("/", 1, OS_SEPARATOR, -1); - _mkdir(m_buffer.str(), ALLPERMS); - struct stat info; - if (stat(m_buffer.str(), &info) != 0){ - logF(true, "cannot create dir %s", m_buffer.str()); - } - return m_buffer.str(); - } - void makeFile(const char* relPath){ - ReByteBuffer path(m_base); - path.append(relPath); - path.replaceAll("/", 1, OS_SEPARATOR, -1); - createFile(path.str(), relPath); - struct stat info; - if (stat(path.str(), &info) != 0){ - logF(true, "cannot create file %s", path.str()); - } - } - void initTree(){ - makeFile("1.txt"); - makeDir("dir1"); - makeDir("dir2"); - makeDir("dir1/dir1_1"); - makeDir("dir1/dir1_2"); - makeDir("dir1/dir1_2/dir1_2_1"); - makeDir("dir1/cache"); - makeFile("dir1/dir1_2/dir1_2_1/x1.txt"); - makeFile("dir1/dir1_2/dir1_2_1/x2.txt"); - makeFile("dir2/2.x"); - makeFile("dir1/cache/cache.txt"); - } - void run(){ - initTree(); - - testBatch(); - - testToolStatistic(); - testBasic(); - testDirOptions(); - checkSetFilterFromProgramArgs(); - testDirStatistic(); - testCopyFile(); - testList(); - testToolSync(); - testBatch(); - } - void testList(){ - const char* argv[] = { "list", m_base.str(), NULL }; - ReDirList().run(2, argv); - } - int secOfFileTime(ReFileTime_t data){ -#if defined __linux__ - return data.tv_sec; -#elif defined __WIN32__ - return (int) ReDirStatus_t::filetimeToTime(&data); -#endif - } - void testCopyFile(){ -#if defined __linux__ - ReByteBuffer src(m_base); - src.append("dir1/dir1_2/dir1_2_1/x1.txt"); - ReByteBuffer trg(testDir()); - trg.append("copy_x1.txt"); - ReByteBuffer buffer; - buffer.ensureSize(5); - ReDirSync::copyFile(src.str(), NULL, trg.str(), buffer, - ReLogger::globalLogger()); - checkFileEqu(src.str(), trg.str()); -#else - log(false, "testCopyFile not implemented"); -#endif - } - - void checkRelDate(ReFileTime_t absTime, int relTime){ - int diffNow = int(time(NULL) - secOfFileTime(absTime)); - int diff = diffNow + relTime; - if (diff < 0) - diff = - diff; - checkT(diff < 2); - } - void testDirOptions(){ - class MyOptions : public ReDirOptions{ - public: - MyOptions() : ReDirOptions(s_empty, s_empty) {} - public: - int count() { return m_countCompoundUsage; } - const char** usage() { return m_compoundUsage; } - }; - static const char* usage1[] = { "line1", "line2", NULL }; - static const char* usage2[] = { "x1", "x2", "x3", NULL }; - MyOptions opts; - opts.initCompoundUsage(sizeof usage1 + sizeof usage2); - opts.addCompoundUsage(usage1); - opts.addCompoundUsage(usage2); - checkEqu(7, opts.count()); - checkEqu("line1", opts.usage()[0]); - checkEqu("line2", opts.usage()[1]); - checkEqu("x1", opts.usage()[2]); - checkEqu("x2", opts.usage()[3]); - checkEqu("x3", opts.usage()[4]); - - // local time: +3600 - const int DIFF = 3600; - checkEqu(tenYear + 24*60*60 - DIFF, secOfFileTime(opts.checkDate("1980.01.02"))); - checkEqu(tenYear + 24*60*60+3600-DIFF, secOfFileTime(opts.checkDate("1980.01.02/1"))); - checkEqu(tenYear + 24*60*60 + 2*3600 + 33*60 - DIFF, secOfFileTime(opts.checkDate("1980.01.02/02:33"))); - checkRelDate(opts.checkDate("now-3m"), 3*60); - checkRelDate(opts.checkDate("now-7h"), 7*60*60); - checkRelDate(opts.checkDate("now-5d"), 5*24*60*60); - - checkEqu(125ll, opts.checkSize("125")); - checkEqu(125ll, opts.checkSize("125b")); - checkEqu(3000ll, opts.checkSize("3k")); - checkEqu(3*1024ll, opts.checkSize("3K")); - checkEqu(4*1000*1000ll, opts.checkSize("4m")); - checkEqu(4*1024*1024ll, opts.checkSize("4M")); - checkEqu(5*1000*1000*1000ll, opts.checkSize("5g")); - checkEqu(5*1024*1024*1024ll, opts.checkSize("5G")); - - } - void checkSetFilterFromProgramArgs(){ - ReDirOptions opts(s_empty, s_empty); - opts.addStandardFilterOptions(); - const char* argv[] = { "x", "-y1980.01.02", "-o1980.01.03", - "-D5", "-d1", "-z1k", "-Z2M", "-p;*;-*~" - }; - ReDirEntryFilter_t filter; - opts.programArgsChangeable().init(sizeof argv / sizeof argv[0], argv); - opts.setFilterFromProgramArgs(filter); - // local time: +3600 - const int DIFF = 3600; - checkEqu(tenYear + 1*24*3600 - DIFF, secOfFileTime(filter.m_maxAge)); - checkEqu(tenYear + 2*24*3600 - DIFF, secOfFileTime(filter.m_minAge)); - checkEqu(5, (int) filter.m_maxDepth); - checkEqu(1, (int) filter.m_minDepth); - checkEqu(1000ll, filter.m_minSize); - checkEqu(2*1024*1024ll, filter.m_maxSize); - checkNN(filter.m_nodePatterns); - checkEqu(";*;-*~", filter.m_nodePatterns->patternString()); - } - void checkOneFile(const char* node, const char* parent, const ReHashList& hash){ - ReByteBuffer path, expected; - checkT(hash.get(ReByteBuffer(node), path)); - expected.set(parent, -1); - if (! expected.endsWith(OS_SEPARATOR)) - expected.append(OS_SEPARATOR); - if (! path.endsWith(expected.str(), -1)) - checkT(false); - } - void testBasic(){ - ReTraverser traverser(m_base.str()); - RePatternList patterns; - // exclude */cache/* - ReByteBuffer buffer(";*;-cache"); - patterns.set(buffer.str()); - traverser.setDirPattern(&patterns); - int level = 0; - ReDirStatus_t* entry; - ReHashList hash; - while( (entry = traverser.rawNextFile(level)) != NULL){ - hash.put(ReByteBuffer(entry->node(), -1), entry->m_path); - logF(false, "%d: %-12s %2d %s", - level, entry->node(), - int(entry->fileSize()), - entry->m_path.str()); - } - checkOneFile("x1.txt", "dir1_2_1", hash); - checkOneFile("x2.txt", "dir1_2_1", hash); - checkOneFile("dir1_2_1", "dir1_2", hash); - checkOneFile("dir1_1", "dir1", hash); - checkOneFile("dir1_2", "dir1", hash); - checkF(hash.get("cache.txt", buffer)); - } - void testDirStatistic(){ - ReDirStatistic stat; - const ReStringList& list = stat.calculate(m_base.str(), 1); - ReByteBuffer buffer; - ReByteBuffer expected; - log(false, list.join("\n", buffer).str()); - checkEqu(3u, list.count()); - // "1 t:\temp\winfried\2\retestunit\dir1\n" - buffer.set(list.strOf(0), list.strLengthOf(0)); - checkT(buffer.startsWith("1\t")); - expected.set(m_base.str(), m_base.length()).append("dir1", -1) - .append(OS_SEPARATOR); - // .append(OS_SEPARATOR, -1) - checkT(buffer.endsWith(expected.str())); - - buffer.setLength(0); - const ReStringList& list2 = stat.calculate(m_base.str(), 1, formatWithSizeFilesAndDirs); - log(false, list2.join("\n", buffer).str()); - - buffer.set(list.strOf(0), list.strLengthOf(0)); - checkT(buffer.startsWith(" 0.000074 MB 3 4\t")); - expected.set(m_base.str(), m_base.length()).append("dir1", -1) - .append(OS_SEPARATOR); - checkT(buffer.endsWith(expected.str())); - - buffer.set(list.strOf(1), list.strLengthOf(1)); - checkT(buffer.startsWith(" 0.000008 MB 1 0\t")); - expected.set(m_base.str(), m_base.length()).append("dir2", -1) - .append(OS_SEPARATOR); - checkT(buffer.endsWith(expected.str())); - - buffer.set(list.strOf(2), list.strLengthOf(2)); - checkT(buffer.startsWith(" 0.000087 MB 5 6\t")); - expected.set(m_base.str(), m_base.length()); - } - /** - * Tests a file with a given context. - * - * @param filename name of the file to test - * @param content expected content: each line must contain a '*', - * separating the start of line and the end of line - */ - void checkFile(ReByteBuffer& filename, const char* content){ - ReStringList current; - current.readFromFile(filename.str()); - ReStringList expected; - expected.split(content, '\n'); - checkEqu(expected.count(), current.count()); - ReByteBuffer line; - ReStringList cols; - for (size_t ix = 0; ix < current.count(); ix++){ - line.setLength(0).append(current.strOf(ix), -1); -#if defined __WIN32__ - line.replaceAll(OS_SEPARATOR, 1, "/", 1); -#endif - cols.split(expected.strOf(ix), '*'); - if (! line.startsWith(cols.strOf(0))) - checkEqu(cols.strOf(0), line.str()); - const char* col2 = cols.strOf(1); - if (! line.endsWith(col2)){ - checkEqu(col2, line.str()); - } - } - } - void testTouch(){ - ReByteBuffer nameCurrent; - buildFilename("current", nameCurrent); - ReByteBuffer optOutput("--output-file="); - optOutput.append(nameCurrent); - ReDirTools tools; - const char* argv[] = { "dt", "touch", "-P;*;-cache", "-tr", "-p;*.txt", - "-m2015.03.28/10:21:31", optOutput.str(), m_base.str(), NULL }; - tools.main(8, (char**) argv); - - checkFile(nameCurrent, - "2015.03.28 10:21:31 *dirtool/1.txt\n" - "2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x1.txt\n" - "2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x2.txt\n" - "=== filtered: 3 file(s) 0.000059 MByte 0 dirs(s) */sec\n" - "=== total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"); - - const char* argv2[] = { "dt", "list", "-P;*;-cache", "-tr", "-p;*.txt", "-h", - optOutput.str(), m_base.str(), NULL }; - tools.main(8, (char**) argv2); - - checkFile(nameCurrent, - " 0.000005 2015.03.28 10:21:31 *dirtool/1.txt\n" - " 0.000027 2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x1.txt\n" - " 0.000027 2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x2.txt\n" - "=== filtered: 3 file(s) 0.000059 MByte 0 dirs(s) */sec\n" - "=== total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"); - - } - void testBatch(){ - ReByteBuffer nameCurrent; - buildFilename("current", nameCurrent); - ReByteBuffer optOutput("--output-file="); - optOutput.append(nameCurrent); - ReDirTools tools; - const char* argv[] = { "dt", "batch", "-P;*;-cache", "-td", "-cmyscript", - optOutput.str(), m_base.str(), NULL }; - tools.main(7, (char**) argv); - ReByteBuffer content("#! /bin/sh*\n" - "myscript '*dirtool/dir1'\n" - "myscript '*dirtool/dir2'\n" - "myscript '*dirtool/dir1/dir1_1'\n" - "myscript '*dirtool/dir1/dir1_2'\n" - "myscript '*dirtool/dir1/cache'\n" - "myscript '*dirtool/dir1/dir1_2/dir1_2_1'\n" - "# === filtered: 0 file(s) 0.000000 MByte 6 dirs(s) */sec\n" - "# === total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"); -#if defined __WIN32__ - content.replaceAll("'", 1, "\"", 1); -#endif - checkFile(nameCurrent, content.str()); - } - void testToolStatistic(){ - ReDirTools tools; - const char* argv[] = { "dt", "stat", "-P;*;-cache", m_base.str(), "2" }; - tools.main(5, (char**) argv); - } - void testToolSync(){ - ReDirTools tools; - ReByteBuffer source(m_base); - source.append("dir1"); - ReByteBuffer target(makeDir("synctest")); - const char* argv[] = { "dt", "sync", "-P;*;-cache", "-p;*.txt", - source.str(), target.str() }; - tools.main(6, (char**) argv); - } -}; -extern void testReDirTools(void); - -void testReDirTools(void){ - TestReDirTools unit; -} +/* + * cuReTraverser.cpp + * + * License: Public domain + * Do what you want. + * No warranties and disclaimer of any damages. + * The latest sources: https://github.com/republib + */ + +#include "base/rebase.hpp" +#include "os/reos.hpp" + +static const char* s_empty[] = { NULL }; +// count of seconds from 1.1.1970 until 1.1.1980: +const int tenYear = (365 * 10 + 2) * 24 * 3600; + +class TestReDirTools : public ReTestUnit { +public: + TestReDirTools() : ReTestUnit("ReDirTools", __FILE__){ + m_base = testDir(); + ReDirectory::deleteTree(m_base.str()); + m_base.append("dirtool"); + _mkdir(m_base.str(), ALLPERMS); + m_base.append(OS_SEPARATOR, -1); + run(); + ReDirectory::deleteTree(m_base.str(), true); + } +private: + ReByteBuffer m_base; + ReByteBuffer m_buffer; +private: + const char* makeDir(const char* relPath){ + m_buffer = m_base; + m_buffer.append(relPath); + m_buffer.replaceAll("/", 1, OS_SEPARATOR, -1); + _mkdir(m_buffer.str(), ALLPERMS); + struct stat info; + if (stat(m_buffer.str(), &info) != 0){ + logF(true, "cannot create dir %s", m_buffer.str()); + } + return m_buffer.str(); + } + void makeFile(const char* relPath){ + ReByteBuffer path(m_base); + path.append(relPath); + path.replaceAll("/", 1, OS_SEPARATOR, -1); + createFile(path.str(), relPath); + struct stat info; + if (stat(path.str(), &info) != 0){ + logF(true, "cannot create file %s", path.str()); + } + } + void initTree(){ + makeFile("1.txt"); + makeDir("dir1"); + makeDir("dir2"); + makeDir("dir1/cache"); + makeDir("dir1/dir1_1"); + makeDir("dir1/dir1_2"); + makeDir("dir1/dir1_2/dir1_2_1"); + makeFile("dir1/dir1_2/dir1_2_1/x1.txt"); + makeFile("dir1/dir1_2/dir1_2_1/x2.txt"); + makeFile("dir2/2.x"); + makeFile("dir1/cache/cache.txt"); + } + void run(){ + initTree(); + + testBatch(); + + testToolStatistic(); + testBasic(); + testDirOptions(); + checkSetFilterFromProgramArgs(); + testDirStatistic(); + testCopyFile(); + testList(); + testToolSync(); + testBatch(); + } + void testList(){ + const char* argv[] = { "list", m_base.str(), NULL }; + ReDirList().run(2, argv); + } + int secOfFileTime(ReFileTime_t data){ +#if defined __linux__ + return data.tv_sec; +#elif defined __WIN32__ + return (int) ReDirStatus_t::filetimeToTime(&data); +#endif + } + void testCopyFile(){ +#if defined __linux__ + ReByteBuffer src(m_base); + src.append("dir1/dir1_2/dir1_2_1/x1.txt"); + ReByteBuffer trg(testDir()); + trg.append("copy_x1.txt"); + ReByteBuffer buffer; + buffer.ensureSize(5); + ReDirSync::copyFile(src.str(), NULL, trg.str(), buffer, + ReLogger::globalLogger()); + checkFileEqu(src.str(), trg.str()); +#else + log(false, "testCopyFile not implemented"); +#endif + } + + void checkRelDate(ReFileTime_t absTime, int relTime){ + int diffNow = int(time(NULL) - secOfFileTime(absTime)); + int diff = diffNow * relTime < 0 ? diffNow + relTime : diffNow - relTime; + if (diff < 0) + diff = - diff; + checkT(diff < 2); + } + void testDirOptions(){ + class MyOptions : public ReDirOptions{ + public: + MyOptions() : ReDirOptions(s_empty, s_empty) {} + public: + int count() { return m_countCompoundUsage; } + const char** usage() { return m_compoundUsage; } + }; + static const char* usage1[] = { "line1", "line2", NULL }; + static const char* usage2[] = { "x1", "x2", "x3", NULL }; + MyOptions opts; + opts.initCompoundUsage(sizeof usage1 + sizeof usage2); + opts.addCompoundUsage(usage1); + opts.addCompoundUsage(usage2); + checkEqu(7, opts.count()); + checkEqu("line1", opts.usage()[0]); + checkEqu("line2", opts.usage()[1]); + checkEqu("x1", opts.usage()[2]); + checkEqu("x2", opts.usage()[3]); + checkEqu("x3", opts.usage()[4]); + + // local time: +3600 + const int DIFF = 3600; + checkEqu(tenYear + 24*60*60 - DIFF, secOfFileTime(opts.checkDate("1980.01.02"))); + checkEqu(tenYear + 24*60*60+3600-DIFF, secOfFileTime(opts.checkDate("1980.01.02/1"))); + checkEqu(tenYear + 24*60*60 + 2*3600 + 33*60 - DIFF, secOfFileTime(opts.checkDate("1980.01.02/02:33"))); + checkRelDate(opts.checkDate("now-3m"), 3*60); + checkRelDate(opts.checkDate("now-7h"), 7*60*60); + checkRelDate(opts.checkDate("now-5d"), 5*24*60*60); + + checkEqu(125ll, opts.checkSize("125")); + checkEqu(125ll, opts.checkSize("125b")); + checkEqu(3000ll, opts.checkSize("3k")); + checkEqu(3*1024ll, opts.checkSize("3K")); + checkEqu(4*1000*1000ll, opts.checkSize("4m")); + checkEqu(4*1024*1024ll, opts.checkSize("4M")); + checkEqu(5*1000*1000*1000ll, opts.checkSize("5g")); + checkEqu(5*1024*1024*1024ll, opts.checkSize("5G")); + + } + void checkSetFilterFromProgramArgs(){ + ReDirOptions opts(s_empty, s_empty); + opts.addStandardFilterOptions(); + const char* argv[] = { "x", "-y1980.01.02", "-o1980.01.03", + "-D5", "-d1", "-z1k", "-Z2M", "-p;*;-*~" + }; + ReDirEntryFilter_t filter; + opts.programArgsChangeable().init(sizeof argv / sizeof argv[0], argv); + opts.setFilterFromProgramArgs(filter); + // local time: +3600 + const int DIFF = 3600; + checkEqu(tenYear + 1*24*3600 - DIFF, secOfFileTime(filter.m_maxAge)); + checkEqu(tenYear + 2*24*3600 - DIFF, secOfFileTime(filter.m_minAge)); + checkEqu(5, (int) filter.m_maxDepth); + checkEqu(1, (int) filter.m_minDepth); + checkEqu(1000ll, filter.m_minSize); + checkEqu(2*1024*1024ll, filter.m_maxSize); + checkNN(filter.m_nodePatterns); + checkEqu(";*;-*~", filter.m_nodePatterns->patternString()); + } + void checkOneFile(const char* node, const char* parent, const ReHashList& hash){ + ReByteBuffer path, expected; + checkT(hash.get(ReByteBuffer(node), path)); + expected.set(parent, -1); + if (! expected.endsWith(OS_SEPARATOR)) + expected.append(OS_SEPARATOR); + if (! path.endsWith(expected.str(), -1)) + checkT(false); + } + void testBasic(){ + ReTraverser traverser(m_base.str()); + RePatternList patterns; + // exclude */cache/* + ReByteBuffer buffer(";*;-cache"); + patterns.set(buffer.str()); + traverser.setDirPattern(&patterns); + int level = 0; + ReDirStatus_t* entry; + ReHashList hash; + while( (entry = traverser.rawNextFile(level)) != NULL){ + hash.put(ReByteBuffer(entry->node(), -1), entry->m_path); + logF(false, "%d: %-12s %2d %s", + level, entry->node(), + int(entry->fileSize()), + entry->m_path.str()); + } + checkOneFile("x1.txt", "dir1_2_1", hash); + checkOneFile("x2.txt", "dir1_2_1", hash); + checkOneFile("dir1_2_1", "dir1_2", hash); + checkOneFile("dir1_1", "dir1", hash); + checkOneFile("dir1_2", "dir1", hash); + checkF(hash.get("cache.txt", buffer)); + } + void testDirStatistic(){ + ReDirStatistic stat; + const ReStringList& list = stat.calculate(m_base.str(), 1); + ReByteBuffer buffer; + ReByteBuffer expected; + log(false, list.join("\n", buffer).str()); + checkEqu(3u, list.count()); + // "1 t:\temp\winfried\2\retestunit\dir1\n" + buffer.set(list.strOf(0), list.strLengthOf(0)); + checkT(buffer.startsWith("1\t")); + expected.set(m_base.str(), m_base.length()).append("dir1", -1) + .append(OS_SEPARATOR); + // .append(OS_SEPARATOR, -1) + checkT(buffer.endsWith(expected.str())); + + buffer.setLength(0); + const ReStringList& list2 = stat.calculate(m_base.str(), 1, formatWithSizeFilesAndDirs); + log(false, list2.join("\n", buffer).str()); + + buffer.set(list.strOf(0), list.strLengthOf(0)); + checkT(buffer.startsWith(" 0.000074 MB 3 4\t")); + expected.set(m_base.str(), m_base.length()).append("dir1", -1) + .append(OS_SEPARATOR); + checkT(buffer.endsWith(expected.str())); + + buffer.set(list.strOf(1), list.strLengthOf(1)); + checkT(buffer.startsWith(" 0.000008 MB 1 0\t")); + expected.set(m_base.str(), m_base.length()).append("dir2", -1) + .append(OS_SEPARATOR); + checkT(buffer.endsWith(expected.str())); + + buffer.set(list.strOf(2), list.strLengthOf(2)); + checkT(buffer.startsWith(" 0.000087 MB 5 6\t")); + expected.set(m_base.str(), m_base.length()); + } + /** + * Tests a file with a given context. + * + * @param filename name of the file to test + * @param content expected content: each line must contain a '*', + * separating the start of line and the end of line + */ + void checkFile(ReByteBuffer& filename, const char* content){ + ReStringList current; + current.readFromFile(filename.str()); + ReStringList expected; + expected.split(content, '\n'); + checkEqu(expected.count(), current.count()); + ReByteBuffer line; + ReStringList cols; + for (size_t ix = 0; ix < current.count(); ix++){ + line.setLength(0).append(current.strOf(ix), -1); +#if defined __WIN32__ + line.replaceAll("/", 1, OS_SEPARATOR, 1); +#endif + cols.split(expected.strOf(ix), '*'); + const char* col1 = cols.strOf(0); + if (! line.startsWith(col1)) + checkEqu(col1, line.str()); + const char* col2 = cols.strOf(1); + if (! line.endsWith(col2)){ + checkEqu(col2, line.str()); + } + } + } + void testTouch(){ + ReByteBuffer nameCurrent; + buildFilename("current", nameCurrent); + ReByteBuffer optOutput("--output-file="); + optOutput.append(nameCurrent); + ReDirTools tools; + const char* argv[] = { "dt", "touch", "-P;*;-cache", "-tr", "-p;*.txt", + "-m2015.03.28/10:21:31", optOutput.str(), m_base.str(), NULL }; + tools.main(8, (char**) argv); + + checkFile(nameCurrent, + "2015.03.28 10:21:31 *dirtool/1.txt\n" + "2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x1.txt\n" + "2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x2.txt\n" + "=== filtered: 3 file(s) 0.000059 MByte 0 dirs(s) */sec\n" + "=== total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"); + + const char* argv2[] = { "dt", "list", "-P;*;-cache", "-tr", "-p;*.txt", "-h", + optOutput.str(), m_base.str(), NULL }; + tools.main(8, (char**) argv2); + + checkFile(nameCurrent, + " 0.000005 2015.03.28 10:21:31 *dirtool/1.txt\n" + " 0.000027 2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x1.txt\n" + " 0.000027 2015.03.28 10:21:31 *dirtool/dir1/dir1_2/dir1_2_1/x2.txt\n" + "=== filtered: 3 file(s) 0.000059 MByte 0 dirs(s) */sec\n" + "=== total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"); + + } + void testBatch(){ + ReByteBuffer nameCurrent; + buildFilename("current", nameCurrent); + ReByteBuffer optOutput("--output-file="); + optOutput.append(nameCurrent); + ReDirTools tools; + const char* argv[] = { "dt", "batch", "-P;*;-cache", "-td", "-cmyscript", + optOutput.str(), m_base.str(), NULL }; + tools.main(7, (char**) argv); +#if defined __linux__ + static const char* content = "#! /bin/sh*\n" + "myscript '*dirtool/dir1'\n" + "myscript '*dirtool/dir2'\n" + "myscript '*dirtool/dir1/dir1_1'\n" + "myscript '*dirtool/dir1/dir1_2'\n" + "myscript '*dirtool/dir1/cache'\n" + "myscript '*dirtool/dir1/dir1_2/dir1_2_1'\n" + "# === filtered: 0 file(s) 0.000000 MByte 6 dirs(s) */sec\n" + "# === total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"; +#elif defined __WIN32__ + static const char* content = "rem this batch is created by*dirtool\n" + "call myscript *dirtool\\dir1\"\n" + "call myscript *dirtool\\dir2\"\n" + "call myscript *dirtool\\dir1\\cache\"\n" + "call myscript *dirtool\\dir1\\dir1_1\"\n" + "call myscript *dirtool\\dir1\\dir1_2\"\n" + "call myscript *dirtool\\dir1\\dir1_2\\dir1_2_1\"\n" + "rem === filtered: 0 file(s) 0.000000 MByte 6 dirs(s) *sec\n" + "rem === total: 4 file(s) 0.000067 MByte 6 dirs(s) * sec"; +#endif + checkFile(nameCurrent, content); + } + void testToolStatistic(){ + ReDirTools tools; + const char* argv[] = { "dt", "stat", "-P;*;-cache", m_base.str(), "2" }; + tools.main(5, (char**) argv); + } + void testToolSync(){ + ReDirTools tools; + ReByteBuffer source(m_base); + source.append("dir1"); + ReByteBuffer target(testDir()); + target.ensureLastChar(OS_SEPARATOR_CHAR); + target.append("synctest", -1); + _mkdir(target.str(), ALLPERMS); + const char* argv[] = { "dt", "sync", "-P;*;-cache", "-p;*.txt", + source.str(), target.str() }; + tools.main(6, (char**) argv); + } +}; +extern void testReDirTools(void); + +void testReDirTools(void){ + TestReDirTools unit; +} diff --git a/cunit/testall.cpp b/cunit/testall.cpp index f9ca28a..3ef40e8 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -62,7 +62,7 @@ void testOs(){ void testReTraverser(); testReTraverser(); void testReDirTools(); - testReDirTools(); + //testReDirTools(); } void testMath(){ extern void testReMD5(); diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index ceb6019..c0a33e8 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -299,7 +299,7 @@ ReFileTime_t ReDirOptions::checkDate(const char* value){ // a time distance value: char unit = 'm'; int count = 0; - bool negativeFactor = 1; + int negativeFactor = 1; bool fromNow = theValue.startsWith("now"); if (fromNow){ theValue.remove(0, 3); @@ -684,6 +684,7 @@ 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); doIt(); } catch (ReOptionException& exc) { m_programArgs.help(exc.getMessage(), false, stdout); @@ -766,7 +767,6 @@ void ReTool::processSingleFile(const char* filename){ */ void ReTool::processTree(const char* directory){ m_traverser.changeBase(directory); - setFilterFromProgramArgs(m_filter); m_traverser.setPropertiesFromFilter(&m_filter); ReDirStatus_t* entry; int level; @@ -898,8 +898,7 @@ ReDirBatch::ReDirBatch() : ReTool(s_batchUsage, s_batchExamples, 0, 0, 0, true), m_arguments(), m_script(), - m_isExe(false), - m_first() + m_isExe(false) { // standard short options: D d O o P p T t v y Z z m_programArgs.addString("first", @@ -988,7 +987,9 @@ void ReDirBatch::doIt(){ #if defined __WIN32__ m_isExe = m_programArgs.getBool("isexe"); #endif - m_programArgs.getString("first", m_first); + m_programArgs.getString("first", buffer); + if (buffer.length() > 0) + fprintf(m_output, "%s\n", buffer.str()); processFileArguments(); #if defined __linux__ static const char* prefix = "# "; @@ -1019,10 +1020,6 @@ void ReDirBatch::processFile(ReDirStatus_t* entry){ #elif defined __WIN32__ static const char* delim = "\""; #endif - if (m_first.length() > 0){ - fprintf(m_output, "%s\n", m_first.str()); - m_first.setLength(0); - } if (m_script.length() > 0){ #if defined __WIN32__ if (! m_isExe) diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index 8985ec6..4bbac2e 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -137,7 +137,6 @@ protected: ReByteBuffer m_arguments; ReByteBuffer m_script; bool m_isExe; - ReByteBuffer m_first; }; /**