示例#1
0
int gridfs_open(const char *path, struct fuse_file_info *fi)
{
  path = fuse_to_mongo_path(path);

  if((fi->flags & O_ACCMODE) == O_RDONLY) {
    if(open_files.find(path) != open_files.end()) {
      return 0;
    }

    ScopedDbConnection sdc(*gridfs_options.conn_string);
    ScopedDbConnection_init(sdc);
    GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);

    GridFile file = gf.findFile(path);
    sdc.done();

    if(file.exists()) {
      return 0;
    }

    return -ENOENT;
  } else {
    return -EACCES;
  }
}
示例#2
0
int gridfs_open(const char *path, struct fuse_file_info *fi)
{
  path = fuse_to_mongo_path(path);

  if((fi->flags & O_ACCMODE) == O_RDONLY) {
    if(open_files.find(path) != open_files.end()) {
      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);
    GridFile file = gf.findFile(path);
    sdc.done();

    if(file.exists()) {
      return 0;
    }

    return -ENOENT;
  } else {
    return -EACCES;
  }
}
示例#3
0
/*
 * bool = gridfile:exists()
 */
static int gridfile_exists(lua_State *L) {
    GridFile *gridfile = userdata_to_gridfile(L, 1);

    lua_pushboolean(L, gridfile->exists());

    return 1;
}
示例#4
0
int gridfs_getattr(const char *path, struct stat *stbuf)
{
  int res = 0;
  memset(stbuf, 0, sizeof(struct stat));

  if(strcmp(path, "/") == 0) {
    stbuf->st_mode = S_IFDIR | 0777;
    stbuf->st_nlink = 2;
    return 0;
  }

  path = fuse_to_mongo_path(path);

  map<string,LocalGridFile*>::const_iterator file_iter;
  file_iter = open_files.find(path);

  if(file_iter != open_files.end()) {
    stbuf->st_mode = S_IFREG | 0555;
    stbuf->st_nlink = 1;
    stbuf->st_ctime = time(NULL);
    stbuf->st_mtime = time(NULL);
    stbuf->st_size = file_iter->second->getLength();
    return 0;
  }

  // HACK: This is a protective measure to ensure we don't spin off into forever if a path without a period is requested.
  if(path_depth(path) > 10) {
    return -ENOENT;
  }

  // HACK: Assumes that if the last part of the path has a '.' in it, it's the leaf of the path, and if we haven't found a match by now,
  // give up and go home. This works just dandy as long as you avoid putting periods in your 'directory' names.
  /*if(!is_leaf(path)) {
    stbuf->st_mode = S_IFDIR | 0777;
    stbuf->st_nlink = 2;
    return 0;
  }*/

  ScopedDbConnection sdc(*gridfs_options.conn_string);
  ScopedDbConnection_init(sdc);
  GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
  GridFile file = gf.findFile(path);
  sdc.done();

  if(!file.exists()) {
    return -ENOENT;
  }

  stbuf->st_mode = S_IFREG | 0555;
  stbuf->st_nlink = 1;
  stbuf->st_size = file.getContentLength();

  time_t upload_time = mongo_time_to_unix_time(file.getUploadDate());
  stbuf->st_ctime = upload_time;
  stbuf->st_mtime = upload_time;

  return 0;
}
示例#5
0
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;
}
示例#6
0
int gridfs_getxattr(const char* path, const char* name, char* value, size_t size)
{
  if(strcmp(path, "/") == 0) {
    return -ENOATTR;
  }

  path = fuse_to_mongo_path(path);
  const char* attr_name = unnamespace_xattr(name);
  if(!attr_name) {
    return -ENOATTR;
  }

  if(open_files.find(path) != open_files.end()) {
    return -ENOATTR;
  }

  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);
  sdc.done();

  if(!file.exists()) {
    return -ENOENT;
  }

  BSONObj metadata = file.getMetadata();
  if(metadata.isEmpty()) {
    return -ENOATTR;
  }

  BSONElement field = metadata[attr_name];
  if(field.eoo()) {
    return -ENOATTR;
  }

  string field_str = field.toString();
  int len = field_str.size() + 1;
  if(size == 0) {
    return len;
  } else if(size < len) {
    return -ERANGE;
  }

  memcpy(value, field_str.c_str(), len);

  return len;
}
示例#7
0
/*
 * gridfile, err = gridfs:find_file(filename)
 */
static int gridfs_find_file(lua_State *L) {
    GridFS *gridfs = userdata_to_gridfs(L, 1);
    int resultcount = 1;

    if (!lua_isnoneornil(L, 2)) {
        try {
            int type = lua_type(L, 2);
            if (type == LUA_TTABLE) {
                BSONObj obj;
                lua_to_bson(L, 2, obj);
                GridFile gridfile = gridfs->findFile(obj);
                if(gridfile.exists()) {
                    resultcount = gridfile_create(L, gridfile);
                } else {
                    lua_pushnil(L);
                    resultcount = 1;
                }
            } else {
                GridFile gridfile = gridfs->findFile(luaL_checkstring(L, 2));
                if(gridfile.exists()) {
                    resultcount = gridfile_create(L, gridfile);
                } else {
                    lua_pushnil(L);
                    resultcount = 1;
                }
            }

        } catch (std::exception &e) {
            lua_pushboolean(L, 0);
            lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "find_file", e.what());
            resultcount = 2;
        }
    }

    return resultcount;

}
示例#8
0
	int gridfs_getxattr(const char* path, const char* name, char* value, size_t size)
#endif /* __APPLE__ */
{
  if(strcmp(path, "/") == 0) {
    return -ENOATTR;
  }

  path = fuse_to_mongo_path(path);
  const char* attr_name = unnamespace_xattr(name);
  if(!attr_name) {
    return -ENOATTR;
  }

  if(open_files.find(path) != open_files.end()) {
    return -ENOATTR;
  }

  ScopedDbConnection sdc(*gridfs_options.conn_string);
  ScopedDbConnection_init(sdc);
  GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
  GridFile file = gf.findFile(path);
  sdc.done();

  if(!file.exists()) {
    return -ENOENT;
  }

  BSONObj metadata = file.getMetadata();
  if(metadata.isEmpty()) {
    return -ENOATTR;
  }

  BSONElement field = metadata[attr_name];
  if(field.eoo()) {
    return -ENOATTR;
  }

  string field_str = field.toString();
  int len = field_str.size() + 1;
  if(size == 0) {
    return len;
  } else if(size < len) {
    return -ERANGE;
  }

  memcpy(value, field_str.c_str(), len);

  return len;
}
示例#9
0
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;
}
示例#10
0
int gridfs_listxattr(const char* path, char* list, size_t size)
{
  path = fuse_to_mongo_path(path);

  if(open_files.find(path) != open_files.end()) {
    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);
  GridFile file = gf.findFile(path);
  sdc.done();

  if(!file.exists()) {
    return -ENOENT;
  }

  int len = 0;
  BSONObj metadata = file.getMetadata();
  set<string> field_set;
  metadata.getFieldNames(field_set);
  for(set<string>::const_iterator s = field_set.begin(); s != field_set.end(); s++) {
    string attr_name = namespace_xattr(*s);
    int field_len = attr_name.size() + 1;
    len += field_len;
    if(size >= len) {
      memcpy(list, attr_name.c_str(), field_len);
      list += field_len;
    }
  }

  if(size == 0) {
    return len;
  } else if(size < len) {
    return -ERANGE;
  }

  return len;
}
示例#11
0
int gridfs_listxattr(const char* path, char* list, size_t size)
{
  path = fuse_to_mongo_path(path);

  if(open_files.find(path) != open_files.end()) {
    return 0;
  }

  ScopedDbConnection sdc(*gridfs_options.conn_string);
  ScopedDbConnection_init(sdc);
  GridFS gf(sdc.conn(), gridfs_options.db, gridfs_options.prefix);
  GridFile file = gf.findFile(path);
  sdc.done();

  if(!file.exists()) {
    return -ENOENT;
  }

  int len = 0;
  BSONObj metadata = file.getMetadata();
  set<string> field_set;
  metadata.getFieldNames(field_set);
  for(set<string>::const_iterator s = field_set.begin(); s != field_set.end(); s++) {
    string attr_name = namespace_xattr(*s);
    int field_len = attr_name.size() + 1;
    len += field_len;
    if(size >= len) {
      memcpy(list, attr_name.c_str(), field_len);
      list += field_len;
    }
  }

  if(size == 0) {
    return len;
  } else if(size < len) {
    return -ERANGE;
  }

  return len;
}
示例#12
0
BOOL CMongoDB::GetFileFromMongoDB(const string& strFileName, const string&strSavePath, string& strErr)
{
	BOOL bRet = TRUE;
	string strTemp, strTempPath;
	int nStart(0);
	if (strFileName.length() <= 0)
		strErr = "FileName is empty!";
	gridfs_offset size(0), afsize(0);
	string strPath(strSavePath);

	if (connect.isStillConnected() && m_bConnect)
	{
		GridFS fs(connect, m_strDBName);
		auto pos = strFileName.find(";", nStart);
		while ((pos > 0) && (pos != strFileName.npos))
		{
			strTemp = strFileName.substr(nStart, pos - nStart);
			nStart = pos + 1;
			if (strTemp.length() > 0)
			{
				GridFile file = fs.findFileByName(strTemp);
				if (file.exists())
				{
					strPath = strSavePath;
					strPath.append("\\");
					strPath.append(strTemp);
					size = file.getContentLength();
					afsize = file.write(strPath);
				}
				else strErr = "File not exists!";
			}
			if (size > 0 && size == afsize)
				bRet = bRet ? TRUE : FALSE;
			else bRet = FALSE;
			pos = strFileName.find(";", nStart);
		}
	}
	return bRet;
}
示例#13
0
int LocalMemoryGridFile::openRemote(int fileFlags) {
	trace() << " -> LocalMemoryGridFile::openRemote {fileFlags: " << fileFlags << "}" << endl;
	try {
		ScopedDbConnection dbc(globalFSOptions._connectString);
		GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);
		GridFile origGridFile = gridFS.findFile(BSON("filename" << _filename));

		if (!origGridFile.exists()) {
			dbc.done();
			error() << "Requested file not found for opening from remote {file: " << _filename << "}" << endl;
			return -EBADF;
		}

		if (origGridFile.getContentLength() > globalFSOptions._maxMemFileSize) {
			// Don't support opening files of size > MAX_MEMORY_FILE_CAPACITY in R/W mode
			error() << "Requested file length is beyond supported length for in-memory files {file: " 
				<< _filename << ", length: {requested: " << origGridFile.getContentLength()
				<< ", max-supported: " << globalFSOptions._maxMemFileSize << "} }" << endl;
			return -EROFS;
		}

		if (!initLocalBuffers(origGridFile)) {
			error() << "Failed to initialize local buffers {file: " << _filename << "}" << endl;
			return -EIO;
		}

		_dirty = false;
		dbc.done();
	} catch (DBException& e) {
		// Something failed in getting the file from GridFS
		error() << "Caught exception in getting remote file for flush {filename: " << _filename
			<< ", code: " << e.getCode() << ", what: " << e.what() << ", exception: " << e.toString() 
			<< "}" << endl;
		return -EIO;
	}

	return 0;
}
示例#14
0
文件: files.cpp 项目: whachoe/mongo
    int run(){
        string cmd = getParam( "command" );
        if ( cmd.size() == 0 ){
            cerr << "ERROR: need command" << endl << endl;
            printHelp(cout);
            return -1;
        }

        GridFS g( conn() , _db );
        auth();

        string filename = getParam( "file" );

        if ( cmd == "list" ){
            BSONObjBuilder b;
            if ( filename.size() )
                b.appendRegex( "filename" , ( (string)"^" + filename ).c_str() );
            display( &g , b.obj() );
            return 0;
        }

        if ( filename.size() == 0 ){
            cerr << "ERROR: need a filename" << endl << endl;
            printHelp(cout);
            return -1;
        }

        if ( cmd == "search" ){
            BSONObjBuilder b;
            b.appendRegex( "filename" , filename.c_str() );
            display( &g , b.obj() );
            return 0;
        }

        if ( cmd == "get" ){
            GridFile f = g.findFile( filename );
            if ( ! f.exists() ){
                cerr << "ERROR: file not found" << endl;
                return -2;
            }

            string out = getParam("local", f.getFilename());
            f.write( out );

            if (out != "-")
                cout << "done write to: " << out << endl;

            return 0;
        }

        if ( cmd == "put" ){
            const string& infile = getParam("local", filename);
            const string& type = getParam("type", "");

            BSONObj file = g.storeFile(infile, filename, type);
            cout << "added file: " << file << endl;

            if (hasParam("replace")){
                auto_ptr<DBClientCursor> cursor = conn().query(_db+".fs.files", BSON("filename" << filename << "_id" << NE << file["_id"] ));
                while (cursor->more()){
                    BSONObj o = cursor->nextSafe();
                    conn().remove(_db+".fs.files", BSON("_id" << o["_id"]));
                    conn().remove(_db+".fs.chunks", BSON("_id" << o["_id"]));
                    cout << "removed file: " << o << endl;
                }

            }

            conn().getLastError();
            cout << "done!";
            return 0;
        }

        if ( cmd == "delete" ){
            g.removeFile(filename);
            conn().getLastError();
            cout << "done!";
            return 0;
        }

        cerr << "ERROR: unknown command '" << cmd << "'" << endl << endl;
        printHelp(cout);
        return -1;
    }
