簡體   English   中英

如何在 32 位程序中使用 statfs64()?

[英]How to use statfs64() in a 32bit program?

這是我的代碼:

#include <stdio.h>
#include <sys/statfs.h>

int main(int argc, char** argv)
{
    struct statfs64 mystatfs64;
    statfs64("/", &mystatfs64);  
    return 0;
}

但我得到這個錯誤:

error: storage size of ‘mystatfs64’ isn’t known
warning: implicit declaration of function ‘statfs64’; did you mean ‘statfs’?

在手冊頁上它說:glibc statfs() 和 fstatfs() 包裝函數透明地處理內核差異。

所以我將代碼更改為:

#include <stdio.h>
#include <sys/statfs.h>

int main(int argc, char** argv)
{
    struct statfs mystatfs;
    statfs("/", &mystatfs);
    return 0;
}

它現在可以編譯,但 sizeof(((struct statfs*)0)->f_blocks) 是 4,所以我無法處理大文件系統。

我也嘗試定義 __USE_LARGEFILE64 和 __USE_FILE_OFFSET64 沒有任何成功。

__USE_*宏是由 glibc 的內部頭文件features.h定義的,而不是你。 如果您嘗試自己定義它們,它們將不起作用。

相反,您應該從不同的集合中定義宏,稱為“功能測試宏”或“功能選擇宏”,部分由 POSIX 指定。 glibc 理解的全套功能測試宏的最新和連貫的文檔位於 Linux 聯機幫助頁中: feature_test_macros(7) 請注意,這些宏中的大多數必須在包含任何系統頭文件之前定義。

編寫您嘗試編寫的代碼的最佳方法是使用_FILE_OFFSET_BITS功能測試宏將正常的off_tfsblkcnt_t等設為 64 位寬:

#define _FILE_OFFSET_BITS 64
#include <inttypes.h>
#include <stdio.h>
#include <sys/statfs.h>

int main(int argc, char** argv)
{
    struct statfs mystatfs;
    statfs("/", &mystatfs);
    printf("block count: %"PRIu64"\n", mystatfs.f_blocks);
    return 0;
}

如果您不想將此#define添加到程序的每個.c文件的頂部,則可以將-D_FILE_OFFSET_BITS=64添加到 Makefile 或等效文件中的編譯選項中(例如,將其添加到CPPFLAGS如果您使用的是 Make 的標准內置編譯規則)。

您還可以選擇使用_LARGEFILE64_SOURCE來訪問xxx64函數和類型,但 C 庫維護人員不鼓勵這樣做:請注意我在上面鏈接的聯機幫助頁中的說明

_LARGEFILE64_SOURCE
將 LFS(大文件峰會)指定的替代 API 的定義公開為單一 UNIX 規范的“過渡擴展”。 (參見⟨ https://www.opengroup.org/platform/lfs.html ⟩。)替代 API 由一組名稱以“64”為后綴的新對象(即函數和類型)組成(例如off64_t與 off_t、 lseek64()lseek()等)。 新程序不應使用此宏; 相反,應該使用_FILE_OFFSET_BITS=64

(粗體字:我的重點)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM