]> gitweb.hamatoma.de Git - crepublib/commitdiff
ReHashList::dump(), ReDirTool::copyFile()
authorhama <hama@siduction.net>
Sun, 25 Jan 2015 10:43:57 +0000 (11:43 +0100)
committerhama <hama@siduction.net>
Sun, 25 Jan 2015 10:43:57 +0000 (11:43 +0100)
base/ReHashList.cpp
base/ReHashList.hpp
base/ReProgramArgs.cpp
cunit/cuReTraverser.cpp
cunit/testall.cpp
os/ReDirTools.cpp
os/ReDirTools.hpp
os/ReTraverser.cpp
os/ReTraverser.hpp
os/reos.hpp

index a4eb368623a8379f2db1a1afc2dca5c5222b5d37..c0bd2958d9edb8ba7764e7cd6879b56c532859a1 100644 (file)
@@ -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.
index 2a7eea732e8e41fc5d9fc88a4908b4662bb13275..f00f89bcd797a6b7052933e9c7ddb06ab7fd50d8 100644 (file)
@@ -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;
index 537579c8bdf92244754646b4c3e621ed769af2c5..e00ff4779aa622c3bce05869a03efc5e847466c7 100644 (file)
@@ -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);
index 229752904d25649bec053de1a64c8b45d171ca5b..9ba27750a2fb0a5574a0f036b732bd202e798bff 100644 (file)
@@ -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");
index 45e9f01030254f14b0f46712aa7f3e4c2913eca0..2d9cb084ea5ebd9271365f75ad9bd54f44d6e540 100644 (file)
@@ -65,8 +65,8 @@ void testMath(){
 void testAll(){
        try
        {
-               testBase();
                testOs();
+               testBase();
                testMath();
                testString();
        } catch (ReException e){
index 7fe7660cea094d0f69e9765f0269a301dd7e1563..caef2c7598feeb301311cb793f5d43f9fb3bfb5d 100644 (file)
@@ -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<br>
  *                                     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, &times);
                        }
                        fclose(fpSource);
                }
index f1d5afb740ee5e6c176a946c82d6e6aa54f16ccf..7d675f8e5f7942728d2635a22d33125a753857ee 100644 (file)
@@ -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;
 };
index 5703154047469f84305c0074cb804e0d96f1040c..c88ebfd2678ddac8078a95ed34bd1da03ae2cfad 100644 (file)
@@ -261,6 +261,19 @@ const FileTime_t* ReDirStatus_t::modified() {
 #endif\r
 }\r
 \r
+/**\r
+ * Returns the last access time.\r
+ *\r
+ * @return     the last access time\r
+ */\r
+const FileTime_t* ReDirStatus_t::accessed() {\r
+#ifdef __linux__\r
+    return &(getStatus()->st_atime);\r
+#elif defined __WIN32__\r
+    return &m_data.ftLastAccessTime;\r
+#endif\r
+}\r
+\r
 time_t ReDirStatus_t::filetimeToTime(const FileTime_t* filetime){\r
 #ifdef __linux__\r
     return *filetime;\r
index 68c3424c363ff0543a016e6b0c7e9f71facadb82..62e692d83c8ae5507a8b0396578870b0346b433f 100644 (file)
@@ -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);
index cbdb7b724831c000a00225729bfa4577a609b926..696bdae637d61262127782f7fea921b82502de5c 100644 (file)
@@ -13,6 +13,7 @@
 #if defined __linux__
 #include "unistd.h"
 #include <dirent.h>
+#include <utime.h>
 #elif defined __WIN32__
 #include <tchar.h>
 #include "windows.h"