| 73 | namespace { |
| 74 | |
| 75 | bool HandleWriteImpl(std::vector<std::string> const& args, bool append, |
| 76 | cmExecutionStatus& status) |
| 77 | { |
| 78 | if (args.size() < 2) { |
| 79 | status.SetError(cmStrCat( |
| 80 | args[0], " must be called with at least one additional argument.")); |
| 81 | return false; |
| 82 | } |
| 83 | auto i = args.begin(); |
| 84 | |
| 85 | i++; // Get rid of subcommand |
| 86 | |
| 87 | std::string fileName = *i; |
| 88 | if (!cmsys::SystemTools::FileIsFullPath(*i)) { |
| 89 | fileName = |
| 90 | cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', *i); |
| 91 | } |
| 92 | |
| 93 | i++; |
| 94 | |
| 95 | if (!status.GetMakefile().CanIWriteThisFile(fileName)) { |
| 96 | std::string e = cmStrCat("attempted to write a file: ", fileName, |
| 97 | " into a source directory."); |
| 98 | status.SetError(e); |
| 99 | cmSystemTools::SetFatalErrorOccurred(); |
| 100 | return false; |
| 101 | } |
| 102 | std::string dir = cmSystemTools::GetFilenamePath(fileName); |
| 103 | cmSystemTools::MakeDirectory(dir); |
| 104 | |
| 105 | mode_t mode = 0; |
| 106 | bool writable = false; |
| 107 | |
| 108 | // Set permissions to writable |
| 109 | if (cmSystemTools::GetPermissions(fileName, mode)) { |
| 110 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 111 | writable = (mode & S_IWRITE) != 0; |
| 112 | mode_t newMode = mode | S_IWRITE; |
| 113 | #else |
| 114 | writable = mode & S_IWUSR; |
| 115 | mode_t newMode = mode | S_IWUSR | S_IWGRP; |
| 116 | #endif |
| 117 | if (!writable) { |
| 118 | cmSystemTools::SetPermissions(fileName, newMode); |
| 119 | } |
| 120 | } |
| 121 | // If GetPermissions fails, pretend like it is ok. File open will fail if |
| 122 | // the file is not writable |
| 123 | cmsys::ofstream file(fileName.c_str(), |
| 124 | append ? std::ios::app : std::ios::out); |
| 125 | if (!file) { |
| 126 | std::string error = |
| 127 | cmStrCat("failed to open for writing (", |
| 128 | cmSystemTools::GetLastSystemError(), "):\n ", fileName); |
| 129 | status.SetError(error); |
| 130 | return false; |
| 131 | } |
| 132 | std::string message = cmJoin(cmMakeRange(i, args.end()), std::string()); |
no test coverage detected
searching dependent graphs…