diff --git a/storage/connect/filamdbf.cpp b/storage/connect/filamdbf.cpp index 26cf33d9c71..4299622a4b4 100644 --- a/storage/connect/filamdbf.cpp +++ b/storage/connect/filamdbf.cpp @@ -215,10 +215,8 @@ PQRYRES DBFColumns(PGLOBAL g, char *fn, BOOL info) /**************************************************************************/ PlugSetPath(filename, fn, PlgGetDataPath(g)); - if (!(infile = fopen(filename, "rb"))) { - sprintf(g->Message, MSG(CANNOT_OPEN), filename); + if (!(infile= global_fopen(g, MSGID_CANNOT_OPEN, filename, "rb"))) return NULL; - } // endif file /**************************************************************************/ /* Get the first 32 bytes of the header. */ @@ -384,10 +382,8 @@ int DBFBASE::ScanHeader(PGLOBAL g, PSZ fname, int lrecl, char *defpath) /************************************************************************/ PlugSetPath(filename, fname, defpath); - if (!(infile = fopen(filename, "rb"))) { - sprintf(g->Message, MSG(CANNOT_OPEN), filename); + if (!(infile= global_fopen(g, MSGID_CANNOT_OPEN, filename, "rb"))) return 0; // Assume file does not exist - } // endif file /************************************************************************/ /* Get the first 32 bytes of the header. */ @@ -487,9 +483,6 @@ bool DBFFAM::OpenTableFile(PGLOBAL g) PlugSetPath(filename, To_File, Tdbp->GetPath()); if (!(Stream = PlugOpenFile(g, filename, opmode))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - opmode, (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); #ifdef DEBTRACE htrc("%s\n", g->Message); #endif @@ -839,16 +832,18 @@ void DBFFAM::CloseTableFile(PGLOBAL g) char filename[_MAX_PATH]; PlugSetPath(filename, To_File, Tdbp->GetPath()); - Stream = fopen(filename, "r+b"); - fseek(Stream, 4, SEEK_SET); // Get header.Records position - fwrite(&n, sizeof(int), 1, Stream); - fclose(Stream); - Stream = NULL; - Records = n; // Update Records value - } // endif n - + if ((Stream= global_fopen(g, MSGID_OPEN_MODE_STRERROR, filename, "r+b"))) + { + fseek(Stream, 4, SEEK_SET); // Get header.Records position + fwrite(&n, sizeof(int), 1, Stream); + fclose(Stream); + Stream= NULL; + Records= n; // Update Records value + } } // endif n + } // endif n + } else // Finally close the file rc = PlugCloseFile(g, To_Fb); diff --git a/storage/connect/filamfix.cpp b/storage/connect/filamfix.cpp index d3c7662ddb0..37c84677e74 100644 --- a/storage/connect/filamfix.cpp +++ b/storage/connect/filamfix.cpp @@ -430,10 +430,8 @@ int FIXFAM::DeleteRecords(PGLOBAL g, int irc) rc = PlugCloseFile(g, To_Fb); PlugSetPath(filename, To_File, Tdbp->GetPath()); - if (!(h = open(filename, O_WRONLY))) { - sprintf(g->Message, MSG(OPEN_STRERROR), strerror(errno)); + if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0) return RC_FX; - } // endif /*****************************************************************/ /* Remove extra records. */ @@ -849,12 +847,10 @@ bool BGXFAM::OpenTableFile(PGLOBAL g) return true; } // endswitch - Hfile = open(filename, oflag, tmode); + Hfile= global_open(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, oflag, tmode); if (Hfile == INVALID_HANDLE_VALUE) { rc = errno; - sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename); - strcat(g->Message, strerror(errno)); } else rc = 0; diff --git a/storage/connect/filamtxt.cpp b/storage/connect/filamtxt.cpp index d6aa98cd611..5fb87991b85 100644 --- a/storage/connect/filamtxt.cpp +++ b/storage/connect/filamtxt.cpp @@ -158,24 +158,22 @@ int TXTFAM::GetFileLength(PGLOBAL g) int len; PlugSetPath(filename, To_File, Tdbp->GetPath()); - h = open(filename, _O_RDONLY); + h= global_open(g, MSGID_OPEN_MODE_STRERROR, filename, _O_RDONLY); if (trace) htrc("GetFileLength: fn=%s h=%d\n", filename, h); if (h == -1) { if (errno != ENOENT) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - "r", (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); - - if (trace) - htrc("%s\n", g->Message); - + if (trace) + htrc("%s\n", g->Message); len = -1; - } else + } + else + { len = 0; // File does not exist yet - + g->Message[0]= '\0'; + } } else { if ((len = _filelength(h)) < 0) sprintf(g->Message, MSG(FILELEN_ERROR), "_filelength", filename); @@ -385,10 +383,6 @@ bool DOSFAM::OpenTableFile(PGLOBAL g) PlugSetPath(filename, To_File, Tdbp->GetPath()); if (!(Stream = PlugOpenFile(g, filename, opmode))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - opmode, (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); - if (trace) htrc("%s\n", g->Message); @@ -799,10 +793,8 @@ int DOSFAM::DeleteRecords(PGLOBAL g, int irc) PlugSetPath(filename, To_File, Tdbp->GetPath()); rc = PlugCloseFile(g, To_Fb); - if (!(h = open(filename, O_WRONLY))) { - sprintf(g->Message, MSG(OPEN_STRERROR), strerror(errno)); + if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0) return RC_FX; - } // endif /*****************************************************************/ /* Remove extra records. */ @@ -848,10 +840,6 @@ bool DOSFAM::OpenTempFile(PGLOBAL g) strcat(PlugRemoveType(tempname, tempname), ".t"); if (!(T_Stream = PlugOpenFile(g, tempname, "wb"))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - "wb", (int)errno, tempname); - strcat(strcat(g->Message, ": "), strerror(errno)); - if (trace) htrc("%s\n", g->Message); diff --git a/storage/connect/filamvct.cpp b/storage/connect/filamvct.cpp index 11150c78bce..7342ef45a2f 100644 --- a/storage/connect/filamvct.cpp +++ b/storage/connect/filamvct.cpp @@ -169,7 +169,7 @@ int VCTFAM::GetBlockInfo(PGLOBAL g) if (Header == 2) strcat(PlugRemoveType(filename, filename), ".blk"); - if (!(s = fopen(filename, "rb"))) { + if (!(s= global_fopen(g, MSGID_CANNOT_OPEN, filename, "rb"))) { // Consider this is a void table Last = Nrec; Block = 0; @@ -215,11 +215,11 @@ bool VCTFAM::SetBlockInfo(PGLOBAL g) k = fseek(s, 0, SEEK_SET); } else - s = fopen(filename, "r+b"); + s= global_fopen(g, MSGID_CANNOT_OPEN, filename, "r+b"); } else { // Header == 2 strcat(PlugRemoveType(filename, filename), ".blk"); - s = fopen(filename, "wb"); + s= global_fopen(g, MSGID_CANNOT_OPEN, filename, "wb"); } // endif Header if (!s) { @@ -341,15 +341,13 @@ bool VCTFAM::MakeEmptyFile(PGLOBAL g, char *fn) PlugSetPath(filename, fn, Tdbp->GetPath()); #if defined(WIN32) - h = open(filename, _O_CREAT | _O_WRONLY, S_IREAD | S_IWRITE); + h= global_open(g, MSGID_OPEN_EMPTY_FILE, filename, _O_CREAT | _O_WRONLY, S_IREAD | S_IWRITE); #else // !WIN32 - h = open(filename, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); + h= global_open(g, MSGID_OPEN_EMPTY_FILE, filename, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE); #endif // !WIN32 - if (h == -1) { - sprintf(g->Message, MSG(OPEN_EMPTY_FILE), To_File, strerror(errno)); + if (h == -1) return true; - } // endif h n = (Header == 1 || Header == 3) ? sizeof(VECHEADER) : 0; @@ -429,9 +427,6 @@ bool VCTFAM::OpenTableFile(PGLOBAL g) PlugSetPath(filename, To_File, Tdbp->GetPath()); if (!(Stream = PlugOpenFile(g, filename, opmode))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - opmode, (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); #ifdef DEBTRACE htrc("%s\n", g->Message); #endif @@ -663,10 +658,7 @@ int VCTFAM::WriteBuffer(PGLOBAL g) fclose(Stream); PlugSetPath(filename, To_File, Tdbp->GetPath()); - if (!(Stream = fopen(filename, "ab"))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - "ab", (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); + if (!(Stream= global_fopen(g, MSGID_OPEN_MODE_STRERROR, filename, "ab"))) { Closing = true; // Tell CloseDB of error return RC_FX; } // endif Stream @@ -791,10 +783,8 @@ int VCTFAM::DeleteRecords(PGLOBAL g, int irc) Stream = NULL; // For SetBlockInfo PlugSetPath(filename, To_File, Tdbp->GetPath()); - if (!(h = open(filename, O_WRONLY))) { - sprintf(g->Message, MSG(OPEN_STRERROR), strerror(errno)); + if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0) return RC_FX; - } // endif /***************************************************************/ /* Remove extra blocks. */ @@ -857,9 +847,6 @@ bool VCTFAM::OpenTempFile(PGLOBAL g) opmode = "wb"; if (!(T_Stream = PlugOpenFile(g, tempname, opmode))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - opmode, (int)errno, tempname); - strcat(strcat(g->Message, ": "), strerror(errno)); #ifdef DEBTRACE htrc("%s\n", g->Message); #endif @@ -1963,9 +1950,6 @@ bool VECFAM::OpenColumnFile(PGLOBAL g, char *opmode, int i) sprintf(filename, Colfn, i+1); if (!(Streams[i] = PlugOpenFile(g, filename, opmode))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - opmode, (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); #ifdef DEBTRACE htrc("%s\n", g->Message); #endif @@ -2240,10 +2224,8 @@ int VECFAM::DeleteRecords(PGLOBAL g, int irc) sprintf(filename, Colfn, i + 1); rc = PlugCloseFile(g, To_Fbs[i]); - if (!(h = open(filename, O_WRONLY))) { - sprintf(g->Message, MSG(OPEN_STRERROR), strerror(errno)); + if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0) return RC_FX; - } // endif /***************************************************************/ /* Remove extra records. */ @@ -2302,9 +2284,6 @@ bool VECFAM::OpenTempFile(PGLOBAL g) sprintf(tempname, Tempat, i+1); if (!(T_Streams[i] = PlugOpenFile(g, tempname, "wb"))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - "wb", (int)errno, tempname); - strcat(strcat(g->Message, ": "), strerror(errno)); #ifdef DEBTRACE htrc("%s\n", g->Message); #endif diff --git a/storage/connect/libdoc.cpp b/storage/connect/libdoc.cpp index 00c206dc862..dedee068ca0 100644 --- a/storage/connect/libdoc.cpp +++ b/storage/connect/libdoc.cpp @@ -224,7 +224,7 @@ int LIBXMLDOC::DumpDoc(PGLOBAL g, char *ofn) int rc; FILE *of; - if (!(of = fopen(ofn, "w"))) + if (!(of= global_fopen(g, MSGID_CANNOT_OPEN, ofn, "w"))) return -1; #if 1 diff --git a/storage/connect/maputil.cpp b/storage/connect/maputil.cpp index 6f824e4cdda..d7389d26032 100644 --- a/storage/connect/maputil.cpp +++ b/storage/connect/maputil.cpp @@ -141,7 +141,7 @@ HANDLE CreateFileMap(PGLOBAL g, LPCSTR fileName, } // endswitch // Try to open the addressed file. - fd = open(fileName, openMode); + fd= global_open(g, MSGID_NONE, fileName, openMode); if (fd != INVALID_HANDLE_VALUE && mode != MODE_INSERT) { /* We must know about the size of the file. */ diff --git a/storage/connect/plgdbsem.h b/storage/connect/plgdbsem.h index e690a0e4efa..13d2f81cdc1 100644 --- a/storage/connect/plgdbsem.h +++ b/storage/connect/plgdbsem.h @@ -508,3 +508,16 @@ DllExport PSZ GetIniString(PGLOBAL, void *, LPCSTR, LPCSTR, LPCSTR, LPCSTR); DllExport int GetIniSize(char *, char *, char *, char *); DllExport bool WritePrivateProfileInt(LPCSTR, LPCSTR, int, LPCSTR); DllExport void NewPointer(PTABS, void *, void *); + + +#define MSGID_NONE 0 +#define MSGID_CANNOT_OPEN 1 +#define MSGID_OPEN_MODE_ERROR 2 +#define MSGID_OPEN_STRERROR 3 +#define MSGID_OPEN_ERROR_AND_STRERROR 4 +#define MSGID_OPEN_MODE_STRERROR 5 +#define MSGID_OPEN_EMPTY_FILE 6 + +FILE *global_fopen(GLOBAL *g, int msgid, const char *path, const char *mode); +int global_open(GLOBAL *g, int msgid, const char *filename, int flags); +int global_open(GLOBAL *g, int msgid, const char *filename, int flags, int mode); diff --git a/storage/connect/plgdbutl.cpp b/storage/connect/plgdbutl.cpp index 3fd2b9ff949..fd697fec314 100644 --- a/storage/connect/plgdbutl.cpp +++ b/storage/connect/plgdbutl.cpp @@ -128,6 +128,91 @@ void CloseXMLFile(PGLOBAL, PFBLOCK, bool); void CloseXML2File(PGLOBAL, PFBLOCK, bool); #endif // LIBXML2_SUPPORT + +/***********************************************************************/ +/* Routines for file IO with error reporting to g->Message */ +/***********************************************************************/ +static void +global_open_error_msg(GLOBAL *g, int msgid, const char *path, const char *mode) +{ + int len; + switch (msgid) + { + case MSGID_CANNOT_OPEN: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(CANNOT_OPEN), // Cannot open %s + path); + break; + + case MSGID_OPEN_MODE_ERROR: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(OPEN_MODE_ERROR), // "Open(%s) error %d on %s" + mode, (int) errno, path); + break; + + case MSGID_OPEN_MODE_STRERROR: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(OPEN_MODE_ERROR) ": %s", // Open(%s) error %d on %s: %s + mode, (int) errno, path, strerror(errno)); + break; + + case MSGID_OPEN_STRERROR: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(OPEN_STRERROR), // "open error: %s" + strerror(errno)); + break; + + case MSGID_OPEN_ERROR_AND_STRERROR: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(OPEN_ERROR) "%s",// "Open error %d in mode %d on %s: %s" + errno, mode, path, strerror(errno)); + break; + + case MSGID_OPEN_EMPTY_FILE: + len= snprintf(g->Message, sizeof(g->Message) - 1, + MSG(OPEN_EMPTY_FILE), // "Opening empty file %s: %s" + path, strerror(errno)); + default: + DBUG_ASSERT(0); + /* Fall through*/ + case 0: + len= 0; + } + g->Message[len]= '\0'; +} + + +FILE *global_fopen(GLOBAL *g, int msgid, const char *path, const char *mode) +{ + FILE *f; + if (!(f= fopen(path, mode))) + global_open_error_msg(g, msgid, path, mode); + return f; +} + + +int global_open(GLOBAL *g, int msgid, const char *path, int flags) +{ + int h; + if ((h= open(path, flags)) <= 0) + global_open_error_msg(g, msgid, path, ""); + return h; +} + + +int global_open(GLOBAL *g, int msgid, const char *path, int flags, int mode) +{ + int h; + if ((h= open(path, flags, mode)) <= 0) + { + char modestr[64]; + snprintf(modestr, sizeof(modestr), "%d", mode); + global_open_error_msg(g, msgid, path, modestr); + } + return h; +} + + /**************************************************************************/ /* Utility for external callers (such as XDB) */ /**************************************************************************/ @@ -739,7 +824,7 @@ FILE *PlugOpenFile(PGLOBAL g, LPCSTR fname, LPCSTR ftype) htrc("dbuserp=%p\n", dbuserp); } // endif trace - if ((fop = fopen(fname, ftype)) != NULL) { + if ((fop= global_fopen(g, MSGID_OPEN_MODE_STRERROR, fname, ftype)) != NULL) { if (trace) htrc(" fop=%p\n", fop); @@ -1417,9 +1502,9 @@ int FileComp(PGLOBAL g, char *file1, char *file2) for (i = 0; i < 2; i++) { #if defined(WIN32) - h[i] = open(fn[i], _O_RDONLY | _O_BINARY); + h[i]= global_open(g, MSGID_NONE, fn[i], _O_RDONLY | _O_BINARY); #else // !WIN32 - h[i] = open(fn[i], O_RDONLY); + h[i]= global_open(g, MSGOD_NONE, fn[i], O_RDONLY); #endif // !WIN32 if (h[i] == -1) { diff --git a/storage/connect/tabfmt.cpp b/storage/connect/tabfmt.cpp index 4660de932ed..f2efa775d5e 100644 --- a/storage/connect/tabfmt.cpp +++ b/storage/connect/tabfmt.cpp @@ -130,10 +130,8 @@ PQRYRES CSVColumns(PGLOBAL g, char *fn, char sep, char q, int hdr, int mxr) /*********************************************************************/ PlugSetPath(filename, fn, PlgGetDataPath(g)); - if (!(infile = fopen(filename, "r"))) { - sprintf(g->Message, MSG(CANNOT_OPEN), filename); + if (!(infile= global_fopen(g, MSGID_CANNOT_OPEN, filename, "r"))) return NULL; - } // endif infile if (hdr) { /*******************************************************************/ diff --git a/storage/connect/tabmul.cpp b/storage/connect/tabmul.cpp index 21fdba42d3d..1b272196e0f 100644 --- a/storage/connect/tabmul.cpp +++ b/storage/connect/tabmul.cpp @@ -231,12 +231,8 @@ bool TDBMUL::InitFileNames(PGLOBAL g) char *p; FILE *stream; - if (!(stream = fopen(filename, "r"))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), - "r", (int)errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); + if (!(stream= global_fopen(g, MSGID_OPEN_MODE_STRERROR, filename, "r"))) return true; - } // endif stream while (n < PFNZ) { if (!fgets(filename, sizeof(filename), stream)) { diff --git a/storage/connect/tabxml.cpp b/storage/connect/tabxml.cpp index d4cb0168ae7..5a465687521 100644 --- a/storage/connect/tabxml.cpp +++ b/storage/connect/tabxml.cpp @@ -388,7 +388,7 @@ int TDBXML::LoadTableFile(PGLOBAL g) // Parse the XML file if (Docp->ParseFile(filename)) { // Does the file exist? - int h = open(filename, _O_RDONLY); + int h= global_open(g, MSGID_NONE, filename, _O_RDONLY); rc = (h == -1 && errno == ENOENT) ? RC_NF : RC_INFO; if (h != -1) close(h); diff --git a/storage/connect/xindex.cpp b/storage/connect/xindex.cpp index 02d3127f820..e19f7243f72 100644 --- a/storage/connect/xindex.cpp +++ b/storage/connect/xindex.cpp @@ -2097,9 +2097,7 @@ bool XFILE::Open(PGLOBAL g, char *filename, MODE mode) return true; } // endswitch mode - if (!(Xfile = fopen(filename, pmod))) { - sprintf(g->Message, MSG(OPEN_MODE_ERROR), pmod, errno, filename); - strcat(strcat(g->Message, ": "), strerror(errno)); + if (!(Xfile= global_fopen(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, pmod))) { #if defined(TRACE) printf("Open: %s\n", g->Message); #endif // TRACE @@ -2431,12 +2429,10 @@ bool XHUGE::Open(PGLOBAL g, char *filename, MODE mode) return true; } // endswitch - Hfile = open(filename, oflag, pmod); + Hfile= global_open(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, oflag, pmod); if (Hfile == INVALID_HANDLE_VALUE) { rc = errno; - sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename); - strcat(g->Message, strerror(errno)); #if defined(TRACE) printf("Open: %s\n", g->Message); #endif // TRACE