Provide useful error message if datadir is not writable. #12630

pull murrayn wants to merge 1 commits into bitcoin:master from murrayn:datadir_writable changing 4 files +33 −0
  1. murrayn commented at 11:37 AM on March 7, 2018: contributor

    If the --datadir exists, but is not writable, the current error message on startup is 'Cannot obtain a lock on data directory foo. Bitcoin Core is probably already running.' This is misleading.

    I believe this PR addresses #11668, although the issue is not Windows-specific.

  2. MarcoFalke commented at 1:02 PM on March 7, 2018: member

    Don't forget to add a test.

  3. in src/util.cpp:428 in 6df3caf6e9 outdated
     421 | @@ -418,6 +422,20 @@ void ReleaseDirectoryLocks()
     422 |      dir_locks.clear();
     423 |  }
     424 |  
     425 | +bool DirIsWritable(const fs::path& directory)
     426 | +{
     427 | +    // Use a UUID for a unique temp filename.
     428 | +    fs::path tmpFile = directory / boost::lexical_cast<std::string>((boost::uuids::random_generator())());
    


    laanwj commented at 1:21 PM on March 7, 2018:

    no need for lexical cast and uuid here: you can use fs::unique_path if it needs to be unique, but I'm not sure if hardcoding a name for the probe file wouldn't be better for troubleshooting.

    Another (potentially better, as it will follow for wallet paths too) possibility would be to report a different failure from the LockDirectory function, as creating the lock file is done separately. E.g. change this to actually report an error:

        // Create empty lock file if it doesn't exist.
        FILE* file = fsbridge::fopen(pathLockFile, "a");
        if (file) fclose(file);
    

    promag commented at 5:07 PM on March 7, 2018:

    Agree, just improve error handling.

  4. laanwj added the label Utils/log/libs on Mar 7, 2018
  5. practicalswift commented at 2:08 PM on March 7, 2018: contributor

    Concept ACK, but please try to solve this without introducing more Boost usage. We want to get rid of Boost – see https://github.com/bitcoin/bitcoin/projects/3 :-)

  6. randolf commented at 3:18 PM on March 7, 2018: contributor

    Concept ACK. I agree with not needing UUID, etc., and not using the Boost libraries.

  7. randolf approved
  8. eklitzke commented at 4:31 PM on March 10, 2018: contributor

    On POSIX you can do this in one line of code with access(3), which just relied on <unistd.h>. I believe that MingW (which we use for Windows builds) provides a compatibility version of that header, so I think you can just use access() on the directory and completely remove the boost fs stuff.

  9. murrayn commented at 8:23 AM on March 12, 2018: contributor

    @eklitzke, access() works on Linux for me, but not Windows. I consistently get failure with errno ENOENT. I believe access() can be flaky with NFS as well, but I didn't test that myself. My most recent commit works on Windows and Linux.

  10. eklitzke commented at 5:10 AM on March 13, 2018: contributor

    Alright in that case disregard. utACK 8a79879d2dfb5bcf1a020949044f246ce666e3d3 (and I'll note there aren't any credible proposed alternatives for boost::filesystem so I don't think that should be an objection).

  11. murrayn force-pushed on Mar 13, 2018
  12. laanwj commented at 4:10 PM on March 14, 2018: member

    utACK after squash. I still think the check for write-ability could be rolled into LockDirectory, as that function creates the lock file by writing by definition (and that would take care of other directories too, such as wallet directories), but anyhow I'm ok with this solution too.

    and I'll note there aren't any credible proposed alternatives for boost::filesystem

    Right - we'll just wait for std::filesystem from C++17. @theuni has a minimal patch somewhere that makes it work with that.

  13. Provide relevant error message if datadir is not writable. 8674e74b47
  14. murrayn force-pushed on Mar 15, 2018
  15. laanwj commented at 3:49 PM on March 19, 2018: member

    It works:

    $ src/bitcoind -datadir=/tmp/test1
    Error: Specified data directory "/tmp/test1" does not exist.
    $ mkdir /tmp/test2 && chmod 0 /tmp/test2
    $ src/bitcoind -datadir=/tmp/test2
    Error: Cannot write to data directory '/tmp/test2'; check permissions.
    $ mkdir /tmp/test3
    $ src/bitcoind -datadir=/tmp/test3 -daemon
    $ src/bitcoind -datadir=/tmp/test3
    Error: Cannot obtain a lock on data directory /tmp/test3. Bitcoin Core is probably already running.
    

    Nit: It's kind of funny that all three error messages wrap the path differently. The first puts it in ", the second uses ', the third prints it unwrapped.

  16. eklitzke commented at 7:45 AM on March 20, 2018: contributor

    utACK 8674e74b47c1f6e86a367cfbc738fcc9812b616b

  17. laanwj merged this on Mar 22, 2018
  18. laanwj closed this on Mar 22, 2018

  19. laanwj referenced this in commit c290508a5e on Mar 22, 2018
  20. PastaPastaPasta referenced this in commit a6ae63e57b on Sep 27, 2020
  21. PastaPastaPasta referenced this in commit ef0a58ba37 on Oct 22, 2020
  22. random-zebra referenced this in commit 61a098a775 on Aug 5, 2021
  23. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me