linux标头中的TMPFS_MAGIC和确定可用磁盘空间的错误

时间:2022-10-24 17:14:20

I am sorry if I make a "double question" but I don't want separate these two :) By any chance does anyone know what TMPFS_MAGIC having the value 0x01021994 is used for? It is defined in /usr/include/linux/magic.h and I have found it while running the following code which tries to determine the available space in a linux directory (usually a partition):

我很抱歉,如果我提出“双重问题”,但我不想将这两个分开:)任何人都知道有什么TMPFS_MAGIC用于值0x01021994?它在/usr/include/linux/magic.h中定义,我在运行以下代码时发现它,它试图确定linux目录(通常是分区)中的可用空间:

QString path = "/dev/sde1";
struct stat stst;
struct statfs stfs;

if ( ::stat(path.toLocal8Bit(), &stst) == -1 )
{
    return 0.0;
}

if ( ::statfs(path.toLocal8Bit(), &stfs) == -1 )
{
    return 0.0;
}

return m_diskFree = stfs.f_bavail * ( stst.st_blksize / 1024 );

from the gdb command prompt:

从gdb命令提示符:

(gdb) print stfs
$1 = {f_type = 16914836, f_bsize = 4096, f_blocks = 2042909, f_bfree = 2042907, f_bavail = 2042907, f_files = 2042909, f_ffree = 2042267, f_fsid = {__val = {0, 0}}, f_namelen = 255, f_frsize = 4096, f_flags = 4128, f_spare = {0, 
0, 0, 0}}

where f_type = 16914836 is the magic from above. For your information "/dev/sde1" is an external drive, with an NTFS partition on it.

其中f_type = 16914836是上面的魔力。对于您的信息,“/ dev / sde1”是一个外部驱动器,上面有一个NTFS分区。

And also the code above does not work correctly for NTFS partitions mounted on linux systems (it works for ext*fs file systems), so I am asking: are you aware of finding correctly the free space for NTFS drives mounted on Linux hosts? The linux command df seems to do the job correctly, but I did not manage to find the correct way to do it (yet...).

而且上面的代码对于安装在linux系统上的NTFS分区也不能正常工作(它适用于ext * fs文件系统),所以我问:你是否知道正确找到Linux主机上安装的NTFS驱动器的可用空间? linux命令df似乎正确地完成了工作,但我没有设法找到正确的方法(但是......)。

Thanks, f.

1 个解决方案

#1


0  

So, after some debugging and sleepless nights, the solution was pretty straightforward. Find the source for df.c and read it. Long live free and open source code.

所以,经过一些调试和不眠之夜,解决方案非常简单。找到df.c的源代码并阅读它。万岁免费和开源代码。

And the explanation: when you use stat and statfs to find the free space on a hard disk, you do not try to find the free space of the device itself, because that is magic... but, you try to find the free space of the directory where the device is mounted. So, instead of

并解释:当你使用stat和statfs来查找硬盘上的可用空间时,你不会试图找到设备本身的可用空间,因为那是神奇的......但是,你试图找到*空间安装设备的目录。所以,而不是

QString path = "/dev/sde1";

there should be

应该有

QString path = "/mnt/DISK";

where "mnt/DISK" is the directory where the disk "/dev/sde1" is mounted. I have used libext2fs to find the mount point of the disk, but I am sure there are other better solutions to find it out, such as parsing /etc/mtab

其中“mnt / DISK”是安装磁盘“/ dev / sde1”的目录。我已经使用libext2fs来查找磁盘的挂载点,但我确信还有其他更好的解决方案可以找到它,例如解析/ etc / mtab

#1


0  

So, after some debugging and sleepless nights, the solution was pretty straightforward. Find the source for df.c and read it. Long live free and open source code.

所以,经过一些调试和不眠之夜,解决方案非常简单。找到df.c的源代码并阅读它。万岁免费和开源代码。

And the explanation: when you use stat and statfs to find the free space on a hard disk, you do not try to find the free space of the device itself, because that is magic... but, you try to find the free space of the directory where the device is mounted. So, instead of

并解释:当你使用stat和statfs来查找硬盘上的可用空间时,你不会试图找到设备本身的可用空间,因为那是神奇的......但是,你试图找到*空间安装设备的目录。所以,而不是

QString path = "/dev/sde1";

there should be

应该有

QString path = "/mnt/DISK";

where "mnt/DISK" is the directory where the disk "/dev/sde1" is mounted. I have used libext2fs to find the mount point of the disk, but I am sure there are other better solutions to find it out, such as parsing /etc/mtab

其中“mnt / DISK”是安装磁盘“/ dev / sde1”的目录。我已经使用libext2fs来查找磁盘的挂载点,但我确信还有其他更好的解决方案可以找到它,例如解析/ etc / mtab