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