/* * Copy a file from Sector to the local filesystem. * * src is path to Sector file. * dest is path to new local file. * * Returns: on success, a non-negative value. On failure, a negative value. */ static PyObject* download( PyObject* self, PyObject* args ) { int status = 0; SectorFile f; const char* src; const char* dest; if( !PyArg_ParseTuple( args, "ss", &src, &dest ) ) { return( NULL ); } status = f.open( src, SF_MODE::READ ); if( status < 0 ) { PyErr_SetString( PyExc_IOError, "failed to open source, status=" + status ); return( NULL ); } status = f.download( dest ); if( status < 0 ) { PyErr_SetString( PyExc_IOError, "download failed, status=" + status ); } f.close(); return( Py_BuildValue( "i", status ) ); }
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; }