POSIX.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Description
28  POSIX versions of the functions declared in OSspecific.H
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #if defined(__sun__) && defined(__GNUC__)
33  // Not certain if this is still required
34  #define _SYS_VNODE_H
35 #endif
36 
37 #include "OSspecific.H"
38 #include "POSIX.H"
39 #include "fileName.H"
40 #include "fileStat.H"
41 #include "timer.H"
42 #include "DynamicList.H"
43 #include "CStringList.H"
44 #include "IOstreams.H"
45 #include "Pstream.H"
46 
47 #include <fstream>
48 #include <cstdlib>
49 #include <cctype>
50 
51 #include <cstdio>
52 #include <unistd.h>
53 #include <dirent.h>
54 #include <pwd.h>
55 #include <errno.h>
56 #include <sys/types.h>
57 #include <sys/wait.h>
58 #include <sys/stat.h>
59 #include <sys/socket.h>
60 #include <netdb.h>
61 #include <netinet/in.h>
62 #include <dlfcn.h>
63 
64 #ifdef __APPLE__
65  #define EXT_SO "dylib"
66  #include <mach-o/dyld.h>
67 #else
68  #define EXT_SO "so"
69 
70  // PGI does not have __int128_t
71  #ifdef __PGIC__
72  #define __ILP32__
73  #endif
74 
75  #include <link.h>
76 #endif
77 
78 
79 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
80 
81 namespace Foam
82 {
83  defineTypeNameAndDebug(POSIX, 0);
84 }
85 
86 static bool cwdPreference_(Foam::debug::optimisationSwitch("cwd", 0));
87 
88 
89 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
90 
91 // After a fork in system(), before the exec() do the following
92 // - close stdin when executing in background (daemon-like)
93 // - redirect stdout to stderr when infoDetailLevel == 0
94 static inline void redirects(const bool bg)
95 {
96  if (bg)
97  {
98  // Close stdin(0) - unchecked return value
99  (void) ::close(STDIN_FILENO);
100  }
101 
102  // Redirect stdout(1) to stderr(2) '1>&2'
103  if (Foam::infoDetailLevel == 0)
104  {
105  // This is correct. 1>&2 means dup2(2, 1);
106  (void) ::dup2(STDERR_FILENO, STDOUT_FILENO);
107  }
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * //
112 
113 namespace Foam
114 {
115 namespace POSIX
116 {
117 
118 //- A simple directory contents iterator
120 {
121  DIR* dirptr_;
122 
123  bool exists_;
124 
125  bool hidden_;
126 
127  std::string item_;
128 
129  //- Accept file/dir name
130  inline bool accept() const
131  {
132  return
133  (
134  item_.size() && item_ != "." && item_ != ".."
135  && (hidden_ || item_[0] != '.')
136  );
137  }
138 
139 
140 public:
141 
142  // Constructors
143 
144  //- Construct for dirName, optionally allowing hidden files/dirs
145  directoryIterator(const fileName& dirName, bool allowHidden = false)
146  :
147  dirptr_(nullptr),
148  exists_(false),
149  hidden_(allowHidden),
150  item_()
151  {
152  if (!dirName.empty())
153  {
154  dirptr_ = ::opendir(dirName.c_str());
155  exists_ = (dirptr_ != nullptr);
156  next(); // Move to first element
157  }
158  }
159 
160 
161  //- Destructor
163  {
164  close();
165  }
166 
167 
168  // Member Functions
169 
170  //- Directory open succeeded
171  bool exists() const
172  {
173  return exists_;
174  }
175 
176  //- Directory pointer is valid
177  bool good() const
178  {
179  return dirptr_;
180  }
181 
182  //- Close directory
183  void close()
184  {
185  if (dirptr_)
186  {
187  ::closedir(dirptr_);
188  dirptr_ = nullptr;
189  }
190  }
191 
192  //- The current item
193  const std::string& val() const
194  {
195  return item_;
196  }
197 
198  //- Read next item, always ignoring "." and ".." entries.
199  // Normally also ignore hidden files/dirs (beginning with '.')
200  // Automatically close when there are no more items
201  bool next()
202  {
203  struct dirent *list;
204 
205  while (dirptr_ && (list = ::readdir(dirptr_)) != nullptr)
206  {
207  item_ = list->d_name;
208 
209  if (accept())
210  {
211  return true;
212  }
213  }
214  close(); // No more items
215 
216  return false;
217  }
218 
219 
220  // Member Operators
221 
222  //- Same as good()
223  operator bool() const
224  {
225  return good();
226  }
227 
228  //- Same as val()
229  const std::string& operator*() const
230  {
231  return val();
232  }
233 
234  //- Same as next()
236  {
237  next();
238  return *this;
239  }
240 };
241 
242 } // End namespace POSIX
243 } // End namespace Foam
244 
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 pid_t Foam::pid()
249 {
250  return ::getpid();
251 }
252 
253 
254 pid_t Foam::ppid()
255 {
256  return ::getppid();
257 }
258 
259 
260 pid_t Foam::pgid()
261 {
262  return ::getpgrp();
263 }
264 
265 
266 bool Foam::hasEnv(const std::string& envName)
267 {
268  // An empty envName => always false
269  return !envName.empty() && ::getenv(envName.c_str()) != nullptr;
270 }
271 
272 
273 Foam::string Foam::getEnv(const std::string& envName)
274 {
275  // Ignore an empty envName => always ""
276  char* env = envName.empty() ? nullptr : ::getenv(envName.c_str());
277 
278  if (env)
279  {
280  return string(env);
281  }
282 
283  // Return null-constructed string rather than string::null
284  // to avoid cyclic dependencies in the construction of globals
285  return string();
286 }
287 
288 
289 bool Foam::setEnv
290 (
291  const word& envName,
292  const std::string& value,
293  const bool overwrite
294 )
295 {
296  // Ignore an empty envName => always false
297  return
298  (
299  !envName.empty()
300  && ::setenv(envName.c_str(), value.c_str(), overwrite) == 0
301  );
302 }
303 
304 
305 Foam::string Foam::hostName(bool full)
306 {
307  char buf[128];
308  ::gethostname(buf, sizeof(buf));
309 
310  // implementation as per hostname from net-tools
311  if (full)
312  {
313  struct hostent *hp = ::gethostbyname(buf);
314  if (hp)
315  {
316  return hp->h_name;
317  }
318  }
319 
320  return buf;
321 }
322 
323 
325 {
326  char buf[128];
327  ::gethostname(buf, sizeof(buf));
328 
329  // implementation as per hostname from net-tools
330  struct hostent *hp = ::gethostbyname(buf);
331  if (hp)
332  {
333  char *p = ::strchr(hp->h_name, '.');
334  if (p)
335  {
336  ++p;
337  return p;
338  }
339  }
340 
341  return string::null;
342 }
343 
344 
346 {
347  struct passwd* pw = ::getpwuid(::getuid());
348  if (pw != nullptr)
349  {
350  return pw->pw_name;
351  }
352 
353  return string();
354 }
355 
356 
358 {
359  return (::geteuid() == 0);
360 }
361 
362 
364 {
365  char* env = ::getenv("HOME");
366  if (env)
367  {
368  return fileName(env);
369  }
370 
371  struct passwd* pw = ::getpwuid(::getuid());
372  if (pw)
373  {
374  return pw->pw_dir;
375  }
376 
377  return fileName();
378 }
379 
380 
381 Foam::fileName Foam::home(const std::string& userName)
382 {
383  // An empty userName => same as home()
384  if (userName.empty())
385  {
386  return Foam::home();
387  }
388 
389  struct passwd* pw = ::getpwnam(userName.c_str());
390  if (pw)
391  {
392  return pw->pw_dir;
393  }
394 
395  return fileName();
396 }
397 
398 
399 namespace Foam
400 {
401 
402 //- The physical current working directory path name (pwd -P).
404 {
405  label pathLengthLimit = POSIX::pathLengthChunk;
406  List<char> path(pathLengthLimit);
407 
408  // Resize path if getcwd fails with an ERANGE error
409  while (pathLengthLimit == path.size())
410  {
411  if (::getcwd(path.data(), path.size()))
412  {
413  return path.data();
414  }
415  else if (errno == ERANGE)
416  {
417  // Increment path length up to the pathLengthMax limit
418  if
419  (
420  (pathLengthLimit += POSIX::pathLengthChunk)
422  )
423  {
425  << "Attempt to increase path length beyond limit of "
427  << exit(FatalError);
428  }
429 
430  path.resize(pathLengthLimit);
431  }
432  else
433  {
434  break;
435  }
436  }
437 
439  << "Couldn't get the current working directory"
440  << exit(FatalError);
441 
442  return fileName();
443 }
444 
445 
446 //- The logical current working directory path name.
447 // From the PWD environment, same as pwd -L.
449 {
450  const char* env = ::getenv("PWD");
451 
452  // Basic check
453  if (!env || env[0] != '/')
454  {
456  << "PWD is invalid - reverting to physical description"
457  << nl;
458 
459  return cwd_P();
460  }
461 
462  fileName dir(env);
463 
464  // Check for "/."
465  for
466  (
468  std::string::npos != (pos = dir.find("/.", pos));
469  /*nil*/
470  )
471  {
472  pos += 2;
473 
474  if
475  (
476  // Ends in "/." or has "/./"
477  !dir[pos] || dir[pos] == '/'
478 
479  // Ends in "/.." or has "/../"
480  || (dir[pos] == '.' && (!dir[pos+1] || dir[pos+1] == '/'))
481  )
482  {
484  << "PWD contains /. or /.. - reverting to physical description"
485  << nl;
486 
487  return cwd_P();
488  }
489  }
490 
491  // Finally, verify that PWD actually corresponds to the "." directory
492  if (!fileStat(dir, true).sameINode(fileStat(".", true)))
493  {
495  << "PWD is not the cwd() - reverting to physical description"
496  << nl;
497 
498  return cwd_P();
499  }
500 
501 
502  return fileName(dir);
503 }
504 
505 } // End namespace Foam
506 
507 
509 {
510  return cwd(cwdPreference_);
511 }
512 
513 
514 Foam::fileName Foam::cwd(bool logical)
515 {
516  if (logical)
517  {
518  return cwd_L();
519  }
520 
521  return cwd_P();
522 }
523 
524 
525 bool Foam::chDir(const fileName& dir)
526 {
527  // Ignore an empty dir name => always false
528  return !dir.empty() && ::chdir(dir.c_str()) == 0;
529 }
530 
531 
532 bool Foam::mkDir(const fileName& pathName, mode_t mode)
533 {
534  if (POSIX::debug)
535  {
536  Pout<< FUNCTION_NAME << " : pathName:" << pathName << " mode:" << mode
537  << endl;
538  if ((POSIX::debug & 2) && !Pstream::master())
539  {
540  error::printStack(Pout);
541  }
542  }
543 
544  // empty names are meaningless
545  if (pathName.empty())
546  {
547  return false;
548  }
549 
550  // Construct path directory if does not exist
551  if (::mkdir(pathName.c_str(), mode) == 0)
552  {
553  // Directory made OK so return true
554  return true;
555  }
556 
557  switch (errno)
558  {
559  case EPERM:
560  {
562  << "The filesystem containing " << pathName
563  << " does not support the creation of directories."
564  << exit(FatalError);
565  break;
566  }
567 
568  case EEXIST:
569  {
570  // Directory already exists so simply return true
571  return true;
572  }
573 
574  case EFAULT:
575  {
577  << "" << pathName
578  << " points outside your accessible address space."
579  << exit(FatalError);
580  break;
581  }
582 
583  case EACCES:
584  {
586  << "The parent directory does not allow write "
587  "permission to the process,"<< nl
588  << " or one of the directories in " << pathName
589  << " did not allow search (execute) permission."
590  << exit(FatalError);
591  break;
592  }
593 
594  case ENAMETOOLONG:
595  {
597  << "" << pathName << " is too long."
598  << exit(FatalError);
599  break;
600  }
601 
602  case ENOENT:
603  {
604  // Part of the path does not exist so try to create it
605  if (pathName.path().size() && mkDir(pathName.path(), mode))
606  {
607  return mkDir(pathName, mode);
608  }
609 
611  << "Couldn't create directory " << pathName
612  << exit(FatalError);
613  break;
614  }
615 
616  case ENOTDIR:
617  {
619  << "A component used as a directory in " << pathName
620  << " is not, in fact, a directory."
621  << exit(FatalError);
622  break;
623  }
624 
625  case ENOMEM:
626  {
628  << "Insufficient kernel memory was available to make directory "
629  << pathName << '.'
630  << exit(FatalError);
631  break;
632  }
633 
634  case EROFS:
635  {
637  << "" << pathName
638  << " refers to a file on a read-only filesystem."
639  << exit(FatalError);
640  break;
641  }
642 
643  case ELOOP:
644  {
646  << "Too many symbolic links were encountered in resolving "
647  << pathName << '.'
648  << exit(FatalError);
649  break;
650  }
651 
652  case ENOSPC:
653  {
655  << "The device containing " << pathName
656  << " has no room for the new directory or "
657  << "the user's disk quota is exhausted."
658  << exit(FatalError);
659  break;
660  }
661 
662  default:
663  {
665  << "Couldn't create directory " << pathName
666  << exit(FatalError);
667  break;
668  }
669  }
670 
671  return false;
672 }
673 
674 
675 bool Foam::chMod(const fileName& name, const mode_t m)
676 {
677  if (POSIX::debug)
678  {
679  Pout<< FUNCTION_NAME << " : name:" << name << endl;
680  if ((POSIX::debug & 2) && !Pstream::master())
681  {
682  error::printStack(Pout);
683  }
684  }
685 
686  // Ignore an empty name => always false
687  return !name.empty() && ::chmod(name.c_str(), m) == 0;
688 }
689 
690 
691 mode_t Foam::mode(const fileName& name, const bool followLink)
692 {
693  if (POSIX::debug)
694  {
695  Pout<< FUNCTION_NAME << " : name:" << name << endl;
696  if ((POSIX::debug & 2) && !Pstream::master())
697  {
698  error::printStack(Pout);
699  }
700  }
701 
702  // Ignore an empty name => always 0
703  if (!name.empty())
704  {
705  fileStat fileStatus(name, followLink);
706  if (fileStatus.valid())
707  {
708  return fileStatus.status().st_mode;
709  }
710  }
711 
712  return 0;
713 }
714 
715 
716 Foam::fileName::Type Foam::type(const fileName& name, const bool followLink)
717 {
718  // Ignore an empty name => always UNDEFINED
719  if (name.empty())
720  {
721  return fileName::UNDEFINED;
722  }
723 
724  if (POSIX::debug)
725  {
726  Pout<< FUNCTION_NAME << " : name:" << name << endl;
727  }
728 
729  mode_t m = mode(name, followLink);
730 
731  if (S_ISREG(m))
732  {
733  return fileName::FILE;
734  }
735  else if (S_ISLNK(m))
736  {
737  return fileName::LINK;
738  }
739  else if (S_ISDIR(m))
740  {
741  return fileName::DIRECTORY;
742  }
743 
744  return fileName::UNDEFINED;
745 }
746 
747 
748 bool Foam::exists
749 (
750  const fileName& name,
751  const bool checkGzip,
752  const bool followLink
753 )
754 {
755  if (POSIX::debug)
756  {
757  Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip
758  << endl;
759  if ((POSIX::debug & 2) && !Pstream::master())
760  {
761  error::printStack(Pout);
762  }
763  }
764 
765  // Ignore an empty name => always false
766  return
767  (
768  !name.empty()
769  && (mode(name, followLink) || isFile(name, checkGzip, followLink))
770  );
771 }
772 
773 
774 bool Foam::isDir(const fileName& name, const bool followLink)
775 {
776  if (POSIX::debug)
777  {
778  Pout<< FUNCTION_NAME << " : name:" << name << endl;
779  if ((POSIX::debug & 2) && !Pstream::master())
780  {
781  error::printStack(Pout);
782  }
783  }
784 
785  // Ignore an empty name => always false
786  return !name.empty() && S_ISDIR(mode(name, followLink));
787 }
788 
789 
790 bool Foam::isFile
791 (
792  const fileName& name,
793  const bool checkGzip,
794  const bool followLink
795 )
796 {
797  if (POSIX::debug)
798  {
799  Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip
800  << endl;
801  if ((POSIX::debug & 2) && !Pstream::master())
802  {
803  error::printStack(Pout);
804  }
805  }
806 
807  // Ignore an empty name => always false
808  return
809  (
810  !name.empty()
811  && (
812  S_ISREG(mode(name, followLink))
813  || (checkGzip && S_ISREG(mode(name + ".gz", followLink)))
814  )
815  );
816 }
817 
818 
819 off_t Foam::fileSize(const fileName& name, const bool followLink)
820 {
821  if (POSIX::debug)
822  {
823  Pout<< FUNCTION_NAME << " : name:" << name << endl;
824  if ((POSIX::debug & 2) && !Pstream::master())
825  {
826  error::printStack(Pout);
827  }
828  }
829 
830  // Ignore an empty name
831  if (!name.empty())
832  {
833  fileStat fileStatus(name, followLink);
834  if (fileStatus.valid())
835  {
836  return fileStatus.status().st_size;
837  }
838  }
839 
840  return -1;
841 }
842 
843 
844 time_t Foam::lastModified(const fileName& name, const bool followLink)
845 {
846  if (POSIX::debug)
847  {
848  Pout<< FUNCTION_NAME << " : name:" << name << endl;
849  if ((POSIX::debug & 2) && !Pstream::master())
850  {
851  error::printStack(Pout);
852  }
853  }
854 
855  // Ignore an empty name
856  return name.empty() ? 0 : fileStat(name, followLink).modTime();
857 }
858 
859 
860 double Foam::highResLastModified(const fileName& name, const bool followLink)
861 {
862  if (POSIX::debug)
863  {
864  Pout<< FUNCTION_NAME << " : name:" << name << endl;
865  if ((POSIX::debug & 2) && !Pstream::master())
866  {
867  error::printStack(Pout);
868  }
869  }
870 
871  // Ignore an empty name
872  return name.empty() ? 0 : fileStat(name, followLink).dmodTime();
873 }
874 
875 
877 (
878  const fileName& directory,
879  const fileName::Type type,
880  const bool filtergz,
881  const bool followLink
882 )
883 {
884  // Initial filename list size and the increment when resizing the list
885  constexpr int maxNnames = 100;
886 
887  // Basic sanity: cannot strip '.gz' from directory names
888  const bool stripgz = filtergz && (type != fileName::DIRECTORY);
889  const word extgz("gz");
890 
891  fileNameList dirEntries;
892 
893  // Iterate contents (ignores an empty directory name)
894 
895  POSIX::directoryIterator dirIter(directory);
896  if (!dirIter.exists())
897  {
898  if (POSIX::debug)
899  {
901  << "cannot open directory " << directory << endl;
902  }
903 
904  return dirEntries;
905  }
906 
907  if (POSIX::debug)
908  {
909  // InfoInFunction
910  Pout<< FUNCTION_NAME << " : reading directory " << directory << endl;
911  if ((POSIX::debug & 2) && !Pstream::master())
912  {
913  error::printStack(Pout);
914  }
915  }
916 
917  label nFailed = 0; // Entries with invalid characters
918  label nEntries = 0; // Number of selected entries
919  dirEntries.resize(maxNnames);
920 
921  // Process the directory entries
922  for (/*nil*/; dirIter; ++dirIter)
923  {
924  const std::string& item = *dirIter;
925 
926  // Validate filename without spaces, quotes, etc in the name.
927  // No duplicate slashes to strip - dirent will not have them anyhow.
928 
929  const fileName name(fileName::validate(item));
930  if (name != item)
931  {
932  ++nFailed;
933  }
934  else if
935  (
936  (type == fileName::DIRECTORY)
937  || (type == fileName::FILE && !fileName::isBackup(name))
938  )
939  {
940  if ((directory/name).type(followLink) == type)
941  {
942  if (nEntries >= dirEntries.size())
943  {
944  dirEntries.resize(dirEntries.size() + maxNnames);
945  }
946 
947  if (stripgz && name.hasExt(extgz))
948  {
949  dirEntries[nEntries++] = name.lessExt();
950  }
951  else
952  {
953  dirEntries[nEntries++] = name;
954  }
955  }
956  }
957  }
958 
959  // Finalize the length of the entries list
960  dirEntries.resize(nEntries);
961 
962  if (nFailed && POSIX::debug)
963  {
964  std::cerr
965  << "Foam::readDir() : reading directory " << directory << nl
966  << nFailed << " entries with invalid characters in their name"
967  << std::endl;
968  }
969 
970  return dirEntries;
971 }
972 
973 
974 bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
975 {
976  if (POSIX::debug)
977  {
978  Pout<< FUNCTION_NAME << " : src:" << src << " dest:" << dest << endl;
979  if ((POSIX::debug & 2) && !Pstream::master())
980  {
981  error::printStack(Pout);
982  }
983  }
984 
985  // Make sure source exists - this also handles an empty source name
986  if (!exists(src))
987  {
988  return false;
989  }
990 
991  const fileName::Type srcType = src.type(followLink);
992 
993  fileName destFile(dest);
994 
995  // Check type of source file.
996  if (srcType == fileName::FILE)
997  {
998  // If dest is a directory, create the destination file name.
999  if (destFile.type() == fileName::DIRECTORY)
1000  {
1001  destFile /= src.name();
1002  }
1003 
1004  // Make sure the destination directory exists.
1005  if (!isDir(destFile.path()) && !mkDir(destFile.path()))
1006  {
1007  return false;
1008  }
1009 
1010  // Open and check streams. Enforce binary for extra safety
1011  std::ifstream srcStream(src, ios_base::in | ios_base::binary);
1012  if (!srcStream)
1013  {
1014  return false;
1015  }
1016 
1017  std::ofstream destStream(destFile, ios_base::out | ios_base::binary);
1018  if (!destStream)
1019  {
1020  return false;
1021  }
1022 
1023  // Copy character data.
1024  char ch;
1025  while (srcStream.get(ch))
1026  {
1027  destStream.put(ch);
1028  }
1029 
1030  // Final check.
1031  if (!srcStream.eof() || !destStream)
1032  {
1033  return false;
1034  }
1035  }
1036  else if (srcType == fileName::LINK)
1037  {
1038  // If dest is a directory, create the destination file name.
1039  if (destFile.type() == fileName::DIRECTORY)
1040  {
1041  destFile /= src.name();
1042  }
1043 
1044  // Make sure the destination directory exists.
1045  if (!isDir(destFile.path()) && !mkDir(destFile.path()))
1046  {
1047  return false;
1048  }
1049 
1050  ln(src, destFile);
1051  }
1052  else if (srcType == fileName::DIRECTORY)
1053  {
1054  // If dest is a directory, create the destination file name.
1055  if (destFile.type() == fileName::DIRECTORY)
1056  {
1057  destFile /= src.components().last();
1058  }
1059 
1060  // Make sure the destination directory exists.
1061  if (!isDir(destFile) && !mkDir(destFile))
1062  {
1063  return false;
1064  }
1065 
1066  char* realSrcPath = realpath(src.c_str(), nullptr);
1067  char* realDestPath = realpath(destFile.c_str(), nullptr);
1068  const bool samePath = strcmp(realSrcPath, realDestPath) == 0;
1069 
1070  if (POSIX::debug && samePath)
1071  {
1073  << "Attempt to copy " << realSrcPath << " to itself" << endl;
1074  }
1075 
1076  if (realSrcPath)
1077  {
1078  free(realSrcPath);
1079  }
1080 
1081  if (realDestPath)
1082  {
1083  free(realDestPath);
1084  }
1085 
1086  // Do not copy over self when src is actually a link to dest
1087  if (samePath)
1088  {
1089  return false;
1090  }
1091 
1092  // Copy files
1093  fileNameList files = readDir(src, fileName::FILE, false, followLink);
1094  for (const fileName& item : files)
1095  {
1096  if (POSIX::debug)
1097  {
1099  << "Copying : " << src/item
1100  << " to " << destFile/item << endl;
1101  }
1102 
1103  // File to file.
1104  cp(src/item, destFile/item, followLink);
1105  }
1106 
1107  // Copy sub directories.
1108  fileNameList dirs = readDir
1109  (
1110  src,
1111  fileName::DIRECTORY,
1112  false,
1113  followLink
1114  );
1115 
1116  for (const fileName& item : dirs)
1117  {
1118  if (POSIX::debug)
1119  {
1121  << "Copying : " << src/item
1122  << " to " << destFile << endl;
1123  }
1124 
1125  // Dir to Dir.
1126  cp(src/item, destFile, followLink);
1127  }
1128  }
1129  else
1130  {
1131  return false;
1132  }
1133 
1134  return true;
1135 }
1136 
1137 
1138 bool Foam::ln(const fileName& src, const fileName& dst)
1139 {
1140  if (POSIX::debug)
1141  {
1142  //InfoInFunction
1144  << " : Create softlink from : " << src << " to " << dst << endl;
1145  if ((POSIX::debug & 2) && !Pstream::master())
1146  {
1147  error::printStack(Pout);
1148  }
1149  }
1150 
1151  if (src.empty())
1152  {
1154  << "source name is empty: not linking." << endl;
1155  return false;
1156  }
1157 
1158  if (dst.empty())
1159  {
1161  << "destination name is empty: not linking." << endl;
1162  return false;
1163  }
1164 
1165  if (exists(dst))
1166  {
1168  << "destination " << dst << " already exists. Not linking."
1169  << endl;
1170  return false;
1171  }
1172 
1173  if (src.isAbsolute() && !exists(src))
1174  {
1176  << "source " << src << " does not exist." << endl;
1177  return false;
1178  }
1179 
1180  if (::symlink(src.c_str(), dst.c_str()) == 0)
1181  {
1182  return true;
1183  }
1184 
1186  << "symlink from " << src << " to " << dst << " failed." << endl;
1187  return false;
1188 }
1189 
1190 
1191 bool Foam::mv(const fileName& src, const fileName& dst, const bool followLink)
1192 {
1193  if (POSIX::debug)
1194  {
1195  //InfoInFunction
1196  Pout<< FUNCTION_NAME << " : Move : " << src << " to " << dst << endl;
1197  if ((POSIX::debug & 2) && !Pstream::master())
1198  {
1199  error::printStack(Pout);
1200  }
1201  }
1202 
1203  // Ignore an empty names => always false
1204  if (src.empty() || dst.empty())
1205  {
1206  return false;
1207  }
1208 
1209  if
1210  (
1211  dst.type() == fileName::DIRECTORY
1212  && src.type(followLink) != fileName::DIRECTORY
1213  )
1214  {
1215  const fileName dstName(dst/src.name());
1216 
1217  return (0 == std::rename(src.c_str(), dstName.c_str()));
1218  }
1219 
1220  return (0 == std::rename(src.c_str(), dst.c_str()));
1221 }
1222 
1223 
1224 bool Foam::mvBak(const fileName& src, const std::string& ext)
1225 {
1226  if (POSIX::debug)
1227  {
1228  //InfoInFunction
1230  << " : moving : " << src << " to extension " << ext << endl;
1231  if ((POSIX::debug & 2) && !Pstream::master())
1232  {
1233  error::printStack(Pout);
1234  }
1235  }
1236 
1237  // Ignore an empty name or extension => always false
1238  if (src.empty() || ext.empty())
1239  {
1240  return false;
1241  }
1242 
1243  if (exists(src, false))
1244  {
1245  constexpr const int maxIndex = 99;
1246  char index[3];
1247 
1248  for (int n = 0; n <= maxIndex; ++n)
1249  {
1250  fileName dstName(src + "." + ext);
1251  if (n)
1252  {
1253  ::sprintf(index, "%02d", n);
1254  dstName += index;
1255  }
1256 
1257  // avoid overwriting existing files, except for the last
1258  // possible index where we have no choice
1259  if (!exists(dstName, false) || n == maxIndex)
1260  {
1261  return (0 == std::rename(src.c_str(), dstName.c_str()));
1262  }
1263  }
1264  }
1265 
1266  // fallthrough: nothing to do
1267  return false;
1268 }
1269 
1270 
1271 bool Foam::rm(const fileName& file)
1272 {
1273  if (POSIX::debug)
1274  {
1275  //InfoInFunction
1276  Pout<< FUNCTION_NAME << " : Removing : " << file << endl;
1277  if ((POSIX::debug & 2) && !Pstream::master())
1278  {
1279  error::printStack(Pout);
1280  }
1281  }
1282 
1283  // Ignore an empty name => always false
1284  if (file.empty())
1285  {
1286  return false;
1287  }
1288 
1289  // If removal of plain file name fails, try with .gz
1290 
1291  return
1292  (
1293  0 == ::remove(file.c_str())
1294  || 0 == ::remove((file + ".gz").c_str())
1295  );
1296 }
1297 
1298 
1299 bool Foam::rmDir(const fileName& directory, const bool silent)
1300 {
1301  // Iterate contents (ignores an empty directory name)
1302  // Also retain hidden files/dirs for removal
1303 
1304  POSIX::directoryIterator dirIter(directory, true);
1305  if (!dirIter.exists())
1306  {
1307  if (!silent)
1308  {
1310  << "cannot open directory " << directory << endl;
1311  }
1312 
1313  return false;
1314  }
1315 
1316  if (POSIX::debug)
1317  {
1318  //InfoInFunction
1319  Pout<< FUNCTION_NAME << " : removing directory " << directory << endl;
1320  if ((POSIX::debug & 2) && !Pstream::master())
1321  {
1322  error::printStack(Pout);
1323  }
1324  }
1325 
1326  // Process each directory entry, counting any errors encountered
1327  label nErrors = 0;
1328 
1329  for (/*nil*/; dirIter; ++dirIter)
1330  {
1331  const std::string& item = *dirIter;
1332 
1333  // Allow invalid characters (spaces, quotes, etc),
1334  // otherwise we cannot remove subdirs with these types of names.
1335  // -> const fileName path = directory/name; <-
1336 
1337  const fileName path(fileName::concat(directory, item));
1338 
1339  if (path.type(false) == fileName::DIRECTORY)
1340  {
1341  if (!rmDir(path, true)) // Only report errors at the top-level
1342  {
1343  ++nErrors;
1344  }
1345  }
1346  else
1347  {
1348  if (!rm(path))
1349  {
1350  ++nErrors;
1351  }
1352  }
1353  }
1354 
1355  if (nErrors)
1356  {
1357  if (!silent)
1358  {
1360  << "failed to remove directory " << directory << nl
1361  << "could not remove " << nErrors << " sub-entries" << endl;
1362  }
1363  }
1364  else
1365  {
1366  if (!rm(directory))
1367  {
1368  ++nErrors;
1369  if (!silent)
1370  {
1372  << "failed to remove directory " << directory << endl;
1373  }
1374  }
1375  }
1376 
1377  // clean up
1378  return !nErrors;
1379 }
1380 
1381 
1382 unsigned int Foam::sleep(const unsigned int sec)
1383 {
1384  return ::sleep(sec);
1385 }
1386 
1387 
1388 void Foam::fdClose(const int fd)
1389 {
1390  if (close(fd) != 0)
1391  {
1393  << "close error on " << fd << endl
1394  << abort(FatalError);
1395  }
1396 }
1397 
1398 
1399 bool Foam::ping
1400 (
1401  const std::string& destName,
1402  const label destPort,
1403  const label timeOut
1404 )
1405 {
1406  struct hostent *hostPtr;
1407  volatile int sockfd;
1408  struct sockaddr_in destAddr; // will hold the destination addr
1409  u_int addr;
1410 
1411  if ((hostPtr = ::gethostbyname(destName.c_str())) == nullptr)
1412  {
1414  << "gethostbyname error " << h_errno << " for host " << destName
1415  << abort(FatalError);
1416  }
1417 
1418  // Get first of the SLL of addresses
1419  addr = (reinterpret_cast<struct in_addr*>(*(hostPtr->h_addr_list)))->s_addr;
1420 
1421  // Allocate socket
1422  sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1423  if (sockfd < 0)
1424  {
1426  << "socket error"
1427  << abort(FatalError);
1428  }
1429 
1430  // Fill sockaddr_in structure with dest address and port
1431  std::memset(reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr));
1432  destAddr.sin_family = AF_INET;
1433  destAddr.sin_port = htons(ushort(destPort));
1434  destAddr.sin_addr.s_addr = addr;
1435 
1436 
1437  timer myTimer(timeOut);
1438 
1439  if (timedOut(myTimer))
1440  {
1441  // Setjmp from timer jumps back to here
1442  fdClose(sockfd);
1443  return false;
1444  }
1445 
1446  if
1447  (
1448  ::connect
1449  (
1450  sockfd,
1451  reinterpret_cast<struct sockaddr*>(&destAddr),
1452  sizeof(struct sockaddr)
1453  ) != 0
1454  )
1455  {
1456  // Connection refused. Check if network was actually used or not.
1457 
1458  int connectErr = errno;
1459 
1460  fdClose(sockfd);
1461 
1462  if (connectErr == ECONNREFUSED)
1463  {
1464  return true;
1465  }
1466  //perror("connect");
1467 
1468  return false;
1469  }
1470 
1471  fdClose(sockfd);
1472 
1473  return true;
1474 }
1475 
1476 
1477 bool Foam::ping(const std::string& host, const label timeOut)
1478 {
1479  return ping(host, 222, timeOut) || ping(host, 22, timeOut);
1480 }
1481 
1482 
1483 namespace Foam
1484 {
1486 static int waitpid(const pid_t pid)
1487 {
1488  // child status, return code from the exec etc.
1489  int status = 0;
1490 
1491  // in parent - blocking wait
1492  // modest treatment of signals (in child)
1493  // treat 'stopped' like exit (suspend/continue)
1494 
1495  while (true)
1496  {
1497  pid_t wpid = ::waitpid(pid, &status, WUNTRACED);
1498 
1499  if (wpid == -1)
1500  {
1502  << "some error occurred in child"
1503  << exit(FatalError);
1504  break;
1505  }
1506 
1507  if (WIFEXITED(status))
1508  {
1509  // child exited, get its return status
1510  return WEXITSTATUS(status);
1511  }
1512 
1513  if (WIFSIGNALED(status))
1514  {
1515  // child terminated by some signal
1516  return WTERMSIG(status);
1517  }
1518 
1519  if (WIFSTOPPED(status))
1520  {
1521  // child stopped by some signal
1522  return WSTOPSIG(status);
1523  }
1524 
1526  << "programming error, status from waitpid() not handled: "
1527  << status
1528  << exit(FatalError);
1529  }
1530 
1531  return -1; // should not happen
1532 }
1534 }
1535 
1536 
1537 int Foam::system(const std::string& command, const bool bg)
1538 {
1539  if (command.empty())
1540  {
1541  // Treat an empty command as a successful no-op.
1542  // From 'man sh' POSIX (man sh):
1543  // "If the command_string operand is an empty string,
1544  // sh shall exit with a zero exit status."
1545  return 0;
1546  }
1547 
1548  const pid_t child_pid = ::vfork(); // NB: vfork, not fork!
1549 
1550  if (child_pid == -1)
1551  {
1553  << "vfork() failed for system command " << command
1554  << exit(FatalError);
1555 
1556  return -1; // fallback error value
1557  }
1558  else if (child_pid == 0)
1559  {
1560  // In child
1561 
1562  // Close or redirect file descriptors
1563  redirects(bg);
1564 
1565  // execl uses the current environ
1566  (void) ::execl
1567  (
1568  "/bin/sh", // Path of the shell
1569  "sh", // Command-name (name for the shell)
1570  "-c", // Read commands from command_string operand
1571  command.c_str(), // Command string
1572  reinterpret_cast<char*>(0)
1573  );
1574 
1575  // Obviously failed, since exec should not return
1577  << "exec failed: " << command
1578  << exit(FatalError);
1579 
1580  return -1; // fallback error value
1581  }
1582 
1583 
1584  // In parent
1585  // - started as background process, or blocking wait for the child
1586 
1587  return (bg ? 0 : waitpid(child_pid));
1588 }
1589 
1590 
1591 int Foam::system(const CStringList& command, const bool bg)
1592 {
1593  if (command.empty())
1594  {
1595  // Treat an empty command as a successful no-op.
1596  // For consistency with POSIX (man sh) behaviour for (sh -c command),
1597  // which is what is mostly being replicated here.
1598  return 0;
1599  }
1600 
1601  // NB: use vfork, not fork!
1602  // vfork behaves more like a thread and avoids copy-on-write problems
1603  // triggered by fork.
1604  // The normal system() command has a fork buried in it that causes
1605  // issues with infiniband and openmpi etc.
1606 
1607  const pid_t child_pid = ::vfork();
1608 
1609  if (child_pid == -1)
1610  {
1612  << "vfork() failed for system command " << command[0]
1613  << exit(FatalError);
1614 
1615  return -1; // fallback error value
1616  }
1617  else if (child_pid == 0)
1618  {
1619  // In child
1620 
1621  // Close or redirect file descriptors
1622  redirects(bg);
1623 
1624  // execvp searches the path, uses the current environ
1625  (void) ::execvp(command[0], command.strings());
1626 
1627  // Obviously failed, since exec should not return
1629  << "exec(" << command[0] << ", ...) failed"
1630  << exit(FatalError);
1631 
1632  return -1; // fallback error value
1633  }
1634 
1635 
1636  // In parent
1637  // - started as background process, or blocking wait for the child
1638 
1639  return (bg ? 0 : waitpid(child_pid));
1640 }
1641 
1642 
1643 int Foam::system(const Foam::UList<Foam::string>& command, const bool bg)
1644 {
1645  if (command.empty())
1646  {
1647  // Treat an empty command as a successful no-op.
1648  return 0;
1649  }
1650 
1651  // Make a deep copy as C-strings
1652  const CStringList cmd(command);
1653  return Foam::system(cmd, bg);
1654 }
1655 
1656 
1657 void* Foam::dlOpen(const fileName& libName, const bool check)
1658 {
1659  constexpr int ldflags = (RTLD_LAZY|RTLD_GLOBAL);
1660 
1661  if (POSIX::debug)
1662  {
1663  std::cout
1664  << "dlOpen(const fileName&)"
1665  << " : dlopen of " << libName << std::endl;
1666  }
1667 
1668  void* handle = ::dlopen(libName.c_str(), ldflags);
1669 
1670  if (!handle)
1671  {
1672  fileName libso;
1673 
1674  if
1675  (
1676  libName.find('/') == std::string::npos
1677  && !libName.starts_with("lib")
1678  )
1679  {
1680  // Try with 'lib' prefix
1681  libso = "lib" + libName;
1682  handle = ::dlopen(libso.c_str(), ldflags);
1683 
1684  if (POSIX::debug)
1685  {
1686  std::cout
1687  << "dlOpen(const fileName&)"
1688  << " : dlopen of " << libso << std::endl;
1689  }
1690  }
1691  else
1692  {
1693  libso = libName;
1694  }
1695 
1696  // With canonical library extension ("so" or "dylib"), which remaps
1697  // "libXX" to "libXX.so" as well as "libXX.so" -> "libXX.dylib"
1698  if (!handle && !libso.hasExt(EXT_SO))
1699  {
1700  libso = libso.lessExt().ext(EXT_SO);
1701  handle = ::dlopen(libso.c_str(), ldflags);
1702 
1703  if (POSIX::debug)
1704  {
1705  std::cout
1706  << "dlOpen(const fileName&)"
1707  << " : dlopen of " << libso << std::endl;
1708  }
1709  }
1710  }
1711 
1712  if (!handle && check)
1713  {
1715  << "dlopen error : " << ::dlerror() << endl;
1716  }
1717 
1718  if (POSIX::debug)
1719  {
1720  std::cout
1721  << "dlOpen(const fileName&)"
1722  << " : dlopen of " << libName
1723  << " handle " << handle << std::endl;
1724  }
1725 
1726  return handle;
1727 }
1728 
1729 
1730 void* Foam::dlOpen(const fileName& libName, std::string& errorMsg)
1731 {
1732  // Call without emitting error message - we capture that ourselves
1733  void* handle = Foam::dlOpen(libName, false);
1734 
1735  if (!handle)
1736  {
1737  // Capture error message
1738  errorMsg = ::dlerror();
1739  }
1740  else
1741  {
1742  // No errors
1743  errorMsg.clear();
1744  }
1745 
1746  return handle;
1747 }
1748 
1749 
1750 Foam::label Foam::dlOpen
1751 (
1752  std::initializer_list<fileName> libNames,
1753  const bool check
1754 )
1755 {
1756  label nLoaded = 0;
1757 
1758  for (const fileName& libName : libNames)
1759  {
1760  if (Foam::dlOpen(libName, check))
1761  {
1762  ++nLoaded;
1763  }
1764  }
1765 
1766  return nLoaded;
1767 }
1768 
1769 
1770 bool Foam::dlClose(void* handle)
1771 {
1772  if (POSIX::debug)
1773  {
1774  std::cout
1775  << "dlClose(void*)"
1776  << " : dlclose of handle " << handle << std::endl;
1777  }
1778  return ::dlclose(handle) == 0;
1779 }
1780 
1781 
1782 void* Foam::dlSymFind(void* handle, const std::string& symbol, bool required)
1783 {
1784  if (!required && (!handle || symbol.empty()))
1785  {
1786  return nullptr;
1787  }
1788 
1789  if (POSIX::debug)
1790  {
1791  std::cout
1792  << "dlSymFind(void*, const std::string&, bool)"
1793  << " : dlsym of " << symbol << std::endl;
1794  }
1795 
1796  // Clear any old errors - see manpage dlopen
1797  (void) ::dlerror();
1798 
1799  // Get address of symbol
1800  void* fun = ::dlsym(handle, symbol.c_str());
1801 
1802  // Any error?
1803  char *err = ::dlerror();
1804 
1805  if (err)
1806  {
1807  if (!required)
1808  {
1809  return nullptr;
1810  }
1811 
1813  << "Cannot lookup symbol " << symbol << " : " << err
1814  << endl;
1815  }
1816 
1817  return fun;
1818 }
1819 
1820 
1821 #ifndef __APPLE__
1822 static int collectLibsCallback
1824  struct dl_phdr_info *info,
1825  size_t size,
1826  void *data
1827 )
1828 {
1830  reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
1831  ptr->append(info->dlpi_name);
1832  return 0;
1833 }
1834 #endif
1835 
1836 
1838 {
1839  DynamicList<fileName> libs;
1840  #ifdef __APPLE__
1841  for (uint32_t i=0; i < _dyld_image_count(); ++i)
1842  {
1843  libs.append(_dyld_get_image_name(i));
1844  }
1845  #else
1846  dl_iterate_phdr(collectLibsCallback, &libs);
1847  #endif
1848 
1849  if (POSIX::debug)
1850  {
1851  std::cout
1852  << "dlLoaded()"
1853  << " : determined loaded libraries :" << libs.size() << std::endl;
1854  }
1855  return libs;
1856 }
1857 
1858 
1859 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::word::lessExt
word lessExt() const
Return word without extension (part before last .)
Definition: word.C:113
Foam::domainName
string domainName()
Return the system's domain name, as per hostname(1) with the '-d' option.
Definition: MSwindows.C:420
Foam::POSIX::pathLengthMax
constexpr label pathLengthMax
Definition: POSIX.H:55
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
p
volScalarField & p
Definition: createFieldRefs.H:8
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::exists
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:625
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::chMod
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition: MSwindows.C:557
cp
const volScalarField & cp
Definition: setRegionSolidFields.H:8
Foam::DynamicList< Foam::fileName >
Foam::POSIX::directoryIterator::close
void close()
Close directory.
Definition: POSIX.C:183
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1140
redirects
static void redirects(const bool bg)
Definition: POSIX.C:94
validate
thermo validate(args.executable(), "h")
Foam::fileName::Type
Type
Enumerations to handle directory entry types.
Definition: fileName.H:76
Foam::fileStat
Wrapper for stat() and lstat() system calls.
Definition: fileStat.H:67
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:658
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::fdClose
void fdClose(const int fd)
Close file descriptor.
Definition: MSwindows.C:1104
Foam::setEnv
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable, return true on success.
Definition: MSwindows.C:395
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::POSIX::directoryIterator::operator*
const std::string & operator*() const
Same as val()
Definition: POSIX.C:229
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::POSIX::pathLengthChunk
constexpr label pathLengthChunk
Definition: POSIX.H:54
Foam::POSIX::directoryIterator::good
bool good() const
Directory pointer is valid.
Definition: POSIX.C:177
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
Foam::pgid
pid_t pgid()
Return the group PID of this process.
Definition: MSwindows.C:350
timedOut
#define timedOut(x)
Check if timeout has occurred.
Definition: timer.H:73
Foam::isAdministrator
bool isAdministrator()
Is the current user the administrator (root)
Definition: MSwindows.C:442
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::POSIX::directoryIterator::val
const std::string & val() const
The current item.
Definition: POSIX.C:193
Foam::hasEnv
bool hasEnv(const std::string &envName)
True if environment variable of given name is defined.
Definition: MSwindows.C:363
Foam::userName
string userName()
Return the user's login name.
Definition: MSwindows.C:429
Foam::cwd_L
static Foam::fileName cwd_L()
The logical current working directory path name.
Definition: POSIX.C:448
Foam::fileSize
off_t fileSize(const fileName &name, const bool followLink=true)
Return size of file or -1 on failure (normally follows symbolic links).
Definition: MSwindows.C:676
Foam::word::hasExt
bool hasExt() const
Return true if it has an extension or simply ends with a '.'.
Definition: wordI.H:165
Foam::dlSymFind
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition: MSwindows.C:1321
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:474
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
POSIX.H
Foam::POSIX::directoryIterator
A simple directory contents iterator.
Definition: POSIX.C:119
Foam::POSIX::directoryIterator::~directoryIterator
~directoryIterator()
Destructor.
Definition: POSIX.C:162
Foam::ppid
pid_t ppid()
Return the parent PID of this process.
Definition: MSwindows.C:337
fileName.H
Foam::highResLastModified
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
Definition: MSwindows.C:699
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::ping
bool ping(const std::string &destName, const label port, const label timeOut)
Check if machine is up by pinging given port.
Definition: MSwindows.C:1116
Foam::POSIX::directoryIterator::directoryIterator
directoryIterator(const fileName &dirName, bool allowHidden=false)
Construct for dirName, optionally allowing hidden files/dirs.
Definition: POSIX.C:145
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:237
Foam::UList::empty
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:374
Foam::FatalError
error FatalError
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:929
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::fileNameList
List< fileName > fileNameList
A List of fileNames.
Definition: fileNameList.H:58
Foam::mvBak
bool mvBak(const fileName &src, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: MSwindows.C:958
Foam::chDir
bool chDir(const fileName &dir)
Change current directory to the one specified and return true on success.
Definition: MSwindows.C:500
cwdPreference_
static bool cwdPreference_(Foam::debug::optimisationSwitch("cwd", 0))
CStringList.H
Foam::POSIX::directoryIterator::next
bool next()
Read next item, always ignoring "." and ".." entries.
Definition: POSIX.C:201
Foam::home
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:449
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::cp
bool cp(const fileName &src, const fileName &dst, const bool followLink=true)
Copy the source to the destination (recursively if necessary).
Definition: MSwindows.C:802
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::cwd_P
static Foam::fileName cwd_P()
The physical current working directory path name (pwd -P).
Definition: POSIX.C:403
Foam::rmDir
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1018
Foam::env
bool env(const std::string &envName)
Deprecated(2020-05) check for existence of environment variable.
Definition: OSspecific.H:82
Foam::List< char >
Foam::POSIX::directoryIterator::operator++
directoryIterator & operator++()
Same as next()
Definition: POSIX.C:235
Foam::POSIX::directoryIterator::exists
bool exists() const
Directory open succeeded.
Definition: POSIX.C:171
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::hostName
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
bool
bool
Definition: EEqn.H:20
Foam::dlClose
bool dlClose(void *handle)
Close a dlopened library using handle. Return true if successful.
Definition: MSwindows.C:1301
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
collectLibsCallback
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
Definition: POSIX.C:1823
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: MSwindows.C:915
Foam::cwd
fileName cwd()
The physical or logical current working directory path name.
Definition: MSwindows.C:468
DynamicList.H
Foam::dlLoaded
fileNameList dlLoaded()
Return all loaded libraries.
Definition: MSwindows.C:1354
EXT_SO
#define EXT_SO
Definition: POSIX.C:68
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::readDir
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition: MSwindows.C:707
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::sleep
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition: MSwindows.C:1096
Foam::dlOpen
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition: MSwindows.C:1206
Foam::lastModified
time_t lastModified(const fileName &name, const bool followLink=true)
Return time of last file modification (normally follows symbolic links).
Definition: MSwindows.C:692
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177