如何解决有关大文件支持的兼容性问题?

发布于 2024-07-07 22:40:26 字数 318 浏览 6 评论 0 原文

使用 off_t 作为一个函数(seek)的参数的库。 库和应用程序的编译方式不同,一个关闭大文件支持,另一个则关闭大文件支持。 这种情况会导致奇怪的运行时错误,因为两者对 off_t 的解释不同。 库如何在运行时检查应用程序的 off_t 大小? 或者是否有其他解决方案,以便至少用户得到有意义的错误?

编辑:该库(用 c 和 autoconf 编程)已经存在,并且一些第三方应用程序使用它。 该库可以使用大文件支持进行编译(默认情况下通过 AC_SYS_LARGEFILE)。 它是多平台的,不仅仅是linux。 如何检测/防止已安装的应用程序因 LFS 的更改而损坏?

A library using off_t as a parameter for one function (seek). Library and application are compiled differently, one with large file support switched off, the other with large file support. This situation results in strange runtime errors, because both interpret off_t differently. How can the library check at runtime the size of off_t for the app? Or is there another solution, so that at least the user gets a meaningful error?

EDIT: The library (programmed in c and with autoconf) already exists and some third-party application use it. The library can be compiled with large file support (by default via AC_SYS_LARGEFILE). It is multiplatform, not only linux. How can be detected/prevented that installed applications will be broken by the change in LFS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

计㈡愣 2024-07-14 22:40:26

您可以向库添加一个 API 以返回 sizeof(off_t),然后从客户端检查它。 或者,库可能要求每个应用程序提供 API 才能成功链接:

library.c:

size_t lib_get_off_t_size (void)
{
    return (sizeof(off_t));
}

client.c (init_function):

if (lib_get_off_t_size() != sizeof(off_t) {
    printf("Oh no!\n");
    exit();
}

如果库有 init 函数,那么您可以将检查放在那里,但客户端必须提供 API 来获取其 off_t 的大小,这通常不是库的工作方式。

You could add an API to the library to return the sizeof(off_t) and then check it from the client. Alternatively the library could require every app to provide the API in order to successfully link:

library.c:

size_t lib_get_off_t_size (void)
{
    return (sizeof(off_t));
}

client.c (init_function):

if (lib_get_off_t_size() != sizeof(off_t) {
    printf("Oh no!\n");
    exit();
}

If the library has an init function then you could put the check there, but then the client would have to supply the API to get the size of its off_t, which generally isn't how libraries work.

燃情 2024-07-14 22:40:26

在 Linux 上,当在打开大文件支持的情况下编译库时,off_t 定义为与 off64_t 相同。 因此,如果该库是使用大文件支持编译的库,您可以更改其接口以始终使用 off64_t 而不是 off_t (这可能需要 _LARGEFILE64_SOURCE code>) 并完全避免该问题。

您还可以检查应用程序是否正在使用大文件支持进行编译(通过查看 _FILE_OFFSET_BITS 是否未定义或 32)并拒绝编译(使用 # error)如果编译方式错误; 请参阅 /usr/include/features.h功能测试宏

On Linux, when the library is compiled with large file support switched on, off_t is defined to be the same as off64_t. So, if the library is the one compiled with large file support, you could change its interface to always use off64_t instead of off_t (this might need _LARGEFILE64_SOURCE) and completely avoid the problem.

You can also check whether the application is being compiled with large file support or not (by seeing if _FILE_OFFSET_BITS is not defined or 32) and refuse compiling (with #error) if it's being compiled the wrong way; see /usr/include/features.h and Feature Test Macros.

落叶缤纷 2024-07-14 22:40:26

如前所述,库将无法知道应用程序(作为库的客户端)是如何编译的,但反之亦然。 此外,我认为您正在谈论动态链接,因为静态链接在同一构建时间肯定不会有不同的开关。

与“Andrew Johnson”已经给出的答案类似,该库可以提供一种方法来确定它是否是使用大文件支持进行编译的。 知道此类构建时切换主要是通过 C 中的定义完成的,这可能看起来像这样:

//in library:
BOOL isLargeFileSupport (void)
{
#ifdef LARGE_FILE_SUPPORT
    return TRUE;
#else
    return FALSE;
#endif
}

然后应用程序知道如何处理该库报告的文件大小,或者在不兼容时拒绝工作:

//in application
BOOL bLibLFS = lib_isLargeFileSupport();
BOOL bAppLFS = FALSE;
#ifdef LARGE_FILE_SUPPORT
    bAppLFS = TRUE;
#endif

if (bLibLFS != bAppLFS)
    //incompatible versions, bail out
    exit(0);

As said before, the library will not be able to know how the application (being client to the library) is compiled, but the other way round has to work. Besides, I think you are talking about dynamic linking, since static linking certainly would not have different switches at same build time.

Similar to the already given answer by "Andrew Johnson", the library could provide a method for finding out whether it was compiled with large file support or not. Knowing that such build-time switches are mostly done with defines in C, this could be looking like this:

//in library:
BOOL isLargeFileSupport (void)
{
#ifdef LARGE_FILE_SUPPORT
    return TRUE;
#else
    return FALSE;
#endif
}

The application then knows how to handle file sizes reported by that lib, or can refuse to work when incompatible:

//in application
BOOL bLibLFS = lib_isLargeFileSupport();
BOOL bAppLFS = FALSE;
#ifdef LARGE_FILE_SUPPORT
    bAppLFS = TRUE;
#endif

if (bLibLFS != bAppLFS)
    //incompatible versions, bail out
    exit(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文