| 204 | } |
| 205 | |
| 206 | bool PutFile::putFile(core::ProcessSession *session, std::shared_ptr<core::FlowFile> flowFile, const std::string &tmpFile, const std::string &destFile, const std::string &destDir) { |
| 207 | struct stat dir_stat; |
| 208 | |
| 209 | if (stat(destDir.c_str(), &dir_stat) && try_mkdirs_) { |
| 210 | // Attempt to create directories in file's path |
| 211 | std::stringstream dir_path_stream; |
| 212 | |
| 213 | logger_->log_debug("Destination directory does not exist; will attempt to create: ", destDir); |
| 214 | size_t i = 0; |
| 215 | auto pos = destFile.find(utils::file::FileUtils::get_separator()); |
| 216 | |
| 217 | while (pos != std::string::npos) { |
| 218 | auto dir_path_component = destFile.substr(i, pos - i); |
| 219 | dir_path_stream << dir_path_component; |
| 220 | auto dir_path = dir_path_stream.str(); |
| 221 | |
| 222 | if (!dir_path_component.empty()) { |
| 223 | logger_->log_debug("Attempting to create directory if it does not already exist: %s", dir_path); |
| 224 | if (!utils::file::FileUtils::exists(dir_path)) { |
| 225 | utils::file::FileUtils::create_dir(dir_path, false); |
| 226 | #ifndef WIN32 |
| 227 | if (directory_permissions_.valid()) { |
| 228 | utils::file::FileUtils::set_permissions(dir_path, directory_permissions_.getValue()); |
| 229 | } |
| 230 | #endif |
| 231 | } |
| 232 | |
| 233 | dir_path_stream << utils::file::FileUtils::get_separator(); |
| 234 | } else if (pos == 0) { |
| 235 | // Support absolute paths |
| 236 | dir_path_stream << utils::file::FileUtils::get_separator(); |
| 237 | } |
| 238 | |
| 239 | i = pos + 1; |
| 240 | pos = destFile.find(utils::file::FileUtils::get_separator(), pos + 1); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool success = false; |
| 245 | |
| 246 | if (flowFile->getSize() > 0) { |
| 247 | ReadCallback cb(tmpFile, destFile); |
| 248 | session->read(flowFile, &cb); |
| 249 | logger_->log_debug("Committing %s", destFile); |
| 250 | success = cb.commit(); |
| 251 | } else { |
| 252 | std::ofstream outfile(destFile, std::ios::out | std::ios::binary); |
| 253 | if (!outfile.good()) { |
| 254 | logger_->log_error("Failed to create empty file: %s", destFile); |
| 255 | } else { |
| 256 | success = true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #ifndef WIN32 |
| 261 | if (permissions_.valid()) { |
| 262 | utils::file::FileUtils::set_permissions(destFile, permissions_.getValue()); |
| 263 | } |
nothing calls this directly
no test coverage detected