From f96eb1bdb83c51be9d4534fd442b8c8e979ffc3f Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 23 Nov 2015 01:41:11 +0100 Subject: [PATCH] Code cleanup --- src/actionfilenode.c | 49 ++++++++++++--------- src/filenode.c | 24 +++++----- src/fileutil.c | 40 ++++++++++++----- src/main.c | 101 ++++++++++++++++++++++--------------------- src/util.c | 29 ++++++++----- src/util.h | 10 ++++- 6 files changed, 148 insertions(+), 105 deletions(-) diff --git a/src/actionfilenode.c b/src/actionfilenode.c index a0118ee..18527de 100644 --- a/src/actionfilenode.c +++ b/src/actionfilenode.c @@ -149,19 +149,20 @@ void AccionFileNode_CompareChilds( } -void ActionFileNode_Statistics(ActionFileNode actionFileNode, +int ActionFileNode_Statistics(ActionFileNode actionFileNode, ActionQueueStatistics *statistics) { statistics->readLeft = 0; statistics->writeLeft = 0; statistics->readRight = 0; statistics->writeRight = 0; + statistics->deleteLeft = 0; + statistics->deleteRight = 0; + statistics->fullCopyCount = 0; statistics->dateCopyCount = 0; statistics->directoryCount = 0; statistics->deleteCount = 0; - statistics->deleteLeft = 0; - statistics->deleteRight = 0; while (actionFileNode != NULL) { @@ -202,6 +203,12 @@ void ActionFileNode_Statistics(ActionFileNode actionFileNode, actionFileNode = actionFileNode->next; } + return ( + statistics->fullCopyCount + + statistics->dateCopyCount + + statistics->directoryCount + + statistics->deleteCount + ); } void ActionFileNode_Print(ActionFileNode actionFileNode) { @@ -219,34 +226,34 @@ void ActionFileNode_Print(ActionFileNode actionFileNode) { //printff("%s == %s\n",pathIzq,pathDer); break; case ActionFileCmp_LeftToRight: - printff(" => %s\n", showPath); + Print(" => %s\n", showPath); break; case ActionFileCmp_RightToLeft: - printff(" <= %s\n", showPath); + Print(" <= %s\n", showPath); break; case ActionFileCmp_DeleteLeft: - printff(" *- %s\n", showPath); + Print(" *- %s\n", showPath); break; case ActionFileCmp_DeleteRight: - printff(" -* %s\n", showPath); + Print(" -* %s\n", showPath); break; case ActionFileCmp_DateLeftToRight: - printff(" -> %s\n", showPath); + Print(" -> %s\n", showPath); break; case ActionFileCmp_DateRightToLeft: - printff(" <- %s\n", showPath); + Print(" <- %s\n", showPath); break; case ActionFileCmp_MakeRightDirectory: - printff(" -D %s\n", showPath); + Print(" -D %s\n", showPath); break; case ActionFileCmp_MakeLeftDirectory: - printff(" D- %s\n", showPath); + Print(" D- %s\n", showPath); break; } actionFileNode = actionFileNode->next; } - printff("End\n"); + Print("End\n"); } void AccionFileNodeAux_CopyDate(char *pathOrig, char *pathDest) { @@ -302,42 +309,42 @@ int ActionFileNode_RunList(ActionFileNode actionFileNode, char *pathLeft, //printff("%s == %s\n",pathIzq,pathDer); break; case ActionFileCmp_LeftToRight: - printff(" => %s\n", showPath); + Print(" => %s\n", showPath); AccionFileNodeAux_Copy(fullPathLeft, fullPathRight); numActions++; break; case ActionFileCmp_RightToLeft: - printff(" <= %s\n", showPath); + Print(" <= %s\n", showPath); AccionFileNodeAux_Copy(fullPathRight, fullPathLeft); numActions++; break; case ActionFileCmp_DeleteLeft: - printff(" *- %s\n", showPath); + Print(" *- %s\n", showPath); AccionFileNodeAux_Delete(fullPathRight, fullPathLeft); numActions++; break; case ActionFileCmp_DeleteRight: - printff(" -* %s\n", showPath); + Print(" -* %s\n", showPath); AccionFileNodeAux_Delete(fullPathLeft, fullPathRight); numActions++; break; case ActionFileCmp_DateLeftToRight: - printff(" -> %s\n", showPath); + Print(" -> %s\n", showPath); AccionFileNodeAux_CopyDate(fullPathLeft, fullPathRight); numActions++; break; case ActionFileCmp_DateRightToLeft: - printff(" <- %s\n", showPath); + Print(" <- %s\n", showPath); AccionFileNodeAux_CopyDate(fullPathRight, fullPathLeft); numActions++; break; case ActionFileCmp_MakeRightDirectory: - printff(" -D %s\n", showPath); + Print(" -D %s\n", showPath); AccionFileNodeAux_MakeDir(fullPathLeft, fullPathRight); numActions++; break; case ActionFileCmp_MakeLeftDirectory: - printff(" D- %s\n", showPath); + Print(" D- %s\n", showPath); AccionFileNodeAux_MakeDir(fullPathRight, fullPathLeft); numActions++; break; } actionFileNode = actionFileNode->next; } - printff("End\n"); + Print("End\n"); return numActions; } diff --git a/src/filenode.c b/src/filenode.c index fc8f098..7593719 100644 --- a/src/filenode.c +++ b/src/filenode.c @@ -359,33 +359,33 @@ FileNode FileNode_Load(char *filePath) { } void FileNode_PrintNode(FileNode fileNode) { - printff(FileNode_GetPath(fileNode, NULL)); + Print(FileNode_GetPath(fileNode, NULL)); if (fileNode->flags & FileFlag_Normal) { - printff(" File"); + Print(" File"); } else { - printff(" Dir"); + Print(" Dir"); } - printff(" %d", fileNode->status); + Print(" %d", fileNode->status); if (fileNode->status == FileStatus_New) { - printff(" New"); + Print(" New"); } if (fileNode->status == FileStatus_Modified) { - printff(" Modified"); + Print(" Modified"); } if (fileNode->status == FileStatus_Deleted) { - printff(" Deleted!!!"); + Print(" Deleted!!!"); } - printff("\n"); + Print("\n"); if (fileNode->flags&FileFlag_HasSize) { - printff("\\-Size : %lld\n", fileNode->size); + Print("\\-Size : %lld\n", fileNode->size); } if (fileNode->flags&FileFlag_HasTime) { - printff("\\-Date : "); FileTime_Print(fileNode->fileTime); printff("\n"); + Print("\\-Date : "); FileTime_Print(fileNode->fileTime); Print("\n"); } if (fileNode->flags&FileFlag_HasCRC) { - printff("\\-CRC : [%08X]\n", fileNode->crc); + Print("\\-CRC : [%08X]\n", fileNode->crc); } } @@ -407,7 +407,7 @@ void FileNode_Print(FileNode fileNode) { while (fileNodeAux->next == NULL) { fileNodeAux = fileNodeAux->parent; if (fileNodeAux == fileNode || fileNodeAux == NULL) { - printff("End\n"); + Print("End\n"); end = 1; break; } diff --git a/src/fileutil.c b/src/fileutil.c index 856cb01..4efcbf4 100644 --- a/src/fileutil.c +++ b/src/fileutil.c @@ -57,6 +57,10 @@ FILETIME POSIX_to_FileTime(FileTime fileTime) { return fileTimeOut; } +///////////////////////////// +// FileTime_Get +// +// Gets the current time in POSIX. FileTime FileTime_Get(char *fileName) { HANDLE hFile; FILETIME ftCreate, ftAccess, ftWrite; @@ -67,6 +71,10 @@ FileTime FileTime_Get(char *fileName) { return (FileTime_to_POSIX(ftWrite)); } +///////////////////////////// +// FileTime_Set +// +// Sets the current time in POSIX. void FileTime_Set(char *fileName, FileTime fileTime) { HANDLE hFile; FILETIME ftWrite; @@ -79,12 +87,20 @@ void FileTime_Set(char *fileName, FileTime fileTime) { #else +///////////////////////////// +// FileTime_Get +// +// Gets the current time in POSIX. FileTime FileTime_Get(char *fileName) { struct stat fs; lstat(fileName, &fs); return (fs.st_mtime); } +///////////////////////////// +// FileTime_Set +// +// Sets the current time in POSIX. void FileTime_Set(char *fileName, FileTime t) { struct utimbuf utb; @@ -95,6 +111,19 @@ void FileTime_Set(char *fileName, FileTime t) { #endif +///////////////////////////// +// FileTime_Print +// +// Prints the filetime +void FileTime_Print(FileTime fileTime) { + struct tm *tms; + + tms = localtime((time_t *)&fileTime); + Print("%04d-%02d-%02d %02d:%02d:%02d", tms->tm_year + 1900, + tms->tm_mon + 1, tms->tm_mday, tms->tm_hour, tms->tm_min, + tms->tm_sec); +} + #ifdef WIN32 long long File_GetSize(char *fileName) { @@ -137,17 +166,6 @@ void File_GetSizeAndTime(char *fileName, long long *size, FileTime *time) { } #endif - - -void FileTime_Print(FileTime fileTime) { - struct tm *tms; - - tms = localtime((time_t *)&fileTime); - printff("%04d-%02d-%02d %02d:%02d:%02d", tms->tm_year + 1900, - tms->tm_mon + 1, tms->tm_mday, tms->tm_hour, tms->tm_min, - tms->tm_sec); -} - void File_GetName(char *path, char *name) { int i, j; diff --git a/src/main.c b/src/main.c index 8c307e8..91df714 100644 --- a/src/main.c +++ b/src/main.c @@ -14,23 +14,23 @@ void Help(char *exe) { char exeFilename[MaxPath]; File_GetName(exe, exeFilename); - printff("Usage:\n"); - printff(" %s info [file] {[file] {..}}\n", exeFilename); - printff(" %s scan [dir] [tree] \n", exeFilename); - printff(" %s rescan [dir] [tree] \n", exeFilename); - printff(" %s read [file] [tree]\n", exeFilename); - printff(" %s dir [dir]\n", exeFilename); - printff(" %s check [dir]\n", exeFilename); - printff("\n"); - printff(" %s sync [dirA] [dirB]\n", exeFilename); - printff(" %s resync [dirA] [dirB]\n", exeFilename); - printff(" %s synctest [dirA] [dirB]\n", exeFilename); - printff(" %s resynctest [dirA] [dirB]\n", exeFilename); - printff("\n"); - printff(" %s copy [dirA] [dirB]\n", exeFilename); - printff(" %s recopy [dirA] [dirB]\n", exeFilename); - printff(" %s copytest [dirA] [dirB]\n", exeFilename); - printff(" %s recopytest [dirA] [dirB]\n", exeFilename); + Print("Usage:\n"); + Print(" %s info [file] {[file] {..}}\n", exeFilename); + Print(" %s scan [dir] [tree] \n", exeFilename); + Print(" %s rescan [dir] [tree] \n", exeFilename); + Print(" %s read [file] [tree]\n", exeFilename); + Print(" %s dir [dir]\n", exeFilename); + Print(" %s check [dir]\n", exeFilename); + Print("\n"); + Print(" %s sync [dirA] [dirB]\n", exeFilename); + Print(" %s resync [dirA] [dirB]\n", exeFilename); + Print(" %s synctest [dirA] [dirB]\n", exeFilename); + Print(" %s resynctest [dirA] [dirB]\n", exeFilename); + Print("\n"); + Print(" %s copy [dirA] [dirB]\n", exeFilename); + Print(" %s recopy [dirA] [dirB]\n", exeFilename); + Print(" %s copytest [dirA] [dirB]\n", exeFilename); + Print(" %s recopytest [dirA] [dirB]\n", exeFilename); } FileNode CheckDir(char *path, int recheck); @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { return 0; } - printff("\n================================ FileSync ===================================\n"); + Print("\n================================ FileSync ===================================\n"); if (!strcmp(argv[1], "info") && argc >= 3) { // Informacion de ficheros int i; @@ -59,31 +59,31 @@ int main(int argc, char *argv[]) { // Scan directory information tree and save long long tScan = Time_GetTime(); FileNode fileNode; - printff("Building FileNode..\n"); + Print("Building FileNode..\n"); fileNode = FileNode_Build(argv[2]); tScan = Time_GetTime() - tScan; - printff("\ttScan :"); PrintElapsedTime(tScan); printff("\n"); + Print("\ttScan :"); PrintElapsedTime(tScan); Print("\n"); FileNode_Save(fileNode, argv[3]); } else if (!strcmp(argv[1], "rescan") && argc == 4) { // Scan directory information and save tree FileNode fileNode; - printff("Loading FileNode..\n"); + Print("Loading FileNode..\n"); fileNode = FileNode_Load(argv[3]); if (fileNode) { - printff("Rebuilding FileNode..\n"); + Print("Rebuilding FileNode..\n"); long long tScan = Time_GetTime(); fileNode = FileNode_Refresh(fileNode, argv[2]); tScan = Time_GetTime() - tScan; - printff("\ttScan :"); PrintElapsedTime(tScan); printff("\n"); + Print("\ttScan :"); PrintElapsedTime(tScan); Print("\n"); FileNode_Save(fileNode, argv[3]); } else { - printff("Building FileNode..\n"); + Print("Building FileNode..\n"); long long tScan = Time_GetTime(); fileNode = FileNode_Build(argv[2]); tScan = Time_GetTime() - tScan; - printff("\ttScan :"); PrintElapsedTime(tScan); printff("\n"); + Print("\ttScan :"); PrintElapsedTime(tScan); Print("\n"); FileNode_Save(fileNode, argv[3]); } } @@ -154,7 +154,7 @@ FileNode CheckDir(char *path, int recheck) { // Check directory snprintf(dirNodesFile, MaxPath, "%s/"FileNode_Filename, path); if (recheck) { - printff("Checking Directory.. %s\n", path); + Print("Checking Directory.. %s\n", path); long long tScan = Time_GetTime(); fileNode = FileNode_Load(dirNodesFile); if (fileNode) { @@ -164,14 +164,14 @@ FileNode CheckDir(char *path, int recheck) { fileNode = FileNode_Build(path); } tScan = Time_GetTime() - tScan; - printff("\ttScan :"); PrintElapsedTime(tScan); printff("\n"); + Print("\ttScan :"); PrintElapsedTime(tScan); Print("\n"); FileNode_Save(fileNode, dirNodesFile); } else { - printff("Loading Directory.. %s\n", path); + Print("Loading Directory.. %s\n", path); fileNode = FileNode_Load(dirNodesFile); if (!fileNode) { - printff("Error, no nodesFile.fs\n"); + Print("Error, no nodesFile.fs\n"); return NULL; } } @@ -180,27 +180,30 @@ FileNode CheckDir(char *path, int recheck) { void PrintStatistics(ActionFileNode actionFileNode) { ActionQueueStatistics statistics; - ActionFileNode_Statistics(actionFileNode, &statistics); - printff("Statistics\n"); + if (ActionFileNode_Statistics(actionFileNode, &statistics) == 0) { + Print("Noting to do.\n"); + return; + } + Print("Statistics\n"); - printff(" % 8s % 8s % 8s\n", + Print(" % 8s % 8s % 8s\n", "Read", "Write", "Delete"); - printff("Left :"); + Print("Left :"); PrintDataSize(statistics.readLeft); PrintDataSize(statistics.writeLeft); PrintDataSize(statistics.deleteLeft); - printff("\n"); - printff("Right:"); + Print("\n"); + Print("Right:"); PrintDataSize(statistics.readRight); PrintDataSize(statistics.writeRight); PrintDataSize(statistics.deleteRight); - printff("\n"); + Print("\n"); - printff("\n"); - printff("Copy count : % 10d\n", statistics.fullCopyCount); - printff("Date copy count: % 10d\n", statistics.dateCopyCount); - printff("Directory count: % 10d\n", statistics.directoryCount); - printff("Delete count : % 10d\n", statistics.deleteCount); + Print("\n"); + Print("Copy count : % 10d\n", statistics.fullCopyCount); + Print("Date copy count: % 10d\n", statistics.dateCopyCount); + Print("Directory count: % 10d\n", statistics.directoryCount); + Print("Delete count : % 10d\n", statistics.deleteCount); } int Sync(char *pathLeft, char *pathRight, int recheck, int dryRun) { @@ -209,11 +212,11 @@ int Sync(char *pathLeft, char *pathRight, int recheck, int dryRun) { // Check and load directories if (!File_ExistsPath(pathLeft) || !File_IsDirectory(pathLeft)) { - printff("Error, directory does not exist: %s\n", pathLeft); + Print("Error, directory does not exist: %s\n", pathLeft); return 0; } if (!File_ExistsPath(pathRight) || !File_IsDirectory(pathRight)) { - printff("Error, directory does not exist: %s\n", pathRight); + Print("Error, directory does not exist: %s\n", pathRight); return 0; } fileNodeLeft = CheckDir(pathLeft, recheck); @@ -227,11 +230,11 @@ int Sync(char *pathLeft, char *pathRight, int recheck, int dryRun) { // Build actions long long tBuild = Time_GetTime(); - printff("Building action list.. \n"); + Print("Building action list.. \n"); ActionFileNode actionFileNode = NULL; actionFileNode = ActionFileNode_BuildSync(fileNodeLeft, fileNodeRight); tBuild = Time_GetTime() - tBuild; - printff("\ttBuild:"); PrintElapsedTime(tBuild); printff("\n"); + Print("\ttBuild:"); PrintElapsedTime(tBuild); Print("\n"); if (dryRun) { // Show action list @@ -255,11 +258,11 @@ int Copy(char *pathLeft, char *pathRight, int reCheck, int dryRun) { // Check and load directories if (!File_ExistsPath(pathLeft) || !File_IsDirectory(pathLeft)) { - printff("Error, directory does not exist: %s\n", pathLeft); + Print("Error, directory does not exist: %s\n", pathLeft); return 0; } if (!File_ExistsPath(pathRight) || !File_IsDirectory(pathRight)) { - printff("Error, directory does not exist: %s\n", pathRight); + Print("Error, directory does not exist: %s\n", pathRight); return 0; } fileNodeLeft = CheckDir(pathLeft, reCheck); @@ -273,11 +276,11 @@ int Copy(char *pathLeft, char *pathRight, int reCheck, int dryRun) { // Build actions long long tBuild = Time_GetTime(); - printff("Building action list.. \n"); + Print("Building action list.. \n"); ActionFileNode actionFileNode = NULL; actionFileNode = ActionFileNode_BuildCopy(fileNodeLeft, fileNodeRight); tBuild = Time_GetTime() - tBuild; - printff("\ttBuild:"); PrintElapsedTime(tBuild); printff("\n"); + Print("\ttBuild:"); PrintElapsedTime(tBuild); Print("\n"); if (dryRun) { // Show action list diff --git a/src/util.c b/src/util.c index 1ace7f3..0ca129a 100644 --- a/src/util.c +++ b/src/util.c @@ -5,6 +5,10 @@ #include "util.h" +///////////////////////////// +// String_Copy +// +// Copies a string. char *String_Copy(char *str) { char *strnew; int len; @@ -16,7 +20,6 @@ char *String_Copy(char *str) { return (strnew); } - #if WIN32 #include // WIN32 @@ -95,21 +98,21 @@ long long Time_GetCurrentTime() { // Prints the elapsed time (input in microseconds (us)) int PrintElapsedTime(long long time) { if (time < 1000) { - return printff("%8lld us", time); + return Print("%8lld us", time); } double msTime = (double)time / 1000; if (msTime < 1000) { - return printff("% 8.3f ms", msTime); + return Print("% 8.3f ms", msTime); } double seconds = msTime / 1000; if (seconds < 60) { - return printff("% 8.3f s", seconds); + return Print("% 8.3f s", seconds); } int minutes = (int)seconds / 60; seconds = seconds - (minutes * 60); int hours = minutes / 60; minutes = minutes % 60; - return printff("%02d:%02d:%06.3f", hours, minutes, seconds); + return Print("%02d:%02d:%06.3f", hours, minutes, seconds); } ///////////////////////////// @@ -118,25 +121,29 @@ int PrintElapsedTime(long long time) { // Prints the data size (input in bytes) int PrintDataSize(long long size) { if (size < 1024) { - return printff("%8lld B ", size); + return Print("%8lld B ", size); } double kibSize = (double)size / 1024; if (kibSize < 1024) { - return printff("% 8.3f KiB", kibSize); + return Print("% 8.3f KiB", kibSize); } double mibSize = kibSize / 1024; if (mibSize < 1024) { - return printff("% 8.3f MiB", mibSize); + return Print("% 8.3f MiB", mibSize); } double gibSize = mibSize / 1024; if (gibSize < 1024) { - return printff("% 8.3f GiB", gibSize); + return Print("% 8.3f GiB", gibSize); } double tibSize = gibSize / 1024; - return printff("% 8.3f TiB", tibSize); + return Print("% 8.3f TiB", tibSize); } -int printff(char *fmt, ...) { +///////////////////////////// +// Print +// +// Prints the formated text screen +int Print(char *fmt, ...) { va_list ap; int n; diff --git a/src/util.h b/src/util.h index 6fe003f..bb2a0e8 100644 --- a/src/util.h +++ b/src/util.h @@ -4,6 +4,10 @@ #include #include +///////////////////////////// +// String_Copy +// +// Copies a string. char *String_Copy(char *str); ///////////////////////////// @@ -37,6 +41,10 @@ int PrintElapsedTime(long long time); // Prints the data size (input in bytes) int PrintDataSize(long long size); -int printff(char *fmt, ...); +///////////////////////////// +// Print +// +// Prints the formated text screen +int Print(char *fmt, ...); #endif