Better error reporting

This commit is contained in:
2016-09-17 09:21:34 +02:00
parent 8c50fdc8ba
commit 6c67153607
5 changed files with 65 additions and 16 deletions

View File

@@ -250,19 +250,23 @@ void AccionFileNodeAux_Copy(char *pathOrig, char *pathDest) {
AccionFileNodeAux_CopyDate(pathOrig, pathDest); AccionFileNodeAux_CopyDate(pathOrig, pathDest);
} else { } else {
File_Delete(pathDest); File_Delete(pathDest);
Print("Error Copying to: %s\n", pathDest); Print("Error Copying to: %s, %s\n", pathDest, GetError());
} }
} }
void AccionFileNodeAux_Delete(char *pathOrig, char *pathDest) { void AccionFileNodeAux_Delete(char *pathOrig, char *pathDest) {
if (File_IsDirectory(pathDest)) { if (File_IsDirectory(pathDest)) {
File_DeleteDirectory(pathDest); if (File_DeleteDirectory(pathDest)) {
Print("Error Deleting Directory: %s, %s\n", pathDest, GetError());
}
} else { } else {
File_Delete(pathDest); if (File_Delete(pathDest)) {
Print("Error Deleting File: %s, %s\n", pathDest, GetError());
}
} }
} }
void AccionFileNodeAux_MakeDir(char *pathOrig, char *pathDest) { void AccionFileNodeAux_MakeDir(char *pathOrig, char *pathDest) {
if (File_MakeDirectory(pathDest) == 0) { if (File_MakeDirectory(pathDest) == 0) {
Print("Error Making Directory: %s\n", pathDest); Print("Error Making Directory: %s, %s\n", pathDest, GetError());
} }
} }

View File

