Commit 22a8fb03 authored by Alexander Barkov's avatar Alexander Barkov

Introducing functions global_open() and global_fopen() for these purposes:

- Removing duplicate code to generate error message text
- In the future they will most likely check secure_file_priv directory.


modified:
  storage/connect/filamdbf.cpp
  storage/connect/filamfix.cpp
  storage/connect/filamtxt.cpp
  storage/connect/filamvct.cpp
  storage/connect/libdoc.cpp
  storage/connect/maputil.cpp
  storage/connect/plgdbsem.h
  storage/connect/plgdbutl.cpp
  storage/connect/tabfmt.cpp
  storage/connect/tabmul.cpp
  storage/connect/tabxml.cpp
  storage/connect/xindex.cpp
parent 9aa88d50
......@@ -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);
......
......@@ -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;
......
......@@ -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);
......
......@@ -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
......
......@@ -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
......
......@@ -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. */
......
......@@ -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);
......@@ -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) {
......
......@@ -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) {
/*******************************************************************/
......
......@@ -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)) {
......
......@@ -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);
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment