------------------------------------ * pg_file_write_internal - Workhorse for pg_file_write functions. * * This handles the actual work for pg_file_write. */
| 173 | * This handles the actual work for pg_file_write. |
| 174 | */ |
| 175 | static int64 |
| 176 | pg_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 |
no test coverage detected