Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nextcloud-config-parser = "0.14.1"
url = "2.5.4"
clap = { version = "4.5.43", features = ["derive"] }
sd-notify = { version = "0.4.5", optional = true }
md5 = "0.8.0"

[dev-dependencies]
mini-redis = "0.4.1"
Expand Down
24 changes: 19 additions & 5 deletions src/storage_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

use crate::error::DatabaseError;
use crate::metrics::METRICS;
use crate::{Result, UserId};
use ahash::RandomState;
use dashmap::mapref::one::Ref;
use dashmap::DashMap;
use log::debug;
use md5;
use rand::{thread_rng, Rng};
use sqlx::any::AnyConnectOptions;
use sqlx::{query_as, Any, AnyPool, FromRow};
use std::collections::HashMap;
use std::time::Instant;
use tokio::time::Duration;

#[derive(Debug, Clone, FromRow)]
pub struct UserStorageAccess {
#[sqlx(rename = "user_id")]
user: UserId,
#[sqlx(rename = "path")]
root: String,
#[sqlx(rename = "path_hash")]
root_hash: String,
}

struct CachedAccess {
Expand Down Expand Up @@ -86,12 +87,25 @@ impl StorageMapping {
storage: u32,
path: &str,
) -> Result<impl Iterator<Item = UserId>, DatabaseError> {
let mut parents: HashMap<String, ()> = HashMap::new();
parents.insert(format!("{:x}", md5::compute("")), ());

let mut current_path = "".to_string();
for name in path.split("/") {
if current_path.is_empty() {
current_path = name.to_string();
} else {
current_path = format!("{}/{}", current_path.clone(), name);
}
parents.insert(format!("{:x}", md5::compute(&current_path)), ());
}

let cached = self.get_storage_mapping(storage).await?;
Ok(cached
.access
.iter()
.filter_map(move |access| {
if path.starts_with(&access.root) {
if parents.contains_key(&access.root_hash) {
Some(access.user.clone())
} else {
None
Expand All @@ -108,7 +122,7 @@ impl StorageMapping {
debug!("querying storage mapping for {storage}");
let users = query_as::<Any, UserStorageAccess>(&format!(
"\
SELECT user_id, path \
SELECT user_id, path_hash \
FROM {prefix}mounts \
INNER JOIN {prefix}filecache ON root_id = fileid \
WHERE storage_id = {storage}",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Services {
.await
.expect("Failed to connect sqlite database");

sqlx::query("CREATE TABLE oc_filecache(fileid BIGINT, path TEXT)")
sqlx::query("CREATE TABLE oc_filecache(fileid BIGINT, path_hash TEXT)")
.execute(&db)
.await
.unwrap();
Expand Down Expand Up @@ -237,9 +237,9 @@ impl Services {
}

async fn add_filecache_item(&self, fileid: u32, path: &str) {
sqlx::query("INSERT INTO oc_filecache(fileid, path) VALUES(?, ?)")
sqlx::query("INSERT INTO oc_filecache(fileid, path_hash) VALUES(?, ?)")
.bind(fileid as i64)
.bind(path)
.bind(format!("{:x}", md5::compute(path)))
.execute(&self.db)
.await
.unwrap();
Expand Down
Loading