MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / write_file

Function write_file

subprojects/llama.cpp/common/download.cpp:81–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

79}
80
81static void write_file(const std::string & fname, const std::string & content) {
82 const std::string fname_tmp = fname + ".tmp";
83 std::ofstream file(fname_tmp);
84 if (!file) {
85 throw std::runtime_error(string_format("error: failed to open file '%s'\n", fname.c_str()));
86 }
87
88 try {
89 file << content;
90 file.close();
91
92 // Makes write atomic
93 if (rename(fname_tmp.c_str(), fname.c_str()) != 0) {
94 LOG_ERR("%s: unable to rename file: %s to %s\n", __func__, fname_tmp.c_str(), fname.c_str());
95 // If rename fails, try to delete the temporary file
96 if (remove(fname_tmp.c_str()) != 0) {
97 LOG_ERR("%s: unable to delete temporary file: %s\n", __func__, fname_tmp.c_str());
98 }
99 }
100 } catch (...) {
101 // If anything fails, try to delete the temporary file
102 if (remove(fname_tmp.c_str()) != 0) {
103 LOG_ERR("%s: unable to delete temporary file: %s\n", __func__, fname_tmp.c_str());
104 }
105
106 throw std::runtime_error(string_format("error: failed to write file '%s'\n", fname.c_str()));
107 }
108}
109
110static void write_etag(const std::string & path, const std::string & etag) {
111 const std::string etag_path = path + ".etag";

Callers 2

write_etagFunction · 0.85
common_get_hf_fileFunction · 0.85

Calls 2

string_formatFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected