glib-aux: handle errors from close() in nm_utils_file_set_contents()

glib's g_file_set_contents() also does that.
This commit is contained in:
Thomas Haller 2022-10-20 12:02:45 +02:00
parent 3254f33b33
commit 70417fda9e
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -352,6 +352,7 @@ nm_utils_file_set_contents(const char *filename,
int errsv;
gssize s;
int fd;
int r;
g_return_val_if_fail(filename, FALSE);
g_return_val_if_fail(contents || !length, FALSE);
@ -415,7 +416,16 @@ nm_utils_file_set_contents(const char *filename,
tmp_name);
}
nm_close(fd);
r = nm_close_with_error(fd);
if (r < 0) {
errsv = NM_ERRNO_NATIVE(-r);
unlink(tmp_name);
return _get_contents_error(error,
errsv,
out_errsv,
"failed close() after writing file %s",
tmp_name);
}
if (rename(tmp_name, filename)) {
errsv = NM_ERRNO_NATIVE(errno);