OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

POSIX.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     POSIX versions of the functions declared in OSspecific.H
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #ifdef solarisGcc
00031 # define _SYS_VNODE_H
00032 #endif
00033 
00034 #include "OSspecific.H"
00035 #include "POSIX.H"
00036 #include "foamVersion.H"
00037 #include "fileName.H"
00038 #include "fileStat.H"
00039 #include "timer.H"
00040 
00041 #include <fstream>
00042 #include <cstdlib>
00043 #include <cctype>
00044 
00045 #include <stdio.h>
00046 #include <unistd.h>
00047 #include <dirent.h>
00048 #include <pwd.h>
00049 #include <errno.h>
00050 #include <sys/types.h>
00051 #include <sys/stat.h>
00052 #include <sys/socket.h>
00053 #include <netdb.h>
00054 
00055 #include <netinet/in.h>
00056 
00057 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00058 
00059 defineTypeNameAndDebug(Foam::POSIX, 0);
00060 
00061 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00062 
00063 pid_t Foam::pid()
00064 {
00065     return getpid();
00066 }
00067 
00068 pid_t Foam::ppid()
00069 {
00070     return getppid();
00071 }
00072 
00073 pid_t Foam::pgid()
00074 {
00075     return getpgrp();
00076 }
00077 
00078 bool Foam::env(const word& envName)
00079 {
00080     return getenv(envName.c_str()) != NULL;
00081 }
00082 
00083 
00084 Foam::string Foam::getEnv(const word& envName)
00085 {
00086     char* env = getenv(envName.c_str());
00087 
00088     if (env)
00089     {
00090         return string(env);
00091     }
00092     else
00093     {
00094         // Return null-constructed string rather than string::null
00095         // to avoid cyclic dependencies in the construction of globals
00096         return string();
00097     }
00098 }
00099 
00100 
00101 bool Foam::setEnv
00102 (
00103     const word& envName,
00104     const string& value,
00105     const bool overwrite
00106 )
00107 {
00108     return setenv(envName.c_str(), value.c_str(), overwrite) == 0;
00109 }
00110 
00111 
00112 Foam::word Foam::hostName()
00113 {
00114     char buffer[256];
00115     gethostname(buffer, 256);
00116 
00117     return buffer;
00118 }
00119 
00120 
00121 Foam::word Foam::userName()
00122 {
00123     struct passwd* pw = getpwuid(getuid());
00124 
00125     if (pw != NULL)
00126     {
00127         return pw->pw_name;
00128     }
00129     else
00130     {
00131         return word::null;
00132     }
00133 }
00134 
00135 
00136 // use $HOME environment variable or passwd info
00137 Foam::fileName Foam::home()
00138 {
00139     char* env = getenv("HOME");
00140 
00141     if (env != NULL)
00142     {
00143         return fileName(env);
00144     }
00145     else
00146     {
00147         struct passwd* pw = getpwuid(getuid());
00148 
00149         if (pw != NULL)
00150         {
00151             return pw->pw_dir;
00152         }
00153         else
00154         {
00155             return fileName::null;
00156         }
00157     }
00158 }
00159 
00160 
00161 Foam::fileName Foam::home(const word& userName)
00162 {
00163     struct passwd* pw;
00164 
00165     if (userName.size())
00166     {
00167         pw = getpwnam(userName.c_str());
00168     }
00169     else
00170     {
00171         char* env = getenv("HOME");
00172 
00173         if (env != NULL)
00174         {
00175             return fileName(env);
00176         }
00177 
00178         pw = getpwuid(getuid());
00179     }
00180 
00181     if (pw != NULL)
00182     {
00183         return pw->pw_dir;
00184     }
00185     else
00186     {
00187         return fileName::null;
00188     }
00189 }
00190 
00191 
00192 Foam::fileName Foam::cwd()
00193 {
00194     char buf[255];
00195     if (getcwd(buf, 255))
00196     {
00197         return buf;
00198     }
00199     else
00200     {
00201         FatalErrorIn("Foam::cwd()")
00202             << "Couldn't get the current working directory"
00203             << exit(FatalError);
00204 
00205         return fileName::null;
00206     }
00207 }
00208 
00209 
00210 bool Foam::chDir(const fileName& dir)
00211 {
00212     return chdir(dir.c_str()) != 0;
00213 }
00214 
00215 
00216 Foam::fileName Foam::findEtcFile(const fileName& name, bool mandatory)
00217 {
00218     // Search user files:
00219     // ~~~~~~~~~~~~~~~~~~
00220     fileName searchDir = home()/".OpenFOAM";
00221     if (isDir(searchDir))
00222     {
00223         // Check for user file in ~/.OpenFOAM/VERSION
00224         fileName fullName = searchDir/FOAMversion/name;
00225         if (isFile(fullName))
00226         {
00227             return fullName;
00228         }
00229 
00230         // Check for version-independent user file in ~/.OpenFOAM
00231         fullName = searchDir/name;
00232         if (isFile(fullName))
00233         {
00234             return fullName;
00235         }
00236     }
00237 
00238 
00239     // Search site files:
00240     // ~~~~~~~~~~~~~~~~~~
00241     searchDir = getEnv("WM_PROJECT_INST_DIR");
00242     if (isDir(searchDir))
00243     {
00244         // Check for site file in $WM_PROJECT_INST_DIR/site/VERSION
00245         fileName fullName = searchDir/"site"/FOAMversion/name;
00246         if (isFile(fullName))
00247         {
00248             return fullName;
00249         }
00250 
00251         // Check for version-independent site file in $WM_PROJECT_INST_DIR/site
00252         fullName = searchDir/"site"/name;
00253         if (isFile(fullName))
00254         {
00255             return fullName;
00256         }
00257     }
00258 
00259     // Search installation files:
00260     // ~~~~~~~~~~~~~~~~~~~~~~~~~~
00261     searchDir = getEnv("WM_PROJECT_DIR");
00262     if (isDir(searchDir))
00263     {
00264         // Check for shipped OpenFOAM file in $WM_PROJECT_DIR/etc
00265         fileName fullName = searchDir/"etc"/name;
00266         if (isFile(fullName))
00267         {
00268             return fullName;
00269         }
00270     }
00271 
00272     // Not found
00273     // abort if the file is mandatory, otherwise return null
00274     if (mandatory)
00275     {
00276         cerr<< "--> FOAM FATAL ERROR in Foam::findEtcFile() :"
00277                " could not find mandatory file\n    '"
00278             << name.c_str() << "'\n\n" << std::endl;
00279         ::exit(1);
00280     }
00281 
00282     // Return null-constructed fileName rather than fileName::null
00283     // to avoid cyclic dependencies in the construction of globals
00284     return fileName();
00285 }
00286 
00287 
00288 bool Foam::mkDir(const fileName& pathName, mode_t mode)
00289 {
00290     // empty names are meaningless
00291     if (pathName.empty())
00292     {
00293         return false;
00294     }
00295 
00296     // Construct instance path directory if does not exist
00297     if (::mkdir(pathName.c_str(), mode) == 0)
00298     {
00299         // Directory made OK so return true
00300         return true;
00301     }
00302     else
00303     {
00304         switch (errno)
00305         {
00306             case EPERM:
00307             {
00308                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00309                     << "The filesystem containing " << pathName
00310                     << " does not support the creation of directories."
00311                     << exit(FatalError);
00312 
00313                 return false;
00314             }
00315 
00316             case EEXIST:
00317             {
00318                 // Directory already exists so simply return true
00319                 return true;
00320             }
00321 
00322             case EFAULT:
00323             {
00324                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00325                     << "" << pathName
00326                     << " points outside your accessible address space."
00327                     << exit(FatalError);
00328 
00329                 return false;
00330             }
00331 
00332             case EACCES:
00333             {
00334                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00335                     << "The parent directory does not allow write "
00336                        "permission to the process,"<< nl
00337                     << "or one of the directories in " << pathName
00338                     << " did not allow search (execute) permission."
00339                     << exit(FatalError);
00340 
00341                 return false;
00342             }
00343 
00344             case ENAMETOOLONG:
00345             {
00346                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00347                     << "" << pathName << " is too long."
00348                     << exit(FatalError);
00349 
00350                 return false;
00351             }
00352 
00353             case ENOENT:
00354             {
00355                 // Part of the path does not exist so try to create it
00356                 if (pathName.path().size() && mkDir(pathName.path(), mode))
00357                 {
00358                     return mkDir(pathName, mode);
00359                 }
00360                 else
00361                 {
00362                     FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00363                         << "Couldn't create directory " << pathName
00364                         << exit(FatalError);
00365 
00366                     return false;
00367                 }
00368             }
00369 
00370             case ENOTDIR:
00371             {
00372                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00373                     << "A component used as a directory in " << pathName
00374                     << " is not, in fact, a directory."
00375                     << exit(FatalError);
00376 
00377                 return false;
00378             }
00379 
00380             case ENOMEM:
00381             {
00382                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00383                     << "Insufficient kernel memory was available to make "
00384                        "directory " << pathName << '.'
00385                     << exit(FatalError);
00386 
00387                 return false;
00388             }
00389 
00390             case EROFS:
00391             {
00392                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00393                     << "" << pathName
00394                     << " refers to a file on a read-only filesystem."
00395                     << exit(FatalError);
00396 
00397                 return false;
00398             }
00399 
00400             case ELOOP:
00401             {
00402                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00403                     << "Too many symbolic links were encountered in resolving "
00404                     << pathName << '.'
00405                     << exit(FatalError);
00406 
00407                 return false;
00408             }
00409 
00410             case ENOSPC:
00411             {
00412                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00413                     << "The device containing " << pathName
00414                     << " has no room for the new directory or "
00415                     << "the user's disk quota is exhausted."
00416                     << exit(FatalError);
00417 
00418                 return false;
00419             }
00420 
00421             default:
00422             {
00423                 FatalErrorIn("Foam::mkDir(const fileName&, mode_t)")
00424                     << "Couldn't create directory " << pathName
00425                     << exit(FatalError);
00426 
00427                 return false;
00428             }
00429         }
00430     }
00431 }
00432 
00433 
00434 // Set the file mode
00435 bool Foam::chMod(const fileName& name, const mode_t m)
00436 {
00437     return ::chmod(name.c_str(), m) == 0;
00438 }
00439 
00440 
00441 // Return the file mode
00442 mode_t Foam::mode(const fileName& name)
00443 {
00444     fileStat fileStatus(name);
00445     if (fileStatus.isValid())
00446     {
00447         return fileStatus.status().st_mode;
00448     }
00449     else
00450     {
00451         return 0;
00452     }
00453 }
00454 
00455 
00456 // Return the file type: FILE or DIRECTORY
00457 Foam::fileName::Type Foam::type(const fileName& name)
00458 {
00459     mode_t m = mode(name);
00460 
00461     if (S_ISREG(m))
00462     {
00463         return fileName::FILE;
00464     }
00465     else if (S_ISDIR(m))
00466     {
00467         return fileName::DIRECTORY;
00468     }
00469     else
00470     {
00471         return fileName::UNDEFINED;
00472     }
00473 }
00474 
00475 
00476 // Does the name exist in the filing system?
00477 bool Foam::exists(const fileName& name, const bool checkGzip)
00478 {
00479     return mode(name) || isFile(name, checkGzip);
00480 }
00481 
00482 
00483 // Does the directory exist?
00484 bool Foam::isDir(const fileName& name)
00485 {
00486     return S_ISDIR(mode(name));
00487 }
00488 
00489 
00490 // Does the file exist?
00491 bool Foam::isFile(const fileName& name, const bool checkGzip)
00492 {
00493     return S_ISREG(mode(name)) || (checkGzip && S_ISREG(mode(name + ".gz")));
00494 }
00495 
00496 
00497 // Return size of file
00498 off_t Foam::fileSize(const fileName& name)
00499 {
00500     fileStat fileStatus(name);
00501     if (fileStatus.isValid())
00502     {
00503         return fileStatus.status().st_size;
00504     }
00505     else
00506     {
00507         return -1;
00508     }
00509 }
00510 
00511 
00512 // Return time of last file modification
00513 time_t Foam::lastModified(const fileName& name)
00514 {
00515     fileStat fileStatus(name);
00516     if (fileStatus.isValid())
00517     {
00518         return fileStatus.status().st_mtime;
00519     }
00520     else
00521     {
00522         return 0;
00523     }
00524 }
00525 
00526 
00527 // Read a directory and return the entries as a string list
00528 Foam::fileNameList Foam::readDir
00529 (
00530     const fileName& directory,
00531     const fileName::Type type,
00532     const bool filtergz
00533 )
00534 {
00535     // Initial filename list size
00536     // also used as increment if initial size found to be insufficient
00537     static const int maxNnames = 100;
00538 
00539     if (POSIX::debug)
00540     {
00541         Info<< "readDir(const fileName&, const fileType, const bool filtergz)"
00542             << " : reading directory " << directory << endl;
00543     }
00544 
00545     // Setup empty string list MAXTVALUES long
00546     fileNameList dirEntries(maxNnames);
00547 
00548     // Pointers to the directory entries
00549     DIR *source;
00550     struct dirent *list;
00551 
00552     // Temporary variables and counters
00553     label nEntries = 0;
00554 
00555     // Attempt to open directory and set the structure pointer
00556     if ((source = opendir(directory.c_str())) == NULL)
00557     {
00558         dirEntries.setSize(0);
00559 
00560         if (POSIX::debug)
00561         {
00562             Info<< "readDir(const fileName&, const fileType, "
00563                    "const bool filtergz) : cannot open directory "
00564                 << directory << endl;
00565         }
00566     }
00567     else
00568     {
00569         // Read and parse all the entries in the directory
00570         while ((list = readdir(source)) != NULL)
00571         {
00572             fileName fName(list->d_name);
00573 
00574             // ignore files begining with ., i.e. '.', '..' and '.*'
00575             if (fName.size() && fName[0] != '.')
00576             {
00577                 word fExt = fName.ext();
00578 
00579                 if
00580                 (
00581                     (type == fileName::DIRECTORY)
00582                  ||
00583                     (
00584                         type == fileName::FILE
00585                      && fName[fName.size()-1] != '~'
00586                      && fExt != "bak"
00587                      && fExt != "BAK"
00588                      && fExt != "old"
00589                      && fExt != "save"
00590                     )
00591                 )
00592                 {
00593                     if ((directory/fName).type() == type)
00594                     {
00595                         if (nEntries >= dirEntries.size())
00596                         {
00597                             dirEntries.setSize(dirEntries.size() + maxNnames);
00598                         }
00599 
00600                         if (filtergz && fExt == "gz")
00601                         {
00602                             dirEntries[nEntries++] = fName.lessExt();
00603                         }
00604                         else
00605                         {
00606                             dirEntries[nEntries++] = fName;
00607                         }
00608                     }
00609                 }
00610             }
00611         }
00612 
00613         // Reset the length of the entries list
00614         dirEntries.setSize(nEntries);
00615 
00616         closedir(source);
00617     }
00618 
00619     return dirEntries;
00620 }
00621 
00622 
00623 // Copy, recursively if necessary, the source to the destination
00624 bool Foam::cp(const fileName& src, const fileName& dest)
00625 {
00626     // Make sure source exists.
00627     if (!exists(src))
00628     {
00629         return false;
00630     }
00631 
00632     fileName destFile(dest);
00633 
00634     // Check type of source file.
00635     if (src.type() == fileName::FILE)
00636     {
00637         // If dest is a directory, create the destination file name.
00638         if (destFile.type() == fileName::DIRECTORY)
00639         {
00640             destFile = destFile/src.name();
00641         }
00642 
00643         // Make sure the destination directory exists.
00644         if (!isDir(destFile.path()) && !mkDir(destFile.path()))
00645         {
00646             return false;
00647         }
00648 
00649         // Open and check streams.
00650         std::ifstream srcStream(src.c_str());
00651         if (!srcStream)
00652         {
00653             return false;
00654         }
00655 
00656         std::ofstream destStream(destFile.c_str());
00657         if (!destStream)
00658         {
00659             return false;
00660         }
00661 
00662         // Copy character data.
00663         char ch;
00664         while (srcStream.get(ch))
00665         {
00666             destStream.put(ch);
00667         }
00668 
00669         // Final check.
00670         if (!srcStream.eof() || !destStream)
00671         {
00672             return false;
00673         }
00674     }
00675     else if (src.type() == fileName::DIRECTORY)
00676     {
00677         // If dest is a directory, create the destination file name.
00678         if (destFile.type() == fileName::DIRECTORY)
00679         {
00680             destFile = destFile/src.component(src.components().size() -1);
00681         }
00682 
00683         // Make sure the destination directory exists.
00684         if (!isDir(destFile) && !mkDir(destFile))
00685         {
00686             return false;
00687         }
00688 
00689         // Copy files
00690         fileNameList contents = readDir(src, fileName::FILE, false);
00691         forAll(contents, i)
00692         {
00693             if (POSIX::debug)
00694             {
00695                 Info<< "Copying : " << src/contents[i]
00696                     << " to " << destFile/contents[i] << endl;
00697             }
00698 
00699             // File to file.
00700             cp(src/contents[i], destFile/contents[i]);
00701         }
00702 
00703         // Copy sub directories.
00704         fileNameList subdirs = readDir(src, fileName::DIRECTORY);
00705         forAll(subdirs, i)
00706         {
00707             if (POSIX::debug)
00708             {
00709                 Info<< "Copying : " << src/subdirs[i]
00710                     << " to " << destFile << endl;
00711             }
00712 
00713             // Dir to Dir.
00714             cp(src/subdirs[i], destFile);
00715         }
00716     }
00717 
00718     return true;
00719 }
00720 
00721 
00722 // Create a softlink. dst should not exist. Returns true if successful.
00723 bool Foam::ln(const fileName& src, const fileName& dst)
00724 {
00725     if (POSIX::debug)
00726     {
00727         Info<< "Create softlink from : " << src << " to " << dst
00728             << endl;
00729     }
00730 
00731     if (exists(dst))
00732     {
00733         WarningIn("ln(const fileName&, const fileName&)")
00734             << "destination " << dst << " already exists. Not linking."
00735             << endl;
00736         return false;
00737     }
00738 
00739     if (!exists(src))
00740     {
00741         WarningIn("ln(const fileName&, const fileName&)")
00742             << "source " << src << " does not exist." << endl;
00743         return false;
00744     }
00745 
00746     if (symlink(src.c_str(), dst.c_str()) == 0)
00747     {
00748         return true;
00749     }
00750     else
00751     {
00752         WarningIn("ln(const fileName&, const fileName&)")
00753             << "symlink from " << src << " to " << dst << " failed." << endl;
00754         return false;
00755     }
00756 }
00757 
00758 
00759 // Rename srcFile dstFile
00760 bool Foam::mv(const fileName& src, const fileName& dst)
00761 {
00762     if (POSIX::debug)
00763     {
00764         Info<< "Move : " << src << " to " << dst << endl;
00765     }
00766 
00767     if
00768     (
00769         dst.type() == fileName::DIRECTORY
00770      && src.type() != fileName::DIRECTORY
00771     )
00772     {
00773         const fileName dstName(dst/src.name());
00774 
00775         return rename(src.c_str(), dstName.c_str()) == 0;
00776     }
00777     else
00778     {
00779         return rename(src.c_str(), dst.c_str()) == 0;
00780     }
00781 }
00782 
00783 
00784 //- Rename to a corresponding backup file
00785 //  If the backup file already exists, attempt with "01" .. "99" index
00786 bool Foam::mvBak(const fileName& src, const std::string& ext)
00787 {
00788     if (POSIX::debug)
00789     {
00790         Info<< "mvBak : " << src << " to extension " << ext << endl;
00791     }
00792 
00793     if (exists(src, false))
00794     {
00795         const int maxIndex = 99;
00796         char index[3];
00797 
00798         for (int n = 0; n <= maxIndex; n++)
00799         {
00800             fileName dstName(src + "." + ext);
00801             if (n)
00802             {
00803                 sprintf(index, "%02d", n);
00804                 dstName += index;
00805             }
00806 
00807             // avoid overwriting existing files, except for the last
00808             // possible index where we have no choice
00809             if (!exists(dstName, false) || n == maxIndex)
00810             {
00811                 return rename(src.c_str(), dstName.c_str()) == 0;
00812             }
00813 
00814         }
00815     }
00816 
00817     // fall-through: nothing to do
00818     return false;
00819 }
00820 
00821 
00822 
00823 // Remove a file, returning true if successful otherwise false
00824 bool Foam::rm(const fileName& file)
00825 {
00826     if (POSIX::debug)
00827     {
00828         Info<< "Removing : " << file << endl;
00829     }
00830 
00831     // Try returning plain file name; if not there, try with .gz
00832     if (remove(file.c_str()) == 0)
00833     {
00834         return true;
00835     }
00836     else
00837     {
00838         return remove(string(file + ".gz").c_str()) == 0;
00839     }
00840 }
00841 
00842 
00843 // Remove a dirctory and its contents
00844 bool Foam::rmDir(const fileName& directory)
00845 {
00846     if (POSIX::debug)
00847     {
00848         Info<< "rmDir(const fileName&) : "
00849             << "removing directory " << directory << endl;
00850     }
00851 
00852     // Pointers to the directory entries
00853     DIR *source;
00854     struct dirent *list;
00855 
00856     // Attempt to open directory and set the structure pointer
00857     if ((source = opendir(directory.c_str())) == NULL)
00858     {
00859         WarningIn("rmDir(const fileName&)")
00860             << "cannot open directory " << directory << endl;
00861 
00862         return false;
00863     }
00864     else
00865     {
00866         // Read and parse all the entries in the directory
00867         while ((list = readdir(source)) != NULL)
00868         {
00869             fileName fName(list->d_name);
00870 
00871             if (fName != "." && fName != "..")
00872             {
00873                 fileName path = directory/fName;
00874 
00875                 if (path.type() == fileName::DIRECTORY)
00876                 {
00877                     if (!rmDir(path))
00878                     {
00879                         WarningIn("rmDir(const fileName&)")
00880                             << "failed to remove directory " << fName
00881                             << " while removing directory " << directory
00882                             << endl;
00883 
00884                         closedir(source);
00885 
00886                         return false;
00887                     }
00888                 }
00889                 else
00890                 {
00891                     if (!rm(path))
00892                     {
00893                         WarningIn("rmDir(const fileName&)")
00894                             << "failed to remove file " << fName
00895                             << " while removing directory " << directory
00896                             << endl;
00897 
00898                         closedir(source);
00899 
00900                         return false;
00901                     }
00902                 }
00903             }
00904 
00905         }
00906 
00907         if (!rm(directory))
00908         {
00909             WarningIn("rmDir(const fileName&)")
00910                 << "failed to remove directory " << directory << endl;
00911 
00912             closedir(source);
00913 
00914             return false;
00915         }
00916 
00917         closedir(source);
00918 
00919         return true;
00920     }
00921 }
00922 
00923 
00924 unsigned int Foam::sleep(const unsigned int s)
00925 {
00926     return ::sleep(s);
00927 }
00928 
00929 
00930 void Foam::fdClose(const int fd)
00931 {
00932     if (close(fd) != 0)
00933     {
00934         FatalErrorIn
00935         (
00936             "fdClose(const int fd)"
00937         )   << "close error on " << fd << endl
00938             << abort(FatalError);
00939     }
00940 }
00941 
00942 
00943 bool Foam::ping
00944 (
00945     const word& destName,
00946     const label destPort,
00947     const label timeOut
00948 )
00949 {
00950     char *serverAddress;
00951     struct in_addr *ptr;
00952     struct hostent *hostPtr;
00953     volatile int sockfd;
00954     struct sockaddr_in destAddr;      // will hold the destination addr
00955     u_int addr;
00956 
00957     if ((hostPtr = gethostbyname(destName.c_str())) == NULL)
00958     {
00959         FatalErrorIn
00960         (
00961             "Foam::ping(const word&, const label)"
00962         )   << "gethostbyname error " << h_errno << " for host " << destName
00963             << abort(FatalError);
00964     }
00965 
00966     // Get first of the SLL of addresses
00967     serverAddress = *(hostPtr->h_addr_list);
00968     ptr = reinterpret_cast<struct in_addr*>(serverAddress);
00969     addr = ptr->s_addr;
00970 
00971     // Allocate socket
00972     sockfd = socket(AF_INET, SOCK_STREAM, 0);
00973     if (sockfd < 0)
00974     {
00975         FatalErrorIn
00976         (
00977             "Foam::ping(const word&, const label)"
00978         )   << "socket error"
00979             << abort(FatalError);
00980     }
00981 
00982     // Fill sockaddr_in structure with dest address and port
00983     memset (reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr));
00984     destAddr.sin_family = AF_INET;
00985     destAddr.sin_port = htons(ushort(destPort));
00986     destAddr.sin_addr.s_addr = addr;
00987 
00988 
00989     timer myTimer(timeOut);
00990 
00991     if (timedOut(myTimer))
00992     {
00993         // Setjmp from timer jumps back to here
00994         fdClose(sockfd);
00995         return false;
00996     }
00997 
00998     if
00999     (
01000         connect
01001         (
01002             sockfd,
01003             reinterpret_cast<struct sockaddr*>(&destAddr),
01004             sizeof(struct sockaddr)
01005         ) != 0
01006     )
01007     {
01008         // Connection refused. Check if network was actually used or not.
01009 
01010         int connectErr = errno;
01011 
01012         fdClose(sockfd);
01013 
01014         if (connectErr == ECONNREFUSED)
01015         {
01016             return true;
01017         }
01018         //perror("connect");
01019 
01020         return false;
01021     }
01022 
01023     fdClose(sockfd);
01024 
01025     return true;
01026 }
01027 
01028 
01029 bool Foam::ping(const word& hostname, const label timeOut)
01030 {
01031     return ping(hostname, 222, timeOut) || ping(hostname, 22, timeOut);
01032 }
01033 
01034 
01035 int Foam::system(const string& command)
01036 {
01037     return ::system(command.c_str());
01038 }
01039 
01040 
01041 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd