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; }
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; }
int gridfs_flush(const char* path, struct fuse_file_info *ffi) { path = fuse_to_mongo_path(path); if(!ffi->fh) { return 0; } map<string,LocalGridFile*>::iterator file_iter; file_iter = open_files.find(path); if(file_iter == open_files.end()) { return -ENOENT; } LocalGridFile *lgf = file_iter->second; if(!lgf->dirty()) { return 0; } 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); if(gf.findFile(path).exists()) { gf.removeFile(path); } size_t len = lgf->getLength(); char *buf = new char[len]; lgf->read(buf, len, 0); gf.storeFile(buf, len, path); sdc.done(); lgf->flushed(); return 0; }
int gridfs_flush(const char* path, struct fuse_file_info *ffi) { path = fuse_to_mongo_path(path); if(!ffi->fh) { return 0; } map<string,LocalGridFile*>::iterator file_iter; file_iter = open_files.find(path); if(file_iter == open_files.end()) { return -ENOENT; } LocalGridFile *lgf = file_iter->second; if(!lgf->dirty()) { return 0; } ScopedDbConnection sdc(*gridfs_options.conn_string); ScopedDbConnection_init(sdc); GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix); if(gf.findFile(path).exists()) { gf.removeFile(path); } size_t len = lgf->getLength(); char *buf = new char[len]; lgf->read(buf, len, 0); gf.storeFile(buf, len, path); sdc.done(); lgf->flushed(); return 0; }