COMP: deprecate domainName and full hostName (#2280)

- unused in regular OpenFOAM code
- POSIX version uses deprecated gethostbyname()
- Windows version never worked

COMP: localize, noexcept on internal OSspecific methods

STYLE: support fileName::Type SYMLINK and LINK as synonyms
This commit is contained in:
Mark Olesen
2022-02-02 11:01:59 +01:00
parent debbcfb7df
commit 62ec2f2ddf
13 changed files with 96 additions and 66 deletions

View File

@ -633,10 +633,10 @@ int main(int argc, char *argv[])
fileName::Type lnAType = lnA.type(false); fileName::Type lnAType = lnA.type(false);
if (lnAType != fileName::LINK) if (lnAType != fileName::SYMLINK)
{ {
FatalErrorIn("Test-fileName") << "Type of softlink " << lnA FatalErrorIn("Test-fileName") << "Type of softlink " << lnA
<< " should be " << fileName::LINK << " should be " << fileName::SYMLINK
<< " but is " << lnAType << exit(FatalError); << " but is " << lnAType << exit(FatalError);
} }
@ -655,10 +655,10 @@ int main(int argc, char *argv[])
<< endl; << endl;
Foam::cp(lnA, lnB, false); Foam::cp(lnA, lnB, false);
if (lnB.type(false) != fileName::LINK) if (lnB.type(false) != fileName::SYMLINK)
{ {
FatalErrorIn("Test-fileName") << "Type of softlink " << lnB FatalErrorIn("Test-fileName") << "Type of softlink " << lnB
<< " should be " << fileName::LINK << " should be " << fileName::SYMLINK
<< " but is " << lnB.type(false) << exit(FatalError); << " but is " << lnB.type(false) << exit(FatalError);
} }
if (lnB.type(true) != fileName::DIRECTORY) if (lnB.type(true) != fileName::DIRECTORY)

View File

@ -459,7 +459,7 @@ bool Foam::removeEmptyDir(const fileName& path)
Foam::readDir Foam::readDir
( (
path, path,
fileName::LINK, fileName::SYMLINK,
false, // filterGz false, // filterGz
false // followLink false // followLink
) )

View File

@ -7,7 +7,7 @@
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2011 Symscape Copyright (C) 2011 Symscape
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -83,7 +83,7 @@ namespace Foam
// Move file, overwriting existing // Move file, overwriting existing
static bool renameFile(const fileName& src, const fileName& dst) static bool renameFile(const std::string& src, const std::string& dst)
{ {
constexpr const int flags constexpr const int flags
( (
@ -132,7 +132,7 @@ class directoryIterator
public: public:
//- Construct for dirName, optionally allowing hidden files/dirs //- Construct for dirName, optionally allowing hidden files/dirs
directoryIterator(const fileName& dirName, bool allowHidden = false) directoryIterator(const std::string& dirName, bool allowHidden = false)
: :
handle_(INVALID_HANDLE_VALUE), handle_(INVALID_HANDLE_VALUE),
exists_(false), exists_(false),
@ -170,13 +170,13 @@ public:
// Member Functions // Member Functions
//- Directory existed for opening //- Directory existed for opening
bool exists() const bool exists() const noexcept
{ {
return exists_; return exists_;
} }
//- Directory pointer is valid //- Directory pointer is valid
bool good() const bool good() const noexcept
{ {
return (INVALID_HANDLE_VALUE != handle_); return (INVALID_HANDLE_VALUE != handle_);
} }
@ -192,7 +192,7 @@ public:
} }
//- The current item //- The current item
const std::string& val() const const std::string& val() const noexcept
{ {
return item_; return item_;
} }
@ -225,13 +225,13 @@ public:
// Member Operators // Member Operators
//- Same as good() //- Same as good()
operator bool() const operator bool() const noexcept
{ {
return good(); return good();
} }
//- Same as val() //- Same as val()
const std::string& operator*() const const std::string& operator*() const noexcept
{ {
return val(); return val();
} }
@ -407,7 +407,7 @@ bool Foam::setEnv
} }
Foam::string Foam::hostName(bool) Foam::string Foam::hostName()
{ {
const DWORD bufLen = MAX_COMPUTERNAME_LENGTH + 1; const DWORD bufLen = MAX_COMPUTERNAME_LENGTH + 1;
TCHAR buf[bufLen]; TCHAR buf[bufLen];
@ -417,12 +417,20 @@ Foam::string Foam::hostName(bool)
} }
// DEPRECATED (2022-01)
Foam::string Foam::hostName(bool)
{
return Foam::hostName();
}
// DEPRECATED (2022-01)
Foam::string Foam::domainName() Foam::string Foam::domainName()
{ {
// Could use ::gethostname and ::gethostbyname like POSIX.C, but would // Could use ::gethostname and ::gethostbyname like POSIX.C, but would
// then need to link against ws_32. Prefer to minimize dependencies. // then need to link against ws_32. Prefer to minimize dependencies.
return string::null; return string();
} }
@ -452,7 +460,7 @@ Foam::fileName Foam::home()
if (env.empty()) if (env.empty())
{ {
env = Foam::getEnv("USERPROFILE"); env = Foam::getEnv("USERPROFILE");
} }
return env; return env;
@ -595,21 +603,21 @@ Foam::fileName::Type Foam::type
// Ignore an empty name => always UNDEFINED // Ignore an empty name => always UNDEFINED
if (name.empty()) if (name.empty())
{ {
return fileName::UNDEFINED; return fileName::Type::UNDEFINED;
} }
const DWORD m = ::GetFileAttributes(name.c_str()); const DWORD m = ::GetFileAttributes(name.c_str());
if (ms_isreg(m)) if (ms_isreg(m))
{ {
return fileName::FILE; return fileName::Type::FILE;
} }
else if (ms_isdir(m)) else if (ms_isdir(m))
{ {
return fileName::DIRECTORY; return fileName::Type::DIRECTORY;
} }
return fileName::UNDEFINED; return fileName::Type::UNDEFINED;
} }

