OSspecific/POSIX: Dynamically resize the path buffer in cwd

Starting from an initial buffer size of 256 it is incremented in steps
of 256 upto the maximum of 4096 as required.
Based on patch provided by Bruno Santos
Resolves bug-report http://www.openfoam.org/mantisbt/view.php?id=1944
This commit is contained in:
Henry Weller
2015-12-13 18:33:01 +00:00
parent 107b9370f0
commit a55d5a9b55
2 changed files with 57 additions and 24 deletions

View File

@ -246,19 +246,44 @@ Foam::fileName Foam::home(const string& userName)
Foam::fileName Foam::cwd() Foam::fileName Foam::cwd()
{ {
char buf[256]; label pathLengthLimit = POSIX::pathLengthChunk;
if (::getcwd(buf, sizeof(buf))) List<char> path(pathLengthLimit);
{
return buf;
}
else
{
FatalErrorInFunction
<< "Couldn't get the current working directory"
<< exit(FatalError);
return fileName::null; // Resize path if getcwd fails with an ERANGE error
while(pathLengthLimit == path.size())
{
if (::getcwd(path.data(), path.size()))
{
return path.data();
}
else if(errno == ERANGE)
{
// Increment path length upto the pathLengthMax limit
if
(
(pathLengthLimit += POSIX::pathLengthChunk)
>= POSIX::pathLengthMax
)
{
FatalErrorInFunction
<< "Attempt to increase path length beyond limit of "
<< POSIX::pathLengthMax
<< exit(FatalError);
}
path.setSize(pathLengthLimit);
}
else
{
break;
}
} }
FatalErrorInFunction
<< "Couldn't get the current working directory"
<< exit(FatalError);
return fileName::null;
} }
@ -670,8 +695,8 @@ Foam::fileNameList Foam::readDir
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "readDir(const fileName&, const fileType, const bool filtergz)" InfoInFunction
<< " : reading directory " << directory << endl; << "reading directory " << directory << endl;
} }
// Setup empty string list MAXTVALUES long // Setup empty string list MAXTVALUES long
@ -691,9 +716,8 @@ Foam::fileNameList Foam::readDir
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "readDir(const fileName&, const fileType, " InfoInFunction
"const bool filtergz) : cannot open directory " << "cannot open directory " << directory << endl;
<< directory << endl;
} }
} }
else else
@ -824,7 +848,8 @@ bool Foam::cp(const fileName& src, const fileName& dest)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "Copying : " << src/contents[i] InfoInFunction
<< "Copying : " << src/contents[i]
<< " to " << destFile/contents[i] << endl; << " to " << destFile/contents[i] << endl;
} }
@ -838,7 +863,8 @@ bool Foam::cp(const fileName& src, const fileName& dest)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "Copying : " << src/subdirs[i] InfoInFunction
<< "Copying : " << src/subdirs[i]
<< " to " << destFile << endl; << " to " << destFile << endl;
} }
@ -856,7 +882,8 @@ bool Foam::ln(const fileName& src, const fileName& dst)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "Create softlink from : " << src << " to " << dst InfoInFunction
<< "Create softlink from : " << src << " to " << dst
<< endl; << endl;
} }
@ -893,7 +920,8 @@ bool Foam::mv(const fileName& src, const fileName& dst)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "Move : " << src << " to " << dst << endl; InfoInFunction
<< "Move : " << src << " to " << dst << endl;
} }
if if
@ -919,7 +947,8 @@ bool Foam::mvBak(const fileName& src, const std::string& ext)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "mvBak : " << src << " to extension " << ext << endl; InfoInFunction
<< "mvBak : " << src << " to extension " << ext << endl;
} }
if (exists(src, false)) if (exists(src, false))
@ -956,7 +985,8 @@ bool Foam::rm(const fileName& file)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "Removing : " << file << endl; InfoInFunction
<< "Removing : " << file << endl;
} }
// Try returning plain file name; if not there, try with .gz // Try returning plain file name; if not there, try with .gz
@ -976,7 +1006,7 @@ bool Foam::rmDir(const fileName& directory)
{ {
if (POSIX::debug) if (POSIX::debug)
{ {
Info<< "rmDir(const fileName&) : " InfoInFunction
<< "removing directory " << directory << endl; << "removing directory " << directory << endl;
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -48,6 +48,9 @@ namespace POSIX
{ {
//- Declare name of the class and its debug switch //- Declare name of the class and its debug switch
NamespaceName("POSIX"); NamespaceName("POSIX");
const label pathLengthChunk = 256;
const label pathLengthMax = 4096;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //