aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/directory_handler.h
blob: caa3ecd6f5bc00b96ee31f71366a91d78183efe1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>
#include <filesystem>

namespace search::test {

class DirectoryHandler
{
private:
    vespalib::string _mkdir;
    vespalib::string _rmdir;
    bool             _cleanup;

public:
    DirectoryHandler(const vespalib::string &mkdir)
        : DirectoryHandler(mkdir, mkdir)
    {
    }
    DirectoryHandler(const vespalib::string &mkdir,
                     const vespalib::string &rmdir)
        : _mkdir(mkdir),
          _rmdir(rmdir),
          _cleanup(true)
    {
        std::filesystem::create_directories(std::filesystem::path(_mkdir));
    }
    ~DirectoryHandler() {
        if (_cleanup) {
            std::filesystem::remove_all(std::filesystem::path(_rmdir));
        }
    }
    void cleanup(bool v) { _cleanup = v; }
    const  vespalib::string & getDir() const { return _mkdir; }
};

}