Replace the file at this path with a new file that contains the given bytes, trying to avoid data-loss in the meanwhile. On UNIX-like platforms, this method does its best to ensure that by the time this method returns, either the old contents I{or} the new c
(self, content: bytes, ext: str | bytes = ".new")
| 1544 | return self.clonePath(self.dirname()) |
| 1545 | |
| 1546 | def setContent(self, content: bytes, ext: str | bytes = ".new") -> None: |
| 1547 | """ |
| 1548 | Replace the file at this path with a new file that contains the given |
| 1549 | bytes, trying to avoid data-loss in the meanwhile. |
| 1550 | |
| 1551 | On UNIX-like platforms, this method does its best to ensure that by the |
| 1552 | time this method returns, either the old contents I{or} the new |
| 1553 | contents of the file will be present at this path for subsequent |
| 1554 | readers regardless of premature device removal, program crash, or power |
| 1555 | loss, making the following assumptions: |
| 1556 | |
| 1557 | - your filesystem is journaled (i.e. your filesystem will not |
| 1558 | I{itself} lose data due to power loss) |
| 1559 | |
| 1560 | - your filesystem's C{rename()} is atomic |
| 1561 | |
| 1562 | - your filesystem will not discard new data while preserving new |
| 1563 | metadata (see U{http://mjg59.livejournal.com/108257.html} for |
| 1564 | more detail) |
| 1565 | |
| 1566 | On most versions of Windows there is no atomic C{rename()} (see |
| 1567 | U{http://bit.ly/win32-overwrite} for more information), so this method |
| 1568 | is slightly less helpful. There is a small window where the file at |
| 1569 | this path may be deleted before the new file is moved to replace it: |
| 1570 | however, the new file will be fully written and flushed beforehand so |
| 1571 | in the unlikely event that there is a crash at that point, it should be |
| 1572 | possible for the user to manually recover the new version of their |
| 1573 | data. In the future, Twisted will support atomic file moves on those |
| 1574 | versions of Windows which I{do} support them: see U{Twisted ticket |
| 1575 | 3004<http://twistedmatrix.com/trac/ticket/3004>}. |
| 1576 | |
| 1577 | This method should be safe for use by multiple concurrent processes, |
| 1578 | but note that it is not easy to predict which process's contents will |
| 1579 | ultimately end up on disk if they invoke this method at close to the |
| 1580 | same time. |
| 1581 | |
| 1582 | @param content: The desired contents of the file at this path. |
| 1583 | @type content: L{bytes} |
| 1584 | |
| 1585 | @param ext: An extension to append to the temporary filename used to |
| 1586 | store the bytes while they are being written. This can be used to |
| 1587 | make sure that temporary files can be identified by their suffix, |
| 1588 | for cleanup in case of crashes. |
| 1589 | @type ext: L{bytes} |
| 1590 | """ |
| 1591 | sib = self.temporarySibling(ext) |
| 1592 | with sib.open("w") as f: |
| 1593 | f.write(content) |
| 1594 | if _IS_WINDOWS and exists(self.path): |
| 1595 | os.unlink(self.path) |
| 1596 | os.rename(sib.path, self.asBytesMode().path) |
| 1597 | |
| 1598 | def __cmp__(self, other: object) -> int | NotImplementedType: |
| 1599 | if not isinstance(other, FilePath): |