Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small bugfix #859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/sdk/bfs_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DECLARE_string(nameserver_nodes);
extern "C"{

struct bfs_fs_t {
baidu::bfs::FS* rep;
baidu::bfs::FS* pImpl;
};

bfs_fs_t* bfs_open_file_system(const char* flag_file) {
Expand All @@ -44,7 +44,7 @@ bfs_fs_t* bfs_open_file_system(const char* flag_file) {
bfs_fs_t* fs = new bfs_fs_t;
std::string ns_address = FLAGS_nameserver_nodes;
bool ret = baidu::bfs::FS::OpenFileSystem(ns_address.c_str(),
&(fs->rep), baidu::bfs::FSOptions());
&(fs->pImpl), baidu::bfs::FSOptions());
if (!ret) {
delete fs;
return NULL;
Expand All @@ -53,14 +53,14 @@ bfs_fs_t* bfs_open_file_system(const char* flag_file) {
}

int bfs_create_directory(bfs_fs_t* fs, const char* path) {
return fs->rep->CreateDirectory(path);
return fs->pImpl->CreateDirectory(path);
}

int bfs_list_directory(bfs_fs_t* fs, const char* path) {
char file_types[10] ={'-', 'd', 'l'};
baidu::bfs::BfsFileInfo* files = NULL;
int num;
int32_t ret = fs->rep->ListDirectory(path, &files, &num);
int32_t ret = fs->pImpl->ListDirectory(path, &files, &num);
if (ret != 0) {
return ret;
}
Expand Down Expand Up @@ -95,33 +95,32 @@ int bfs_list_directory(bfs_fs_t* fs, const char* path) {
timestr, prefix.c_str(), files[i].name);
}
delete[] files;
return 0;
return ret;
}

int bfs_delete_file(bfs_fs_t* fs, const char* path) {
return fs->rep->DeleteFile(path);
return fs->pImpl->DeleteFile(path);
}

int bfs_rename(bfs_fs_t* fs, const char* oldpath, const char* newpath) {
return fs->rep->Rename(oldpath, newpath);
return fs->pImpl->Rename(oldpath, newpath);
}

int bfs_touchz(bfs_fs_t* fs, const char* path) {
baidu::bfs::File* file;
int32_t ret = fs->rep->OpenFile(path, O_WRONLY, 0644,
int32_t ret = fs->pImpl->OpenFile(path, O_WRONLY, 0644,
&file, baidu::bfs::WriteOptions());
delete file;
return ret;
}

int bfs_symlink(bfs_fs_t* fs, const char* src, const char* dst) {
return fs->rep->Symlink(src, dst);
return fs->pImpl->Symlink(src, dst);
}

int bfs_cat(bfs_fs_t* fs, const char* path) {
int64_t bytes = 0;
baidu::bfs::File* file;
int32_t ret = fs->rep->OpenFile(path, O_RDONLY, &file,
int32_t ret = fs->pImpl->OpenFile(path, O_RDONLY, &file,
baidu::bfs::ReadOptions());
if (ret != 0) {
return ret;
Expand All @@ -131,13 +130,16 @@ int bfs_cat(bfs_fs_t* fs, const char* path) {
while (1) {
len = file->Read(buf, sizeof(buf));
if (len <= 0) {
if (len < 0) {
fprintf(stderr, "Read from %s fail.\n", path);
ret = 1;
}
break;
}
bytes += len;
write(1, buf, len);
}
delete file;
return len;
return ret;
}

int bfs_get(bfs_fs_t* fs, const char* bfs, const char* local) {
Expand All @@ -155,7 +157,7 @@ int bfs_get(bfs_fs_t* fs, const char* bfs, const char* local) {
}
baidu::common::timer::AutoTimer at(0, "BfsGet", bfs);
baidu::bfs::File* file;
if (fs->rep->OpenFile(bfs, O_RDONLY, &file,
if (fs->pImpl->OpenFile(bfs, O_RDONLY, &file,
baidu::bfs::ReadOptions()) != 0) {
return 2;
}
Expand Down Expand Up @@ -183,7 +185,7 @@ int bfs_get(bfs_fs_t* fs, const char* bfs, const char* local) {
printf("Read %ld bytes from %s\n", bytes, source.c_str());
delete file;
fclose(fp);
return len;
return 0;
}

int bfs_put(bfs_fs_t* fs, const char* local, const char* bfs) {
Expand Down Expand Up @@ -214,7 +216,7 @@ int bfs_put(bfs_fs_t* fs, const char* local, const char* bfs) {
return 3;
}
baidu::bfs::File* file;
if (fs->rep->OpenFile(target.c_str(), O_WRONLY | O_TRUNC, st.st_mode,
if (fs->pImpl->OpenFile(target.c_str(), O_WRONLY | O_TRUNC, st.st_mode,
&file, baidu::bfs::WriteOptions()) != 0) {
fclose(fp);
return 4;
Expand Down Expand Up @@ -242,7 +244,7 @@ int bfs_put(bfs_fs_t* fs, const char* local, const char* bfs) {

int64_t bfs_du_v2(bfs_fs_t* fs, const char* path) {
int64_t du_size = 0;
if (fs->rep->DiskUsage(path, &du_size) != 0) {
if (fs->pImpl->DiskUsage(path, &du_size) != 0) {
fprintf(stderr, "Compute Disk Usage fail: %s\n", path);
return -1;
}
Expand All @@ -267,7 +269,7 @@ int bfs_du(bfs_fs_t* fs, const char* path) {
std::string prefix = str_path.substr(ppath.size());
int num = 0;
baidu::bfs::BfsFileInfo* files = NULL;
int32_t ret = fs->rep->ListDirectory(ppath.c_str(), &files, &num);
int32_t ret = fs->pImpl->ListDirectory(ppath.c_str(), &files, &num);
if (ret != 0) {
return ret;
}
Expand All @@ -286,15 +288,15 @@ int bfs_du(bfs_fs_t* fs, const char* path) {
}

int bfs_rm_dir(bfs_fs_t* fs, const char* path, bool recursive) {
return fs->rep->DeleteDirectory(path, recursive);
return fs->pImpl->DeleteDirectory(path, recursive);
}

int bfs_change_replica_num(bfs_fs_t* fs, const char* path,
const char* replica_num) {
const char* replica_num) {
if (!isdigit(*replica_num)) {
return -1;
}
return fs->rep->ChangeReplicaNum(path, strtol(replica_num, NULL, 10));
return fs->pImpl->ChangeReplicaNum(path, strtol(replica_num, NULL, 10));
}

int bfs_chmod(bfs_fs_t* fs, const char* str_mode, const char* path) {
Expand All @@ -303,23 +305,22 @@ int bfs_chmod(bfs_fs_t* fs, const char* str_mode, const char* path) {
if (end_pos != NULL) {
return -1;
}
return fs->rep->Chmod(mode, path);
return fs->pImpl->Chmod(mode, path);
}

int bfs_location(bfs_fs_t* fs, const char* path) {
std::map<int64_t, std::vector<std::string> > locations;
int32_t ret = fs->rep->GetFileLocation(path, &locations);
int32_t ret = fs->pImpl->GetFileLocation(path, &locations);
if (ret != 0) {
return ret;
}
for (std::map<int64_t, std::vector<std::string> >::iterator it =
locations.begin(); it != locations.end(); ++it) {
for (auto it = locations.begin(); it != locations.end(); ++it) {
printf("block_id #%ld:\n", it->first);
for (size_t i = 0; i < (it->second).size(); ++i) {
printf("%s\n", (it->second)[i].c_str());
}
}
return 0;
return ret;
}

}
Expand Down