From 7c514acde015c0bd1a5227d047430b81aec4b119 Mon Sep 17 00:00:00 2001 From: Zhu Yuejun Date: Wed, 22 Feb 2017 16:43:13 +0800 Subject: [PATCH] A small bugfix --- src/sdk/bfs_c.cc | 53 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/sdk/bfs_c.cc b/src/sdk/bfs_c.cc index a4b28ec3..d6205a73 100644 --- a/src/sdk/bfs_c.cc +++ b/src/sdk/bfs_c.cc @@ -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) { @@ -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; @@ -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; } @@ -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; @@ -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) { @@ -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; } @@ -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) { @@ -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; @@ -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; } @@ -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; } @@ -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) { @@ -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 > 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 >::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; } }