| 737 | |
| 738 | |
| 739 | bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink) |
| 740 | { |
| 741 | if (POSIX::debug) |
| 742 | { |
| 743 | Pout<< FUNCTION_NAME << " : src:" << src << " dest:" << dest << endl; |
| 744 | if ((POSIX::debug & 2) && !Pstream::master()) |
| 745 | { |
| 746 | error::printStack(Pout); |
| 747 | } |
| 748 | } |
| 749 | // Make sure source exists. |
| 750 | if (!exists(src)) |
| 751 | { |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | const fileName::Type srcType = src.type(followLink); |
| 756 | |
| 757 | fileName destFile(dest); |
| 758 | |
| 759 | // Check type of source file. |
| 760 | if (srcType == fileName::FILE) |
| 761 | { |
| 762 | // If dest is a directory, create the destination file name. |
| 763 | if (destFile.type() == fileName::DIRECTORY) |
| 764 | { |
| 765 | destFile = destFile/src.name(); |
| 766 | } |
| 767 | |
| 768 | // Make sure the destination directory exists. |
| 769 | if (!isDir(destFile.path()) && !mkDir(destFile.path())) |
| 770 | { |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | // Open and check streams. |
| 775 | std::ifstream srcStream(src.c_str()); |
| 776 | if (!srcStream) |
| 777 | { |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | std::ofstream destStream(destFile.c_str()); |
| 782 | if (!destStream) |
| 783 | { |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | // Copy character data. |
| 788 | char ch; |
| 789 | while (srcStream.get(ch)) |
| 790 | { |
| 791 | destStream.put(ch); |
| 792 | } |
| 793 | |
| 794 | // Final check. |
| 795 | if (!srcStream.eof() || !destStream) |
| 796 | { |
no test coverage detected