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-2021 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  Foam::ln(src, destFile);
1051  }
1052  else if (srcType == fileName::DIRECTORY)
1053  {
1054  if (destFile.type() == fileName::DIRECTORY)
1055  {
1056  // Both are directories. Could mean copy contents or copy
1057  // recursively. Don't actually know what the user wants,
1058  // but assume that if names are identical == copy contents.
1059  //
1060  // So: "path1/foo" "path2/foo" copy contents
1061  // So: "path1/foo" "path2/bar" copy directory
1062 
1063  const word srcDirName = src.name();
1064  if (destFile.name() != srcDirName)
1065  {
1066  destFile /= srcDirName;
1067  }
1068  }
1069 
1070  // Make sure the destination directory exists.
1071  if (!isDir(destFile) && !mkDir(destFile))
1072  {
1073  return false;
1074  }
1075 
1076  char* realSrcPath = realpath(src.c_str(), nullptr);
1077  char* realDestPath = realpath(destFile.c_str(), nullptr);
1078  const bool samePath = strcmp(realSrcPath, realDestPath) == 0;
1079 
1080  if (POSIX::debug && samePath)
1081  {
1083  << "Attempt to copy " << realSrcPath << " to itself" << endl;
1084  }
1085 
1086  if (realSrcPath)
1087  {
1088  free(realSrcPath);
1089  }
1090 
1091  if (realDestPath)
1092  {
1093  free(realDestPath);
1094  }
1095 
1096  // Do not copy over self when src is actually a link to dest
1097  if (samePath)
1098  {
1099  return false;
1100  }
1101 
1102  // Copy files
1103  fileNameList files = readDir(src, fileName::FILE, false, followLink);
1104  for (const fileName& item : files)
1105  {
1106  if (POSIX::debug)
1107  {
1109  << "Copying : " << src/item
1110  << " to " << destFile/item << endl;
1111  }
1112 
1113  // File to file.
1114  Foam::cp(src/item, destFile/item, followLink);
1115  }
1116 
1117  // Copy sub directories.
1118  fileNameList dirs = readDir
1119  (
1120  src,
1121  fileName::DIRECTORY,
1122  false,
1123  followLink
1124  );
1125 
1126  for (const fileName& item : dirs)
1127  {
1128  if (POSIX::debug)
1129  {
1131  << "Copying : " << src/item
1132  << " to " << destFile << endl;
1133  }
1134 
1135  // Dir to Dir.
1136  Foam::cp(src/item, destFile, followLink);
1137  }
1138  }
1139  else
1140  {
1141  return false;
1142  }
1143 
1144  return true;
1145 }
1146 
1147 
1148 bool Foam::ln(const fileName& src, const fileName& dst)
1149 {
1150  if (POSIX::debug)
1151  {
1152  //InfoInFunction
1154  << " : Create softlink from : " << src << " to " << dst << endl;
1155  if ((POSIX::debug & 2) && !Pstream::master())
1156  {
1157  error::printStack(Pout);
1158  }
1159  }
1160 
1161  if (src.empty())
1162  {
1164  << "source name is empty: not linking." << endl;
1165  return false;
1166  }
1167 
1168  if (dst.empty())
1169  {
1171  << "destination name is empty: not linking." << endl;
1172  return false;
1173  }
1174 
1175  if (exists(dst))
1176  {
1178  << "destination " << dst << " already exists. Not linking."
1179  << endl;
1180  return false;
1181  }
1182 
1183  if (src.isAbsolute() && !exists(src))
1184  {
1186  << "source " << src << " does not exist." << endl;
1187  return false;
1188  }
1189 
1190  if (::symlink(src.c_str(), dst.c_str()) == 0)
1191  {
1192  return true;
1193  }
1194 
1196  << "symlink from " << src << " to " << dst << " failed." << endl;
1197  return false;
1198 }
1199 
1200 
1201 bool Foam::mv(const fileName& src, const fileName& dst, const bool followLink)
1202 {
1203  if (POSIX::debug)
1204  {
1205  //InfoInFunction
1206  Pout<< FUNCTION_NAME << " : Move : " << src << " to " << dst << endl;
1207  if ((POSIX::debug & 2) && !Pstream::master())
1208  {
1209  error::printStack(Pout);
1210  }
1211  }
1212 
1213  // Ignore an empty names => always false
1214  if (src.empty() || dst.empty())
1215  {
1216  return false;
1217  }
1218 
1219  if
1220  (
1221  dst.type() == fileName::DIRECTORY
1222  && src.type(followLink) != fileName::DIRECTORY
1223  )
1224  {
1225  const fileName dstName(dst/src.name());
1226 
1227  return (0 == std::rename(src.c_str(), dstName.c_str()));
1228  }
1229 
1230  return (0 == std::rename(src.c_str(), dst.c_str()));
1231 }
1232 
1233 
1234 bool Foam::mvBak(const fileName& src, const std::string& ext)
1235 {
1236  if (POSIX::debug)
1237  {
1238  //InfoInFunction
1240  << " : moving : " << src << " to extension " << ext << endl;
1241  if ((POSIX::debug & 2) && !Pstream::master())
1242  {
1243  error::printStack(Pout);
1244  }
1245  }
1246 
1247  // Ignore an empty name or extension => always false
1248  if (src.empty() || ext.empty())
1249  {
1250  return false;
1251  }
1252 
1253  if (exists(src, false))
1254  {
1255  constexpr const int maxIndex = 99;
1256  char index[3];
1257 
1258  for (int n = 0; n <= maxIndex; ++n)
1259  {
1260  fileName dstName(src + "." + ext);
1261  if (n)
1262  {
1263  ::sprintf(index, "%02d", n);
1264  dstName += index;
1265  }
1266 
1267  // avoid overwriting existing files, except for the last
1268  // possible index where we have no choice
1269  if (!exists(dstName, false) || n == maxIndex)
1270  {
1271  return (0 == std::rename(src.c_str(), dstName.c_str()));
1272  }
1273  }
1274  }
1275 
1276  // fallthrough: nothing to do
1277  return false;
1278 }
1279 
1280 
1281 bool Foam::rm(const fileName& file)
1282 {
1283  if (POSIX::debug)
1284  {
1285  //InfoInFunction
1286  Pout<< FUNCTION_NAME << " : Removing : " << file << endl;
1287  if ((POSIX::debug & 2) && !Pstream::master())
1288  {
1289  error::printStack(Pout);
1290  }
1291  }
1292 
1293  // Ignore an empty name => always false
1294  if (file.empty())
1295  {
1296  return false;
1297  }
1298 
1299  // If removal of plain file name fails, try with .gz
1300 
1301  return
1302  (
1303  0 == ::remove(file.c_str())
1304  || 0 == ::remove((file + ".gz").c_str())
1305  );
1306 }
1307 
1308 
1309 bool Foam::rmDir(const fileName& directory, const bool silent)
1310 {
1311  // Iterate contents (ignores an empty directory name)
1312  // Also retain hidden files/dirs for removal
1313 
1314  POSIX::directoryIterator dirIter(directory, true);
1315  if (!dirIter.exists())
1316  {
1317  if (!silent)
1318  {
1320  << "cannot open directory " << directory << endl;
1321  }
1322 
1323  return false;
1324  }
1325 
1326  if (POSIX::debug)
1327  {
1328  //InfoInFunction
1329  Pout<< FUNCTION_NAME << " : removing directory " << directory << endl;
1330  if ((POSIX::debug & 2) && !Pstream::master())
1331  {
1332  error::printStack(Pout);
1333  }
1334  }
1335 
1336  // Process each directory entry, counting any errors encountered
1337  label nErrors = 0;
1338 
1339  for (/*nil*/; dirIter; ++dirIter)
1340  {
1341  const std::string& item = *dirIter;
1342 
1343  // Allow invalid characters (spaces, quotes, etc),
1344  // otherwise we cannot remove subdirs with these types of names.
1345  // -> const fileName path = directory/name; <-
1346 
1347  const fileName path(fileName::concat(directory, item));
1348 
1349  if (path.type(false) == fileName::DIRECTORY)
1350  {
1351  if (!rmDir(path, true)) // Only report errors at the top-level
1352  {
1353  ++nErrors;
1354  }
1355  }
1356  else
1357  {
1358  if (!rm(path))
1359  {
1360  ++nErrors;
1361  }
1362  }
1363  }
1364 
1365  if (nErrors)
1366  {
1367  if (!silent)
1368  {
1370  << "failed to remove directory " << directory << nl
1371  << "could not remove " << nErrors << " sub-entries" << endl;
1372  }
1373  }
1374  else
1375  {
1376  if (!rm(directory))
1377  {
1378  ++nErrors;
1379  if (!silent)
1380  {
1382  << "failed to remove directory " << directory << endl;
1383  }
1384  }
1385  }
1386 
1387  // clean up
1388  return !nErrors;
1389 }
1390 
1391 
1392 unsigned int Foam::sleep(const unsigned int sec)
1393 {
1394  return ::sleep(sec);
1395 }
1396 
1397 
1398 void Foam::fdClose(const int fd)
1399 {
1400  if (close(fd) != 0)
1401  {
1403  << "close error on " << fd << endl
1404  << abort(FatalError);
1405  }
1406 }
1407 
1408 
1409 bool Foam::ping
1410 (
1411  const std::string& destName,
1412  const label destPort,
1413  const label timeOut
1414 )
1415 {
1416  struct hostent *hostPtr;
1417  volatile int sockfd;
1418  struct sockaddr_in destAddr; // will hold the destination addr
1419  u_int addr;
1420 
1421  if ((hostPtr = ::gethostbyname(destName.c_str())) == nullptr)
1422  {
1424  << "gethostbyname error " << h_errno << " for host " << destName
1425  << abort(FatalError);
1426  }
1427 
1428  // Get first of the SLL of addresses
1429  addr = (reinterpret_cast<struct in_addr*>(*(hostPtr->h_addr_list)))->s_addr;
1430 
1431  // Allocate socket
1432  sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1433  if (sockfd < 0)
1434  {
1436  << "socket error"
1437  << abort(FatalError);
1438  }
1439 
1440  // Fill sockaddr_in structure with dest address and port
1441  std::memset(reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr));
1442  destAddr.sin_family = AF_INET;
1443  destAddr.sin_port = htons(ushort(destPort));
1444  destAddr.sin_addr.s_addr = addr;
1445 
1446 
1447  timer myTimer(timeOut);
1448 
1449  if (timedOut(myTimer))
1450  {
1451  // Setjmp from timer jumps back to here
1452  fdClose(sockfd);
1453  return false;
1454  }
1455 
1456  if
1457  (
1458  ::connect
1459  (
1460  sockfd,
1461  reinterpret_cast<struct sockaddr*>(&destAddr),
1462  sizeof(struct sockaddr)
1463  ) != 0
1464  )
1465  {
1466  // Connection refused. Check if network was actually used or not.
1467 
1468  int connectErr = errno;
1469 
1470  fdClose(sockfd);
1471 
1472  if (connectErr == ECONNREFUSED)
1473  {
1474  return true;
1475  }
1476  //perror("connect");
1477 
1478  return false;
1479  }
1480 
1481  fdClose(sockfd);
1482 
1483  return true;
1484 }
1485 
1486 
1487 bool Foam::ping(const std::string& host, const label timeOut)
1488 {
1489  return ping(host, 222, timeOut) || ping(host, 22, timeOut);
1490 }
1491 
1492 
1493 namespace Foam
1494 {
1496 static int waitpid(const pid_t pid)
1497 {
1498  // child status, return code from the exec etc.
1499  int status = 0;
1500 
1501  // in parent - blocking wait
1502  // modest treatment of signals (in child)
1503  // treat 'stopped' like exit (suspend/continue)
1504 
1505  while (true)
1506  {
1507  pid_t wpid = ::waitpid(pid, &status, WUNTRACED);
1508 
1509  if (wpid == -1)
1510  {
1512  << "some error occurred in child"
1513  << exit(FatalError);
1514  break;
1515  }
1516 
1517  if (WIFEXITED(status))
1518  {
1519  // child exited, get its return status
1520  return WEXITSTATUS(status);
1521  }
1522 
1523  if (WIFSIGNALED(status))
1524  {
1525  // child terminated by some signal
1526  return WTERMSIG(status);
1527  }
1528 
1529  if (WIFSTOPPED(status))
1530  {
1531  // child stopped by some signal
1532  return WSTOPSIG(status);
1533  }
1534 
1536  << "programming error, status from waitpid() not handled: "
1537  << status
1538  << exit(FatalError);
1539  }
1540 
1541  return -1; // should not happen
1542 }
1544 }
1545 
1546 
1547 int Foam::system(const std::string& command, const bool bg)
1548 {
1549  if (command.empty())
1550  {
1551  // Treat an empty command as a successful no-op.
1552  // From 'man sh' POSIX (man sh):
1553  // "If the command_string operand is an empty string,
1554  // sh shall exit with a zero exit status."
1555  return 0;
1556  }
1557 
1558  const pid_t child_pid = ::vfork(); // NB: vfork, not fork!
1559 
1560  if (child_pid == -1)
1561  {
1563  << "vfork() failed for system command " << command
1564  << exit(FatalError);
1565 
1566  return -1; // fallback error value
1567  }
1568  else if (child_pid == 0)
1569  {
1570  // In child
1571 
1572  // Close or redirect file descriptors
1573  redirects(bg);
1574 
1575  // execl uses the current environ
1576  (void) ::execl
1577  (
1578  "/bin/sh", // Path of the shell
1579  "sh", // Command-name (name for the shell)
1580  "-c", // Read commands from command_string operand
1581  command.c_str(), // Command string
1582  reinterpret_cast<char*>(0)
1583  );
1584 
1585  // Obviously failed, since exec should not return
1587  << "exec failed: " << command
1588  << exit(FatalError);
1589 
1590  return -1; // fallback error value
1591  }
1592 
1593 
1594  // In parent
1595  // - started as background process, or blocking wait for the child
1596 
1597  return (bg ? 0 : waitpid(child_pid));
1598 }
1599 
1600 
1601 int Foam::system(const CStringList& command, const bool bg)
1602 {
1603  if (command.empty())
1604  {
1605  // Treat an empty command as a successful no-op.
1606  // For consistency with POSIX (man sh) behaviour for (sh -c command),
1607  // which is what is mostly being replicated here.
1608  return 0;
1609  }
1610 
1611  // NB: use vfork, not fork!
1612  // vfork behaves more like a thread and avoids copy-on-write problems
1613  // triggered by fork.
1614  // The normal system() command has a fork buried in it that causes
1615  // issues with infiniband and openmpi etc.
1616 
1617  const pid_t child_pid = ::vfork();
1618 
1619  if (child_pid == -1)
1620  {
1622  << "vfork() failed for system command " << command[0]
1623  << exit(FatalError);
1624 
1625  return -1; // fallback error value
1626  }
1627  else if (child_pid == 0)
1628  {
1629  // In child
1630 
1631  // Close or redirect file descriptors
1632  redirects(bg);
1633 
1634  // execvp searches the path, uses the current environ
1635  (void) ::execvp(command[0], command.strings());
1636 
1637  // Obviously failed, since exec should not return
1639  << "exec(" << command[0] << ", ...) failed"
1640  << exit(FatalError);
1641 
1642  return -1; // fallback error value
1643  }
1644 
1645 
1646  // In parent
1647  // - started as background process, or blocking wait for the child
1648 
1649  return (bg ? 0 : waitpid(child_pid));
1650 }
1651 
1652 
1653 int Foam::system(const Foam::UList<Foam::string>& command, const bool bg)
1654 {
1655  if (command.empty())
1656  {
1657  // Treat an empty command as a successful no-op.
1658  return 0;
1659  }
1660 
1661  // Make a deep copy as C-strings
1662  const CStringList cmd(command);
1663  return Foam::system(cmd, bg);
1664 }
1665 
1666 
1667 void* Foam::dlOpen(const fileName& libName, const bool check)
1668 {
1669  constexpr int ldflags = (RTLD_LAZY|RTLD_GLOBAL);
1670 
1671  if (POSIX::debug)
1672  {
1673  std::cout
1674  << "dlOpen(const fileName&)"
1675  << " : dlopen of " << libName << std::endl;
1676  }
1677 
1678  void* handle = ::dlopen(libName.c_str(), ldflags);
1679 
1680  if (!handle)
1681  {
1682  fileName libso;
1683 
1684  if
1685  (
1686  libName.find('/') == std::string::npos
1687  && !libName.starts_with("lib")
1688  )
1689  {
1690  // Try with 'lib' prefix
1691  libso = "lib" + libName;
1692  handle = ::dlopen(libso.c_str(), ldflags);
1693 
1694  if (POSIX::debug)
1695  {
1696  std::cout
1697  << "dlOpen(const fileName&)"
1698  << " : dlopen of " << libso << std::endl;
1699  }
1700  }
1701  else
1702  {
1703  libso = libName;
1704  }
1705 
1706  // With canonical library extension ("so" or "dylib"), which remaps
1707  // "libXX" to "libXX.so" as well as "libXX.so" -> "libXX.dylib"
1708  if (!handle && !libso.hasExt(EXT_SO))
1709  {
1710  libso = libso.lessExt().ext(EXT_SO);
1711  handle = ::dlopen(libso.c_str(), ldflags);
1712 
1713  if (POSIX::debug)
1714  {
1715  std::cout
1716  << "dlOpen(const fileName&)"
1717  << " : dlopen of " << libso << std::endl;
1718  }
1719  }
1720  }
1721 
1722  if (!handle && check)
1723  {
1725  << "dlopen error : " << ::dlerror() << endl;
1726  }
1727 
1728  if (POSIX::debug)
1729  {
1730  std::cout
1731  << "dlOpen(const fileName&)"
1732  << " : dlopen of " << libName
1733  << " handle " << handle << std::endl;
1734  }
1735 
1736  return handle;
1737 }
1738 
1739 
1740 void* Foam::dlOpen(const fileName& libName, std::string& errorMsg)
1741 {
1742  // Call without emitting error message - we capture that ourselves
1743  void* handle = Foam::dlOpen(libName, false);
1744 
1745  if (!handle)
1746  {
1747  // Capture error message
1748  errorMsg = ::dlerror();
1749  }
1750  else
1751  {
1752  // No errors
1753  errorMsg.clear();
1754  }
1755 
1756  return handle;
1757 }
1758 
1759 
1760 Foam::label Foam::dlOpen
1761 (
1762  std::initializer_list<fileName> libNames,
1763  const bool check
1764 )
1765 {
1766  label nLoaded = 0;
1767 
1768  for (const fileName& libName : libNames)
1769  {
1770  if (Foam::dlOpen(libName, check))
1771  {
1772  ++nLoaded;
1773  }
1774  }
1775 
1776  return nLoaded;
1777 }
1778 
1779 
1780 bool Foam::dlClose(void* handle)
1781 {
1782  if (POSIX::debug)
1783  {
1784  std::cout
1785  << "dlClose(void*)"
1786  << " : dlclose of handle " << handle << std::endl;
1787  }
1788  return ::dlclose(handle) == 0;
1789 }
1790 
1791 
1792 void* Foam::dlSymFind(void* handle, const std::string& symbol, bool required)
1793 {
1794  if (!required && (!handle || symbol.empty()))
1795  {
1796  return nullptr;
1797  }
1798 
1799  if (POSIX::debug)
1800  {
1801  std::cout
1802  << "dlSymFind(void*, const std::string&, bool)"
1803  << " : dlsym of " << symbol << std::endl;
1804  }
1805 
1806  // Clear any old errors - see manpage dlopen
1807  (void) ::dlerror();
1808 
1809  // Get address of symbol
1810  void* fun = ::dlsym(handle, symbol.c_str());
1811 
1812  // Any error?
1813  char *err = ::dlerror();
1814 
1815  if (err)
1816  {
1817  if (!required)
1818  {
1819  return nullptr;
1820  }
1821 
1823  << "Cannot lookup symbol " << symbol << " : " << err
1824  << endl;
1825  }
1826 
1827  return fun;
1828 }
1829 
1830 
1831 #ifndef __APPLE__
1832 static int collectLibsCallback
1834  struct dl_phdr_info *info,
1835  size_t size,
1836  void *data
1837 )
1838 {
1840  reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
1841  ptr->append(info->dlpi_name);
1842  return 0;
1843 }
1844 #endif
1845 
1846 
1848 {
1849  DynamicList<fileName> libs;
1850  #ifdef __APPLE__
1851  for (uint32_t i=0; i < _dyld_image_count(); ++i)
1852  {
1853  libs.append(_dyld_get_image_name(i));
1854  }
1855  #else
1856  dl_iterate_phdr(collectLibsCallback, &libs);
1857  #endif
1858 
1859  if (POSIX::debug)
1860  {
1861  std::cout
1862  << "dlLoaded()"
1863  << " : determined loaded libraries :" << libs.size() << std::endl;
1864  }
1865  return libs;
1866 }
1867 
1868 
1869 // ************************************************************************* //
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:350
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:73
Foam::chMod
bool chMod(const fileName &name, const mode_t mode)
Set the file/directory mode, return true on success.
Definition: MSwindows.C:557
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:1150
redirects
static void redirects(const bool bg)
Definition: POSIX.C:94
Foam::fileName::Type
Type
Enumerations to handle directory entry types.
Definition: fileName.H:80
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:1004
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::fdClose
void fdClose(const int fd)
Close file descriptor.
Definition: MSwindows.C:1114
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:76
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::check
static void check(const int retVal, const char *what)
Definition: ptscotchDecomp.C:80
Foam::word::hasExt
bool hasExt() const
Various checks for extensions.
Definition: stringI.H:56
Foam::dlSymFind
void * dlSymFind(void *handle, const std::string &symbol, bool required=false)
Look for symbol in a dlopened library.
Definition: MSwindows.C:1331
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
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:1126
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:427
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:939
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:968
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:453
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:404
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:1028
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
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::hostName
string hostName(bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
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:1311
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
collectLibsCallback
static int collectLibsCallback(struct dl_phdr_info *info, size_t size, void *data)
Definition: POSIX.C:1833
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::ln
bool ln(const fileName &src, const fileName &dst)
Create a softlink. dst should not exist. Returns true if successful.
Definition: MSwindows.C:925
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:1364
Foam::stringOps::validate
StringType validate(const std::string &str, const UnaryPredicate &accept, const bool invert=false)
Return a copy of the input string with validated characters.
Definition: stringOpsTemplates.C:71
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:328
Foam::sleep
unsigned int sleep(const unsigned int sec)
Sleep for the specified number of seconds.
Definition: MSwindows.C:1106
Foam::dlOpen
void * dlOpen(const fileName &libName, const bool check=true)
Open a shared library and return handle to library.
Definition: MSwindows.C:1216
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