1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/fs/auto_cleaners.hpp"
31 #include "utils/format/macros.hpp"
32 #include "utils/fs/exceptions.hpp"
33 #include "utils/fs/operations.hpp"
34 #include "utils/logging/macros.hpp"
35 #include "utils/sanity.hpp"
36 #include "utils/signals/interrupts.hpp"
38 namespace fs
= utils::fs
;
39 namespace signals
= utils::signals
;
42 /// Shared implementation of the auto_directory.
43 struct utils::fs::auto_directory::impl
{
44 /// The path to the directory being managed.
47 /// Whether cleanup() has been already executed or not.
52 /// \param directory_ The directory to grab the ownership of.
53 impl(const path
& directory_
) :
54 _directory(directory_
),
64 } catch (const fs::error
& e
) {
65 LW(F("Failed to auto-cleanup directory '%s': %s") % _directory
%
70 /// Removes the directory.
72 /// See the cleanup() method of the auto_directory class for details.
77 // Mark this as cleaned first so that, in case of failure, we don't
78 // reraise the error from the destructor.
81 fs::rmdir(_directory
);
87 /// Constructs a new auto_directory and grabs ownership of a directory.
89 /// \param directory_ The directory to grab the ownership of.
90 fs::auto_directory::auto_directory(const path
& directory_
) :
91 _pimpl(new impl(directory_
))
96 /// Deletes the managed directory; must be empty.
98 /// This should not be relied on because it cannot provide proper error
99 /// reporting. Instead, the caller should use the cleanup() method.
100 fs::auto_directory::~auto_directory(void)
105 /// Creates a self-destructing temporary directory.
107 /// \param path_template The template for the temporary path, which is a
108 /// basename that is created within the TMPDIR. Must contain the XXXXXX
109 /// pattern, which is atomically replaced by a random unique string.
111 /// \return The self-destructing directory.
113 /// \throw fs::error If the creation fails.
115 fs::auto_directory::mkdtemp(const std::string
& path_template
)
117 signals::interrupts_inhibiter inhibiter
;
118 const fs::path directory_
= fs::mkdtemp(path_template
);
120 return auto_directory(directory_
);
122 fs::rmdir(directory_
);
128 /// Gets the directory managed by this auto_directory.
130 /// \return The path to the managed directory.
132 fs::auto_directory::directory(void) const
134 return _pimpl
->_directory
;
138 /// Deletes the managed directory; must be empty.
140 /// This operation is idempotent.
142 /// \throw fs::error If there is a problem removing any directory or file.
144 fs::auto_directory::cleanup(void)
150 /// Shared implementation of the auto_file.
151 struct utils::fs::auto_file::impl
{
152 /// The path to the file being managed.
155 /// Whether removed() has been already executed or not.
160 /// \param file_ The file to grab the ownership of.
161 impl(const path
& file_
) :
172 } catch (const fs::error
& e
) {
173 LW(F("Failed to auto-cleanup file '%s': %s") % _file
%
178 /// Removes the file.
180 /// See the remove() method of the auto_file class for details.
185 // Mark this as cleaned first so that, in case of failure, we don't
186 // reraise the error from the destructor.
195 /// Constructs a new auto_file and grabs ownership of a file.
197 /// \param file_ The file to grab the ownership of.
198 fs::auto_file::auto_file(const path
& file_
) :
199 _pimpl(new impl(file_
))
204 /// Deletes the managed file.
206 /// This should not be relied on because it cannot provide proper error
207 /// reporting. Instead, the caller should use the remove() method.
208 fs::auto_file::~auto_file(void)
213 /// Creates a self-destructing temporary file.
215 /// \param path_template The template for the temporary path, which is a
216 /// basename that is created within the TMPDIR. Must contain the XXXXXX
217 /// pattern, which is atomically replaced by a random unique string.
219 /// \return The self-destructing file.
221 /// \throw fs::error If the creation fails.
223 fs::auto_file::mkstemp(const std::string
& path_template
)
225 signals::interrupts_inhibiter inhibiter
;
226 const fs::path file_
= fs::mkstemp(path_template
);
228 return auto_file(file_
);
236 /// Gets the file managed by this auto_file.
238 /// \return The path to the managed file.
240 fs::auto_file::file(void) const
242 return _pimpl
->_file
;
246 /// Deletes the managed file.
248 /// This operation is idempotent.
250 /// \throw fs::error If there is a problem removing the file.
252 fs::auto_file::remove(void)