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