Check the existence of directory for logging.
authorHiroshi Inoue <h-inoue@dream.email.ne.jp>
Sat, 29 Oct 2016 05:38:11 +0000 (14:38 +0900)
committerHiroshi Inoue <h-inoue@dream.email.ne.jp>
Sat, 29 Oct 2016 05:38:11 +0000 (14:38 +0900)
dlg_wingui.c
mylog.c

index 7bdd10680cb39632311bd8220ef28c3fe97adc58..9a484d7c0003f777849c30f80f456d569708faa0 100644 (file)
@@ -335,6 +335,27 @@ mylog("GetProcAddres for %s\n", procname);
 }
 #endif /* _HANDLE_ENLIST_IN_DTC_ */
 
+#include <sys/stat.h>
+static const char *IsAnExistingDirectory(const char *path)
+{
+       
+   struct stat st;
+
+   if (stat(path, &st) < 0)
+   {
+       CSTR errmsg_doesnt_exist = "doesn't exist";
+
+       return errmsg_doesnt_exist;
+   }
+   if ((st.st_mode & S_IFDIR) == 0)
+   {
+       CSTR errmsg_isnt_a_dir = "isn't a directory";
+       
+       return errmsg_isnt_a_dir;
+   }
+   return NULL;
+}
+
 LRESULT            CALLBACK
 global_optionsProc(HWND hdlg,
                   UINT wMsg,
@@ -347,6 +368,7 @@ global_optionsProc(HWND hdlg,
 #endif /* _HANDLE_ENLIST_IN_DTC_ */
    ConnInfo    *ci;
    char logdir[PATH_MAX];
+   const char *logmsg;
 
    switch (wMsg)
    {
@@ -377,11 +399,16 @@ global_optionsProc(HWND hdlg,
            switch (GET_WM_COMMAND_ID(wParam, lParam))
            {
                case IDOK:
+                   GetDlgItemText(hdlg, DS_LOGDIR, logdir, sizeof(logdir));
+                   if (logdir[0] && (logmsg = IsAnExistingDirectory(logdir)) != NULL)
+                   {
+                       MessageBox(hdlg, "Folder for logging error", logmsg, MB_ICONEXCLAMATION | MB_OK);
+                       break;
+                   }
                    globals.commlog = IsDlgButtonChecked(hdlg, DRV_COMMLOG);
                    globals.debug = IsDlgButtonChecked(hdlg, DRV_DEBUG);
                    if (writeDriverCommoninfo(ODBCINST_INI, NULL, &globals) < 0)
                        MessageBox(hdlg, "Sorry, impossible to update the values\nWrite permission seems to be needed", "Update Error", MB_ICONEXCLAMATION | MB_OK);
-                   GetDlgItemText(hdlg, DS_LOGDIR, logdir, sizeof(logdir));
                    setLogDir(logdir[0] ? logdir : NULL);
 #ifdef _HANDLE_ENLIST_IN_DTC_
                    hmodule = DtcProc("SetMsdtclog", &proc);
diff --git a/mylog.c b/mylog.c
index 2374da4bf94b5599e9d94d5d779c533085295676..abad7ca4c73ebc2039c0b890685a36ccd6cdf130 100644 (file)
--- a/mylog.c
+++ b/mylog.c
@@ -214,7 +214,8 @@ static FILE *MLOGFP = NULL;
 
 static void MLOG_open()
 {
-   char        filebuf[80];
+   char        filebuf[80], errbuf[160];
+   BOOL        open_error = FALSE;
 
    if (MLOGFP) return;
 
@@ -222,11 +223,19 @@ static void MLOG_open()
    MLOGFP = fopen(filebuf, PG_BINARY_A);
    if (!MLOGFP)
    {
+       int lasterror = GENERAL_ERRNO;
+       open_error = TRUE;
+       snprintf(errbuf, sizeof(errbuf), "%s open error %d\n", filebuf, lasterror);
        generate_homefile(MYLOGFILE, filebuf);
        MLOGFP = fopen(filebuf, PG_BINARY_A);
    }
    if (MLOGFP)
+   {
        setbuf(MLOGFP, NULL);
+       if (open_error)
+           fputs(errbuf, MLOGFP);
+   }
 }
 
 DLL_DECLARE void