Skip to main content

Python package for creating and managing sparse files.

Project description

sparse_file Current status

Python package for creating and managing sparse files

This is a pure-Python package that uses ctypes to call OS-native libraries to create sparse files and to remove physical storage assigned to those files. It was inspired by the fallocate PyPi module, but requires no compilation. I would be willing to extend this to work on OS X but personally lack the means to test the code.

Installation

$ pip install sparse-file

Usage

open_sparse(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

This function wraps the built-in open() function. An additional two methods, hole() and size_on_disk() are attached to the object returned by open. Refer to the Python documentation for open for explanations on the arguments, returned object, and potential exceptions.

As a requirement for use, the opened file must be writable, as verified by the writable() method of the file object. If not, this function raises a RuntimeError and the file is closed.

file.hole(start, length)

This method attempts to create a "hole" in the file by removing the associated storage used by a file starting at byte start for a length of length bytes. Future reads of that portion of the file will return zeroes for all bytes in the deallocated range. Future writes to that range will reallocate file storage for written data (including all zeroes).

If successful at deallocating the storage at that range, returns True. If unsuccessful, it throws an OSError exception containing the appropriate error code generated by the operating system.

file.size_on_disk()

This method retreives the size in bytes of the actual file on disk, as calculated by OS functions.

If the function runs without problems, the size of the file in bytes on disk is returned. If unsuccessful, it throws an OSError exception containing the appropriate error code generated by the operating system.

Implementation Details

Windows

Windows supports sparse files on certain versions of Windows and on specific filesystems. See FSCTL_SET_ZERO_DATA IOCTL for more details.

open_sparse() Notes:

Windows requires a file to be flagged as a sparse file in order to be able to make a file sparse. This means that the file must already exist in order for this function to work. This package attempts to set the file as sparse immediately after the file is opened. If unable, an OSError is thown.

Linux

Broadly speaking, a file in Linux only needs to be on a supported filesystem in order to become sparse. The Debian Man Pages site has a current list as of this writing.

WSL

I have tested this code on both an Ubuntu VM and Ubuntu 22.04 on WSL. It does work on WSL as long as the filesystem is supported. WSL mounts on /mnt are not supported. However, the root directory is ext4 and I was able to create spares files in my home directory.

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

sparse-file was created by Michael Wilmes. It is licensed under the terms of the MIT license.

Credits

sparse-file was created with cookiecutter and the py-pkgs-cookiecutter template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparse_file-0.0.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

sparse_file-0.0.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sparse_file-0.0.2.tar.gz.

File metadata

  • Download URL: sparse_file-0.0.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for sparse_file-0.0.2.tar.gz
Algorithm Hash digest
SHA256 48ae58cee21a4e0a935e8453a2e52dbf091f47a2240d6d656d807937d1e61e21
MD5 935aa7e483e784e884b0cf42d17f6c95
BLAKE2b-256 61a8c4bfebecc9ef426277b4d82ade4b1c428568424609cf96c249976d3e5837

See more details on using hashes here.

File details

Details for the file sparse_file-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: sparse_file-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for sparse_file-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 681d24316defcfbffdd7fefe6f5ed34cd0c29ff996f5a65afca6ebd68cb1e7be
MD5 37b2696c8839af92b3cc02afbdcb2e1f
BLAKE2b-256 36e3253d4d71c1d3633f63896a63551d52bd00330586b4193657a040fc28c973

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page