Skip to content

Commit

Permalink
Link (baidu#725)
Browse files Browse the repository at this point in the history
* Support symlink: only for file symlink

(baidu#704) This version is just for file symlink. Dir symlink is not
supported!
  • Loading branch information
DoDo1992 authored and lylei committed Dec 30, 2016
1 parent 5a66fb6 commit c7391e3
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 43 deletions.
Empty file modified docs/cn/symlink.md
100755 → 100644
Empty file.
31 changes: 26 additions & 5 deletions src/client/bfs_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void print_usage() {
printf("\t rmr <path>... : remove directory recursively\n");
printf("\t du <path>... : count disk usage for path\n");
printf("\t stat : list current stat of the file system\n");
printf("\t ln <src> <dst>: create symlink\n");
printf("\t chmod <mode> <path> : change file mode bits\n");
}

Expand Down Expand Up @@ -104,6 +105,19 @@ int BfsRename(baidu::bfs::FS* fs, int argc, char* argv[]) {
return 0;
}

int BfsLink(baidu::bfs::FS* fs, int argc, char* argv[]) {
if (argc < 2) {
print_usage();
return 1;
}
int32_t ret = fs->Symlink(argv[0], argv[1]);
if (ret != 0) {
fprintf(stderr, "CreateSymlink %s to %s fail\n", argv[0], argv[1]);
return 1;
}
return 0;
}

int BfsCat(baidu::bfs::FS* fs, int argc, char* argv[]) {
if (argc < 1) {
print_usage();
Expand Down Expand Up @@ -311,6 +325,7 @@ int BfsDu(baidu::bfs::FS* fs, int argc, char* argv[]) {

int BfsList(baidu::bfs::FS* fs, int argc, char* argv[]) {
std::string path("/");
char file_types[10] ={'-', 'd', 'l'};
if (argc == 3) {
path = argv[2];
if (path.size() && path[path.size()-1] != '/') {
Expand All @@ -325,11 +340,15 @@ int BfsList(baidu::bfs::FS* fs, int argc, char* argv[]) {
return 1;
}
printf("Found %d items\n", num);

for (int i = 0; i < num; i++) {
int32_t type = files[i].mode;
char statbuf[16] = "drwxrwxrwx";
for (int j = 0; j < 10; j++) {
if ((type & (1<<(9-j))) == 0) {
char statbuf[16] = "-rwxrwxrwx";
int32_t file_type = files[i].mode >> 9;
int32_t file_perm = files[i].mode & 0777;
statbuf[0] = file_types[file_type];

for (int j = 1; j < 10; j++) {
if ((file_perm & (1<<(9-j))) == 0) {
statbuf[j] = '-';
}
}
Expand Down Expand Up @@ -532,8 +551,10 @@ int main(int argc, char* argv[]) {
ret = BfsLocation(fs, argc - 2, argv + 2);
} else if (strcmp(argv[1], "shutdownchunkserver") == 0) {
ret = BfsShutdownChunkServer(fs, argc - 2, argv + 2);
} else if(strcmp(argv[1], "shutdownstat") == 0) {
} else if (strcmp(argv[1], "shutdownstat") == 0) {
ret = BfsShutdownStat(fs);
} else if (strcmp(argv[1], "ln") == 0) {
ret = BfsLink(fs, argc - 2, argv + 2);
} else {
fprintf(stderr, "Unknow common: %s\n", argv[1]);
}
Expand Down
32 changes: 32 additions & 0 deletions src/nameserver/nameserver_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,36 @@ void NameServerImpl::Rename(::google::protobuf::RpcController* controller,
controller, request, response, done, removed, std::placeholders::_1));
}

void NameServerImpl::Symlink(::google::protobuf::RpcController* controller,
const SymlinkRequest* request,
SymlinkResponse* response,
::google::protobuf::Closure* done) {
if (!is_leader_) {
response->set_status(kIsFollower);
done->Run();
return;
}
response->set_sequence_id(request->sequence_id());
std::string src = NameSpace::NormalizePath(request->src());
std::string dst = NameSpace::NormalizePath(request->dst());

NameServerLog log;
StatusCode status = namespace_->Symlink(src, dst, &log);
sofa::pbrpc::RpcController* ctl = reinterpret_cast<sofa::pbrpc::RpcController*>(controller);
LOG(INFO, "SDK %s, dst:%s -> src:%s create symlink %s returns %s",
ctl->RemoteAddress().c_str(), dst.c_str(), src.c_str(), StatusCode_Name(status).c_str());
response->set_status(status);

if (status != kOK) {
done->Run();
return;
}

LogRemote(log, std::bind(&NameServerImpl::SyncLogCallback, this,
controller, request, response, done,
(std::vector<FileInfo>*)NULL, std::placeholders::_1));
}

void NameServerImpl::Unlink(::google::protobuf::RpcController* controller,
const UnlinkRequest* request,
UnlinkResponse* response,
Expand Down Expand Up @@ -1450,6 +1480,8 @@ void NameServerImpl::CallMethod(const ::google::protobuf::MethodDescriptor* meth
std::make_pair("PushBlockReport", work_thread_pool_),
std::make_pair("SysStat", read_thread_pool_),
std::make_pair("Chmod", work_thread_pool_),
std::make_pair("Symlink", work_thread_pool_)

};
static int method_num = sizeof(ThreadPoolOfMethod) /
sizeof(std::pair<std::string, ThreadPool*>);
Expand Down
5 changes: 4 additions & 1 deletion src/nameserver/nameserver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ class NameServerImpl : public NameServer {
const DiskUsageRequest* request,
DiskUsageResponse* response,
::google::protobuf::Closure* done);
void Symlink(::google::protobuf::RpcController* controller,
const SymlinkRequest* request,
SymlinkResponse* response,
::google::protobuf::Closure* done);
void Chmod(::google::protobuf::RpcController* controller,
const ChmodRequest* request,
ChmodResponse* response,
::google::protobuf::Closure* done);

bool WebService(const sofa::pbrpc::HTTPRequest&, sofa::pbrpc::HTTPResponse&);

private:
Expand Down
Loading

0 comments on commit c7391e3

Please sign in to comment.