View File

@ -35,8 +35,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef MSwindows_H #ifndef Foam_MSwindows_H
#define MSwindows_H #define Foam_MSwindows_H
#include "className.H" #include "className.H"

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -142,7 +142,7 @@ public:
// Constructors // Constructors
//- Construct for dirName, optionally allowing hidden files/dirs //- Construct for dirName, optionally allowing hidden files/dirs
directoryIterator(const fileName& dirName, bool allowHidden = false) directoryIterator(const std::string& dirName, bool allowHidden = false)
: :
dirptr_(nullptr), dirptr_(nullptr),
exists_(false), exists_(false),
@ -168,13 +168,13 @@ public:
// Member Functions // Member Functions
//- Directory open succeeded //- Directory open succeeded
bool exists() const bool exists() const noexcept
{ {
return exists_; return exists_;
} }
//- Directory pointer is valid //- Directory pointer is valid
bool good() const bool good() const noexcept
{ {
return dirptr_; return dirptr_;
} }
@ -190,7 +190,7 @@ public:
} }
//- The current item //- The current item
const std::string& val() const const std::string& val() const noexcept
{ {
return item_; return item_;
} }
@ -220,13 +220,13 @@ public:
// Member Operators // Member Operators
//- Same as good() //- Same as good()
operator bool() const operator bool() const noexcept
{ {
return good(); return good();
} }
//- Same as val() //- Same as val()
const std::string& operator*() const const std::string& operator*() const noexcept
{ {
return val(); return val();
} }
@ -302,25 +302,36 @@ bool Foam::setEnv
} }
Foam::string Foam::hostName(bool full) Foam::string Foam::hostName()
{ {
char buf[128]; char buf[128];
::gethostname(buf, sizeof(buf)); ::gethostname(buf, sizeof(buf));
return buf;
}
// DEPRECATED (2022-01)
Foam::string Foam::hostName(bool full)
{
// implementation as per hostname from net-tools // implementation as per hostname from net-tools
if (full) if (full)
{ {
char buf[128];
::gethostname(buf, sizeof(buf));
struct hostent *hp = ::gethostbyname(buf); struct hostent *hp = ::gethostbyname(buf);
if (hp) if (hp)
{ {
return hp->h_name; return hp->h_name;
} }
return buf;
} }
return buf; return Foam::hostName();
} }
// DEPRECATED (2022-01)
Foam::string Foam::domainName() Foam::string Foam::domainName()
{ {
char buf[128]; char buf[128];
@ -338,7 +349,7 @@ Foam::string Foam::domainName()
} }
} }
return string::null; return string();
} }
@ -713,12 +724,16 @@ mode_t Foam::mode(const fileName& name, const bool followLink)
} }
Foam::fileName::Type Foam::type(const fileName& name, const bool followLink) Foam::fileName::Type Foam::type
(
const fileName& name,
const bool followLink
)
{ {
// Ignore an empty name => always UNDEFINED // Ignore an empty name => always UNDEFINED
if (name.empty()) if (name.empty())
{ {
return fileName::UNDEFINED; return fileName::Type::UNDEFINED;
} }
if (POSIX::debug) if (POSIX::debug)
@ -730,18 +745,18 @@ Foam::fileName::Type Foam::type(const fileName& name, const bool followLink)
if (S_ISREG(m)) if (S_ISREG(m))
{ {
return fileName::FILE; return fileName::Type::FILE;
} }
else if (S_ISLNK(m)) else if (S_ISLNK(m))
{ {
return fileName::LINK; return fileName::Type::SYMLINK;
} }
else if (S_ISDIR(m)) else if (S_ISDIR(m))
{ {
return fileName::DIRECTORY; return fileName::Type::DIRECTORY;
} }
return fileName::UNDEFINED; return fileName::Type::UNDEFINED;
} }
@ -1033,7 +1048,7 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
return false; return false;
} }
} }
else if (srcType == fileName::LINK) else if (srcType == fileName::SYMLINK)
{ {
// If dest is a directory, create the destination file name. // If dest is a directory, create the destination file name.
if (destFile.type() == fileName::DIRECTORY) if (destFile.type() == fileName::DIRECTORY)

View File

@ -34,8 +34,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef POSIX_H #ifndef Foam_POSIX_H
#define POSIX_H #define Foam_POSIX_H
#include "className.H" #include "className.H"

View File

@ -52,7 +52,7 @@ namespace Foam
const fileName::Type pathType = Foam::type(otherName, false); const fileName::Type pathType = Foam::type(otherName, false);
if (pathType == fileName::FILE || pathType == fileName::LINK) if (pathType == fileName::FILE || pathType == fileName::SYMLINK)
{ {
Foam::rm(otherName); Foam::rm(otherName);
} }
@ -60,7 +60,7 @@ namespace Foam
// Disallow writing into symlinked files. // Disallow writing into symlinked files.
// Eg, avoid problems with symlinked initial fields // Eg, avoid problems with symlinked initial fields
if (!append && Foam::type(targetName, false) == fileName::LINK) if (!append && Foam::type(targetName, false) == fileName::SYMLINK)
{ {
Foam::rm(targetName); Foam::rm(targetName);
} }

View File

@ -218,7 +218,7 @@ static inline bool accept
) )
{ {
// followLink(true), checkGzip(true) // followLink(true), checkGzip(true)
// -> returns (UNDEFINED | FILE | DIRECTORY), no need to check for (LINK) // -> returns (UNDEFINED | FILE | DIRECTORY), no need to check for (SYMLINK)
const auto t = name.type(true, true); const auto t = name.type(true, true);
return return

View File

@ -38,8 +38,8 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef fileOperation_H #ifndef Foam_fileOperation_H
#define fileOperation_H #define Foam_fileOperation_H
#include "ISstream.H" #include "ISstream.H"
#include "Ostream.H" #include "Ostream.H"
@ -262,7 +262,7 @@ public:
const bool followLink = true const bool followLink = true
) const = 0; ) const = 0;
//- Return the file type: DIRECTORY, FILE or LINK //- Return the file type: DIRECTORY, FILE or SYMLINK
virtual fileName::Type type virtual fileName::Type type
( (
const fileName&, const fileName&,

View File

@ -59,8 +59,8 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef fileOperations_masterUncollatedFileOperation_H #ifndef Foam_fileOperations_masterUncollatedFileOperation_H
#define fileOperations_masterUncollatedFileOperation_H #define Foam_fileOperations_masterUncollatedFileOperation_H
#include "fileOperation.H" #include "fileOperation.H"
#include "OSspecific.H" #include "OSspecific.H"
@ -515,7 +515,7 @@ public:
const bool followLink = true const bool followLink = true
) const; ) const;
//- Return the file type: DIRECTORY, FILE or LINK //- Return the file type: DIRECTORY, FILE or SYMLINK
virtual fileName::Type type virtual fileName::Type type
( (
const fileName&, const fileName&,

View File

@ -32,8 +32,8 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef fileOperations_uncollatedFileOperation_H #ifndef Foam_fileOperations_uncollatedFileOperation_H
#define fileOperations_uncollatedFileOperation_H #define Foam_fileOperations_uncollatedFileOperation_H
#include "fileOperation.H" #include "fileOperation.H"
#include "OSspecific.H" #include "OSspecific.H"
@ -110,7 +110,7 @@ public:
const bool followLink = true const bool followLink = true
) const; ) const;
//- Return the file type: DIRECTORY, FILE or LINK //- Return the file type: DIRECTORY, FILE or SYMLINK
virtual fileName::Type type virtual fileName::Type type
( (
const fileName&, const fileName&,

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,8 +36,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef OSspecific_H #ifndef Foam_OSspecific_H
#define OSspecific_H #define Foam_OSspecific_H
#include "fileNameList.H" #include "fileNameList.H"
#include "stringList.H" #include "stringList.H"
@ -82,10 +82,16 @@ FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function")
inline bool env(const std::string& envName) { return Foam::hasEnv(envName); } inline bool env(const std::string& envName) { return Foam::hasEnv(envName); }
//- Return the system's host name, as per hostname(1) //- Return the system's host name, as per hostname(1)
// \note the full name (as per the '-f' option) may be unreliable string hostName();
string hostName(bool full=false);
//- Return the system's domain name, as per hostname(1) with the '-d' option //- Deprecated(2022-01) full hostname resolution may be unreliable
// \deprecated(2022-01) - use hostname() function without parameter
FOAM_DEPRECATED_FOR(2022-01, "hostname() function without parameter")
string hostName(bool full);
//- Deprecated(2022-01) domain name resolution may be unreliable
// \deprecated(2022-01) - avoid usage entirely
FOAM_DEPRECATED_FOR(2022-01, "nothing : avoid using at all")
string domainName(); string domainName();
//- Return the user's login name //- Return the user's login name

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,8 +45,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef fileName_H #ifndef Foam_fileName_H
#define fileName_H #define Foam_fileName_H
#include "word.H" #include "word.H"
@ -81,9 +81,10 @@ public:
enum Type enum Type
{ {
UNDEFINED = 0, //!< Undefined type UNDEFINED = 0, //!< Undefined type
FILE = 1, //!< A file FILE = 1, //!< A regular file
DIRECTORY = 2, //!< A directory DIRECTORY = 2, //!< A directory
LINK = 4 //!< A symlink SYMLINK = 4, //!< A symbolic link
LINK = SYMLINK //!< Same as symlink
}; };
@ -225,9 +226,9 @@ public:
// Interrogation // Interrogation
//- Return the directory entry type: //- Return the directory entry type:
//- UNDEFINED, FILE, DIRECTORY (or LINK). //- UNDEFINED, FILE, DIRECTORY (or SYMLINK).
// //
// \param followLink when false it will return LINK for a symlink // \param followLink when false it will return SYMLINK for a symlink
// rather than following it. // rather than following it.
// \param checkGzip add an additional test for a gzip FILE // \param checkGzip add an additional test for a gzip FILE
Type type(bool followLink=true, bool checkGzip=false) const; Type type(bool followLink=true, bool checkGzip=false) const;