Path: blob/main/contrib/kyua/utils/fs/auto_cleaners.cpp
48081 views
// Copyright 2010 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/fs/auto_cleaners.hpp"2930#include "utils/format/macros.hpp"31#include "utils/fs/exceptions.hpp"32#include "utils/fs/operations.hpp"33#include "utils/fs/path.hpp"34#include "utils/logging/macros.hpp"35#include "utils/noncopyable.hpp"36#include "utils/sanity.hpp"37#include "utils/signals/interrupts.hpp"3839namespace fs = utils::fs;40namespace signals = utils::signals;414243/// Shared implementation of the auto_directory.44struct utils::fs::auto_directory::impl : utils::noncopyable {45/// The path to the directory being managed.46fs::path _directory;4748/// Whether cleanup() has been already executed or not.49bool _cleaned;5051/// Constructor.52///53/// \param directory_ The directory to grab the ownership of.54impl(const path& directory_) :55_directory(directory_),56_cleaned(false)57{58}5960/// Destructor.61~impl(void)62{63try {64this->cleanup();65} catch (const fs::error& e) {66LW(F("Failed to auto-cleanup directory '%s': %s") % _directory %67e.what());68}69}7071/// Removes the directory.72///73/// See the cleanup() method of the auto_directory class for details.74void75cleanup(void)76{77if (!_cleaned) {78// Mark this as cleaned first so that, in case of failure, we don't79// reraise the error from the destructor.80_cleaned = true;8182fs::rmdir(_directory);83}84}85};868788/// Constructs a new auto_directory and grabs ownership of a directory.89///90/// \param directory_ The directory to grab the ownership of.91fs::auto_directory::auto_directory(const path& directory_) :92_pimpl(new impl(directory_))93{94}959697/// Deletes the managed directory; must be empty.98///99/// This should not be relied on because it cannot provide proper error100/// reporting. Instead, the caller should use the cleanup() method.101fs::auto_directory::~auto_directory(void)102{103}104105106/// Creates a self-destructing temporary directory.107///108/// See the notes for fs::mkdtemp_public() for details on the permissions109/// given to the temporary directory, which are looser than what the standard110/// mkdtemp would grant.111///112/// \param path_template The template for the temporary path, which is a113/// basename that is created within the TMPDIR. Must contain the XXXXXX114/// pattern, which is atomically replaced by a random unique string.115///116/// \return The self-destructing directory.117///118/// \throw fs::error If the creation fails.119fs::auto_directory120fs::auto_directory::mkdtemp_public(const std::string& path_template)121{122signals::interrupts_inhibiter inhibiter;123const fs::path directory_ = fs::mkdtemp_public(path_template);124try {125return auto_directory(directory_);126} catch (...) {127fs::rmdir(directory_);128throw;129}130}131132133/// Gets the directory managed by this auto_directory.134///135/// \return The path to the managed directory.136const fs::path&137fs::auto_directory::directory(void) const138{139return _pimpl->_directory;140}141142143/// Deletes the managed directory; must be empty.144///145/// This operation is idempotent.146///147/// \throw fs::error If there is a problem removing any directory or file.148void149fs::auto_directory::cleanup(void)150{151_pimpl->cleanup();152}153154155/// Shared implementation of the auto_file.156struct utils::fs::auto_file::impl : utils::noncopyable {157/// The path to the file being managed.158fs::path _file;159160/// Whether removed() has been already executed or not.161bool _removed;162163/// Constructor.164///165/// \param file_ The file to grab the ownership of.166impl(const path& file_) :167_file(file_),168_removed(false)169{170}171172/// Destructor.173~impl(void)174{175try {176this->remove();177} catch (const fs::error& e) {178LW(F("Failed to auto-cleanup file '%s': %s") % _file %179e.what());180}181}182183/// Removes the file.184///185/// See the remove() method of the auto_file class for details.186void187remove(void)188{189if (!_removed) {190// Mark this as cleaned first so that, in case of failure, we don't191// reraise the error from the destructor.192_removed = true;193194fs::unlink(_file);195}196}197};198199200/// Constructs a new auto_file and grabs ownership of a file.201///202/// \param file_ The file to grab the ownership of.203fs::auto_file::auto_file(const path& file_) :204_pimpl(new impl(file_))205{206}207208209/// Deletes the managed file.210///211/// This should not be relied on because it cannot provide proper error212/// reporting. Instead, the caller should use the remove() method.213fs::auto_file::~auto_file(void)214{215}216217218/// Creates a self-destructing temporary file.219///220/// \param path_template The template for the temporary path, which is a221/// basename that is created within the TMPDIR. Must contain the XXXXXX222/// pattern, which is atomically replaced by a random unique string.223///224/// \return The self-destructing file.225///226/// \throw fs::error If the creation fails.227fs::auto_file228fs::auto_file::mkstemp(const std::string& path_template)229{230signals::interrupts_inhibiter inhibiter;231const fs::path file_ = fs::mkstemp(path_template);232try {233return auto_file(file_);234} catch (...) {235fs::unlink(file_);236throw;237}238}239240241/// Gets the file managed by this auto_file.242///243/// \return The path to the managed file.244const fs::path&245fs::auto_file::file(void) const246{247return _pimpl->_file;248}249250251/// Deletes the managed file.252///253/// This operation is idempotent.254///255/// \throw fs::error If there is a problem removing the file.256void257fs::auto_file::remove(void)258{259_pimpl->remove();260}261262263