| 223 | } // anonymous namespace |
| 224 | |
| 225 | void ExecutableHelper::write_file(const std::string& name, const std::string& data) { |
| 226 | auto full_name = realpath(name); |
| 227 | FILE* fptr = fopen(full_name.c_str(), "wb"); |
| 228 | mgb_throw_if( |
| 229 | !fptr, SystemError, "failed to open %s: %s", full_name.c_str(), |
| 230 | strerror(errno)); |
| 231 | std::unique_ptr<FILE, int (*)(FILE*)> fptr_close{fptr, ::fclose}; |
| 232 | auto done = fwrite(data.data(), 1, data.size(), fptr); |
| 233 | mgb_throw_if( |
| 234 | done != data.size(), SystemError, |
| 235 | "failed to write file: req=%zu written=%zu: %s", data.size(), done, |
| 236 | strerror(errno)); |
| 237 | fptr_close.release(); |
| 238 | int err = fclose(fptr); |
| 239 | mgb_throw_if(err, SystemError, "failed to close file: %s", strerror(errno)); |
| 240 | } |
| 241 | |
| 242 | ExecutableHelper& ::ExecutableHelper::get() { |
| 243 | static ExecutableHelperImpl inst; |
no test coverage detected