本文整理汇总了C++中Sector::stat方法的典型用法代码示例。如果您正苦于以下问题:C++ Sector::stat方法的具体用法?C++ Sector::stat怎么用?C++ Sector::stat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sector
的用法示例。
在下文中一共展示了Sector::stat方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getFileList
int getFileList(const string& path, vector<string>& fl, Sector& client, unsigned int thresh)
{
SNode attr;
if (client.stat(path.c_str(), attr) < 0)
return -1;
if (attr.m_bIsDir)
{
vector<SNode> subdir;
client.list(path, subdir);
for (vector<SNode>::iterator i = subdir.begin(); i != subdir.end(); ++ i)
{
if (i->m_bIsDir)
getFileList(path + "/" + i->m_strName, fl, client, thresh);
else if ((i->m_sLocation.size() < thresh) && (int(i->m_sLocation.size()) < i->m_iReplicaNum))
fl.push_back(path + "/" + i->m_strName);
}
}
else if ((attr.m_sLocation.size() < thresh) && (int(attr.m_sLocation.size()) < attr.m_iReplicaNum))
{
fl.push_back(path);
}
return fl.size();
}
示例2: main
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << "USAGE: stat file\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;
SNode attr;
int r = client.stat(argv[1], attr);
if (r < 0)
{
cout << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl;
}
else
{
cout << "FILE NAME: " << attr.m_strName << endl;
if (attr.m_bIsDir)
cout << "DIR: TRUE\n";
else
cout << "DIR: FALSE\n";
cout << "SIZE: " << attr.m_llSize << " bytes" << endl;
time_t ft = attr.m_llTimeStamp;
cout << "LAST MODIFIED: " << ctime(&ft);
cout << "LOCATION: ";
for (set<Address, AddrComp>::iterator i = attr.m_sLocation.begin(); i != attr.m_sLocation.end(); ++ i)
{
cout << i->m_strIP << ":" << i->m_iPort << " ";
}
cout << endl;
}
client.logout();
client.close();
return 1;
}
示例3: main
int main(int argc, char** argv)
{
if (argc != 2)
{
cerr << "USAGE: sector_stat file\n";
return -1;
}
Sector client;
if (Utility::login(client) < 0)
return -1;
SNode attr;
int result = client.stat(argv[1], attr);
if (result < 0)
{
Utility::print_error(result);
}
else
{
cout << "File Name: " << attr.m_strName << endl;
if (attr.m_bIsDir)
cout << "DIR: TRUE\n";
else
cout << "DIR: FALSE\n";
cout << "Size: " << attr.m_llSize << " bytes" << endl;
time_t ft = attr.m_llTimeStamp;
cout << "Last Modified: " << ctime(&ft);
if (!attr.m_bIsDir)
{
cout << "Total Number of Replicas: " << attr.m_sLocation.size() << " (target: " << attr.m_iReplicaNum << ")" << endl;
cout << "Location:" << endl;
for (set<Address, AddrComp>::iterator i = attr.m_sLocation.begin(); i != attr.m_sLocation.end(); ++ i)
{
cout << i->m_strIP << ":" << i->m_iPort << "\t" << getDNSName(i->m_strIP) << endl;
}
}
}
Utility::logout(client);
return result;
}
示例4: getFileList
int getFileList(const string& path, vector<string>& fl, Sector& client)
{
SNode attr;
if (client.stat(path, attr) < 0)
return -1;
fl.push_back(path);
if (attr.m_bIsDir)
{
vector<SNode> subdir;
client.list(path, subdir);
for (vector<SNode>::iterator i = subdir.begin(); i != subdir.end(); ++ i)
{
if (i->m_bIsDir)
getFileList(path + "/" + i->m_strName, fl, client);
else
fl.push_back(path + "/" + i->m_strName);
}
}
return fl.size();
}
示例5: main
int main(int argc, char** argv)
{
if (argc < 2)
{
help();
return -1;
}
unsigned int thresh = 65536;
int timeout = 3600 * 24; // default wait for one day
CmdLineParser clp;
if (clp.parse(argc, argv) < 0)
{
help();
return -1;
}
for (map<string, string>::const_iterator i = clp.m_mDFlags.begin(); i != clp.m_mDFlags.end(); ++ i)
{
if (i->first == "w")
thresh = atoi(i->second.c_str());
else if (i->first == "t")
{
timeout = atoi(i->second.c_str());
if (timeout < 0)
timeout = INT_MAX;
}
else
{
help();
return -1;
}
}
Sector client;
if (Utility::login(client) < 0)
return -1;
list<string> filelist;
for (vector<string>::iterator i = clp.m_vParams.begin(); i < clp.m_vParams.end(); ++ i)
{
vector<string> fl;
fl.clear();
bool wc = WildCard::isWildCard(*i);
if (!wc)
{
SNode attr;
if (client.stat(*i, attr) < 0)
{
cerr << "ERROR: source file does not exist.\n";
return -1;
}
getFileList(*i, fl, client, thresh);
}
else
{
string path = *i;
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);
}
vector<SNode> filelist;
int r = client.list(path, filelist);
if (r < 0)
cerr << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl;
for (vector<SNode>::iterator i = filelist.begin(); i != filelist.end(); ++ i)
{
if (WildCard::match(orig, i->m_strName))
getFileList(path + "/" + i->m_strName, fl, client, thresh);
}
}
filelist.insert(filelist.end(), fl.begin(), fl.end());
}
int result = -1;
timeval t;
gettimeofday(&t, NULL);
int exp_time = t.tv_sec + timeout;
int interval = 1;
int total = -1;
int new_total = 0;
while (!filelist.empty())
{
new_total = 0;
for (list<string>::iterator i = filelist.begin(); i != filelist.end();)
//.........这里部分代码省略.........
示例6: upload
int upload(const char* file, const char* dst, Sector& client, const int rep_num, const string& ip, const string& cid, const bool secure, const bool smart)
{
//check if file already exists
SNode s;
if (LocalFS::stat(file, s) < 0)
{
cout << "cannot locate source file " << file << endl;
return -1;
}
SNode attr;
if (client.stat(dst, attr) >= 0)
{
if (attr.m_llSize == s.m_llSize && smart)
{
cout << "destination file " << dst << " exists on Sector FS. skip.\n";
return 0;
}
}
cout << "uploading " << file << " of " << s.m_llSize << " bytes" << endl;
timeval t1, t2;
gettimeofday(&t1, 0);
SectorFile* f = client.createSectorFile();
SF_OPT option;
option.m_llReservedSize = s.m_llSize;
if (option.m_llReservedSize <= 0)
option.m_llReservedSize = 1;
option.m_iReplicaNum = rep_num;
option.m_strHintIP = ip;
option.m_strCluster = cid;
int mode = SF_MODE::WRITE | SF_MODE::TRUNC;
if (secure)
mode |= SF_MODE::SECURE;
int r = f->open(dst, mode, &option);
if (r < 0)
{
cerr << "unable to open file " << dst << endl;
Utility::print_error(r);
return -1;
}
int64_t result = f->upload(file);
f->close();
client.releaseSectorFile(f);
if (result >= 0)
{
gettimeofday(&t2, 0);
float throughput = s.m_llSize * 8.0 / 1000000.0 / ((t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0);
cout << "Uploading accomplished! " << "AVG speed " << throughput << " Mb/s." << endl << endl ;
}
else
{
cout << "Uploading failed! Please retry. " << endl << endl;
Utility::print_error(result);
return result;
}
return 0;
}
示例7: main
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);
//.........这里部分代码省略.........
示例8: download
int download(const char* file, const char* dest, Sector& client, bool encryption)
{
#ifndef WIN32
timeval t1, t2;
#else
DWORD t1, t2;
#endif
#ifndef WIN32
gettimeofday(&t1, 0);
#else
t1 = GetTickCount();
#endif
SNode attr;
int rc;
if ((rc = client.stat(file, attr)) < 0)
{
cerr << "ERROR: cannot locate file " << file << endl;
log().error << "stat of source file " << file << " failed with rc = " << rc << std::endl;
return -1;
}
if (attr.m_bIsDir)
{
rc = ::mkdir((string(dest) + "/" + file).c_str(), S_IRWXU);
if( rc < 0 && errno == EEXIST )
{
log().debug << "directory " << dest << '/' << file << " already exists" << std::endl;
return 1;
}
else if( rc < 0 )
{
int errno_save = errno;
log().error << "Failed to create directory " << dest << '/' << file << ", errno = " << errno_save << std::endl;
return -1;
}
else
return 1;
}
const long long int size = attr.m_llSize;
cout << "downloading " << file << " of " << size << " bytes" << endl;
log().debug << "Downloading " << file << " of " << size << " bytes" << std::endl;
SectorFile* f = client.createSectorFile();
int mode = SF_MODE::READ;
if (encryption)
mode |= SF_MODE::SECURE;
if ((rc = f->open(file, mode)) < 0)
{
cerr << "unable to locate file " << file << endl;
log().error << "Failed to open sector file " << file << " with err = " << rc << std::endl;
return -1;
}
int sn = strlen(file) - 1;
for (; sn >= 0; sn --)
{
if (file[sn] == '/')
break;
}
string localpath;
if (dest[strlen(dest) - 1] != '/')
localpath = string(dest) + string("/") + string(file + sn + 1);
else
localpath = string(dest) + string(file + sn + 1);
log().debug << "Downloading " << file << " to " << localpath << std::endl;
int64_t result = f->download(localpath.c_str(), true);
f->close();
client.releaseSectorFile(f);
if (result >= 0)
{
float throughput = 0.0;
#ifndef WIN32
gettimeofday(&t2, 0);
float span = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0;
#else
float span = (GetTickCount() - t1) / 1000.0;
#endif
if (span > 0.0)
throughput = result * 8.0 / 1000000.0 / span;
cout << "Downloading accomplished! " << "AVG speed " << throughput << " Mb/s." << endl << endl ;
log().debug << "Download of file successful!" << std::endl;
return 0;
}
cerr << "error happened during downloading " << file << endl;
log().debug << "Failed to download file, res = " << result << std::endl;
Utility::print_error(result);
return -1;
}
示例9: main
int main(int argc, char** argv)
{
logger::config( "/tmp", "sector-download" );
for( int arg = 0; arg < argc; ++arg )
log().debug << argv[ arg ] << ' ';
log().debug << std::endl;
if (argc < 3)
{
help(argv[0]);
log().error << "Invalid command-line syntax, exiting with rc = -1" << std::endl;
return -1;
}
CmdLineParser clp;
if (clp.parse(argc, argv) < 0)
{
help(argv[0]);
log().error << "Invalid command-line syntax, exiting with rc = -1" << std::endl;
return -1;
}
if (clp.m_vParams.size() < 2)
{
help(argv[0]);
log().error << "Invalid command-line syntax, exiting with rc = -1" << std::endl;
return -1;
}
bool encryption = false;
bool resume = false;
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" )
resume = true;
else
{
help(argv[0]);
log().error << "Invalid command-line syntax, exiting with rc = -1" << std::endl;
return -1;
}
}
string newdir = *clp.m_vParams.rbegin();
clp.m_vParams.erase(clp.m_vParams.begin() + clp.m_vParams.size() - 1);
// check destination directory, which must exist
SNode s;
int r = LocalFS::stat(newdir, s);
if ((r < 0) || !s.m_bIsDir)
{
cerr << "ERROR: destination directory does not exist.\n";
log().error << "stat failed on destination directory, err = " << r << ", exiting with rc = -1" << std::endl;
return -1;
}
// login to SectorFS
Sector client;
int rc = Utility::login(client);
if (rc < 0)
{
cerr << "ERROR: failed to log in to sector\n";
log().error << "Client login to master failed with err = " << rc << ", exiting with rc = -1" << std::endl;
return -1;
}
// start downloading all files
for (vector<string>::iterator i = clp.m_vParams.begin(); i != clp.m_vParams.end(); ++ i)
{
vector<string> fl;
bool wc = WildCard::isWildCard(*i);
if (!wc)
{
SNode attr;
if ((rc = client.stat(*i, attr)) < 0)
{
cerr << "ERROR: source file does not exist.\n";
log().error << "Failed to stat sector file " << *i << ", err = " << rc << std::endl;
return -1;
}
getFileList(*i, fl, client);
}
else
{
string path = *i;
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);
}
vector<SNode> filelist;
//.........这里部分代码省略.........
示例10: main
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;
//.........这里部分代码省略.........
示例11: upload
int upload(const char* file, const char* dst, Sector& client)
{
//check if file already exists
struct stat st;
if (stat(file, &st) < 0)
{
cout << "cannot locate source file " << file << endl;
return -1;
}
SNode attr;
if (client.stat(dst, attr) >= 0)
{
if (attr.m_llSize == st.st_size)
{
cout << "destination file " << dst << " exists on Sector FS. skip.\n";
return 0;
}
}
CTimer timer;
uint64_t t1 = timer.getTime(); // returns time in microseconds (usecs)
struct stat s;
stat(file, &s);
cout << "uploading " << file << " of " << s.st_size << " bytes" << endl;
SectorFile* f = client.createSectorFile();
int64_t reserve = s.st_size;
if (reserve <= 0)
reserve = 1;
int r = f->open(dst, SF_MODE::WRITE, "", reserve);
if (r < 0)
{
cerr << "unable to open file " << dst << endl;
Utility::print_error(r);
return -1;
}
int64_t result = f->upload(file);
f->close();
client.releaseSectorFile(f);
if (result >= 0)
{
float throughput = s.st_size * 8.0f / 1000000.0f / ((timer.getTime() - t1) / 1000000.0f);
cout << "Uploading accomplished! " << "AVG speed " << throughput << " Mb/s." << endl << endl ;
}
else
{
cout << "Uploading failed! Please retry. " << endl << endl;
Utility::print_error(result);
return -1;
}
return 0;
}
示例12: main
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)
//.........这里部分代码省略.........