mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-10 03:40:29 +00:00
use _write_atomic even if file exists
and add an error message if failure
This commit is contained in:
parent
1beede466d
commit
7d5dbe2925
1 changed files with 13 additions and 6 deletions
19
third_party/python/Python/import.c
vendored
19
third_party/python/Python/import.c
vendored
|
@ -2214,17 +2214,24 @@ static PyObject *_imp_write_atomic(PyObject *module, PyObject **args,
|
|||
Py_buffer data = {NULL, NULL};
|
||||
uint32_t mode = 0666;
|
||||
int fd;
|
||||
int failure = 0;
|
||||
if (!_PyArg_ParseStack(args, nargs, "s#y*|I:_write_atomic", &path, &n, &data,
|
||||
&mode))
|
||||
return 0;
|
||||
goto end;
|
||||
mode &= 0666;
|
||||
if ((fd = open(path, O_EXCL | O_CREAT | O_WRONLY, mode)) == -1 ||
|
||||
write(fd, data.buf, data.len) == -1) {
|
||||
PyErr_Format(PyExc_OSError, "");
|
||||
if (data.obj) PyBuffer_Release(&data);
|
||||
return 0;
|
||||
if ((fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, mode)) == -1) {
|
||||
failure = 1;
|
||||
PyErr_Format(PyExc_OSError, "failed to create file: %s\n", path);
|
||||
goto end;
|
||||
}
|
||||
if (write(fd, data.buf, data.len) == -1) {
|
||||
failure = 1;
|
||||
PyErr_Format(PyExc_OSError, "failed to write to file: %s\n", path);
|
||||
goto end;
|
||||
}
|
||||
end:
|
||||
if (data.obj) PyBuffer_Release(&data);
|
||||
if (failure) return 0;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
PyDoc_STRVAR(_imp_write_atomic_doc, "atomic write to a file");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue