-3

The TL;DR of my code is the following:

server::server(boost::filesystem::path mappath) : mappath(mappath) {
    if(boost::filesystem::is_directory(mappath) && boost::filesystem::exists(mappath)) {
        // Do some stuff here
    } else {
        boost::filesystem::create_directory(mappath);
    }
}

The code works when mappath exists (barely, as I find Boost to be segfaulting in almost every function).
However, when it doesn't, it throws an exception with the message "Bad address".
When I print mappath via std::cout, it returns:

"/home/myusername/.testfolder/huni/ENTER YOUR TEXT HERE"

Which is correct.

Note that, when I try printing mappath inside the else statement, it segfaults.
I've deduced that something messes with mappath in either is_directory or exists, as there were no errors when printing before the if statement.

Dan Joe
  • 17
  • 1
  • 3
  • You might be looking for `boost::filesystem::create_directories`. But that's just a guess since we can't reproduce your problem. – Drew Dormann May 20 '18 at 21:47
  • @DrewDormann After replacing it with `create_directories`, it now segfaults where it threw the exception. – Dan Joe May 20 '18 at 21:49
  • We can only guess what might be wrong with your program. Please [edit] your question to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Drew Dormann May 20 '18 at 22:05
  • This is a minimal verifiable example, as I ran the code I posted myself, and it still errors. – Dan Joe May 20 '18 at 22:08
  • 1
    Funny, I got "use of undeclared identifier 'server'" when I tried to run the code you posted. – T.C. May 20 '18 at 23:06
  • I'd start with the simplest Boost program that you can make segfault, and then *figure out why that's happening*. There's probably a silent failure somewhere that's causing strange behavior. – Steve May 20 '18 at 23:07
  • 1
    Regardless, make that smallest program that fails and post the code here. One function isn't going to help others help you, unfortunately. – Steve May 20 '18 at 23:08
  • @T.C. Haha, very funny /s. That could've obviously been put in the `main` function instead. This is that smallest program that fails, I already said so. There's absolutely no documentation on why Boost fails with `Bad address`, if there were I would've known my problem by now. – Dan Joe May 21 '18 at 07:06
  • What is the concrete error message? Which function throws `Bad address`? – Thomas Sablik May 21 '18 at 13:20
  • It is said in the title that `create_directory` throws `filesystem_error` with the message `Bad address` – Dan Joe May 21 '18 at 13:25

2 Answers2

0

I wrote a MCVE for myself. When the path does not exist, boost throws

terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::create_directory: No such file or directory
Aborted (core dumped)

because your program first checks if the path is a directory and then it checks if the path exists (correct).

When the path exists and it is a directory, the program runs without output and does nothing (correct).

When the path exists and is a file, boost throws

terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::create_directory: File exists
Aborted (core dumped)

because it can't create a directory (correct).

Hence your snippet does what it should do. Maybe you should change the order in your if statement and add a check to your else:

#include <boost/filesystem.hpp>
#include <iostream>

class server {
public:
  server(boost::filesystem::path mappath) : mappath(mappath) {
    if(boost::filesystem::exists(mappath) && boost::filesystem::is_directory(mappath)) {
        // Do some stuff here
    } else if (!boost::filesystem::exists(mappath)) {
        boost::filesystem::create_directory(mappath);
    }
  }
private:
  boost::filesystem::path mappath;
};

int main() {
  server s("/path/test");
  return 0;
}

Now the program checks, if the path exists. If the path exists, the program checks if the path is a directory. If the path doesn't exist, the directory is created.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

Turned out Bad address is a POSIX-specific error.

Also the reason I couldn't print mappath in the else clause of my if statement, was that is_directory was messing with the reference.
What it was actually doing I couldn't figure out.

So, I've switched from Boost to something that actually works.

Dan Joe
  • 17
  • 1
  • 3