Skip to content

Commit

Permalink
feat: hide dot files
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotonQuantum committed Jan 13, 2024
1 parent a34ee97 commit c9f17ab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
19 changes: 16 additions & 3 deletions rsync-gateway/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ pub async fn main_handler(

let locale = select_locale(&accepted_language);

let namespace = &endpoint.namespace;
let s3_prefix = &endpoint.s3_prefix;
let Endpoint {
namespace,
s3_prefix,
list_hidden,
..
} = &**endpoint;
let Some(Revision {
revision,
generated_at,
Expand All @@ -84,7 +88,16 @@ pub async fn main_handler(
let resolved = match cache
.get_or_insert(
&path,
resolve(namespace, &path, revision, s3_prefix, &**db, &op).boxed_local(),
resolve(
namespace,
&path,
revision,
s3_prefix,
*list_hidden,
&**db,
&op,
)
.boxed_local(),
)
.await
{
Expand Down
4 changes: 4 additions & 0 deletions rsync-gateway/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub struct Endpoint {
/// But it must be unique across all endpoints.
#[doku(example = "fedora")]
pub namespace: String,
/// Whether to list hidden files.
#[serde(default)]
#[doku(example = "false")]
pub list_hidden: bool,
}

fn de_trim_end_slash<'de, D>(de: D) -> Result<String, D::Error>
Expand Down
6 changes: 4 additions & 2 deletions rsync-gateway/src/path_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ pub async fn resolve<'a>(
path: &[u8],
revision: i32,
s3_prefix: &str,
list_hidden: bool,
db: impl Acquire<'a, Database = Postgres> + Clone,
op: &Operator,
) -> Result<Resolved> {
Ok(match realpath(path, revision, db.clone()).await {
Ok(Target::Directory(path)) => {
let resolved = resolve_listing(&path, revision, db).await?;
let resolved = resolve_listing(&path, revision, list_hidden, db).await?;
increment_counter!(COUNTER_RESOLVED_LISTING);
resolved
}
Expand Down Expand Up @@ -93,9 +94,10 @@ pub async fn resolve<'a>(
async fn resolve_listing<'a>(
path: &[u8],
revision: i32,
list_hidden: bool,
db: impl Acquire<'a, Database = Postgres>,
) -> Result<Resolved> {
let mut entries = list_directory(path, revision, db).await?;
let mut entries = list_directory(path, revision, list_hidden, db).await?;
entries.shrink_to_fit(); // shrink as much as we can to reduce cache memory usage
Ok(Resolved::Directory { entries })
}
Expand Down
24 changes: 18 additions & 6 deletions rsync-gateway/src/pg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::future::ready;

use bstr::ByteSlice;
use chrono::{DateTime, Utc};
use eyre::Result;
use futures::TryStreamExt;
use sqlx::postgres::types::PgInterval;
use sqlx::types::BigDecimal;
use sqlx::{Acquire, Postgres};
Expand Down Expand Up @@ -164,24 +167,33 @@ impl From<RawResolveEntry> for ListingEntry {
pub async fn list_directory<'a>(
path: &[u8],
revision: i32,
list_hidden: bool,
db: impl Acquire<'a, Database = Postgres>,
) -> Result<Vec<ListingEntry>> {
let executor = &mut *db.acquire().await?;
Ok(if path.is_empty() {
sqlx::query_file_as!(RawResolveEntry, "../sqls/list_root.sql", revision)
.map(Into::into)
.fetch_all(&mut *db.acquire().await?)
.await?
.map(ListingEntry::from)
.fetch(executor)
} else {
sqlx::query_file_as!(
RawResolveEntry,
"../sqls/list_directory.sql",
path,
revision
)
.map(Into::into)
.fetch_all(&mut *db.acquire().await?)
.await?
.map(ListingEntry::from)
.fetch(executor)
}
.try_filter(|entry| {
ready(if list_hidden {
true
} else {
!entry.filename.starts_with(b".")
})
})
.try_collect()
.await?)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion rsync-gateway/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::templates::{
};

impl Resolved {
/// Render the resolved result to a HTTP response.
/// Render the resolved result to an HTTP response.
pub fn to_responder(
&self,
req_path: &[u8],
Expand Down
1 change: 1 addition & 0 deletions rsync-gateway/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ mod db_required {
s3_bucket: String::new(),
s3_prefix: namespace.clone(),
namespace: namespace.clone(),
list_hidden: false
},
},
};
Expand Down

0 comments on commit c9f17ab

Please sign in to comment.