Skip to content

Commit

Permalink
store temporary files on mac/lnx in tmp locations (#220)
Browse files Browse the repository at this point in the history
* store temporary files on mac/lnx in tmp locations

* guess win azure Temp location
  • Loading branch information
PeterPetrik committed Feb 3, 2021
1 parent d16e510 commit 387a5a0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.suo
*.obj
*.bak
Expand All @@ -10,6 +11,8 @@
*.lo
*.la
*build*
.idea
CMakeLists.txt.user
CMakeFiles/
CMakeCache.txt
docs/build
Expand Down
20 changes: 18 additions & 2 deletions src/tools/Tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,26 @@ Tools::TemporaryFile::TemporaryFile()
m_sFile = std::string(tmpName);

#else
char tmpName[7] = "XXXXXX";
// see https://github.com/boostorg/filesystem/blob/b4d606cdd08640c9bca68f7e97b52245aeef398d/src/operations.cpp#L2783
const char* val = nullptr;

(val = std::getenv("TMPDIR" )) ||
(val = std::getenv("TMP" )) ||
(val = std::getenv("TEMP" )) ||
(val = std::getenv("TEMPDIR"));

# ifdef __ANDROID__
const char* default_tmp = "/data/local/tmp";
# else
const char* default_tmp = "/tmp";
# endif
std::string tempDir ((val != nullptr) ? val : default_tmp);

// now contruct the temporary filename
char tmpName[20] = "spatialindex-XXXXXX";
if (mkstemp(tmpName) == -1)
throw std::ios_base::failure("Tools::TemporaryFile: Cannot create temporary file name.");
m_sFile = tmpName;
m_sFile = tempDir + "/" + tmpName;
#endif

m_pFile = new Tools::BufferedFileWriter(m_sFile, Tools::CREATE);
Expand Down
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ set(SOURCES
)


foreach(test ${SOURCES})
add_executable(test-${DIR}-${test} ${DIR}/${test}.cc)
target_link_libraries(test-${DIR}-${test} spatialindex)
endforeach()

set(DIR tools)
set(SOURCES
TemporaryFile
)


foreach(test ${SOURCES})
add_executable(test-${DIR}-${test} ${DIR}/${test}.cc)
target_link_libraries(test-${DIR}-${test} spatialindex)
Expand Down
36 changes: 36 additions & 0 deletions test/tools/TemporaryFile.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <spatialindex/SpatialIndex.h>
#include <spatialindex/tools/Tools.h>

using namespace SpatialIndex;
using namespace std;

/*
* Test the TemporaryFile
*/
int main(int /*argc*/, char** /*argv*/) {
Tools::TemporaryFile temporaryFile;

const std::string path = temporaryFile.getFileName();

#if defined ( _MSC_VER )
const std::string shouldContain = "Temp";
if (path.find(shouldContain) == std::string::npos) {
cerr << "Test failed: TemporaryFile path " << path << " does not contain " << shouldContain << endl;
return -1;
}
#else
const std::string lnxShouldContain = "/tmp/spatialindex-";
const std::string macShouldContain = "/var/folders/";
if ( (path.find(lnxShouldContain) == std::string::npos) &&
(path.find(macShouldContain) == std::string::npos) )
{
cerr << "Test failed: TemporaryFile path " << path << " does not contain " << lnxShouldContain << " nor " << macShouldContain << endl;
return -1;
}
#endif




return 0;
}

0 comments on commit 387a5a0

Please sign in to comment.