@@ -10,6 +10,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h> #include <time.h>
#include <errno.h>
#else #else
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -19,6 +20,7 @@
#include <utime.h> #include <utime.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#endif #endif
#include "util.h" #include "util.h"
@@ -200,6 +202,7 @@ int File_ExistsPath(char *path) {
rc = GetFileAttributes(path); rc = GetFileAttributes(path);
if (rc == INVALID_FILE_ATTRIBUTES) { if (rc == INVALID_FILE_ATTRIBUTES) {
SetError(strerror(errno));
return (0); return (0);
} }
return (1); return (1);
@@ -209,6 +212,7 @@ int File_IsDirectory(char *fileName) {
rc = GetFileAttributes(fileName); rc = GetFileAttributes(fileName);
if (rc == INVALID_FILE_ATTRIBUTES) { if (rc == INVALID_FILE_ATTRIBUTES) {
SetError(strerror(errno));
return (0); return (0);
} }
if (rc & FILE_ATTRIBUTE_DIRECTORY) { if (rc & FILE_ATTRIBUTE_DIRECTORY) {
@@ -221,6 +225,7 @@ int File_IsFile(char *fileName) {
rc = GetFileAttributes(fileName); rc = GetFileAttributes(fileName);
if (rc == INVALID_FILE_ATTRIBUTES) { if (rc == INVALID_FILE_ATTRIBUTES) {
SetError(strerror(errno));
return (0); return (0);
} }
if (rc & FILE_ATTRIBUTE_DIRECTORY) { if (rc & FILE_ATTRIBUTE_DIRECTORY) {
@@ -242,6 +247,7 @@ int File_IsDirectory(char *fileName) {
struct stat info; struct stat info;
if (lstat(fileName, &info) == -1) { if (lstat(fileName, &info) == -1) {
SetError(strerror(errno));
return (0); return (0);
} }
if (S_ISDIR(info.st_mode)) { if (S_ISDIR(info.st_mode)) {
@@ -253,6 +259,7 @@ int File_IsFile(char *fileName) {
struct stat info; struct stat info;
if (lstat(fileName, &info) == -1) { if (lstat(fileName, &info) == -1) {
SetError(strerror(errno));
return (0); return (0);
} }
if (S_ISDIR(info.st_mode)) { if (S_ISDIR(info.st_mode)) {
@@ -265,7 +272,14 @@ int File_IsFile(char *fileName) {
#ifdef WIN32 #ifdef WIN32
int File_MakeDirectory(char *path) { return (CreateDirectory(path, NULL)); } int File_MakeDirectory(char *path) { return (CreateDirectory(path, NULL)); }
#else #else
int File_MakeDirectory(char *path) { return (mkdir(path, 0777)); } int File_MakeDirectory(char *path) {
int rc = mkdir(path, 0777);
if (rc != 0) {
SetError(strerror(errno));
return 0;
}
return 1;
}
#endif #endif
#ifdef WIN32 #ifdef WIN32
@@ -328,20 +342,30 @@ void File_IterateDir(char *path,
} }
#endif #endif
void File_Delete(char *path) { int File_Delete(char *path) {
#ifdef WIN32 #ifdef WIN32
remove(path); int rc = remove(path);
#else #else
unlink(path); int rc = unlink(path);
#endif #endif
if (rc != 0) {
SetError(strerror(errno));
return 0;
}
return 1;
} }
void File_DeleteDirectory(char *path) { int File_DeleteDirectory(char *path) {
#ifndef WIN32 #ifndef WIN32
rmdir(path); int rc = rmdir(path);
#else #else
_rmdir(path); int rc = _rmdir(path);
#endif #endif
if (rc != 0) {
SetError(strerror(errno));
return 0;
}
return 1;
} }
#define MaxBuffer 16384 #define MaxBuffer 16384
@@ -354,9 +378,11 @@ int File_Copy(const char *pathOrig, const char *pathDest) {
int status = 0; int status = 0;
if ((fOrig = fopen(pathOrig, "rb")) == NULL) { if ((fOrig = fopen(pathOrig, "rb")) == NULL) {
SetError(strerror(errno));
goto cleanup; goto cleanup;
} }
if ((fDest = fopen(pathDest, "wb")) == NULL) { if ((fDest = fopen(pathDest, "wb")) == NULL) {
SetError(strerror(errno));
goto cleanup; goto cleanup;
} }
@@ -370,8 +396,7 @@ int File_Copy(const char *pathOrig, const char *pathDest) {
if (readLen > 0) { if (readLen > 0) {
writeLen = fwrite(buffer, 1, readLen, fDest); writeLen = fwrite(buffer, 1, readLen, fDest);
if (writeLen != readLen) { if (writeLen != readLen) {
// Write error SetError("Write error");
status = -1;
goto cleanup; goto cleanup;
} }
} }

View File

@@ -48,8 +48,8 @@ void File_IterateDir(char *path,
int (*func)(char *path, char *name, void *data), int (*func)(char *path, char *name, void *data),
void *data); void *data);
void File_Delete(char *path); int File_Delete(char *path);
void File_DeleteDirectory(char *path); int File_DeleteDirectory(char *path);
int File_Copy(const char *pathOrig, const char *pathDest); int File_Copy(const char *pathOrig, const char *pathDest);

View File

@@ -155,3 +155,16 @@ int Print(char *fmt, ...) {
fflush(stdout); fflush(stdout);
return (n); return (n);
} }
/////////////////////////////
// SetError
// GetError
//
char _errorMessage[2048] = "";
char _errorMessageTemp[2048] = "";
void SetError(char *msg) { strcpy(_errorMessage, msg); }
char *GetError() {
strcpy(_errorMessageTemp, _errorMessage);
strcpy(_errorMessage, "");
return _errorMessageTemp;
}

View File

@@ -46,4 +46,11 @@ int PrintDataSize(long long size);
// Prints the formated text screen // Prints the formated text screen
int Print(char *fmt, ...); int Print(char *fmt, ...);
/////////////////////////////
// SetError
// GetError
//
void SetError(char *msg);
char *GetError();
#endif #endif