early directory validation
Allow early prefix directory validation to help prevent failed games and lost save files due to incorrect config file settings.
This commit is contained in:
@@ -168,3 +168,4 @@ travel option
|
|||||||
mouse_support wincap option
|
mouse_support wincap option
|
||||||
debug mode: #panic routine to test panic() and panic save file generation
|
debug mode: #panic routine to test panic() and panic save file generation
|
||||||
a new PANICLOG optional file to log the reason for panic and impossible messages
|
a new PANICLOG optional file to log the reason for panic and impossible messages
|
||||||
|
added validate_prefix_locations() for early directory prefix validation
|
||||||
|
|||||||
@@ -639,6 +639,7 @@ E void FDECL(check_recordfile, (const char *));
|
|||||||
E void NDECL(read_wizkit);
|
E void NDECL(read_wizkit);
|
||||||
#endif
|
#endif
|
||||||
E void FDECL(paniclog, (const char *, const char *));
|
E void FDECL(paniclog, (const char *, const char *));
|
||||||
|
E int FDECL(validate_prefix_locations, (char *));
|
||||||
|
|
||||||
/* ### fountain.c ### */
|
/* ### fountain.c ### */
|
||||||
|
|
||||||
|
|||||||
38
src/files.c
38
src/files.c
@@ -153,6 +153,42 @@ int whichprefix, buffnum;
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* reasonbuf must be at least BUFSZ, supplied by caller */
|
||||||
|
int
|
||||||
|
validate_prefix_locations(reasonbuf)
|
||||||
|
char *reasonbuf;
|
||||||
|
{
|
||||||
|
#if defined(NOCWD_ASSUMPTIONS)
|
||||||
|
FILE *fp;
|
||||||
|
const char *filename;
|
||||||
|
int prefcnt, failcount = 0;
|
||||||
|
char failbuf[BUFSZ];
|
||||||
|
|
||||||
|
failbuf[0] = '\0';
|
||||||
|
if (reasonbuf) reasonbuf[0] = '\0';
|
||||||
|
for (prefcnt = 1; prefcnt < PREFIX_COUNT; prefcnt++) {
|
||||||
|
filename = fqname("validate", prefcnt, 3);
|
||||||
|
if ((fp = fopen(filename, "w"))) {
|
||||||
|
fclose(fp);
|
||||||
|
(void) unlink(filename);
|
||||||
|
} else {
|
||||||
|
if (failcount) {
|
||||||
|
Strcat(failbuf,", ");
|
||||||
|
if (reasonbuf) Strcat(reasonbuf,", ");
|
||||||
|
}
|
||||||
|
/* the paniclog entry gets the value of errno */
|
||||||
|
Sprintf(eos(failbuf), "%s:%d", fqn_prefix_names[prefcnt], errno);
|
||||||
|
if (reasonbuf) Strcat(reasonbuf, fqn_prefix_names[prefcnt]);
|
||||||
|
failcount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failcount) {
|
||||||
|
paniclog("Invalid locations", failbuf);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* fopen a file, with OS-dependent bells and whistles */
|
/* fopen a file, with OS-dependent bells and whistles */
|
||||||
/* NOTE: a simpler version of this routine also exists in util/dlb_main.c */
|
/* NOTE: a simpler version of this routine also exists in util/dlb_main.c */
|
||||||
@@ -1289,7 +1325,7 @@ char *tmp_levels;
|
|||||||
} else if (match_varname(buf, "LEVELDIR", 4) ||
|
} else if (match_varname(buf, "LEVELDIR", 4) ||
|
||||||
match_varname(buf, "LEVELS", 4)) {
|
match_varname(buf, "LEVELS", 4)) {
|
||||||
adjust_prefix(bufp, LEVELPREFIX);
|
adjust_prefix(bufp, LEVELPREFIX);
|
||||||
} else if (match_varname(buf, "SAVE", 4)) {
|
} else if (match_varname(buf, "SAVEDIR", 4)) {
|
||||||
adjust_prefix(bufp, SAVEPREFIX);
|
adjust_prefix(bufp, SAVEPREFIX);
|
||||||
} else if (match_varname(buf, "BONESDIR", 5)) {
|
} else if (match_varname(buf, "BONESDIR", 5)) {
|
||||||
adjust_prefix(bufp, BONESPREFIX);
|
adjust_prefix(bufp, BONESPREFIX);
|
||||||
|
|||||||
@@ -105,6 +105,9 @@ char *argv[];
|
|||||||
|
|
||||||
register int fd;
|
register int fd;
|
||||||
register char *dir;
|
register char *dir;
|
||||||
|
#ifdef NOCWD_ASSUMPTIONS
|
||||||
|
char failbuf[BUFSZ];
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(__BORLANDC__) && !defined(_WIN32)
|
#if defined(__BORLANDC__) && !defined(_WIN32)
|
||||||
startup();
|
startup();
|
||||||
@@ -171,6 +174,14 @@ char *argv[];
|
|||||||
#endif
|
#endif
|
||||||
initoptions();
|
initoptions();
|
||||||
|
|
||||||
|
#ifdef NOCWD_ASSUMPTIONS
|
||||||
|
if (!validate_prefix_locations(failbuf)) {
|
||||||
|
raw_printf("Some invalid directory locations were specified:\n\t%s\n",
|
||||||
|
failbuf);
|
||||||
|
nethack_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(TOS) && defined(TEXTCOLOR)
|
#if defined(TOS) && defined(TEXTCOLOR)
|
||||||
if (iflags.BIOS && iflags.use_color)
|
if (iflags.BIOS && iflags.use_color)
|
||||||
set_colors();
|
set_colors();
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ gotlock:
|
|||||||
chdirx(orgdir, 0);
|
chdirx(orgdir, 0);
|
||||||
#if defined(WIN32)
|
#if defined(WIN32)
|
||||||
error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n",
|
error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n",
|
||||||
fq_lock, strerror(ern), "Are you sure that the directory",
|
fq_lock, strerror(ern), " Are you sure that the directory",
|
||||||
fqn_prefix[LEVELPREFIX]);
|
fqn_prefix[LEVELPREFIX]);
|
||||||
#else
|
#else
|
||||||
error("cannot creat file (%s.)", fq_lock);
|
error("cannot creat file (%s.)", fq_lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user