Skip to content

Commit

Permalink
bdls_tempdirectoryguard: null prefix was creating sibling, not child,…
Browse files Browse the repository at this point in the history
…… (#5152)

* bdls_tempdirectoryguard: null prefix was creating sibling, not child, directory.  Fix.
  • Loading branch information
lalawawa authored and GitHub Enterprise committed Jan 21, 2025
1 parent 3ad4221 commit e39ba50
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
10 changes: 4 additions & 6 deletions groups/bdl/bdls/bdls_tempdirectoryguard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ TempDirectoryGuard::TempDirectoryGuard(const bsl::string_view& prefix,
int rc = FilesystemUtil::getSystemTemporaryDirectory(&tmpPath);
if (0 != rc) {
// use path relative to current directory
tmpPath = "";
}

rc = bdls::PathUtil::appendIfValid(&tmpPath, prefix);
if (0 != rc) {
BSLS_ASSERT_INVOKE("Unable to form directory root name");
tmpPath.clear();
}

rc = bdls::FilesystemUtil::createTemporaryDirectory(&d_dirName, tmpPath);
rc = bdls::FilesystemUtil::createTemporarySubdirectory(&d_dirName,
tmpPath,
prefix);
if (0 != rc) {
BSLS_ASSERT_INVOKE("Unable to create temporary directory");
}
Expand Down
9 changes: 6 additions & 3 deletions groups/bdl/bdls/bdls_tempdirectoryguard.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class TempDirectoryGuard {
// DATA
bsl::string d_dirName; // path to the created directory

private:
// NOT IMPLEMENTED
TempDirectoryGuard(const TempDirectoryGuard&);
TempDirectoryGuard& operator=(const TempDirectoryGuard&);
Expand All @@ -95,9 +96,11 @@ class TempDirectoryGuard {
/// created at construction.
~TempDirectoryGuard();

/// Remove the created temporary directory from management so that, upon this guards
/// destruction, the created directory is not removed. Calling `release` on a guard
/// that has previously been released has no effect.
// MANIPULATORS

/// Remove the created temporary directory from management so that, upon
/// this guards destruction, the created directory is not removed. Calling
/// `release` on a guard that has previously been released has no effect.
void release();

// ACCESSORS
Expand Down
52 changes: 50 additions & 2 deletions groups/bdl/bdls/bdls_tempdirectoryguard.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <bslma_testallocator.h>
#include <bslma_usesbslmaallocator.h>

#include <bsls_platform.h>

#include <bsl_iostream.h>

#include <bsl_cstdlib.h>
Expand Down Expand Up @@ -273,7 +275,6 @@ int main(int argc, char *argv[])
<< "TESTING: bootstrap" << endl
<< "==================" << endl;


bsl::string name;

bslma::TestAllocator da("default", veryVeryVerbose);
Expand All @@ -297,6 +298,8 @@ int main(int argc, char *argv[])
ASSERTV(name, bdls::FilesystemUtil::exists(name));
ASSERTV(name, bdls::FilesystemUtil::isDirectory(name));

if (veryVerbose) P(name);

bsl::string_view head, tail;
bdls::PathUtil::splitFilename(&head, &tail, name);

Expand All @@ -321,6 +324,8 @@ int main(int argc, char *argv[])

name = guard.getTempDirName();

if (veryVerbose) P(name);

ASSERTV(name, bdls::FilesystemUtil::exists(name));
ASSERTV(name, bdls::FilesystemUtil::isDirectory(name));

Expand All @@ -334,6 +339,42 @@ int main(int argc, char *argv[])
ASSERTV(name, !bdls::FilesystemUtil::isDirectory(name));
}

#ifdef BSLS_PLATFORM_OS_UNIX
if (verbose) {
cout << "Test basic with no $TMPDIR" << endl;
}
{
bsl::string origTmpDir = bsl::getenv("TMPDIR");
::setenv("TMPDIR", "", true);

const bsl::string_view prefix = "testCase2WithALongString_";

{
bdls::TempDirectoryGuard guard(prefix);

ASSERTV(da.numBytesInUse(), 0 < da.numBytesInUse());
ASSERTV(oa.numBytesInUse(), 0 == oa.numBytesInUse());

name = guard.getTempDirName();

if (veryVerbose) P(name);

ASSERTV(name, bsl::string::npos == name.find('/'));
ASSERTV(name, bdls::FilesystemUtil::exists(name));
ASSERTV(name, bdls::FilesystemUtil::isDirectory(name));

bsl::string_view head, tail;
bdls::PathUtil::splitFilename(&head, &tail, name);

ASSERTV(head, tail, tail.starts_with(prefix));
}

ASSERTV(name, !bdls::FilesystemUtil::exists(name));
ASSERTV(name, !bdls::FilesystemUtil::isDirectory(name));

::setenv("TMPDIR", origTmpDir.c_str(), true);
}
#endif

if (verbose) {
cout << "Test with an empty prefix" << endl;
Expand All @@ -346,6 +387,8 @@ int main(int argc, char *argv[])

name = guard.getTempDirName();

if (veryVerbose) P(name);

ASSERTV(name, bdls::FilesystemUtil::exists(name));
ASSERTV(name, bdls::FilesystemUtil::isDirectory(name));
}
Expand All @@ -369,6 +412,12 @@ int main(int argc, char *argv[])
name2 = guard2.getTempDirName();
name3 = guard3.getTempDirName();

if (veryVerbose) {
P(name1);
P(name2);
P(name3);
}

ASSERTV(name1, name2, name1 != name2);
ASSERTV(name1, name3, name1 != name3);
ASSERTV(name2, name3, name2 != name3);
Expand All @@ -386,7 +435,6 @@ int main(int argc, char *argv[])
ASSERTV(name1, !bdls::FilesystemUtil::exists(name1));
ASSERTV(name2, !bdls::FilesystemUtil::exists(name2));
ASSERTV(name3, !bdls::FilesystemUtil::exists(name3));

}
} break;
case 1: {
Expand Down

0 comments on commit e39ba50

Please sign in to comment.