Skip to main content

Atomic file swapping

Sadly, this is not a nuclear-powered utility to swap files.

atomicswap is a Python module that implements the swapping of two files on a filesystem in a single operation that can't be broken up; either the entire operation completes correctly or none of it completes. This prevents the filesystem from being left in an inconsistent state and avoids certain race conditions.

The API is very simple; only a single swap() function is provided. The function takes two file paths for the two files to be swapped. In the event that either path is a relative path, you may also provide file descriptors for directories that the relative paths should start from; if either is missing then the path is relative to the current working directory. Paths can be provided either as Python strings or pathlib paths.

Installation

The latest stable version of atomicswap can be installed from the Python Package Index using pip in the normal manner:

pip install atomicswap

If you want to try out the latest code then you can install it from the GitHub repository:

pip install atomicswap@git+https://github.com/nickovs/atomicswap.git

Example

Swapping the files /etc/something/active and /etc/something/standby in a single operation can be performed as follows:

from atomicswap import swap
...
swap("/etc/something/active", "/etc/something/standby")

Alternatively, if using Path objects, this could be implemented as:

from pathlib import Path
from atomicswap import swap
...
base_dir = Path("/etc/something")
swap(base_dir / "active", base_dir / "standby")

Platform support

Currently atomicswap supports Linux, macOS and Windows. Note that the Windows implementation does not support specifying the base directories for relative paths using directory file descriptors.

Dependencies

On both macOS and Linux atomicswap is not dependent on any non-standard libraries or third party packages. On Windows it requires pywin32.

Implementation details

Both Linux and macOS have kernel system calls that provide the simultaneous, atomic swapping of the names of two files. On Linux the system call is named renameat2 while on macOS it is named renameatx_np, but the operation is much the same: passing a specific flag to the extended version of the rename function causes it to swap the names of two existing files rather than just changing the name of one file. On macOS the renameatx_np is exposed in the standard C library and can be called directly. Not all Linux distributions expose the renameat2 system call in their C library so the syscall wrapper function is used instead.

While there is no equivalent single function to perform the same operation on Windows, it is possible to perform an atomic swap operation using the Windows Kernel Transaction Manager and Transactional NTFS. Unfortunately Microsoft have stated that "TxF may not be available in future versions of Microsoft Windows", which potentially limits the utility of this sort of implementation.

License

atomicswap is released under the MIT license. See LICENSE.md for details.

Metadata

Release files for atomicswap 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for atomicswap 0.2.2
File Size Uploaded Provenance
atomicswap-0.2.2.tar.gz 5.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for atomicswap 0.2.2
File Interpreter ABI Platform
atomicswap-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / atomicswap-0.2.2.tar.gz

Download URL atomicswap-0.2.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3a994cbb23d5bf9bb378fa61f42624aabf64167c461e5498af5dbddf8fb2c44c
BLAKE2b-256 checksum
How to use checksums
a109ba423c4067e8218e71913d0e42236564a6773015ce263040be462138b59d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / atomicswap-0.2.2-py3-none-any.whl

Download URL atomicswap-0.2.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1426d69369c4dc5a758516430e0adcaa31f28e9c36122ab1014828c94b3ade70
BLAKE2b-256 checksum
How to use checksums
16d0c2b6825c50b805d8f92ac5d0b27c6dfcf7ddccee8f6e3a514fa9c7cacd0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for atomicswap 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page