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
|
||||
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
|
||||
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);
|
||||
#endif
|
||||
E void FDECL(paniclog, (const char *, const char *));
|
||||
E int FDECL(validate_prefix_locations, (char *));
|
||||
|
||||
/* ### fountain.c ### */
|
||||
|
||||
|
||||
38
src/files.c
38
src/files.c
@@ -153,6 +153,42 @@ int whichprefix, buffnum;
|
||||
#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 */
|
||||
/* 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) ||
|
||||
match_varname(buf, "LEVELS", 4)) {
|
||||
adjust_prefix(bufp, LEVELPREFIX);
|
||||
} else if (match_varname(buf, "SAVE", 4)) {
|
||||
} else if (match_varname(buf, "SAVEDIR", 4)) {
|
||||
adjust_prefix(bufp, SAVEPREFIX);
|
||||
} else if (match_varname(buf, "BONESDIR", 5)) {
|
||||
adjust_prefix(bufp, BONESPREFIX);
|
||||
|
||||
@@ -105,6 +105,9 @@ char *argv[];
|
||||
|
||||
register int fd;
|
||||
register char *dir;
|
||||
#ifdef NOCWD_ASSUMPTIONS
|
||||
char failbuf[BUFSZ];
|
||||
#endif
|
||||
|
||||
#if defined(__BORLANDC__) && !defined(_WIN32)
|
||||
startup();
|
||||
@@ -171,6 +174,14 @@ char *argv[];
|
||||
#endif
|
||||
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 (iflags.BIOS && iflags.use_color)
|
||||
set_colors();
|
||||
|
||||
@@ -186,7 +186,7 @@ gotlock:
|
||||
chdirx(orgdir, 0);
|
||||
#if defined(WIN32)
|
||||
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]);
|
||||
#else
|
||||
error("cannot creat file (%s.)", fq_lock);
|
||||
|
||||
Reference in New Issue
Block a user