Esempio n. 1
0
int SLNSessionGetFileInfo(SLNSessionRef const session, strarg_t const URI, SLNFileInfo *const info) {
	DB_env *db = NULL;
	int rc = SLNSessionDBOpen(session, SLN_RDONLY, &db);
	if(rc < 0) return rc;
	DB_txn *txn = NULL;
	rc = db_txn_begin(db, NULL, DB_RDONLY, &txn);
	if(rc < 0) {
		SLNSessionDBClose(session, &db);
		return rc;
	}

	DB_cursor *cursor;
	rc = db_txn_cursor(txn, &cursor);
	assert(!rc);

	DB_range fileIDs[1];
	SLNURIAndFileIDRange1(fileIDs, txn, URI);
	DB_val URIAndFileID_key[1];
	rc = db_cursor_firstr(cursor, fileIDs, URIAndFileID_key, NULL, +1);
	DB_val file_val[1];
	if(rc >= 0) {
		strarg_t URI2;
		uint64_t fileID;
		SLNURIAndFileIDKeyUnpack(URIAndFileID_key, txn, &URI2, &fileID);
		assert(0 == strcmp(URI, URI2));
		if(info) {
			DB_val fileID_key[1];
			SLNFileByIDKeyPack(fileID_key, txn, fileID);
			rc = db_get(txn, fileID_key, file_val);
		}
	}
	if(rc < 0) {
		db_txn_abort(txn); txn = NULL;
		SLNSessionDBClose(session, &db);
		return rc;
	}

	if(info) {
		// Clear padding for later assert_zeroed.
		memset(info, 0, sizeof(*info));

		strarg_t const internalHash = db_read_string(file_val, txn);
		strarg_t const type = db_read_string(file_val, txn);
		uint64_t const size = db_read_uint64(file_val);
		info->hash = strdup(internalHash);
		info->path = SLNRepoCopyInternalPath(SLNSessionGetRepo(session), internalHash);
		info->type = strdup(type);
		info->size = size;
		if(!info->hash || !info->path || !info->type) {
			SLNFileInfoCleanup(info);
			db_txn_abort(txn); txn = NULL;
			SLNSessionDBClose(session, &db);
			return DB_ENOMEM;
		}
	}

	db_txn_abort(txn); txn = NULL;
	SLNSessionDBClose(session, &db);
	return 0;
}
Esempio n. 2
0
int SLNFilterSeekToPosition(SLNFilterRef const filter, SLNFilterPosition const *const pos, DB_txn *const txn) {
	if(!pos->URI) {
		SLNFilterSeek(filter, pos->dir, pos->sortID, pos->fileID);
		if(valid(pos->fileID)) SLNFilterStep(filter, pos->dir);
		return 0;
	}

	DB_cursor *cursor = NULL;
	int rc = db_txn_cursor(txn, &cursor);
	if(rc < 0) return rc;

	DB_range range[1];
	DB_val key[1];
	SLNURIAndFileIDRange1(range, txn, pos->URI);
	rc = db_cursor_firstr(cursor, range, key, NULL, +1);
	if(rc < 0) return rc;

	// Test that this URI gives us a unique, unambiguous position.
	// This is guaranteed for the internal hash and effectively
	// guaranteed for other cryptographic hashes, but may not be
	// true for other hash algorithms.
	rc = db_cursor_nextr(cursor, range, NULL, NULL, +1);
	if(rc >= 0) return DB_KEYEXIST;
	if(DB_NOTFOUND != rc) return rc;

	strarg_t u;
	uint64_t fileID;
	SLNURIAndFileIDKeyUnpack(key, txn, &u, &fileID);
	assert(0 == strcmp(pos->URI, u));

	SLNAgeRange const ages = SLNFilterFullAge(filter, fileID);
	if(!valid(ages.min) || ages.min > ages.max) return DB_NOTFOUND;
	uint64_t const sortID = ages.min;

	SLNFilterSeek(filter, pos->dir, sortID, fileID);
	SLNFilterStep(filter, pos->dir); // Start just before/after the URI.
	// TODO: Stepping is almost assuredly wrong if the URI doesn't match
	// the filter. We should check if our seek was a direct hit, and
	// only step if it was.
	return 0;
}
Esempio n. 3
0
int SLNSessionGetSubmittedFile(SLNSessionRef const session, DB_txn *const txn, strarg_t const URI) {
	uint64_t const sessionID = SLNSessionGetID(session);
	DB_cursor *cursor = NULL;
	int rc = db_txn_cursor(txn, &cursor);
	if(rc < 0) return rc;
	DB_range range[1];
	DB_val filekey[1];
	SLNURIAndFileIDRange1(range, txn, URI);
	rc = db_cursor_firstr(cursor, range, filekey, NULL, +1);
	if(rc < 0) return rc;
	for(; rc >= 0; rc = db_cursor_nextr(cursor, range, filekey, NULL, +1)) {
		strarg_t u;
		uint64_t fileID;
		SLNURIAndFileIDKeyUnpack(filekey, txn, &u, &fileID);

		DB_val sessionkey[1];
		SLNFileIDAndSessionIDKeyPack(sessionkey, txn, fileID, sessionID);
		rc = db_cursor_seek(cursor, sessionkey, NULL, 0);
		if(DB_NOTFOUND == rc) continue;
		return rc;
	}
	return SLN_NOSESSION;
}
Esempio n. 4
0
int SLNSessionSetSubmittedFile(SLNSessionRef const session, DB_txn *const txn, strarg_t const URI) {
	uint64_t const sessionID = SLNSessionGetID(session);
	DB_cursor *cursor = NULL;
	int rc = db_txn_cursor(txn, &cursor);
	if(rc < 0) return rc;

	DB_range range[1];
	DB_val filekey[1];
	SLNURIAndFileIDRange1(range, txn, URI);
	rc = db_cursor_firstr(cursor, range, filekey, NULL, +1);
	if(rc < 0) return rc;

	strarg_t u;
	uint64_t fileID;
	SLNURIAndFileIDKeyUnpack(filekey, txn, &u, &fileID);

	DB_val sessionkey[1], null[1];
	SLNFileIDAndSessionIDKeyPack(sessionkey, txn, fileID, sessionID);
	db_nullval(null);
	rc = db_put(txn, sessionkey, null, 0);
	if(rc < 0) return rc;

	return rc;
}