-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions and objects for Temporary Directories #244
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
712bcaa
Add TempDirectory
mjcarroll c379ef3
Use TempDirectory in tests
mjcarroll 019405c
Merge branch 'ign-common4' into mjcarroll/temp_directory
mjcarroll 2f143ab
Add note about `temp_directory_path`
mjcarroll da1e566
Merge branch 'mjcarroll/temp_directory' of ssh://github.com/ignitionr…
mjcarroll 105a1cc
Address reviewer feedback
mjcarroll 3f0f935
Apply suggestions from code review
mjcarroll 8d04849
Switch to IGN_UTILS_IMPL_PTR
mjcarroll 386d21d
Alphebetize
mjcarroll fe0372f
Update src/TempDirectory.cc
mjcarroll 3a6da4e
windows.h included first
mjcarroll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2021 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#ifndef IGNITION_COMMON_TEMPDIRECTORY_HH_ | ||
#define IGNITION_COMMON_TEMPDIRECTORY_HH_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include <ignition/common/Export.hh> | ||
#include <ignition/common/Filesystem.hh> | ||
#include <ignition/utils/ImplPtr.hh> | ||
|
||
namespace ignition | ||
{ | ||
namespace common | ||
{ | ||
class TempDirectoryPrivate; | ||
|
||
/// \brief Return the path to a directory suitable for temporary files. | ||
/// | ||
/// Calls std::filesystem::temp_directory_path, refer to the standard | ||
/// documentation for your platform for behaviors. | ||
/// \return A directory suitable for temporary files. | ||
std::string IGNITION_COMMON_VISIBLE tempDirectoryPath(); | ||
|
||
/// \brief Create a directory in the tempDirectoryPath by expanding | ||
/// a name template | ||
/// | ||
/// On execution, will create the directory: | ||
/// "_parentPath"/"_baseName" + "XXXXXX", where XXXXXX will be filled | ||
/// out by an OS-appropriate method (eg mkdtmp/_mktemp_s) | ||
/// | ||
/// \param[in] _baseName String to be prepended to the expanded template | ||
/// \param[in] _parentPath Location to create the directory | ||
/// \param[in] _warningOp Allow or suppress filesystem warnings | ||
/// \return Path to newly-created temporary directory | ||
std::string IGNITION_COMMON_VISIBLE createTempDirectory( | ||
const std::string &_baseName, | ||
const std::string &_parentPath, | ||
const FilesystemWarningOp _warningOp = FSWO_LOG_WARNINGS); | ||
|
||
/// \class TempDirectory TempDirectory.hh ignitin/common/TempDirectory.hh | ||
/// \brief Create a temporary directory in the OS temp location. | ||
/// Upon construction, the current working directory will be set to this | ||
/// new temporary directory. | ||
/// Upon destruction, the current working directory will be restored to the | ||
/// location when the TempDirectory object was constructed. | ||
class IGNITION_COMMON_VISIBLE TempDirectory | ||
{ | ||
/// \brief Create a directory in the tempDirectoryPath by expanding | ||
/// a name template. This directory can also be automatically cleaned | ||
/// up when the object goes out of scope. | ||
/// | ||
/// The TempDirectory will have the form $TMPDIR/_subdir/_prefixXXXXX/ | ||
/// | ||
/// \param[in] _prefix String to be expanded for the template | ||
/// \param[in] _subDir Subdirectory in OS $TMPDIR, if desired | ||
/// \param[in] _cleanup True to indicate that the filesystem should | ||
/// be cleaned as part of the destructor | ||
public: TempDirectory(const std::string &_prefix = "temp_dir", | ||
const std::string &_subDir = "ignition", | ||
bool _cleanup = true); | ||
|
||
/// \brief Destroy the temporary directory, removing from filesystem | ||
/// if cleanup is true. | ||
public: ~TempDirectory(); | ||
|
||
/// \brief Indicate if the TempDirectory object is in a valid state | ||
/// and that the folder exists on the filesystem | ||
/// \return true if the TempDirectory is valid | ||
public: bool Valid() const; | ||
|
||
/// \brief Set if the folder on disk should be cleaned. | ||
/// | ||
/// This is useful if you wish to clean by default during a test, but | ||
/// retain the contents of the TempDirectory if the test fails. | ||
/// \param[in] _doCleanup True to indicate that the filesystem should | ||
/// be cleaned as part of the destructor | ||
public: void DoCleanup(bool _doCleanup); | ||
|
||
/// \brief Retrieve the current cleanup flag state | ||
/// \return true if filesystem cleanup will occur | ||
public: bool DoCleanup() const; | ||
|
||
/// \brief Retrieve the fully-expanded temporary directory path | ||
/// \return the temporary directory path | ||
public: std::string Path() const; | ||
|
||
/// \brief Private data pointer. | ||
IGN_UTILS_IMPL_PTR(dataPtr) | ||
}; | ||
} // namespace common | ||
} // namespace ignition | ||
#endif // IGNITION_COMMON_TEMPDIRECTORY_HH_ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this means ign-utils1 is now required by the core
ignition-common4
library. we should update the find_package call to make this dependencyREQUIRED