Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: libc fallocate returns with failure

Tags:

python

I am trying to fallocate a tempfile, but unfortunately the following code returns -1 (failure):

import ctypes, tempfile
from ctypes import *

t_file=tempfile.NamedTemporaryFile(delete=True, dir=".", prefix="temp_file_")

libc=cdll.LoadLibrary("libc.so.6")
libc.fallocate(t_file.fileno(), c_int(0), c_longlong(0), c_longlong(102400))

However, I can do fine with the following from command line

hdparm --fallocate 10 ./temp_file_foo

which should do the same. If it matters, this is done on an ext4 filesystem.

A separate question I have is whether libc.fallocate writes zeros to the disk, or does it mark the blocks as uninitialized?

like image 521
Ivan Avatar asked Dec 19 '25 18:12

Ivan


1 Answers

Sounds like you're on a 32-bit system, where the fallocate system call takes a (32-bit) integer by default. Some systems will also have a 64-bit counterpart in fallocate64, which takes a 64-bit offset and length. Try that instead.

Note that native 64-bit systems only have fallocate, but it takes 64-bit arguments.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!