Skip to content
Snippets Groups Projects

Fail loudly when config isn't writable

Merged AJ Martinez requested to merge prevent-permissions-footgun into master

Discord_x0H0GEd8Tb

No write permissions = tons of broken game behavior: settings won't save, addons won't download, screenshots don't work. Let's nudge people in the right direction.

(The list of discouraged directories in the Windows-specific error message is taken from the installer recommendations: I don't actually know a specific reason you'd avoid installing to Downloads or Desktop besides mess.)

srb2kart_GbanIK0HdY

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Screenshot_20220528_120119

    It works, but it seems to behave quite strangely on Linux. If the file already exists, even if it can't write to it, it will not throw out an error.

    Also, the way you're doing this is dangerous to the configuration file itself. If the game closes ungracefully (someone fatfingers their restart button, crashes unexpectedly) then kartconfig.cfg ends up becoming a 0 B file because the game didn't write to it at the end - the configuration file is gone. You should instead write a .temp file to srb2home instead then remove it at the end. Something like...

    		// If config isn't writable, tons of behavior will be broken.
    		// Fail loudly before things get confusing!
    		char testfile[MAX_WADPATH];
    		snprintf(testfile, sizeof testfile, "%s" PATHSEP "file.tmp", srb2home);
    		testfile[sizeof testfile - 1] = '\0';
    
    		tmpfile = fopen(testfile, "w");
    		if (tmpfile == NULL)
    		{
    #if defined (_WIN32)
    			I_Error("Couldn't write game config.\nMake sure the game is installed somewhere it has write permissions.\n\n(Don't use the Downloads folder, Program Files, or your desktop!\nIf unsure, we recommend making a subfolder in your Documents folder.)");
    #else
    			I_Error("Couldn't write game config.\nMake sure you've installed the game somewhere it has write permissions.");
    #endif
    		}
    		else
    		{
    			fclose(tmpfile);
    			remove(testfile);
    		}

    I tested this and it doesn't cause the problems that this current implementation causes, so please feel free to implement it.

    Edit Note: char testfile[MAX_WADPATH]; should be at the start alongside other declarations to avoid the C90 "mixed declarations and code" warning. Tested on Windows and Linux. I'll rescind my :thumbsdown: when this gets properly implemented.

    Edit 2: :thumbsup:

    Edited by JugadorXEI
  • Author Maintainer

    So much for portability. I'll steal that in the morning, thanks!

  • AJ Martinez added 1 commit

    added 1 commit

    • 73aed464 - Use a temporary file instead of checking against config (from JugadorXEI)

    Compare with previous version

  • Sal mentioned in commit a95a8d5c

    mentioned in commit a95a8d5c

  • merged

Please register or sign in to reply
Loading