forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbd_client.cpp
158 lines (123 loc) · 4.93 KB
/
cbd_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2020 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Project: curve
* Date: Fri May 15 15:46:59 CST 2020
* Author: wuhanqing
*/
#include "curvefs_python/cbd_client.h"
#include <vector>
#include "src/client/libcurve_file.h"
inline curve::client::UserInfo ToCurveClientUserInfo(UserInfo_t* userInfo) {
return curve::client::UserInfo(userInfo->owner, userInfo->password);
}
CBDClient::CBDClient() : client_(new curve::client::FileClient()) {}
CBDClient::~CBDClient() {}
int CBDClient::Init(const char* configPath) {
return client_->Init(configPath);
}
void CBDClient::UnInit() {
return client_->UnInit();
}
int CBDClient::Open(const char* filename, UserInfo_t* userInfo) {
return client_->Open(filename, ToCurveClientUserInfo(userInfo));
}
int CBDClient::Close(int fd) {
return client_->Close(fd);
}
int CBDClient::Create(const char* filename, UserInfo_t* userInfo, size_t size) {
return client_->Create(filename, ToCurveClientUserInfo(userInfo), size);
}
int CBDClient::Unlink(const char* filename, UserInfo_t* userInfo) {
return client_->Unlink(filename, ToCurveClientUserInfo(userInfo));
}
int CBDClient::DeleteForce(const char* filename, UserInfo_t* userInfo) {
return client_->Unlink(filename, ToCurveClientUserInfo(userInfo), true);
}
int CBDClient::Rename(UserInfo_t* userInfo, const char* oldPath,
const char* newPath) {
return client_->Rename(ToCurveClientUserInfo(userInfo), oldPath, newPath);
}
int CBDClient::Extend(const char* filename, UserInfo_t* userInfo,
uint64_t size) {
return client_->Extend(filename, ToCurveClientUserInfo(userInfo), size);
}
int CBDClient::Read(int fd, char* buf, unsigned long offset, unsigned long length) { // NOLINT
return client_->Read(fd, buf, offset, length);
}
int CBDClient::Write(int fd, const char* buf, unsigned long offset, unsigned long length) { // NOLINT
return client_->Write(fd, buf, offset, length);
}
int CBDClient::AioRead(int fd, AioContext* aioctx) {
return client_->AioRead(fd, reinterpret_cast<CurveAioContext*>(aioctx));
}
int CBDClient::AioWrite(int fd, AioContext* aioctx) {
return client_->AioWrite(fd, reinterpret_cast<CurveAioContext*>(aioctx));
}
int CBDClient::StatFile(const char* filename, UserInfo_t* userInfo,
FileInfo_t* finfo) {
return client_->StatFile(filename, ToCurveClientUserInfo(userInfo),
reinterpret_cast<FileStatInfo*>(finfo));
}
int CBDClient::ChangeOwner(const char* filename, const char* newOwner,
UserInfo_t* userInfo) {
return client_->ChangeOwner(filename, newOwner,
ToCurveClientUserInfo(userInfo));
}
DirInfos_t* CBDClient::OpenDir(const char* dirpath, UserInfo_t* userInfo) {
DirInfos_t* dirinfo = new (std::nothrow) DirInfos_t();
dirinfo->dirpath = const_cast<char*>(dirpath);
dirinfo->userinfo = userInfo;
dirinfo->fileinfo = nullptr;
return dirinfo;
}
int CBDClient::Listdir(DirInfos_t* dirinfo) {
std::vector<FileStatInfo> fileInfos;
int ret = client_->Listdir(
dirinfo->dirpath, ToCurveClientUserInfo(dirinfo->userinfo), &fileInfos);
if (ret != LIBCURVE_ERROR::OK) {
return ret;
}
dirinfo->dirsize = fileInfos.size();
dirinfo->fileinfo = new FileInfo[dirinfo->dirsize];
for (uint64_t i = 0; i < dirinfo->dirsize; ++i) {
dirinfo->fileinfo[i].id = fileInfos[i].id;
dirinfo->fileinfo[i].parentid = fileInfos[i].parentid;
dirinfo->fileinfo[i].filetype = fileInfos[i].filetype;
dirinfo->fileinfo[i].length = fileInfos[i].length;
dirinfo->fileinfo[i].ctime = fileInfos[i].ctime;
memcpy(dirinfo->fileinfo[i].owner, fileInfos[i].owner, NAME_MAX_SIZE);
memcpy(dirinfo->fileinfo[i].filename, fileInfos[i].filename,
NAME_MAX_SIZE);
}
return ret;
}
void CBDClient::CloseDir(DirInfos_t* dirinfo) {
if (dirinfo->fileinfo != nullptr) {
delete[] dirinfo->fileinfo;
dirinfo->fileinfo = nullptr;
}
delete dirinfo;
}
int CBDClient::Mkdir(const char* dirpath, UserInfo_t* userInfo) {
return client_->Mkdir(dirpath, ToCurveClientUserInfo(userInfo));
}
int CBDClient::Rmdir(const char* dirpath, UserInfo_t* userInfo) {
return client_->Rmdir(dirpath, ToCurveClientUserInfo(userInfo));
}
std::string CBDClient::GetClusterId() {
return client_->GetClusterId();
}