⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kfsshell_main.cc

📁 nandflash文件系统源代码
💻 CC
字号:
//---------------------------------------------------------- -*- Mode: C++ -*-// $Id: kfsshell_main.cc 225 2008-12-17 01:34:14Z sriramsrao $//// Created 2007/09/26// Author: Sriram Rao//// Copyright 2008 Quantcast Corp.// Copyright 2007-2008 Kosmix Corp.//// This file is part of Kosmos File System (KFS).//// 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.//// \brief A simple shell that lets users navigate KFS directory hierarchy.// //----------------------------------------------------------------------------#include <iostream>    #include <fstream>#include <cerrno>#include "libkfsClient/KfsClient.h"#include "common/log.h"#include "tools/KfsShell.h"#include <iostream>#include <tr1/unordered_map>using std::cin;using std::cout;using std::endl;using std::map;using std::vector;using std::string;using namespace KFS;using namespace KFS::tools;typedef map <string, cmdHandler> CmdHandlers;typedef map <string, cmdHandler>::iterator CmdHandlersIter;CmdHandlers handlers;static void setupHandlers();/// @retval: status code from executing the last commandstatic int processCmds(bool quietMode, int nargs, const char **cmdLine);intmain(int argc, char **argv){    string kfsdirname = "";    string serverHost = "";    int port = -1, retval;    bool help = false;    bool quietMode = false;    char optchar;    bool verboseLogging = false;    while ((optchar = getopt(argc, argv, "hqs:p:v")) != -1) {        switch (optchar) {            case 's':                serverHost = optarg;                break;            case 'p':                port = atoi(optarg);                break;            case 'h':                help = true;                break;            case 'v':                verboseLogging = true;                break;            case 'q':                quietMode = true;                break;            default:                cout << "Unrecognized flag : " << optchar;                help = true;                break;        }    }    if (help || (serverHost == "") || (port < 0)) {        cout << "Usage: " << argv[0] << " -s <meta server name> -p <port> {-q}" << endl;        exit(0);    }    KfsClientFactory *factory = getKfsClientFactory();    KfsClientPtr kfsClient = factory->GetClient(serverHost, port);    if (!kfsClient) {        cout << "kfs client failed to initialize...exiting" << endl;        exit(-1);    }    if (verboseLogging) {        KFS::MsgLogger::SetLevel(log4cpp::Priority::DEBUG);    } else {        KFS::MsgLogger::SetLevel(log4cpp::Priority::INFO);    }        factory->SetDefaultClient(kfsClient);    setupHandlers();    retval = processCmds(quietMode, argc - optind, (const char **) &argv[optind]);    return retval;}void printCmds(){    cout << "cd" << endl;    cout << "changeReplication" << endl;    cout << "cp" << endl;    cout << "ls" << endl;    cout << "mkdir" << endl;    cout << "mv" << endl;    cout << "rm" << endl;    cout << "rmdir" << endl;    cout << "stat" << endl;    cout << "pwd" << endl;}int handleHelp(const vector<string> &args){    printCmds();    return 0;}void setupHandlers(){    handlers["cd"] = handleCd;    handlers["changeReplication"] = handleChangeReplication;    handlers["cp"] = handleCopy;    handlers["ls"] = handleLs;    handlers["mkdir"] = handleMkdirs;    handlers["mv"] = handleMv;    handlers["rmdir"] = handleRmdir;    // handlers["ping"] = handlePing;    handlers["rm"] = handleRm;    handlers["stat"] = handleFstat;    handlers["pwd"] = handlePwd;    handlers["help"] = handleHelp;}int processCmds(bool quietMode, int nargs, const char **cmdLine){    char buf[4096];    string s, cmd;    int retval = 0;    while (1) {        if (quietMode) {            if (nargs == 0)                break;            s = "";            for (int i = 0; i < nargs; i++) {                s = s + cmdLine[i];                s = s + " ";            }            nargs = 0;        } else {            // Turn off prompt printing when quiet mode is enabled;            // this allows scripting with KfsShell            cout << "KfsShell> ";            cin.getline(buf, 4096);                        if (cin.eof())                break;            s = buf;        }        // buf contains info of the form: <cmd>{<args>}        // where, <cmd> is one of kfs cmds        string::size_type curr, next;                // get rid of leading spaces        curr = s.find_first_not_of(" \t");        s.erase(0, curr);        curr = s.find(' ');        if (curr != string::npos)            cmd.assign(s, 0, curr);        else            cmd = s;        next = curr;        // extract out the args        vector<string> args;        while (curr != string::npos) {            string component;            // curr points to a ' '            curr++;            next = s.find(' ', curr);                    if (next != string::npos)                component.assign(s, curr, next - curr);            else                component.assign(s, curr, string::npos);            if (component != "")                args.push_back(component);            curr = next;        }        CmdHandlersIter h = handlers.find(cmd);        if (h == handlers.end()) {            cout << "Unknown cmd: " << cmd << endl;            cout << "Supported cmds are: " << endl;            printCmds();            cout << "Type <cmd name> --help for command specific help" << endl;            continue;        }                retval = ((*h).second)(args);    }    return retval;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -