[Gluster-devel] 答复: some questions about gfapi glfs_open
Gong XiaoHui
xhgong at wind.com.cn
Mon Jun 16 11:47:12 UTC 2014
I want to test multi-client read/write the remote file on GlusterFS,
My test code:
bool testGFAPI(){
std::string volName = "rvonly";
std::string serverIP = "10.100.3.110";
int port = 24007;
glfs_t* glfs = glfs_new (volName.c_str());
if (glfs == NULL) {
std::cout<<"glfs_new("<<volName<<") return NULL"<<std::endl;
return false;
}
int ret = glfs_set_volfile_server (glfs, "tcp", serverIP.c_str(), port);
if (ret){
std::cout<<"glfs_set_volfile_server return NULL"<<std::endl;
return false;
}
ret = glfs_set_logging (glfs, "/dev/stderr", 1);
ret = glfs_init (glfs);
if (ret){
std::cout<<"glfs_init return "<<ret<<std::endl;
return false;
}
glfs_setfsuid (0);
glfs_setfsgid (0);
const char* path = "test.txt";
glfs_fd_t * writefd = glfs_open (glfs, path, O_WRONLY|O_TRUNC);
if (writefd){
glfs_write(writefd, (void*)"abcd", 4, 0);
}else{
std::cout<<"open write test.txt error"<<std::endl;
}
char data[1024] = {0};
glfs_fd_t * readfd = glfs_open (glfs, path, O_RDONLY); // I need this return NULL, but it return an available fd,
if (readfd){
glfs_read(readfd,data, 4, 0); //data[] =" abcd"
}else{
std::cout<<"open read test.txt error"<<std::endl;
}
if (writefd) glfs_close (writefd);
if (readfd) glfs_close (readfd);
if (glfs) glfs_fini (glfs);
return true;
}
That is all, thank you.
-----邮件原件-----
发件人: Vijay Bellur [mailto:vbellur at redhat.com]
发送时间: 2014年6月16日 19:03
收件人: Gong XiaoHui; Niels de Vos
抄送: Gluster Devel
主题: Re: some questions about gfapi glfs_open
On 06/16/2014 08:47 AM, Gong XiaoHui wrote:
> Hi
>
> When I use the libgfapi, I need to open and write a file, called ”
> glfs_open(glfs, path, O_WRONLY|O_TRUNC);” return a glfs_fd_t,
>
> before I close it ,there is a new request to read the same file,
>
> I think the read request return NULL,
Does glfs_read() return -1? If so, this seems to be in line with expected behavior. Is there any reason for your application to send read on a fd that has been opened with O_WRONLY?
> but if I call “glfs_open(glfs,
> path, O_RDONLY);”,it return a available glfs_fd_t, so what should I do
> to resolve this problem?
>
Opening fd with O_RDWR | O_TRUNC seems to be the right thing to do if
both read and write operations are expected on the fd.
-Vijay
More information about the Gluster-devel
mailing list