/* * Read data from a Sector file. This function can be called repeatedly * to read through a file in chunks. * * filehandle is the value returned by open(). * len is the number of bytes to read from the file. * * Returns: on success the data read. None on EOF. On failure NULL. */ static PyObject* read( PyObject* self, PyObject* args ) { SectorFile* f; long filehandle; long len; if( !PyArg_ParseTuple( args, "ll", &filehandle, &len ) ) { return( NULL ); } f = (SectorFile*)filehandle; char buf[len]; int status = f->read( buf, len ); if( status == 0 ) { return( Py_None ); } if( status < 0 ) { PyErr_SetString( PyExc_IOError, "read failed, status=" + status ); return( NULL ); } buf[status] = '\0'; return( Py_BuildValue( "s", buf ) ); }
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; }