1*b0d29bc4SBrooks Davis // Copyright 2010 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis // without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis #include "utils/fs/auto_cleaners.hpp"
30*b0d29bc4SBrooks Davis
31*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
32*b0d29bc4SBrooks Davis #include "utils/fs/exceptions.hpp"
33*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
34*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
35*b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
36*b0d29bc4SBrooks Davis #include "utils/noncopyable.hpp"
37*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
38*b0d29bc4SBrooks Davis #include "utils/signals/interrupts.hpp"
39*b0d29bc4SBrooks Davis
40*b0d29bc4SBrooks Davis namespace fs = utils::fs;
41*b0d29bc4SBrooks Davis namespace signals = utils::signals;
42*b0d29bc4SBrooks Davis
43*b0d29bc4SBrooks Davis
44*b0d29bc4SBrooks Davis /// Shared implementation of the auto_directory.
45*b0d29bc4SBrooks Davis struct utils::fs::auto_directory::impl : utils::noncopyable {
46*b0d29bc4SBrooks Davis /// The path to the directory being managed.
47*b0d29bc4SBrooks Davis fs::path _directory;
48*b0d29bc4SBrooks Davis
49*b0d29bc4SBrooks Davis /// Whether cleanup() has been already executed or not.
50*b0d29bc4SBrooks Davis bool _cleaned;
51*b0d29bc4SBrooks Davis
52*b0d29bc4SBrooks Davis /// Constructor.
53*b0d29bc4SBrooks Davis ///
54*b0d29bc4SBrooks Davis /// \param directory_ The directory to grab the ownership of.
implutils::fs::auto_directory::impl55*b0d29bc4SBrooks Davis impl(const path& directory_) :
56*b0d29bc4SBrooks Davis _directory(directory_),
57*b0d29bc4SBrooks Davis _cleaned(false)
58*b0d29bc4SBrooks Davis {
59*b0d29bc4SBrooks Davis }
60*b0d29bc4SBrooks Davis
61*b0d29bc4SBrooks Davis /// Destructor.
~implutils::fs::auto_directory::impl62*b0d29bc4SBrooks Davis ~impl(void)
63*b0d29bc4SBrooks Davis {
64*b0d29bc4SBrooks Davis try {
65*b0d29bc4SBrooks Davis this->cleanup();
66*b0d29bc4SBrooks Davis } catch (const fs::error& e) {
67*b0d29bc4SBrooks Davis LW(F("Failed to auto-cleanup directory '%s': %s") % _directory %
68*b0d29bc4SBrooks Davis e.what());
69*b0d29bc4SBrooks Davis }
70*b0d29bc4SBrooks Davis }
71*b0d29bc4SBrooks Davis
72*b0d29bc4SBrooks Davis /// Removes the directory.
73*b0d29bc4SBrooks Davis ///
74*b0d29bc4SBrooks Davis /// See the cleanup() method of the auto_directory class for details.
75*b0d29bc4SBrooks Davis void
cleanuputils::fs::auto_directory::impl76*b0d29bc4SBrooks Davis cleanup(void)
77*b0d29bc4SBrooks Davis {
78*b0d29bc4SBrooks Davis if (!_cleaned) {
79*b0d29bc4SBrooks Davis // Mark this as cleaned first so that, in case of failure, we don't
80*b0d29bc4SBrooks Davis // reraise the error from the destructor.
81*b0d29bc4SBrooks Davis _cleaned = true;
82*b0d29bc4SBrooks Davis
83*b0d29bc4SBrooks Davis fs::rmdir(_directory);
84*b0d29bc4SBrooks Davis }
85*b0d29bc4SBrooks Davis }
86*b0d29bc4SBrooks Davis };
87*b0d29bc4SBrooks Davis
88*b0d29bc4SBrooks Davis
89*b0d29bc4SBrooks Davis /// Constructs a new auto_directory and grabs ownership of a directory.
90*b0d29bc4SBrooks Davis ///
91*b0d29bc4SBrooks Davis /// \param directory_ The directory to grab the ownership of.
auto_directory(const path & directory_)92*b0d29bc4SBrooks Davis fs::auto_directory::auto_directory(const path& directory_) :
93*b0d29bc4SBrooks Davis _pimpl(new impl(directory_))
94*b0d29bc4SBrooks Davis {
95*b0d29bc4SBrooks Davis }
96*b0d29bc4SBrooks Davis
97*b0d29bc4SBrooks Davis
98*b0d29bc4SBrooks Davis /// Deletes the managed directory; must be empty.
99*b0d29bc4SBrooks Davis ///
100*b0d29bc4SBrooks Davis /// This should not be relied on because it cannot provide proper error
101*b0d29bc4SBrooks Davis /// reporting. Instead, the caller should use the cleanup() method.
~auto_directory(void)102*b0d29bc4SBrooks Davis fs::auto_directory::~auto_directory(void)
103*b0d29bc4SBrooks Davis {
104*b0d29bc4SBrooks Davis }
105*b0d29bc4SBrooks Davis
106*b0d29bc4SBrooks Davis
107*b0d29bc4SBrooks Davis /// Creates a self-destructing temporary directory.
108*b0d29bc4SBrooks Davis ///
109*b0d29bc4SBrooks Davis /// See the notes for fs::mkdtemp_public() for details on the permissions
110*b0d29bc4SBrooks Davis /// given to the temporary directory, which are looser than what the standard
111*b0d29bc4SBrooks Davis /// mkdtemp would grant.
112*b0d29bc4SBrooks Davis ///
113*b0d29bc4SBrooks Davis /// \param path_template The template for the temporary path, which is a
114*b0d29bc4SBrooks Davis /// basename that is created within the TMPDIR. Must contain the XXXXXX
115*b0d29bc4SBrooks Davis /// pattern, which is atomically replaced by a random unique string.
116*b0d29bc4SBrooks Davis ///
117*b0d29bc4SBrooks Davis /// \return The self-destructing directory.
118*b0d29bc4SBrooks Davis ///
119*b0d29bc4SBrooks Davis /// \throw fs::error If the creation fails.
120*b0d29bc4SBrooks Davis fs::auto_directory
mkdtemp_public(const std::string & path_template)121*b0d29bc4SBrooks Davis fs::auto_directory::mkdtemp_public(const std::string& path_template)
122*b0d29bc4SBrooks Davis {
123*b0d29bc4SBrooks Davis signals::interrupts_inhibiter inhibiter;
124*b0d29bc4SBrooks Davis const fs::path directory_ = fs::mkdtemp_public(path_template);
125*b0d29bc4SBrooks Davis try {
126*b0d29bc4SBrooks Davis return auto_directory(directory_);
127*b0d29bc4SBrooks Davis } catch (...) {
128*b0d29bc4SBrooks Davis fs::rmdir(directory_);
129*b0d29bc4SBrooks Davis throw;
130*b0d29bc4SBrooks Davis }
131*b0d29bc4SBrooks Davis }
132*b0d29bc4SBrooks Davis
133*b0d29bc4SBrooks Davis
134*b0d29bc4SBrooks Davis /// Gets the directory managed by this auto_directory.
135*b0d29bc4SBrooks Davis ///
136*b0d29bc4SBrooks Davis /// \return The path to the managed directory.
137*b0d29bc4SBrooks Davis const fs::path&
directory(void) const138*b0d29bc4SBrooks Davis fs::auto_directory::directory(void) const
139*b0d29bc4SBrooks Davis {
140*b0d29bc4SBrooks Davis return _pimpl->_directory;
141*b0d29bc4SBrooks Davis }
142*b0d29bc4SBrooks Davis
143*b0d29bc4SBrooks Davis
144*b0d29bc4SBrooks Davis /// Deletes the managed directory; must be empty.
145*b0d29bc4SBrooks Davis ///
146*b0d29bc4SBrooks Davis /// This operation is idempotent.
147*b0d29bc4SBrooks Davis ///
148*b0d29bc4SBrooks Davis /// \throw fs::error If there is a problem removing any directory or file.
149*b0d29bc4SBrooks Davis void
cleanup(void)150*b0d29bc4SBrooks Davis fs::auto_directory::cleanup(void)
151*b0d29bc4SBrooks Davis {
152*b0d29bc4SBrooks Davis _pimpl->cleanup();
153*b0d29bc4SBrooks Davis }
154*b0d29bc4SBrooks Davis
155*b0d29bc4SBrooks Davis
156*b0d29bc4SBrooks Davis /// Shared implementation of the auto_file.
157*b0d29bc4SBrooks Davis struct utils::fs::auto_file::impl : utils::noncopyable {
158*b0d29bc4SBrooks Davis /// The path to the file being managed.
159*b0d29bc4SBrooks Davis fs::path _file;
160*b0d29bc4SBrooks Davis
161*b0d29bc4SBrooks Davis /// Whether removed() has been already executed or not.
162*b0d29bc4SBrooks Davis bool _removed;
163*b0d29bc4SBrooks Davis
164*b0d29bc4SBrooks Davis /// Constructor.
165*b0d29bc4SBrooks Davis ///
166*b0d29bc4SBrooks Davis /// \param file_ The file to grab the ownership of.
implutils::fs::auto_file::impl167*b0d29bc4SBrooks Davis impl(const path& file_) :
168*b0d29bc4SBrooks Davis _file(file_),
169*b0d29bc4SBrooks Davis _removed(false)
170*b0d29bc4SBrooks Davis {
171*b0d29bc4SBrooks Davis }
172*b0d29bc4SBrooks Davis
173*b0d29bc4SBrooks Davis /// Destructor.
~implutils::fs::auto_file::impl174*b0d29bc4SBrooks Davis ~impl(void)
175*b0d29bc4SBrooks Davis {
176*b0d29bc4SBrooks Davis try {
177*b0d29bc4SBrooks Davis this->remove();
178*b0d29bc4SBrooks Davis } catch (const fs::error& e) {
179*b0d29bc4SBrooks Davis LW(F("Failed to auto-cleanup file '%s': %s") % _file %
180*b0d29bc4SBrooks Davis e.what());
181*b0d29bc4SBrooks Davis }
182*b0d29bc4SBrooks Davis }
183*b0d29bc4SBrooks Davis
184*b0d29bc4SBrooks Davis /// Removes the file.
185*b0d29bc4SBrooks Davis ///
186*b0d29bc4SBrooks Davis /// See the remove() method of the auto_file class for details.
187*b0d29bc4SBrooks Davis void
removeutils::fs::auto_file::impl188*b0d29bc4SBrooks Davis remove(void)
189*b0d29bc4SBrooks Davis {
190*b0d29bc4SBrooks Davis if (!_removed) {
191*b0d29bc4SBrooks Davis // Mark this as cleaned first so that, in case of failure, we don't
192*b0d29bc4SBrooks Davis // reraise the error from the destructor.
193*b0d29bc4SBrooks Davis _removed = true;
194*b0d29bc4SBrooks Davis
195*b0d29bc4SBrooks Davis fs::unlink(_file);
196*b0d29bc4SBrooks Davis }
197*b0d29bc4SBrooks Davis }
198*b0d29bc4SBrooks Davis };
199*b0d29bc4SBrooks Davis
200*b0d29bc4SBrooks Davis
201*b0d29bc4SBrooks Davis /// Constructs a new auto_file and grabs ownership of a file.
202*b0d29bc4SBrooks Davis ///
203*b0d29bc4SBrooks Davis /// \param file_ The file to grab the ownership of.
auto_file(const path & file_)204*b0d29bc4SBrooks Davis fs::auto_file::auto_file(const path& file_) :
205*b0d29bc4SBrooks Davis _pimpl(new impl(file_))
206*b0d29bc4SBrooks Davis {
207*b0d29bc4SBrooks Davis }
208*b0d29bc4SBrooks Davis
209*b0d29bc4SBrooks Davis
210*b0d29bc4SBrooks Davis /// Deletes the managed file.
211*b0d29bc4SBrooks Davis ///
212*b0d29bc4SBrooks Davis /// This should not be relied on because it cannot provide proper error
213*b0d29bc4SBrooks Davis /// reporting. Instead, the caller should use the remove() method.
~auto_file(void)214*b0d29bc4SBrooks Davis fs::auto_file::~auto_file(void)
215*b0d29bc4SBrooks Davis {
216*b0d29bc4SBrooks Davis }
217*b0d29bc4SBrooks Davis
218*b0d29bc4SBrooks Davis
219*b0d29bc4SBrooks Davis /// Creates a self-destructing temporary file.
220*b0d29bc4SBrooks Davis ///
221*b0d29bc4SBrooks Davis /// \param path_template The template for the temporary path, which is a
222*b0d29bc4SBrooks Davis /// basename that is created within the TMPDIR. Must contain the XXXXXX
223*b0d29bc4SBrooks Davis /// pattern, which is atomically replaced by a random unique string.
224*b0d29bc4SBrooks Davis ///
225*b0d29bc4SBrooks Davis /// \return The self-destructing file.
226*b0d29bc4SBrooks Davis ///
227*b0d29bc4SBrooks Davis /// \throw fs::error If the creation fails.
228*b0d29bc4SBrooks Davis fs::auto_file
mkstemp(const std::string & path_template)229*b0d29bc4SBrooks Davis fs::auto_file::mkstemp(const std::string& path_template)
230*b0d29bc4SBrooks Davis {
231*b0d29bc4SBrooks Davis signals::interrupts_inhibiter inhibiter;
232*b0d29bc4SBrooks Davis const fs::path file_ = fs::mkstemp(path_template);
233*b0d29bc4SBrooks Davis try {
234*b0d29bc4SBrooks Davis return auto_file(file_);
235*b0d29bc4SBrooks Davis } catch (...) {
236*b0d29bc4SBrooks Davis fs::unlink(file_);
237*b0d29bc4SBrooks Davis throw;
238*b0d29bc4SBrooks Davis }
239*b0d29bc4SBrooks Davis }
240*b0d29bc4SBrooks Davis
241*b0d29bc4SBrooks Davis
242*b0d29bc4SBrooks Davis /// Gets the file managed by this auto_file.
243*b0d29bc4SBrooks Davis ///
244*b0d29bc4SBrooks Davis /// \return The path to the managed file.
245*b0d29bc4SBrooks Davis const fs::path&
file(void) const246*b0d29bc4SBrooks Davis fs::auto_file::file(void) const
247*b0d29bc4SBrooks Davis {
248*b0d29bc4SBrooks Davis return _pimpl->_file;
249*b0d29bc4SBrooks Davis }
250*b0d29bc4SBrooks Davis
251*b0d29bc4SBrooks Davis
252*b0d29bc4SBrooks Davis /// Deletes the managed file.
253*b0d29bc4SBrooks Davis ///
254*b0d29bc4SBrooks Davis /// This operation is idempotent.
255*b0d29bc4SBrooks Davis ///
256*b0d29bc4SBrooks Davis /// \throw fs::error If there is a problem removing the file.
257*b0d29bc4SBrooks Davis void
remove(void)258*b0d29bc4SBrooks Davis fs::auto_file::remove(void)
259*b0d29bc4SBrooks Davis {
260*b0d29bc4SBrooks Davis _pimpl->remove();
261*b0d29bc4SBrooks Davis }
262