From 2cbbf35a3a6dac8ab575503be0f879a4ee649f9b Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 25 Jan 2015 11:43:57 +0100 Subject: [PATCH] ReHashList::dump(), ReDirTool::copyFile() --- base/ReHashList.cpp | 16 ++++++++++ base/ReHashList.hpp | 1 + base/ReProgramArgs.cpp | 2 +- cunit/cuReTraverser.cpp | 3 +- cunit/testall.cpp | 2 +- os/ReDirTools.cpp | 65 ++++++++++++++++++++++------------------- os/ReDirTools.hpp | 5 ++-- os/ReTraverser.cpp | 13 +++++++++ os/ReTraverser.hpp | 1 + os/reos.hpp | 1 + 10 files changed, 74 insertions(+), 35 deletions(-) diff --git a/base/ReHashList.cpp b/base/ReHashList.cpp index a4eb368..c0bd295 100644 --- a/base/ReHashList.cpp +++ b/base/ReHashList.cpp @@ -61,6 +61,22 @@ void ReHashList::clear(){ m_values.clear(); } +/** + * Writes the content of the list into a stream. + * + * @param stream OUT: output stream + * @param prefix NULL or a string which is written first + */ +void ReHashList::dump(FILE* stream, const char* prefix){ + if (prefix != NULL) + fprintf(stream, "%s\n", prefix); + ReArrayPosition position; + ReByteBuffer key, value; + while(next(position, &key, &value)){ + fprintf(stream, "%s : [%3d] %s\n", key.str(), value.length(), value.str()); + } +} + /** @brief Returns the index of key. * * @param key The byte sequence of the key. diff --git a/base/ReHashList.hpp b/base/ReHashList.hpp index 2a7eea7..f00f89b 100644 --- a/base/ReHashList.hpp +++ b/base/ReHashList.hpp @@ -36,6 +36,7 @@ public: virtual ~ReHashList(); public: void clear(); + void dump(FILE* stream, const char* prefix = NULL); bool get(const Byte* key, size_t keyLength, ReByteBuffer& value) const; bool get(const ReByteBuffer& key, ReByteBuffer& value) const; bool next(ReArrayPosition& position, ReByteBuffer* key, ReByteBuffer* val) const; diff --git a/base/ReProgramArgs.cpp b/base/ReProgramArgs.cpp index 537579c..e00ff47 100644 --- a/base/ReProgramArgs.cpp +++ b/base/ReProgramArgs.cpp @@ -613,7 +613,7 @@ void ReProgramArgs::help(const char* message, bool issueLastError, } void ReProgramArgs::help(const char* message, bool issueLastError, FILE* stream) const{ - ReStringList lines(512, 1024, 2, 2); + ReStringList lines(512, 1024, 8, 2); help(message, issueLastError, lines); for(size_t ii = 0; ii < lines.count(); ii++){ fputs(lines.strOf(ii), stream); diff --git a/cunit/cuReTraverser.cpp b/cunit/cuReTraverser.cpp index 2297529..9ba2775 100644 --- a/cunit/cuReTraverser.cpp +++ b/cunit/cuReTraverser.cpp @@ -80,7 +80,8 @@ private: trg.append("copy_x1.txt"); ReByteBuffer buffer; buffer.ensureSize(5); - ReDirSync::copyFile(src.str(), 0, -1ll, trg.str(), buffer, ReLogger::globalLogger()); + ReDirSync::copyFile(src.str(), 0, 0, -1ll, trg.str(), buffer, + ReLogger::globalLogger()); checkFileEqu(src.str(), trg.str()); #else log(false, "testCopyFile not implemented"); diff --git a/cunit/testall.cpp b/cunit/testall.cpp index 45e9f01..2d9cb08 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -65,8 +65,8 @@ void testMath(){ void testAll(){ try { - testBase(); testOs(); + testBase(); testMath(); testString(); } catch (ReException e){ diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 7fe7660..caef2c7 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -1071,6 +1071,7 @@ ReDirSync::ReDirSync() : */ void ReDirSync::copyFile(ReDirStatus_t* entry, const char* target){ copyFile(entry->fullName(), entry->filetimeToTime(entry->modified()), + entry->filetimeToTime(entry->accessed()), entry->fileSize(), target, m_buffer, ReLogger::globalLogger()); } /** @@ -1078,20 +1079,25 @@ void ReDirSync::copyFile(ReDirStatus_t* entry, const char* target){ * * @param source the source file name * @param modified 0 or modification time + * @param accessed 0 or the access time * @param size -1 or filesize * @param target the name of the target file * @param buffer OUT: the reading uses this buffer
* Set the capacity to make it more efficient * @param logger NULL or the logger for error messages */ -bool ReDirSync::copyFile(const char* source, time_t modified, int64_t size, +bool ReDirSync::copyFile(const char* source, time_t modified, + time_t accessed, int64_t size, const char* target, ReByteBuffer& buffer, ReLogger* logger){ bool rc = false; #ifdef __linux__ - if (size < 0ll){ + if (size < 0ll || modified == 0){ struct stat info; - if (stat(source, &info) == 0) + if (stat(source, &info) == 0){ size = info.st_size; + modified = info.st_mtime; + accessed = info.st_atime; + } else if (logger != NULL) logger->sayF(LOG_ERROR, LC_COPY_FILE_1, i18n("could not find: $ (errno: $2)")).arg(source).arg(errno).end(); @@ -1111,37 +1117,36 @@ bool ReDirSync::copyFile(const char* source, time_t modified, int64_t size, i18n("cannot open $1 (errno: $2)")) .arg(target).arg(errno).end(); } else{ - // Reserve the space: - if (fseek(fpTarget, size, SEEK_SET) != size){ - if (logger != NULL) - logger->sayF(LOG_ERROR, LC_COPY_FILE_4, - i18n("cannot reserve space for $1 (errno: $2)")) - .arg(target).arg(errno).end(); - } else { - fseek(fpTarget, 0, SEEK_SET); - while(size > 0){ - size_t blockSize = buffer.capacity(); - if ((int) blockSize > size) - blockSize = size; - if (fread(buffer.buffer(), blockSize, 1, fpSource) != 1){ - if (logger != NULL) - logger->sayF(LOG_ERROR, LC_COPY_FILE_5, - i18n("cannot read $1 (errno: $2)")) - .arg(source).arg(errno).end(); - break; - } - if (fwrite(buffer.buffer(), 1, blockSize, fpSource) != 1){ - if (logger != NULL) - logger->sayF(LOG_ERROR, LC_COPY_FILE_6, - i18n("cannot write $1 (errno: $2)")) - .arg(target).arg(errno).end(); - break; - } - size -= blockSize; + while(size > 0){ + size_t blockSize = buffer.capacity(); + if ((int) blockSize > size) + blockSize = size; + if (fread(buffer.buffer(), blockSize, 1, fpSource) != 1){ + if (logger != NULL) + logger->sayF(LOG_ERROR, LC_COPY_FILE_5, + i18n("cannot read $1 (errno: $2)")) + .arg(source).arg(errno).end(); + break; + } + size_t written; + if ((written = fwrite(buffer.buffer(), 1, blockSize, + fpTarget)) != blockSize){ + if (logger != NULL) + logger->sayF(LOG_ERROR, LC_COPY_FILE_6, + i18n("cannot write $1 [$2] (errno: $3)")) + .arg(target).arg(written).arg(errno).end(); + break; } + size -= blockSize; } rc = size == 0ll; fclose(fpTarget); + + struct utimbuf times; + + times.actime = accessed; + times.modtime = modified; + utime(target, ×); } fclose(fpSource); } diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index f1d5afb..7d675f8 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -67,8 +67,9 @@ public: protected: void copyFile(ReDirStatus_t* entry, const char* target); public: - static bool copyFile(const char* source, time_t modified, int64_t size, - const char* target, ReByteBuffer& buffer, ReLogger* logger = NULL); + static bool copyFile(const char* source, time_t modified, time_t accessed, + int64_t size, const char* target, ReByteBuffer& buffer, + ReLogger* logger = NULL); protected: ReByteBuffer m_buffer; }; diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 5703154..c88ebfd 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -261,6 +261,19 @@ const FileTime_t* ReDirStatus_t::modified() { #endif } +/** + * Returns the last access time. + * + * @return the last access time + */ +const FileTime_t* ReDirStatus_t::accessed() { +#ifdef __linux__ + return &(getStatus()->st_atime); +#elif defined __WIN32__ + return &m_data.ftLastAccessTime; +#endif +} + time_t ReDirStatus_t::filetimeToTime(const FileTime_t* filetime){ #ifdef __linux__ return *filetime; diff --git a/os/ReTraverser.hpp b/os/ReTraverser.hpp index 68c3424..62e692d 100644 --- a/os/ReTraverser.hpp +++ b/os/ReTraverser.hpp @@ -56,6 +56,7 @@ public: bool isRegular(); FileSize_t fileSize(); const FileTime_t* modified(); + const FileTime_t* accessed(); bool isDotDir() const; const char* rightsAsString(ReByteBuffer& buffer); const char* filetimeAsString(ReByteBuffer& buffer); diff --git a/os/reos.hpp b/os/reos.hpp index cbdb7b7..696bdae 100644 --- a/os/reos.hpp +++ b/os/reos.hpp @@ -13,6 +13,7 @@ #if defined __linux__ #include "unistd.h" #include +#include #elif defined __WIN32__ #include #include "windows.h" -- 2.39.5