From a55d5a9b552c7d3d692f1217dcc041620125c968 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Sun, 13 Dec 2015 18:33:01 +0000 Subject: [PATCH] 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 --- src/OSspecific/POSIX/POSIX.C | 76 +++++++++++++++++++++++++----------- src/OSspecific/POSIX/POSIX.H | 5 ++- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 44ee3c7088..9188b7e24c 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -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 - { - FatalErrorInFunction - << "Couldn't get the current working directory" - << exit(FatalError); + label pathLengthLimit = POSIX::pathLengthChunk; + List path(pathLengthLimit); - 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) { - 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; } diff --git a/src/OSspecific/POSIX/POSIX.H b/src/OSspecific/POSIX/POSIX.H index 9947f3de74..0b17af7325 100644 --- a/src/OSspecific/POSIX/POSIX.H +++ b/src/OSspecific/POSIX/POSIX.H @@ -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; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //