Better error reporting
This commit is contained in:
@@ -250,19 +250,23 @@ void AccionFileNodeAux_Copy(char *pathOrig, char *pathDest) {
|
||||
AccionFileNodeAux_CopyDate(pathOrig, pathDest);
|
||||
} else {
|
||||
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) {
|
||||
if (File_IsDirectory(pathDest)) {
|
||||
File_DeleteDirectory(pathDest);
|
||||
if (File_DeleteDirectory(pathDest)) {
|
||||
Print("Error Deleting Directory: %s, %s\n", pathDest, GetError());
|
||||
}
|
||||
} else {
|
||||
File_Delete(pathDest);
|
||||
if (File_Delete(pathDest)) {
|
||||
Print("Error Deleting File: %s, %s\n", pathDest, GetError());
|
||||
}
|
||||
}
|
||||
}
|
||||
void AccionFileNodeAux_MakeDir(char *pathOrig, char *pathDest) {
|
||||
if (File_MakeDirectory(pathDest) == 0) {
|
||||
Print("Error Making Directory: %s\n", pathDest);
|
||||
Print("Error Making Directory: %s, %s\n", pathDest, GetError());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -19,6 +20,7 @@
|
||||
#include <utime.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#include "util.h"
|
||||
@@ -200,6 +202,7 @@ int File_ExistsPath(char *path) {
|
||||
rc = GetFileAttributes(path);
|
||||
|
||||
if (rc == INVALID_FILE_ATTRIBUTES) {
|
||||
SetError(strerror(errno));
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
@@ -209,6 +212,7 @@ int File_IsDirectory(char *fileName) {
|
||||
rc = GetFileAttributes(fileName);
|
||||
|
||||
if (rc == INVALID_FILE_ATTRIBUTES) {
|
||||
SetError(strerror(errno));
|
||||
return (0);
|
||||
}
|
||||
if (rc & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
@@ -221,6 +225,7 @@ int File_IsFile(char *fileName) {
|
||||
rc = GetFileAttributes(fileName);
|
||||
|
||||
if (rc == INVALID_FILE_ATTRIBUTES) {
|
||||
SetError(strerror(errno));
|
||||
return (0);
|
||||
}
|
||||
if (rc & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
@@ -242,6 +247,7 @@ int File_IsDirectory(char *fileName) {
|
||||
struct stat info;
|
||||
|
||||
if (lstat(fileName, &info) == -1) {
|
||||
SetError(strerror(errno));
|
||||
return (0);
|
||||
}
|
||||
if (S_ISDIR(info.st_mode)) {
|
||||
@@ -253,6 +259,7 @@ int File_IsFile(char *fileName) {
|
||||
struct stat info;
|
||||
|
||||
if (lstat(fileName, &info) == -1) {
|
||||
SetError(strerror(errno));
|
||||
return (0);
|
||||
}
|
||||
if (S_ISDIR(info.st_mode)) {
|
||||
@@ -265,7 +272,14 @@ int File_IsFile(char *fileName) {
|
||||
#ifdef WIN32
|
||||
int File_MakeDirectory(char *path) { return (CreateDirectory(path, NULL)); }
|
||||
#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
|
||||
|
||||
#ifdef WIN32
|
||||
@@ -328,20 +342,30 @@ void File_IterateDir(char *path,
|
||||
}
|
||||
#endif
|
||||
|
||||
void File_Delete(char *path) {
|
||||
int File_Delete(char *path) {
|
||||
#ifdef WIN32
|
||||
remove(path);
|
||||
int rc = remove(path);
|
||||
#else
|
||||
unlink(path);
|
||||
int rc = unlink(path);
|
||||
#endif
|
||||
if (rc != 0) {
|
||||
SetError(strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void File_DeleteDirectory(char *path) {
|
||||
int File_DeleteDirectory(char *path) {
|
||||
#ifndef WIN32
|
||||
rmdir(path);
|
||||
int rc = rmdir(path);
|
||||
#else
|
||||
_rmdir(path);
|
||||
int rc = _rmdir(path);
|
||||
#endif
|
||||
if (rc != 0) {
|
||||
SetError(strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define MaxBuffer 16384
|
||||
@@ -354,9 +378,11 @@ int File_Copy(const char *pathOrig, const char *pathDest) {
|
||||
int status = 0;
|
||||
|
||||
if ((fOrig = fopen(pathOrig, "rb")) == NULL) {
|
||||
SetError(strerror(errno));
|
||||
goto cleanup;
|
||||
}
|
||||
if ((fDest = fopen(pathDest, "wb")) == NULL) {
|
||||
SetError(strerror(errno));
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -370,8 +396,7 @@ int File_Copy(const char *pathOrig, const char *pathDest) {
|
||||
if (readLen > 0) {
|
||||
writeLen = fwrite(buffer, 1, readLen, fDest);
|
||||
if (writeLen != readLen) {
|
||||
// Write error
|
||||
status = -1;
|
||||
SetError("Write error");
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,8 +48,8 @@ void File_IterateDir(char *path,
|
||||
int (*func)(char *path, char *name, void *data),
|
||||
void *data);
|
||||
|
||||
void File_Delete(char *path);
|
||||
void File_DeleteDirectory(char *path);
|
||||
int File_Delete(char *path);
|
||||
int File_DeleteDirectory(char *path);
|
||||
|
||||
int File_Copy(const char *pathOrig, const char *pathDest);
|
||||
|
||||
|
||||
13
src/util.c
13
src/util.c
@@ -155,3 +155,16 @@ int Print(char *fmt, ...) {
|
||||
fflush(stdout);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -46,4 +46,11 @@ int PrintDataSize(long long size);
|
||||
// Prints the formated text screen
|
||||
int Print(char *fmt, ...);
|
||||
|
||||
/////////////////////////////
|
||||
// SetError
|
||||
// GetError
|
||||
//
|
||||
void SetError(char *msg);
|
||||
char *GetError();
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user