+++ /dev/null
-/*
- * DirTools.hpp
- *
- * Created on: 30.12.2014
- * Author: hm
- */
-
-#ifndef OS_DIRTOOLS_HPP_
-#define OS_DIRTOOLS_HPP_
-
-class ReDirStatisticData{
-public:
- ReDirStatisticData();
- ReDirStatisticData(const ReDirStatisticData& source);
- ReDirStatisticData& operator =(const ReDirStatisticData& source);
-public:
- void clear();
- ReDirStatisticData& add(const ReDirStatisticData& source);
-public:
- int64_t m_sizes;
- int m_files;
- int m_dirs;
- ReByteBuffer m_path;
-};
-
-class ReDirStatistic;
-extern void formatLikeDu(const ReDirStatisticData& data, ReDirStatistic& parent,
- ReByteBuffer& line);
-extern void formatWithSizeFilesAndDirs(const ReDirStatisticData& data,
- ReDirStatistic& parent, ReByteBuffer& line);
-
-/**
- * Calculates a statistic of a directory tree.
- */
-class ReDirStatistic {
-public:
- ReDirStatistic(int deltaList = 512, int deltaBuffer = 0x10000);
- ~ReDirStatistic();
-public:
- const ReStringList& calculate(const char* base, int depth,
- void (*format)(const ReDirStatisticData& data, ReDirStatistic& parent,
- ReByteBuffer& line) = formatLikeDu);
- void setTraceInterval(int interval){
- m_traceInterval = interval;
- }
-private:
- ReStringList m_list;
- int m_traceInterval;
- time_t m_lastTrace;
-};
-
-
-#endif /* OS_DIRTOOLS_HPP_ */
#include "base/rebase.hpp"
#include "os/reos.hpp"
+const char* ReDirTools::m_version = "2015.01.02";
+
/**
* Constructor.
*/
data.m_sizes / 1E6, data.m_files, data.m_dirs);
line.append(buffer, -1).append(data.m_path);
}
+/**
+ * Prints an message how to use the program and exits.
+ *
+ * @param msg an error message
+ * @param msg2 an addition to the error message or NULL
+ */
+void ReDirTools::usage(const char* msg, const char* msg2){
+ printf ("Version: %s\n", m_version);
+ printf ("usage: dirtool <command> <opt>\n");
+ statisticUsage();
+ if (msg != NULL)
+ printf ("+++ %s%s\n", msg, msg2 == NULL ? "" : msg2);
+ exit(1);
+}
+
+/**
+ * Prints an message how to use the statistic module and exits.
+ */
+void ReDirTools::statisticUsage(){
+ printf ("<command>:\nstatistic <path> [<depth>] [<opts_stat>]\n");
+ printf ("<opts_stat>:\n -v verbose\n -t<n> trace interval\n");
+ printf ("example: dirtool statistic c:\\windows 2 -v -t60\n");
+}
+
+/**
+ * Gets the arguments for the "statistic" command and execute this.
+ *
+ * @param argc the number of arguments
+ * @param argav the argument vector
+ */
+void ReDirTools::dirStatistic(int argc, char* argv[]){
+ time_t start = time(NULL);
+ bool verbose = true;
+ int traceInterval = 60;
+ if (argc < 1)
+ usage("statistic: missing path");
+ int depth = 1;
+ if (argc > 1)
+ depth = atol(argv[1]);
+ ReDirStatistic statistic;
+ statistic.setTraceInterval(traceInterval);
+ const ReStringList& list = statistic.calculate(argv[0], depth, formatWithSizeFilesAndDirs);
+ ReByteBuffer buffer;
+ for (size_t ix = 0; ix < list.count(); ix++){
+ buffer.set(list.strOf(ix), list.strLengthOf(ix));
+ printf("%s\n", buffer.str());
+ }
+ if (verbose){
+ int duration = int(time(NULL) - start);
+ printf("Duration: ");
+ if (duration >= 3600)
+ printf("%d:", duration / 3600);
+ printf("%02d:%02d\n", duration % 3600 / 60, duration % 60);
+ }
+}
+
+/**
+ * Gets the arguments for any command and execute this.
+ *
+ * @param argc the number of arguments
+ * @param argav the argument vector
+ */
+int ReDirTools::main(int argc, char* argv[]){
+ ReDirTools tools;
+ if (argc < 2)
+ tools.usage("missing arguments");
+ if (strcmp(argv[1], "statistic") == 0)
+ tools.dirStatistic(argc - 2, argv + 2);
+ else
+ tools.usage("command not implemented: ", argv[1]);
+ //testOs();
+ ReLogger::freeGlobalLogger();
+ return 0;
+}
if (m_handle != INVALID_HANDLE_VALUE)\r
FindClose(m_handle);\r
ReByteBuffer thePath(m_path);\r
- thePath.append("\\*");\r
+ thePath.append(m_path.str()[m_path.length() - 1] == '\\' ? "*" : "\\*");\r
m_handle = FindFirstFileA(thePath.str(), &m_data);\r
rc = m_handle != INVALID_HANDLE_VALUE;\r
#endif\r
do{\r
again = false;\r
if (m_level < 0){\r
+ // Not yet initialized?\r
+ if (m_dirs[0]->m_passNo == 2)\r
+ rc = NULL;\r
+ else {\r
// first call:\r
- if (initEntry(m_base.str(), NULL, 0)){\r
- if (1 != m_passNoForDirSearch)\r
- rc = m_dirs[0];\r
- else\r
- again = alreadyRead = true;\r
+ if (initEntry(m_base.str(), NULL, 0)){\r
+ if (1 != m_passNoForDirSearch)\r
+ rc = m_dirs[0];\r
+ else\r
+ again = alreadyRead = true;\r
+ }\r
}\r
} else {\r
ReDirStatus_t* current = m_dirs[m_level];\r
if (current->m_passNo == 1){\r
// we start the second pass:\r
alreadyRead = initEntry(current->m_path, NULL, -1);\r
+ if (strcmp(current->m_path.str(), "E:\\user\\winfried\\") == 0)\r
+ current->m_passNo = 2;\r
current->m_passNo = 2;\r
again = true;\r
} else {\r