int main(int argc, char** argv) { if (argc != 2) { cerr << "USAGE: mkdir <dir>\n"; return -1; } Sector client; Session s; s.loadInfo("../conf/client.conf"); if (client.init(s.m_ClientConf.m_strMasterIP, s.m_ClientConf.m_iMasterPort) < 0) return -1; if (client.login(s.m_ClientConf.m_strUserName, s.m_ClientConf.m_strPassword, s.m_ClientConf.m_strCertificate.c_str()) < 0) return -1; int r = client.mkdir(argv[1]); if (r < 0) cerr << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl; client.logout(); client.close(); return 1; }
int main(int argc, char** argv) { if (argc != 2) { cerr << "USAGE: mkdir <dir>\n"; return -1; } Sector client; if (Utility::login(client) < 0) return -1; int result = 0; if ((result = client.mkdir(argv[1])) < 0) Utility::print_error(result); Utility::logout(client); return result; }
int main(int argc, char** argv) { if (argc < 3) { help(); return -1; } CmdLineParser clp; if (clp.parse(argc, argv) < 0) { help(); return -1; } if (clp.m_vParams.size() < 2) { help(); return -1; } int replica_num = 1; string ip = ""; string cluster = ""; int parallel = 1; // concurrent uploading multiple files bool encryption = false; bool smart = false; for (map<string, string>::const_iterator i = clp.m_mDFlags.begin(); i != clp.m_mDFlags.end(); ++ i) { if (i->first == "n") replica_num = atoi(i->second.c_str()); else if (i->first == "a") ip = i->second; else if (i->first == "c") cluster = i->second; else if (i->first == "p") parallel = atoi(i->second.c_str()); else { help(); return -1; } } for (vector<string>::const_iterator i = clp.m_vSFlags.begin(); i != clp.m_vSFlags.end(); ++ i) { if (*i == "e") encryption = true; else if (*i == "smart" ) smart = true; else { help(); return -1; } } Sector client; if (Utility::login(client) < 0) return -1; string dstdir = *clp.m_vParams.rbegin(); clp.m_vParams.pop_back(); SNode attr; int r = client.stat(dstdir, attr); if ((r < 0) || (!attr.m_bIsDir)) { cerr << "destination directory on Sector does not exist.\n"; Utility::logout(client); return -1; } bool success = true; // upload multiple files/dirs for (vector<string>::const_iterator param = clp.m_vParams.begin(); param != clp.m_vParams.end(); ++ param) { string prefix = ""; vector<string> fl; bool wc = WildCard::isWildCard(*param); if (!wc) { SNode s; if (LocalFS::stat(*param, s) < 0) { cerr << "ERROR: source file does not exist.\n"; return -1; } if (s.m_bIsDir) prefix = *param; else { size_t pos = param->rfind('/'); if (pos != string::npos) prefix = param->substr(0, pos); } getFileList(*param, fl); } else { size_t pos = param->rfind('/'); if (pos != string::npos) prefix = param->substr(0, pos); string path = *param; string orig = path; size_t p = path.rfind('/'); if (p == string::npos) { path = "."; } else { path = path.substr(0, p); orig = orig.substr(p + 1, orig.length() - p); } // as this is a wildcard, list all files in the current dir, choose those matched ones vector<SNode> curr_fl; if (LocalFS::list_dir(path, curr_fl) < 0) return -1; for (vector<SNode>::iterator s = curr_fl.begin(); s != curr_fl.end(); ++ s) { // skip "." and ".." if ((s->m_strName == ".") || (s->m_strName == "..")) continue; if (WildCard::match(orig, s->m_strName)) { if (path == ".") getFileList(s->m_strName, fl); else getFileList(path + "/" + s->m_strName, fl); } } } // upload all files in the file list for (vector<string>::const_iterator i = fl.begin(); i != fl.end(); ++ i) { // process directory name change: /src/mydata -> /dst/mydata string dst = *i; if (prefix.length() > 0) dst.replace(0, prefix.length(), dstdir + "/"); else dst = dstdir + "/" + dst; SNode s; if (LocalFS::stat(*i, s) < 0) continue; if (s.m_bIsDir) client.mkdir(dst); else { int result = upload(i->c_str(), dst.c_str(), client, replica_num, ip, cluster, encryption, smart); if ((result == SectorError::E_CONNECTION) || (result == SectorError::E_BROKENPIPE)) { // connection fail, retry once. result = upload(i->c_str(), dst.c_str(), client, replica_num, ip, cluster, encryption, smart); } if (result < 0) { // failed, remove the file in Sector. client.remove(dst); success = false; Utility::logout(client); return -1; } } } } Utility::logout(client); return success ? 0 : -1; }
int main(int argc, char** argv) { if (3 != argc) { cout << "usage: upload <src file/dir> <dst dir>" << endl; return 0; } Sector client; Session s; s.loadInfo("../conf/client.conf"); if (client.init(s.m_ClientConf.m_strMasterIP, s.m_ClientConf.m_iMasterPort) < 0) return -1; if (client.login(s.m_ClientConf.m_strUserName, s.m_ClientConf.m_strPassword, s.m_ClientConf.m_strCertificate.c_str()) < 0) return -1; vector<string> fl; bool wc = WildCard::isWildCard(argv[1]); if (!wc) { struct stat64 st; if (stat64(argv[1], &st) < 0) { cout << "ERROR: source file does not exist.\n"; return -1; } getFileList(argv[1], fl); } else { string path = argv[1]; string orig = path; size_t p = path.rfind('/'); if (p == string::npos) path = "/"; else { path = path.substr(0, p); orig = orig.substr(p + 1, orig.length() - p); } dirent **namelist; int n = scandir(path.c_str(), &namelist, 0, alphasort); if (n < 0) return -1; for (int i = 0; i < n; ++ i) { // skip "." and ".." and hidden directory if (namelist[i]->d_name[0] == '.') { free(namelist[i]); continue; } if (WildCard::match(orig, namelist[i]->d_name)) getFileList(path + "/" + namelist[i]->d_name, fl); } } string olddir; for (int i = strlen(argv[1]) - 1; i >= 0; -- i) { if (argv[1][i] != '/') { olddir = string(argv[1]).substr(0, i); break; } } size_t p = olddir.rfind('/'); if (p == string::npos) olddir = ""; else olddir = olddir.substr(0, p); string newdir = argv[2]; SNode attr; int r = client.stat(newdir, attr); if ((r < 0) || (!attr.m_bIsDir)) { cout << "destination directory on Sector does not exist.\n"; return -1; } for (vector<string>::const_iterator i = fl.begin(); i != fl.end(); ++ i) { string dst = *i; if (olddir.length() > 0) dst.replace(0, olddir.length(), newdir); else dst = newdir + "/" + dst; struct stat64 s; if (stat64(i->c_str(), &s) < 0) continue; if (S_ISDIR(s.st_mode)) client.mkdir(dst); else upload(i->c_str(), dst.c_str(), client); } client.logout(); client.close(); return 1; }
int main(int argc, char** argv) { if (argc < 3) { cerr << "usage: upload <src file/dir> <dst dir>" << endl; return 0; } Sector client; if (Utility::login(client) < 0) return -1; string newdir = argv[argc - 1]; SNode attr; int r = client.stat(newdir, attr); if ((r < 0) || (!attr.m_bIsDir)) { cerr << "destination directory on Sector does not exist.\n"; Utility::logout(client); return -1; } bool success = true; for (int i = 1; i < argc - 1; ++ i) { vector<string> fl; string path = argv[1]; #ifdef WIN32 win_to_unix_path (path); #endif bool wc = WildCard::isWildCard(path.c_str()); if (!wc) { struct stat st; if (stat(argv[i], &st) < 0) { cerr << "ERROR: source file does not exist.\n"; return -1; } getFileList(argv[i], fl); } else { string path = argv[i]; #ifdef WIN32 win_to_unix_path (path); #endif string orig = path; size_t p = path.rfind('/'); if (p == string::npos) { path = "."; } else { path = path.substr(0, p); orig = orig.substr(p + 1, orig.length() - p); } dirent **namelist; int n = scandir(path.c_str(), &namelist, 0, alphasort); if (n < 0) return -1; for (int i = 0; i < n; ++ i) { // skip "." and ".." and hidden directory if (namelist[i]->d_name[0] == '.') { free(namelist[i]); continue; } if (WildCard::match(orig, namelist[i]->d_name)) { if (path == ".") getFileList(namelist[i]->d_name, fl); else getFileList(path + "/" + namelist[i]->d_name, fl); } } } string olddir; string input_path = argv[1]; #ifdef WIN32 win_to_unix_path (input_path); #endif for (int j = input_path.length() - 1; j >= 0; -- j) { if (input_path[j] != '/') { olddir = input_path.substr(0, j); break; } } size_t p = olddir.rfind('/'); if (p == string::npos) olddir = ""; else olddir = olddir.substr(0, p); for (vector<string>::const_iterator i = fl.begin(); i != fl.end(); ++ i) { string dst = *i; if (olddir.length() > 0) dst.replace(0, olddir.length(), newdir); else dst = newdir + "/" + dst; struct stat s; if (stat(i->c_str(), &s) < 0) continue; if (S_ISDIR(s.st_mode)) client.mkdir(dst); else { if (upload(i->c_str(), dst.c_str(), client) < 0) success = false; } } } Utility::logout(client); return success ? 0 : -1; }