int upload(const char* file, const char* dst, Sector& client) { 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(); if (f->open(dst, SF_MODE::WRITE) < 0) { cout << "ERROR: unable to connect to server or file already exists." << endl; return -1; } bool finish = true; if (f->upload(file) < 0LL) finish = false; f->close(); client.releaseSectorFile(f); if (finish) { 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; return 1; }
int upload(const char* file, const char* dst, Sector& client) { timeval t1, t2; gettimeofday(&t1, 0); struct stat64 s; stat64(file, &s); cout << "uploading " << file << " of " << s.st_size << " bytes" << endl; SectorFile* f = client.createSectorFile(); if (f->open(dst, SF_MODE::WRITE) < 0) { cout << "ERROR: unable to connect to server or file already exists." << endl; return -1; } bool finish = true; if (f->upload(file) < 0LL) finish = false; f->close(); client.releaseSectorFile(f); if (finish) { gettimeofday(&t2, 0); float throughput = s.st_size * 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; return 1; }
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; }
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; }
int main(int argc, char** argv) { CmdLineParser clp; if ((clp.parse(argc, argv) < 0) || (clp.m_mDFlags.size() != 1)) { cerr << "usage #1: <your_application> | sector_pipe -d dst_file" << endl; cerr << "usage #2: sector_pipe -s src_file | <your_application>" << endl; return 0; } string option = clp.m_mDFlags.begin()->first; Sector client; if (Utility::login(client) < 0) return -1; timeval t1, t2; gettimeofday(&t1, 0); int64_t total_size = 0; SectorFile* f = client.createSectorFile(); if (option == "d") { if (f->open(argv[2], SF_MODE::WRITE | SF_MODE::APPEND) < 0) { cerr << "ERROR: unable to open destination file." << endl; return -1; } int size = 1000000; char* buf = new char[size]; int read_size = size; // read data from sdtin and write to Sector while(true) { read_size = read(0, buf, size); if (read_size <= 0) break; f->write(buf, read_size); total_size += read_size; } delete [] buf; } else if (option == "s") { if (f->open(argv[2], SF_MODE::READ) < 0) { cerr << "ERROR: unable to open source file." << endl; return -1; } int size = 1000000; char* buf = new char[size]; int read_size = size; // read data from Sector and write to stdout while(!f->eof()) { read_size = f->read(buf, size); if (read_size <= 0) break; total_size += read_size; write(1, buf, read_size); } delete [] buf; } f->close(); client.releaseSectorFile(f); gettimeofday(&t2, 0); double throughput = total_size * 8.0 / 1000000.0 / ((t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0); cerr << "Pipeline accomplished! " << "AVG speed " << throughput << " Mb/s." << endl << endl ; Utility::logout(client); return 0; }
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; }