Linux GNU Parted库以某种方式禁用分区打开
我曾经使用常规的Linux GNU Parted库以某种方式禁用分区打开,linux,ubuntu,filesystems,device,Linux,Ubuntu,Filesystems,Device,我曾经使用常规的open调用打开磁盘分区: int fd = open("/dev/sdb1", O_RDWR); 但是,当我的源代码中有许多分开的库调用时,open调用返回没有这样的文件或目录错误,即使这些分开的函数实际上没有被调用。例如,下面的代码: #include <fcntl.h> #include <unistd.h> #include <parted/parted.h> #include <cerrno> #include <
open
调用打开磁盘分区:
int fd = open("/dev/sdb1", O_RDWR);
但是,当我的源代码中有许多分开的库调用时,open
调用返回没有这样的文件或目录
错误,即使这些分开的函数实际上没有被调用。例如,下面的代码:
#include <fcntl.h>
#include <unistd.h>
#include <parted/parted.h>
#include <cerrno>
#include <cstring>
#include <iostream>
void find()
{
PedDevice* pdev = ped_device_get("/dev/sdb");
}
void Out(const std::string& S, int R)
{
std::cout << S << "\t" << R << "\t" << std::strerror(errno) << std::endl;
}
int main()
{
int fd = open("/dev/sdb1", O_RDWR);
Out("Opening ", fd);
int res = close(fd);
Out("Closing ", res);
}
如果只是注释ped\u设备\u get
调用,则程序将输出:
Opening 3 Success
Closing 0 Success
这里发生了什么事
(我使用的是Ubuntu 3.11.0-15,它在MacBook Pro上的VMware Fusion 6.0.2中运行)这确实是一个很好的答案。但自从我投票支持移民后
发生的情况是,您犯了一个新手错误:
open
调用返回没有这样的文件或目录
错误
不,没有。它返回值3,从程序输出中可以很容易地看到。这并不意味着有错误。从close()
返回的值0也不存在
仅当函数指示已设置宏时,才检查并使用该宏的值。如果open()。但既然没有,你就不能
换句话说:这些函数不会在成功时重置errno
宏的值。他们只在失败时设置值
enoint
错误来自之前某个地方失败的系统调用strace
可能会告诉你在哪里。毫无疑问,在这个额外的库中链接会导致初始化代码在main()
之前运行。我仍然很困惑:没有调用的代码(我指的是函数find()
)会如何影响errno
设置?我知道关于
errno,但我对Parted library是新手。我的问题主要是关于这个图书馆的,我怀疑这里发生了一些非常深刻的事情。在写下答案后,我浏览了libparted的来源。它确实有一些在main()
之前运行的代码,这些代码很容易找到。因此,我留下来,看看系统如何称呼它,作为读者的练习。(-:
Opening 3 Success
Closing 0 Success