Commit a55d5a9b authored by Henry Weller's avatar Henry Weller
Browse files

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
parent 107b9370
......@@ -246,19 +246,44 @@ Foam::fileName Foam::home(const string& userName)
Foam::fileName Foam::cwd()
{
char buf[256];
if (::getcwd(buf, sizeof(buf)))
{
return buf;
}
else
label pathLengthLimit = POSIX::pathLengthChunk;
List<char> path(pathLengthLimit);
// Resize path if getcwd fails with an ERANGE error
while(pathLengthLimit == path.size())
{
FatalErrorInFunction
<< "Couldn't get the current working directory"
<< exit(FatalError);
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);
}
return fileName::null;
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)
{
Info<< "readDir(const fileName&, const fileType, const bool filtergz)"
<< " : reading directory " << directory << endl;
InfoInFunction
<< "reading directory " << directory << endl;
}
// Setup empty string list MAXTVALUES long
......@@ -691,9 +716,8 @@ Foam::fileNameList Foam::readDir
if (POSIX::debug)
{
Info<< "readDir(const fileName&, const fileType, "
"const bool filtergz) : cannot open directory "
<< directory << endl;
InfoInFunction
<< "cannot open directory " << directory << endl;
}
}
else
......@@ -824,7 +848,8 @@ bool Foam::cp(const fileName& src, const fileName& dest)
{
if (POSIX::debug)
{
Info<< "Copying : " << src/contents[i]
InfoInFunction
<< "Copying : " << src/contents[i]
<< " to " << destFile/contents[i] << endl;
}
......@@ -838,7 +863,8 @@ bool Foam::cp(const fileName& src, const fileName& dest)
{
if (POSIX::debug)
{
Info<< "Copying : " << src/subdirs[i]
InfoInFunction
<< "Copying : " << src/subdirs[i]
<< " to " << destFile << endl;
}
......@@ -856,7 +882,8 @@ bool Foam::ln(const fileName& src, const fileName& dst)
{
if (POSIX::debug)
{
Info<< "Create softlink from : " << src << " to " << dst
InfoInFunction
<< "Create softlink from : " << src << " to " << dst
<< endl;
}
......@@ -893,7 +920,8 @@ bool Foam::mv(const fileName& src, const fileName& dst)
{
if (POSIX::debug)
{
Info<< "Move : " << src << " to " << dst << endl;
InfoInFunction
<< "Move : " << src << " to " << dst << endl;
}
if
......@@ -919,7 +947,8 @@ bool Foam::mvBak(const fileName& src, const std::string& ext)
{
if (POSIX::debug)
{
Info<< "mvBak : " << src << " to extension " << ext << endl;
InfoInFunction
<< "mvBak : " << src << " to extension " << ext << endl;
}
if (exists(src, false))
......@@ -956,7 +985,8 @@ bool Foam::rm(const fileName& file)
{
if (POSIX::debug)
{
Info<< "Removing : " << file << endl;
InfoInFunction
<< "Removing : " << file << endl;
}
// Try returning plain file name; if not there, try with .gz
......@@ -976,7 +1006,7 @@ bool Foam::rmDir(const fileName& directory)
{
if (POSIX::debug)
{
Info<< "rmDir(const fileName&) : "
InfoInFunction
<< "removing directory " << directory << endl;
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -48,6 +48,9 @@ namespace POSIX
{
//- Declare name of the class and its debug switch
NamespaceName("POSIX");
const label pathLengthChunk = 256;
const label pathLengthMax = 4096;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment