POSIX.C 39.5 KB
Newer Older
Henry's avatar
Henry committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2017 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
Henry's avatar
Henry committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Description
    POSIX versions of the functions declared in OSspecific.H

\*---------------------------------------------------------------------------*/

#ifdef solarisGcc
30
    #define _SYS_VNODE_H
Henry's avatar
Henry committed
31
32
33
34
35
36
37
38
39
40
#endif

#include "OSspecific.H"
#include "POSIX.H"
#include "foamVersion.H"
#include "fileName.H"
#include "fileStat.H"
#include "timer.H"
#include "IFstream.H"
#include "DynamicList.H"
41
42
#include "CStringList.H"
#include "SubList.H"
43
44
#include "IOstreams.H"
#include "Pstream.H"
Henry's avatar
Henry committed
45
46
47
48
49
50
51
52
53
54
55

#include <fstream>
#include <cstdlib>
#include <cctype>

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
#include <errno.h>
#include <sys/types.h>
56
#include <sys/wait.h>
Henry's avatar
Henry committed
57
58
59
60
61
62
63
64
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <dlfcn.h>
#include <link.h>

#include <netinet/in.h>
#ifdef USE_RANDOM
65
66
67
68
69
    #include <climits>
    #if INT_MAX    != 2147483647
        #error "INT_MAX    != 2147483647"
        #error "The random number generator may not work!"
    #endif
Henry's avatar
Henry committed
70
71
72
73
74
75
76
77
78
79
#endif

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineTypeNameAndDebug(POSIX, 0);
}


80
81
82
83
84
85
86
87
88
89
90
91
92
// * * * * * * * * * * * * * * Static Functions  * * * * * * * * * * * * * * //

//
//! \cond fileScope
//
// Return true if filename appears to be a backup file
//
static inline bool isBackupName(const Foam::fileName& name)
{
    if (name.empty())
    {
        return false;
    }
93
    else if (name.back() == '~')
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    {
        return true;
    }

    // Now check the extension
    const Foam::word ext = name.ext();
    if (ext.empty())
    {
        return false;
    }

    return
    (
        ext == "bak" || ext == "BAK"
     || ext == "old" || ext == "save"
    );
}

//! \endcond


Henry's avatar
Henry committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

pid_t Foam::pid()
{
    return ::getpid();
}


pid_t Foam::ppid()
{
    return ::getppid();
}


pid_t Foam::pgid()
{
    return ::getpgrp();
}


135
bool Foam::env(const std::string& envName)
Henry's avatar
Henry committed
136
{
137
138
    // An empty envName => always false
    return !envName.empty() && ::getenv(envName.c_str()) != nullptr;
Henry's avatar
Henry committed
139
140
141
}


142
Foam::string Foam::getEnv(const std::string& envName)
Henry's avatar
Henry committed
143
{
144
145
    // Ignore an empty envName => always ""
    char* env = envName.empty() ? nullptr : ::getenv(envName.c_str());
Henry's avatar
Henry committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    if (env)
    {
        return string(env);
    }
    else
    {
        // Return null-constructed string rather than string::null
        // to avoid cyclic dependencies in the construction of globals
        return string();
    }
}


bool Foam::setEnv
(
    const word& envName,
    const std::string& value,
    const bool overwrite
)
{
167
168
169
170
171
172
    // Ignore an empty envName => always false
    return
    (
        !envName.empty()
     && ::setenv(envName.c_str(), value.c_str(), overwrite) == 0
    );
Henry's avatar
Henry committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
}


Foam::string Foam::hostName(bool full)
{
    char buf[128];
    ::gethostname(buf, sizeof(buf));

    // implementation as per hostname from net-tools
    if (full)
    {
        struct hostent *hp = ::gethostbyname(buf);
        if (hp)
        {
            return hp->h_name;
        }
    }

    return buf;
}


Foam::string Foam::domainName()
{
    char buf[128];
    ::gethostname(buf, sizeof(buf));

    // implementation as per hostname from net-tools
    struct hostent *hp = ::gethostbyname(buf);
    if (hp)
    {
        char *p = ::strchr(hp->h_name, '.');
        if (p)
        {
            ++p;
            return p;
        }
    }

    return string::null;
}


Foam::string Foam::userName()
{
    struct passwd* pw = ::getpwuid(::getuid());
219
    if (pw != nullptr)
Henry's avatar
Henry committed
220
221
222
223
224
    {
        return pw->pw_name;
    }
    else
    {
225
        return string();
Henry's avatar
Henry committed
226
227
228
229
230
231
232
233
234
235
236
237
238
    }
}


bool Foam::isAdministrator()
{
    return (::geteuid() == 0);
}


Foam::fileName Foam::home()
{
    char* env = ::getenv("HOME");
239
    if (env)
Henry's avatar
Henry committed
240
241
242
    {
        return fileName(env);
    }
243
244
245
246
247
248

    struct passwd* pw = ::getpwuid(::getuid());
    if (pw)
    {
        return pw->pw_dir;
    }
Henry's avatar
Henry committed
249
250
    else
    {
251
        return fileName();
Henry's avatar
Henry committed
252
253
254
255
    }
}


256
Foam::fileName Foam::home(const std::string& userName)
Henry's avatar
Henry committed
257
{
258
259
    // An empty userName => same as home()
    if (userName.empty())
Henry's avatar
Henry committed
260
    {
261
        return Foam::home();
Henry's avatar
Henry committed
262
263
    }

264
265
    struct passwd* pw = ::getpwnam(userName.c_str());
    if (pw)
Henry's avatar
Henry committed
266
267
268
269
270
    {
        return pw->pw_dir;
    }
    else
    {
271
        return fileName();
Henry's avatar
Henry committed
272
273
274
275
276
277
    }
}


Foam::fileName Foam::cwd()
{
278
279
280
281
    label pathLengthLimit = POSIX::pathLengthChunk;
    List<char> path(pathLengthLimit);

    // Resize path if getcwd fails with an ERANGE error
282
    while (pathLengthLimit == path.size())
Henry's avatar
Henry committed
283
    {
284
285
286
287
        if (::getcwd(path.data(), path.size()))
        {
            return path.data();
        }
288
        else if (errno == ERANGE)
289
        {
290
            // Increment path length up to the pathLengthMax limit
291
292
293
294
295
296
297
298
299
300
301
            if
            (
                (pathLengthLimit += POSIX::pathLengthChunk)
             >= POSIX::pathLengthMax
            )
            {
                FatalErrorInFunction
                    << "Attempt to increase path length beyond limit of "
                    << POSIX::pathLengthMax
                    << exit(FatalError);
            }
Henry's avatar
Henry committed
302

303
304
305
306
307
308
            path.setSize(pathLengthLimit);
        }
        else
        {
            break;
        }
Henry's avatar
Henry committed
309
    }
310
311
312
313
314
315

    FatalErrorInFunction
        << "Couldn't get the current working directory"
        << exit(FatalError);

    return fileName::null;
Henry's avatar
Henry committed
316
317
318
319
320
}


bool Foam::chDir(const fileName& dir)
{
321
322
    // Ignore an empty dir name => always false
    return !dir.empty() && ::chdir(dir.c_str()) == 0;
Henry's avatar
Henry committed
323
324
325
326
327
}


bool Foam::mkDir(const fileName& pathName, mode_t mode)
{
328
329
330
331
332
333
334
335
336
337
338
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : pathName:" << pathName << " mode:" << mode
            << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

    // empty names are meaningless
Henry's avatar
Henry committed
339
340
341
342
343
    if (pathName.empty())
    {
        return false;
    }

344
    // Construct path directory if does not exist
Henry's avatar
Henry committed
345
346
347
348
349
350
351
352
353
354
355
    if (::mkdir(pathName.c_str(), mode) == 0)
    {
        // Directory made OK so return true
        return true;
    }
    else
    {
        switch (errno)
        {
            case EPERM:
            {
356
                FatalErrorInFunction
Henry's avatar
Henry committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
                    << "The filesystem containing " << pathName
                    << " does not support the creation of directories."
                    << exit(FatalError);

                return false;
            }

            case EEXIST:
            {
                // Directory already exists so simply return true
                return true;
            }

            case EFAULT:
            {
372
                FatalErrorInFunction
Henry's avatar
Henry committed
373
374
375
376
377
378
379
380
381
                    << "" << pathName
                    << " points outside your accessible address space."
                    << exit(FatalError);

                return false;
            }

            case EACCES:
            {
382
                FatalErrorInFunction
Henry's avatar
Henry committed
383
384
385
386
387
388
389
390
391
392
393
                    << "The parent directory does not allow write "
                       "permission to the process,"<< nl
                    << "or one of the directories in " << pathName
                    << " did not allow search (execute) permission."
                    << exit(FatalError);

                return false;
            }

            case ENAMETOOLONG:
            {
394
                FatalErrorInFunction
Henry's avatar
Henry committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
                    << "" << pathName << " is too long."
                    << exit(FatalError);

                return false;
            }

            case ENOENT:
            {
                // Part of the path does not exist so try to create it
                if (pathName.path().size() && mkDir(pathName.path(), mode))
                {
                    return mkDir(pathName, mode);
                }
                else
                {
410
                    FatalErrorInFunction
Henry's avatar
Henry committed
411
412
413
414
415
416
417
418
419
                        << "Couldn't create directory " << pathName
                        << exit(FatalError);

                    return false;
                }
            }

            case ENOTDIR:
            {
420
                FatalErrorInFunction
Henry's avatar
Henry committed
421
422
423
424
425
426
427
428
429
                    << "A component used as a directory in " << pathName
                    << " is not, in fact, a directory."
                    << exit(FatalError);

                return false;
            }

            case ENOMEM:
            {
430
                FatalErrorInFunction
Henry's avatar
Henry committed
431
432
433
434
435
436
437
438
439
                    << "Insufficient kernel memory was available to make "
                       "directory " << pathName << '.'
                    << exit(FatalError);

                return false;
            }

            case EROFS:
            {
440
                FatalErrorInFunction
Henry's avatar
Henry committed
441
442
443
444
445
446
447
448
449
                    << "" << pathName
                    << " refers to a file on a read-only filesystem."
                    << exit(FatalError);

                return false;
            }

            case ELOOP:
            {
450
                FatalErrorInFunction
Henry's avatar
Henry committed
451
452
453
454
455
456
457
458
459
                    << "Too many symbolic links were encountered in resolving "
                    << pathName << '.'
                    << exit(FatalError);

                return false;
            }

            case ENOSPC:
            {
460
                FatalErrorInFunction
Henry's avatar
Henry committed
461
462
463
464
465
466
467
468
469
470
                    << "The device containing " << pathName
                    << " has no room for the new directory or "
                    << "the user's disk quota is exhausted."
                    << exit(FatalError);

                return false;
            }

            default:
            {
471
                FatalErrorInFunction
Henry's avatar
Henry committed
472
473
474
475
476
477
478
479
480
481
482
483
                    << "Couldn't create directory " << pathName
                    << exit(FatalError);

                return false;
            }
        }
    }
}


bool Foam::chMod(const fileName& name, const mode_t m)
{
484
485
486
487
488
489
490
491
492
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

493
494
    // Ignore an empty name => always false
    return !name.empty() && ::chmod(name.c_str(), m) == 0;
Henry's avatar
Henry committed
495
496
497
}


498
mode_t Foam::mode(const fileName& name, const bool followLink)
Henry's avatar
Henry committed
499
{
500
501
502
503
504
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
    }

505
506
    // Ignore an empty name => always 0
    if (!name.empty())
Henry's avatar
Henry committed
507
    {
508
509
510
511
512
        fileStat fileStatus(name, followLink);
        if (fileStatus.isValid())
        {
            return fileStatus.status().st_mode;
        }
Henry's avatar
Henry committed
513
    }
514
515

    return 0;
Henry's avatar
Henry committed
516
517
518
}


519
Foam::fileName::Type Foam::type(const fileName& name, const bool followLink)
Henry's avatar
Henry committed
520
{
521
522
523
524
525
526
    // Ignore an empty name => always UNDEFINED
    if (name.empty())
    {
        return fileName::UNDEFINED;
    }

527
528
529
530
531
532
533
534
535
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

536
    mode_t m = mode(name, followLink);
Henry's avatar
Henry committed
537
538
539
540
541

    if (S_ISREG(m))
    {
        return fileName::FILE;
    }
542
543
544
545
    else if (S_ISLNK(m))
    {
        return fileName::LINK;
    }
Henry's avatar
Henry committed
546
547
548
549
    else if (S_ISDIR(m))
    {
        return fileName::DIRECTORY;
    }
550
551

    return fileName::UNDEFINED;
Henry's avatar
Henry committed
552
553
554
}


555
556
557
558
559
560
bool Foam::exists
(
    const fileName& name,
    const bool checkGzip,
    const bool followLink
)
Henry's avatar
Henry committed
561
{
562
563
564
565
566
567
568
569
570
571
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip
            << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

572
573
574
575
576
577
    // Ignore an empty name => always false
    return
    (
        !name.empty()
     && (mode(name, followLink) || isFile(name, checkGzip, followLink))
    );
Henry's avatar
Henry committed
578
579
580
}


581
bool Foam::isDir(const fileName& name, const bool followLink)
Henry's avatar
Henry committed
582
{
583
584
585
586
587
588
589
590
591
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

592
593
    // Ignore an empty name => always false
    return !name.empty() && S_ISDIR(mode(name, followLink));
Henry's avatar
Henry committed
594
595
596
}


597
598
599
600
601
602
bool Foam::isFile
(
    const fileName& name,
    const bool checkGzip,
    const bool followLink
)
Henry's avatar
Henry committed
603
{
604
605
606
607
608
609
610
611
612
613
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << " checkGzip:" << checkGzip
            << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

614
    // Ignore an empty name => always false
615
    return
616
617
618
619
620
621
622
    (
        !name.empty()
     && (
            S_ISREG(mode(name, followLink))
         || (checkGzip && S_ISREG(mode(name + ".gz", followLink)))
        )
    );
Henry's avatar
Henry committed
623
624
625
}


626
off_t Foam::fileSize(const fileName& name, const bool followLink)
Henry's avatar
Henry committed
627
{
628
629
630
631
632
633
634
635
636
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

637
638
    // Ignore an empty name
    if (!name.empty())
Henry's avatar
Henry committed
639
    {
640
641
642
643
644
        fileStat fileStatus(name, followLink);
        if (fileStatus.isValid())
        {
            return fileStatus.status().st_size;
        }
Henry's avatar
Henry committed
645
    }
646
647

    return -1;
Henry's avatar
Henry committed
648
649
650
}


651
time_t Foam::lastModified(const fileName& name, const bool followLink)
Henry's avatar
Henry committed
652
{
653
654
655
656
657
658
659
660
661
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

662
663
    // Ignore an empty name
    if (!name.empty())
Henry's avatar
Henry committed
664
    {
665
666
667
668
669
        fileStat fileStatus(name, followLink);
        if (fileStatus.isValid())
        {
            return fileStatus.status().st_mtime;
        }
Henry's avatar
Henry committed
670
    }
671
672

    return 0;
Henry's avatar
Henry committed
673
674
675
}


676
double Foam::highResLastModified(const fileName& name, const bool followLink)
677
{
678
679
680
681
682
683
684
685
686
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : name:" << name << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

687
688
    // Ignore an empty name
    if (!name.empty())
689
    {
690
691
692
693
694
695
696
        fileStat fileStatus(name);
        if (fileStatus.isValid())
        {
            return
                fileStatus.status().st_mtime
              + 1e-9*fileStatus.status().st_atim.tv_nsec;
        }
697
    }
698
699

    return 0;
700
701
702
}


Henry's avatar
Henry committed
703
704
705
706
Foam::fileNameList Foam::readDir
(
    const fileName& directory,
    const fileName::Type type,
707
708
    const bool filtergz,
    const bool followLink
Henry's avatar
Henry committed
709
710
711
712
713
714
)
{
    // Initial filename list size
    // also used as increment if initial size found to be insufficient
    static const int maxNnames = 100;

715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    // Basic sanity: cannot strip '.gz' from directory names
    const bool stripgz = filtergz && (type != fileName::DIRECTORY);
    const word extgz("gz");

    // Open directory and set the structure pointer
    // Do not attempt to open an empty directory name
    DIR *source;
    if
    (
        directory.empty()
     || (source = ::opendir(directory.c_str())) == nullptr
    )
    {
        if (POSIX::debug)
        {
            InfoInFunction
                << "cannot open directory " << directory << endl;
        }

        return fileNameList();
    }

Henry's avatar
Henry committed
737
738
    if (POSIX::debug)
    {
739
740
741
742
743
744
        //InfoInFunction
        Pout<< FUNCTION_NAME << " : reading directory " << directory << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
Henry's avatar
Henry committed
745
746
747
    }

    label nEntries = 0;
748
    fileNameList dirEntries(maxNnames);
Henry's avatar
Henry committed
749

750
751
    // Read and parse all the entries in the directory
    for (struct dirent *list; (list = ::readdir(source)) != nullptr; /*nil*/)
Henry's avatar
Henry committed
752
    {
753
        const fileName name(list->d_name);
Henry's avatar
Henry committed
754

755
756
757
        // Ignore files/directories beginning with "."
        // These are the ".", ".." directories and any hidden files/dirs
        if (name.empty() || name[0] == '.')
Henry's avatar
Henry committed
758
        {
759
            continue;
Henry's avatar
Henry committed
760
761
        }

762
763
764
765
766
767
768
        if
        (
            (type == fileName::DIRECTORY)
         || (type == fileName::FILE && !isBackupName(name))
        )
        {
            if ((directory/name).type(followLink) == type)
Henry's avatar
Henry committed
769
            {
770
771
772
773
774
775
                if (nEntries >= dirEntries.size())
                {
                    dirEntries.setSize(dirEntries.size() + maxNnames);
                }

                if (stripgz && name.hasExt(extgz))
Henry's avatar
Henry committed
776
                {
777
778
779
780
781
                    dirEntries[nEntries++] = name.lessExt();
                }
                else
                {
                    dirEntries[nEntries++] = name;
Henry's avatar
Henry committed
782
783
784
785
786
                }
            }
        }
    }

787
788
789
790
    // Reset the length of the entries list
    dirEntries.setSize(nEntries);
    ::closedir(source);

Henry's avatar
Henry committed
791
792
793
794
    return dirEntries;
}


795
bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink)
Henry's avatar
Henry committed
796
{
797
798
799
800
801
802
803
804
805
    if (POSIX::debug)
    {
        Pout<< FUNCTION_NAME << " : src:" << src << " dest:" << dest << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
    }

806
    // Make sure source exists - this also handles an empty source name
Henry's avatar
Henry committed
807
808
809
810
811
    if (!exists(src))
    {
        return false;
    }

812
813
    const fileName::Type srcType = src.type(followLink);

Henry's avatar
Henry committed
814
815
816
    fileName destFile(dest);

    // Check type of source file.
817
    if (srcType == fileName::FILE)
Henry's avatar
Henry committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
    {
        // If dest is a directory, create the destination file name.
        if (destFile.type() == fileName::DIRECTORY)
        {
            destFile = destFile/src.name();
        }

        // Make sure the destination directory exists.
        if (!isDir(destFile.path()) && !mkDir(destFile.path()))
        {
            return false;
        }

        // Open and check streams.
832
        std::ifstream srcStream(src);
Henry's avatar
Henry committed
833
834
835
836
837
        if (!srcStream)
        {
            return false;
        }

838
        std::ofstream destStream(destFile);
Henry's avatar
Henry committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
        if (!destStream)
        {
            return false;
        }

        // Copy character data.
        char ch;
        while (srcStream.get(ch))
        {
            destStream.put(ch);
        }

        // Final check.
        if (!srcStream.eof() || !destStream)
        {
            return false;
        }
    }
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
    else if (srcType == fileName::LINK)
    {
        // If dest is a directory, create the destination file name.
        if (destFile.type() == fileName::DIRECTORY)
        {
            destFile = destFile/src.name();
        }

        // Make sure the destination directory exists.
        if (!isDir(destFile.path()) && !mkDir(destFile.path()))
        {
            return false;
        }

        ln(src, destFile);
    }
    else if (srcType == fileName::DIRECTORY)
Henry's avatar
Henry committed
874
875
876
877
    {
        // If dest is a directory, create the destination file name.
        if (destFile.type() == fileName::DIRECTORY)
        {
878
            destFile = destFile/src.components().last();
Henry's avatar
Henry committed
879
880
881
882
883
884
885
886
        }

        // Make sure the destination directory exists.
        if (!isDir(destFile) && !mkDir(destFile))
        {
            return false;
        }

887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
        char* realSrcPath = realpath(src.c_str(), nullptr);
        char* realDestPath = realpath(destFile.c_str(), nullptr);
        const bool samePath = strcmp(realSrcPath, realDestPath) == 0;

        if (POSIX::debug && samePath)
        {
            InfoInFunction
                << "Attempt to copy " << realSrcPath << " to itself" << endl;
        }

        if (realSrcPath)
        {
            free(realSrcPath);
        }

        if (realDestPath)
        {
            free(realDestPath);
        }

        // Do not copy over self when src is actually a link to dest
        if (samePath)
        {
            return false;
        }

Henry's avatar
Henry committed
913
        // Copy files
914
        fileNameList contents = readDir(src, fileName::FILE, false, followLink);
Henry's avatar
Henry committed
915
916
917
918
        forAll(contents, i)
        {
            if (POSIX::debug)
            {
919
920
                InfoInFunction
                    << "Copying : " << src/contents[i]
Henry's avatar
Henry committed
921
922
923
924
                    << " to " << destFile/contents[i] << endl;
            }

            // File to file.
925
            cp(src/contents[i], destFile/contents[i], followLink);
Henry's avatar
Henry committed
926
927
928
        }

        // Copy sub directories.
929
930
931
932
933
934
935
        fileNameList subdirs = readDir
        (
            src,
            fileName::DIRECTORY,
            false,
            followLink
        );
936

Henry's avatar
Henry committed
937
938
939
940
        forAll(subdirs, i)
        {
            if (POSIX::debug)
            {
941
942
                InfoInFunction
                    << "Copying : " << src/subdirs[i]
Henry's avatar
Henry committed
943
944
945
946
                    << " to " << destFile << endl;
            }

            // Dir to Dir.
947
            cp(src/subdirs[i], destFile, followLink);
Henry's avatar
Henry committed
948
949
        }
    }
950
951
952
953
    else
    {
        return false;
    }
Henry's avatar
Henry committed
954
955
956
957
958
959
960
961
962

    return true;
}


bool Foam::ln(const fileName& src, const fileName& dst)
{
    if (POSIX::debug)
    {
963
964
965
966
967
968
969
        //InfoInFunction
        Pout<< FUNCTION_NAME
            << " : Create softlink from : " << src << " to " << dst << endl;
        if ((POSIX::debug & 2) && !Pstream::master())
        {
            error::printStack(Pout);
        }
Henry's avatar
Henry committed
970
971
    }

972
973
974
975
976
977
978
979
980
981
982
983
984
985
    if (src.empty())
    {
        WarningInFunction
            << "source name is empty: not linking." << endl;
        return false;
    }

    if (dst.empty())
    {
        WarningInFunction
            << "destination name is empty: not linking." << endl;
        return false;
    }

Henry's avatar
Henry committed
986
987
    if (exists(dst))
    {
988
        WarningInFunction
Henry's avatar
Henry committed
989
990
991
992
993
994
995
            << "destination " << dst << " already exists. Not linking."
            << endl;
        return false;
    }

    if (src.isAbsolute() && !exists(src))
    {
996
        WarningInFunction
Henry's avatar
Henry committed
997
998
999
1000
            << "source " << src << " does not exist." << endl;
        return false;
    }

For faster browsing, not all history is shown. View entire blame