示例#15
0
文件: files.cpp 项目: 504com/mongo
    int run() {
        if (mongoFilesGlobalParams.command.size() == 0) {
            cerr << "ERROR: need command" << endl << endl;
            printHelp(cout);
            return -1;
        }

        GridFS g(conn(), toolGlobalParams.db);

        if (mongoFilesGlobalParams.command == "list") {
            BSONObjBuilder b;
            if (mongoFilesGlobalParams.gridFSFilename.size()) {
                b.appendRegex( "filename" , (string)"^" +
                               pcrecpp::RE::QuoteMeta(mongoFilesGlobalParams.gridFSFilename) );
            }
            
            display( &g , b.obj() );
            return 0;
        }

        if (mongoFilesGlobalParams.gridFSFilename.size() == 0) {
            cerr << "ERROR: need a filename" << endl << endl;
            printHelp(cout);
            return -1;
        }

        if (mongoFilesGlobalParams.command == "search") {
            BSONObjBuilder b;
            b.appendRegex("filename", mongoFilesGlobalParams.gridFSFilename);
            display( &g , b.obj() );
            return 0;
        }

        if (mongoFilesGlobalParams.command == "get") {
            GridFile f = g.findFile(mongoFilesGlobalParams.gridFSFilename);
            if ( ! f.exists() ) {
                cerr << "ERROR: file not found" << endl;
                return -2;
            }

            f.write(mongoFilesGlobalParams.localFile);

            if (mongoFilesGlobalParams.localFile != "-") {
                toolInfoOutput() << "done write to: " << mongoFilesGlobalParams.localFile
                                 << std::endl;
            }

            return 0;
        }

        if (mongoFilesGlobalParams.command == "put") {
            BSONObj file = g.storeFile(mongoFilesGlobalParams.localFile,
                                       mongoFilesGlobalParams.gridFSFilename,
                                       mongoFilesGlobalParams.contentType);
            toolInfoOutput() << "added file: " << file << std::endl;

            if (mongoFilesGlobalParams.replace) {
                auto_ptr<DBClientCursor> cursor =
                    conn().query(toolGlobalParams.db + ".fs.files",
                                 BSON("filename" << mongoFilesGlobalParams.gridFSFilename
                                                 << "_id" << NE << file["_id"] ));
                while (cursor->more()) {
                    BSONObj o = cursor->nextSafe();
                    conn().remove(toolGlobalParams.db + ".fs.files", BSON("_id" << o["_id"]));
                    conn().remove(toolGlobalParams.db + ".fs.chunks", BSON("_id" << o["_id"]));
                    toolInfoOutput() << "removed file: " << o << std::endl;
                }

            }

            conn().getLastError();
            toolInfoOutput() << "done!" << std::endl;
            return 0;
        }

        if (mongoFilesGlobalParams.command == "delete") {
            g.removeFile(mongoFilesGlobalParams.gridFSFilename);
            conn().getLastError();
            toolInfoOutput() << "done!" << std::endl;
            return 0;
        }

        cerr << "ERROR: unknown command '" << mongoFilesGlobalParams.command << "'" << endl << endl;
        printHelp(cout);
        return -1;
    }
