MCPcopy Create free account
hub / github.com/apache/cloudberry / pg_file_write_internal

Function pg_file_write_internal

contrib/adminpack/adminpack.c:175–211  ·  view source on GitHub ↗

------------------------------------ * pg_file_write_internal - Workhorse for pg_file_write functions. * * This handles the actual work for pg_file_write. */

Source from the content-addressed store, hash-verified

173 * This handles the actual work for pg_file_write.
174 */
175static int64
176pg_file_write_internal(text *file, text *data, bool replace)
177{
178 FILE *f;
179 char *filename;
180 int64 count = 0;
181
182 filename = convert_and_check_filename(file);
183
184 if (!replace)
185 {
186 struct stat fst;
187
188 if (stat(filename, &fst) >= 0)
189 ereport(ERROR,
190 (errcode(ERRCODE_DUPLICATE_FILE),
191 errmsg("file \"%s\" exists", filename)));
192
193 f = AllocateFile(filename, "wb");
194 }
195 else
196 f = AllocateFile(filename, "ab");
197
198 if (!f)
199 ereport(ERROR,
200 (errcode_for_file_access(),
201 errmsg("could not open file \"%s\" for writing: %m",
202 filename)));
203
204 count = fwrite(VARDATA_ANY(data), 1, VARSIZE_ANY_EXHDR(data), f);
205 if (count != VARSIZE_ANY_EXHDR(data) || FreeFile(f))
206 ereport(ERROR,
207 (errcode_for_file_access(),
208 errmsg("could not write file \"%s\": %m", filename)));
209
210 return (count);
211}
212
213/* ------------------------------------
214 * pg_file_sync

Callers 2

pg_file_writeFunction · 0.70
pg_file_write_v1_1Function · 0.70

Calls 7

AllocateFileFunction · 0.85
FreeFileFunction · 0.85
statClass · 0.70
errcodeFunction · 0.50
errmsgFunction · 0.50
errcode_for_file_accessFunction · 0.50

Tested by

no test coverage detected