bool LocalMemoryGridFile::initLocalBuffers(GridFile& gridFile) { if (!setSize(gridFile.getContentLength())) { return false; } //TODO: Check for size compared to the content length that was specified for consistency off_t offset = 0; int chunkCount = gridFile.getNumChunks(); for (int i = 0; i < chunkCount; ++i) { //TODO: Error checking or getChunk and related handling GridFSChunk chunk = gridFile.getChunk(i); int chunkLen = 0; const char* data = chunk.data(chunkLen); if (!data) { error() << "Failed to get data from expected chunk {file: " << _filename << ", chunkOffset: " << i << ", totalChunks: " << chunkCount << "}, will stop in-between and return error." << endl; return false; } _write(data, chunkLen, offset); offset += chunkLen; } return true; }
int gridfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { path = fuse_to_mongo_path(path); size_t len = 0; map<string,LocalGridFile*>::const_iterator file_iter; file_iter = open_files.find(path); if(file_iter != open_files.end()) { LocalGridFile *lgf = file_iter->second; return lgf->read(buf, size, offset); } ScopedDbConnection sdc(*gridfs_options.conn_string); bool digest = true; string err = ""; sdc.conn().DBClientWithCommands::auth(gridfs_options.db, gridfs_options.username, gridfs_options.password, err, digest); fprintf(stderr, "DEBUG: %s\n", err.c_str()); GridFS gf(sdc.conn(), gridfs_options.db); GridFile file = gf.findFile(path); if(!file.exists()) { sdc.done(); return -EBADF; } int chunk_size = file.getChunkSize(); int chunk_num = offset / chunk_size; while(len < size && chunk_num < file.getNumChunks()) { GridFSChunk chunk = file.getChunk(chunk_num); int to_read; int cl = chunk.len(); const char *d = chunk.data(cl); if(len) { to_read = min((long unsigned)cl, (long unsigned)(size - len)); memcpy(buf + len, d, to_read); } else { to_read = min((long unsigned)(cl - (offset % chunk_size)), (long unsigned)(size - len)); memcpy(buf + len, d + (offset % chunk_size), to_read); } len += to_read; chunk_num++; } sdc.done(); return len; }
gridfs_offset GridFile::write( ostream & out ) { _exists(); const int num = getNumChunks(); for ( int i=0; i<num; i++ ) { GridFSChunk c = getChunk( i ); int len; const char * data = c.data( len ); out.write( data , len ); } return getContentLength(); }
int gridfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { path = fuse_to_mongo_path(path); size_t len = 0; map<string,LocalGridFile*>::const_iterator file_iter; file_iter = open_files.find(path); if(file_iter != open_files.end()) { LocalGridFile *lgf = file_iter->second; return lgf->read(buf, size, offset); } ScopedDbConnection sdc(*gridfs_options.conn_string); ScopedDbConnection_init(sdc); GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix); GridFile file = gf.findFile(path); if(!file.exists()) { sdc.done(); return -EBADF; } int chunk_size = file.getChunkSize(); int chunk_num = offset / chunk_size; while(len < size && chunk_num < file.getNumChunks()) { GridFSChunk chunk = file.getChunk(chunk_num); int to_read; int cl = chunk.len(); const char *d = chunk.data(cl); if(len) { to_read = min((long unsigned)cl, (long unsigned)(size - len)); memcpy(buf + len, d, to_read); } else { to_read = min((long unsigned)(cl - (offset % chunk_size)), (long unsigned)(size - len)); memcpy(buf + len, d + (offset % chunk_size), to_read); } len += to_read; chunk_num++; } sdc.done(); return len; }