File: temp_path.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (223 lines) | stat: -rw-r--r-- 6,678 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/************************************************************************
 *
 * Copyright (C) 2023 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "temp_path.hpp"

#include <core/exceptionmacros.hpp>

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <random>

namespace sight::core::os
{

// To protect the creation of the temporary directory
static std::recursive_mutex s_mutex;

//------------------------------------------------------------------------------

inline static std::string random_name(std::size_t _length)
{
    static constexpr std::string_view s_CHARS {
        "0123456789"
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    };

    static std::mt19937 rg {std::random_device {}()};
    static std::uniform_int_distribution<std::string_view::size_type> pick(0, s_CHARS.size() - 1);

    std::string s;
    s.reserve(_length);

    for(std::size_t i = 0 ; i < _length ; ++i)
    {
        s += s_CHARS[pick(rg)];
    }

    return s;
}

//------------------------------------------------------------------------------

inline static std::filesystem::path generate_temp_dir(
    const std::filesystem::path& _root,
    const std::string& _prefix = std::string()
)
{
    // Protect the static variables
    std::unique_lock guard(s_mutex);

    // Create root, if needed
    std::filesystem::create_directories(_root);

    // Start with 4 random characters, ut we don't more than 64 characters
    for(std::string name = _prefix + random_name(4) ; name.size() < 64 ; name += random_name(2))
    {
        try
        {
            const auto& random_path = _root / name;

            if(!std::filesystem::exists(random_path) && std::filesystem::create_directory(random_path))
            {
                return random_path;
            }
        }
        catch(...)
        {
            // retry...
        }
    }

    SIGHT_THROW("Failed to create temporary directory in '" + _root.string() + "'");
}

//------------------------------------------------------------------------------

inline static std::pair<std::filesystem::path, std::shared_ptr<std::ofstream> > open_temp_file(
    const std::filesystem::path& _path,
    const std::optional<std::ios_base::openmode>& _openmode = std::nullopt
)
{
    std::shared_ptr<std::ofstream> ofstream = std::make_shared<std::ofstream>();
    ofstream->open(_path.string(), _openmode.value_or(std::ios_base::out | std::ios_base::trunc));

    if(ofstream->is_open() && ofstream->good())
    {
        return std::make_pair(_path, _openmode ? ofstream : nullptr);
    }

    SIGHT_THROW("Failed to create a temporary file in '" + _path.string() + "'");
}

//------------------------------------------------------------------------------

inline static std::pair<std::filesystem::path, std::shared_ptr<std::ofstream> > generate_temp_file(
    const std::optional<std::ios_base::openmode>& _openmode = std::nullopt,
    const std::optional<std::filesystem::path>& _path       = std::nullopt
)
{
    if(_path)
    {
        return open_temp_file(*_path, _openmode);
    }

    // Protect the static variables
    std::unique_lock guard(s_mutex);

    const std::filesystem::path& root = temp_path::shared_directory();

    // Start with 4 random characters, ut we don't more than 64 characters
    for(auto name = std::string("f") + random_name(4) ; name.size() < 64 ; name += random_name(2))
    {
        const auto& random_path = root / name;
        if(!std::filesystem::exists(random_path))
        {
            try
            {
                return open_temp_file(random_path, _openmode);
            }
            catch(...)
            {
                // retry...
            }
        }
    }

    SIGHT_THROW("Failed to create a temporary file in '" + root.string() + "'");
}

temp_path::temp_path(const std::pair<std::filesystem::path, std::shared_ptr<std::ofstream> >& _path_and_stream) :
    m_ofstream(_path_and_stream.second),
    m_path(_path_and_stream.first)
{
}

temp_path::~temp_path() noexcept
{
    if(m_ofstream && m_ofstream->is_open())
    {
        m_ofstream->close();
        m_ofstream.reset();
    }

    // use std::error_code instead of exception
    std::error_code error;
    std::filesystem::remove_all(m_path, error);

    SIGHT_WARN_IF("Failed to remove temporary directory '" + m_path.string() + "'", error);
}

//------------------------------------------------------------------------------

std::filesystem::path temp_path::shared_directory(const std::string& _subdirectory_prefix)
{
    // Protect the static variable
    std::unique_lock guard(s_mutex);

    // Create the temporary root directory as a static variable
    static const temp_path s_ROOT(
        std::make_pair(generate_temp_dir(std::filesystem::temp_directory_path()), nullptr)
    );

    if(_subdirectory_prefix.empty())
    {
        // Return the path without creating a subdirectory
        return s_ROOT;
    }

    // Return the path with a subdirectory
    return generate_temp_dir(s_ROOT, _subdirectory_prefix);
}

temp_dir::temp_dir(const std::optional<std::filesystem::path>& _path) :
    temp_path(std::make_pair(_path ? *_path : generate_temp_dir(temp_path::shared_directory()), nullptr))
{
}

temp_file::temp_file(
    const std::optional<std::ios_base::openmode>& _openmode,
    const std::optional<std::filesystem::path>& _path
) :
    temp_path(generate_temp_file(_openmode, _path))
{
}

//------------------------------------------------------------------------------

std::filesystem::path temp_file::unique_path()
{
    return generate_temp_file(std::nullopt).first;
}

//------------------------------------------------------------------------------

std::pair<std::filesystem::path, std::shared_ptr<std::ofstream> > temp_file::unique_stream(
    const std::optional<std::ios_base::openmode>& _openmode
)
{
    return generate_temp_file(_openmode);
}

} // namespace sight::core::os