示例#16
0
int LocalMemoryGridFile::flush() {
	trace() << " -> LocalMemoryGridFile::flush {file: " << _filename << "}" << endl;
	if (!_dirty) {
		// Since, there are no dirty chunks, this does not need a flush
		info() << "buffers are not dirty.. need not flush {filename: " << _filename << "}" << endl;
		return 0;
	}

	size_t bufferLen = 0;
	boost::shared_array<char> buffer = createFlushBuffer(bufferLen);
	if (!buffer.get() && bufferLen > 0) {
		// Failed to create flush buffer
		return -ENOMEM;
	}

	// Get the existing gridfile from GridFS to get metadata and delete the
	// file from the system
	try {
		ScopedDbConnection dbc(globalFSOptions._connectString);
		GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);
		GridFile origGridFile = gridFS.findFile(BSON("filename" << _filename));

		if (!origGridFile.exists()) {
			dbc.done();
			warn() << "Requested file not found for flushing back data {file: " << _filename << "}" << endl;
			return -EBADF;
		}

		//TODO: Make checks for appropriate object correctness
		//i.e. do not update anything that is not a Regular File
		//Check what happens in case of a link

		gridFS.removeFile(_filename);
		trace() << "Removing the current file from GridFS {file: " << _filename << "}" << endl;
		//TODO: Check for remove status if that was successfull or not
		//TODO: Rather have an update along with active / passive flag for the
		//file

		try {
			GridFS gridFS(dbc.conn(), globalFSOptions._db, globalFSOptions._collPrefix);

			// Create an empty file to signify the file creation and open a local file for the same
			trace() << "Adding new file to GridFS {file: " << _filename << "}" << endl;
			BSONObj fileObj = gridFS.storeFile(buffer.get(), bufferLen, _filename);
			if (!fileObj.isValid()) {
				warn() << "Failed to save file object in data flush {file: " << _filename << "}" << std::endl;
				dbc.done();
				return -EBADF;
			}

			// Update the last updated date for the document
			BSONObj metadata = origGridFile.getMetadata();
			BSONElement fileObjId = fileObj.getField("_id");
			dbc->update(globalFSOptions._filesNS, BSON("_id" << fileObjId.OID()), 
					BSON("$set" << BSON(
								"uploadDate" << origGridFile.getUploadDate() 
								<< "metadata.type" << "file"
								<< "metadata.filename" << mgridfs::getPathBasename(_filename)
								<< "metadata.directory" << mgridfs::getPathDirname(_filename)
								<< "metadata.lastUpdated" << jsTime()
								<< "metadata.uid" << metadata["uid"]
								<< "metadata.gid" << metadata["gid"]
								<< "metadata.mode" << metadata["mode"]
							)
						)
					);
	} catch (DBException& e) {
			error() << "Caught exception in saving remote file in flush {code: " << e.getCode() << ", what: " << e.what()
				<< ", exception: " << e.toString() << "}" << endl;
			return -EIO;
		}

		dbc.done();
	} catch (DBException& e) {
		// Something failed in getting the file from GridFS
		error() << "Caught exception in getting remote file for flush {code: " << e.getCode() << ", what: " << e.what()
			<< ", exception: " << e.toString() << "}" << endl;
		return -EIO;
	}

	_dirty = false;
	debug() << "Completed flushing the file content to GridFS {file: " << _filename << "}" << endl;
	return 0;
}
示例#17
0
int
main(int argc, char **argv)
{
  ArgumentParser argp(argc, argv, "ho:fd:c:");
  if (argp.has_arg("h")) {
    print_usage(argv[0]);
    exit(0);
  }

  const std::vector<const char *> &items = argp.items();

  std::string output_dir = "tmp/";
  std::string database = "fflog";
  std::string collection;
  std::string query_coll;
  bool filename_indexed = ! argp.has_arg("f");

  std::vector<std::pair<long long, long long> > times;

  if (argp.has_arg("o")) {
    output_dir = argp.arg("o");
    if (output_dir[output_dir.length() - 1] != '/') {
      output_dir += "/";
    }
  }
  if (argp.has_arg("d")) {
    database = argp.arg("d");
  }
  if (argp.has_arg("c")) {
    collection = argp.arg("c");
  } else {
    print_usage(argv[0]);
    printf("No collection given\n");
    exit(-1);
  }

  query_coll = database + "." + collection;

  if (items.empty()) {
    times.push_back(std::make_pair(0L, std::numeric_limits<long long>::max()));
  } else {
    for (unsigned int i = 0; i < items.size(); ++i) {
      std::string item = items[i];
      std::string::size_type dotpos = item.find("..");
      if (dotpos == std::string::npos) {
	// singular timestamp
	long int ts = argp.parse_item_int(i);
	times.push_back(std::make_pair(ts, ts));	
      } else {
	// range
	std::string first_ts, second_ts;
	first_ts = item.substr(0, dotpos);
	second_ts = item.substr(dotpos + 2);
	times.push_back(std::make_pair(StringConversions::to_long(first_ts),
				       StringConversions::to_long(second_ts)));
      }
    }
  }

  unsigned int image_n = 0;

  DBClientConnection *mongodb_client =
    new DBClientConnection(/* auto reconnect */ true);
  std::string errmsg;
  mongodb_client->connect("localhost", errmsg);

  GridFS *gridfs = new GridFS(*mongodb_client, "fflog");


  for (unsigned int i = 0; i < times.size(); ++i) {
    Query q;

    if (times[i].first == times[i].second) {
      printf("Querying for timestamp %lli\n", times[i].first);
      q = QUERY("timestamp" << times[i].first).sort("timestamp", 1);
    } else {
      printf("Querying for range %lli..%lli\n", times[i].first, times[i].second);
      q = QUERY("timestamp"
		<< mongo::GTE << times[i].first
		<< mongo::LTE << times[i].second)
	.sort("timestamp", 1);
    }

#if __cplusplus >= 201103L
    std::unique_ptr<mongo::DBClientCursor> cursor =
      mongodb_client->query(query_coll, q);
#else
    std::auto_ptr<mongo::DBClientCursor> cursor =
      mongodb_client->query(query_coll, q);
#endif

    while (cursor->more()) {
      BSONObj doc = cursor->next();
      
      BSONObj imgdoc = doc.getObjectField("image");
      if (imgdoc["colorspace"].String() == "RGB") {
	std::string filename = imgdoc.getFieldDotted("data.filename").String();
	long filesize = imgdoc.getFieldDotted("data.length").numberLong();
	std::string image_id = imgdoc["image_id"].String();

	std::string out_filename;
	char *fntmp;
	if (filename_indexed) {
	  if (asprintf(&fntmp, "%s%s-%08d.png", output_dir.c_str(),
		       image_id.c_str(), image_n++) != -1)
	  {
	    out_filename = fntmp;
	    free(fntmp);
	  }
	} else {
	  if (asprintf(&fntmp, "%s%s.png", output_dir.c_str(),
		       filename.c_str()) != -1)
	  {
	    out_filename = fntmp;
	    free(fntmp);
	  }
	  ++image_n;
	}

	printf("Restoring RGB image %s (%s)\n", filename.c_str(), out_filename.c_str());

	GridFile file = gridfs->findFile(filename);
	if (! file.exists()) {
	  printf("File %s does not exist\n", filename.c_str());
	  continue;
	}

	unsigned int width  = imgdoc["width"].Int();
	unsigned int height = imgdoc["height"].Int();

	if (colorspace_buffer_size(RGB, width, height) != (size_t)filesize) {
	  printf("Buffer size mismatch (DB %li vs. exp. %zu)\n",
		 filesize, colorspace_buffer_size(RGB, width, height));
	  continue;
	}

	unsigned char *buffer = malloc_buffer(RGB, width, height);

	unsigned char *tmp = buffer;
	for (int c = 0; c < file.getNumChunks(); ++c) {
	  mongo::GridFSChunk chunk = file.getChunk(c);
	  int len = 0;
	  const char *chunk_data = chunk.data(len);
	  memcpy(tmp, chunk_data, len);
	  tmp += len;
	}

	PNGWriter writer(out_filename.c_str(), width, height);
	writer.set_buffer(RGB, buffer);
	writer.write();
	
	free(buffer);
      }
    }
  }

  delete gridfs;
  delete mongodb_client;

}