Skip to content

Commit

Permalink
fix: Stat root should return a dir object (#143)
Browse files Browse the repository at this point in the history
* fix: Stat root should return a dir object

Signed-off-by: Xuanwo <github@xuanwo.io>

* Add behavior comment for stat

Signed-off-by: Xuanwo <github@xuanwo.io>

* Stat / should also works

Signed-off-by: Xuanwo <github@xuanwo.io>

* Fix check

Signed-off-by: Xuanwo <github@xuanwo.io>

* Enable debug log

Signed-off-by: Xuanwo <github@xuanwo.io>

* Make sure path has been normalized

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored Mar 14, 2022
1 parent 2a1bdcd commit d8a830b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ jobs:
args: --no-fail-fast
env:
RUST_TEST_THREADS: '2'
RUST_LOG: ERROR
RUST_LOG: DEBUG
RUST_BACKTRACE: full
1 change: 1 addition & 0 deletions .github/workflows/service_test_fs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ jobs:
run: cargo test fs
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_FS_TEST: on
OPENDAL_FS_ROOT: /tmp
1 change: 1 addition & 0 deletions .github/workflows/service_test_memory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ jobs:
run: cargo test memory
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_MEMORY_TEST: on
3 changes: 3 additions & 0 deletions .github/workflows/service_test_s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
run: cargo test s3
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_S3_TEST: ${{ secrets.OPENDAL_S3_TEST }}
OPENDAL_S3_ROOT: ${{ secrets.OPENDAL_S3_ROOT }}
OPENDAL_S3_BUCKET: ${{ secrets.OPENDAL_S3_BUCKET }}
Expand Down Expand Up @@ -56,6 +57,7 @@ jobs:
run: cargo test s3
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_S3_TEST: on
OPENDAL_S3_BUCKET: test
OPENDAL_S3_ENDPOINT: "http://127.0.0.1:9000"
Expand Down Expand Up @@ -93,6 +95,7 @@ jobs:
run: cargo test s3
env:
RUST_BACKTRACE: full
RUST_LOG: debug
OPENDAL_S3_TEST: on
OPENDAL_S3_BUCKET: test
OPENDAL_S3_ENDPOINT: "http://127.0.0.1:9000"
7 changes: 7 additions & 0 deletions src/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ pub trait Accessor: Send + Sync + Debug {
unimplemented!()
}
/// Invoke the `stat` operation on the specified path.
///
/// ## Behavior
///
/// - `Stat` empty path means stat backend's root path.
/// - `Stat` a path endswith "/" means stating a dir.
/// - On fs, an error could return if not a dir.
/// - On s3 alike backends, a dir object will return no matter it exist or not.
async fn stat(&self, args: &OpStat) -> Result<Metadata> {
let _ = args;
unimplemented!()
Expand Down
2 changes: 1 addition & 1 deletion src/services/memory/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Accessor for Backend {
async fn stat(&self, args: &OpStat) -> Result<Metadata> {
let path = Backend::normalize_path(&args.path);

if path.ends_with('/') {
if path.ends_with('/') || path.is_empty() {
let mut meta = Metadata::default();
meta.set_path(&path)
.set_mode(ObjectMode::DIR)
Expand Down
13 changes: 13 additions & 0 deletions src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl Backend {

/// get_rel_path will return the relative path of the given path in the s3 format.
pub(crate) fn get_rel_path(&self, path: &str) -> String {
let path = Backend::normalize_path(path);
let path = format!("/{}", path);

match path.strip_prefix(&self.root) {
Expand Down Expand Up @@ -441,6 +442,18 @@ impl Accessor for Backend {
let p = self.get_abs_path(&args.path);
info!("object {} stat start", &p);

// Stat root always returns a DIR.
if self.get_rel_path(&p).is_empty() {
let mut m = Metadata::default();
m.set_path(&args.path);
m.set_content_length(0);
m.set_mode(ObjectMode::DIR);
m.set_complete();

info!("backed root object stat finished");
return Ok(m);
}

let meta = self
.client
.head_object()
Expand Down
12 changes: 12 additions & 0 deletions tests/behavior/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl BehaviorTest {

pub async fn run(&mut self) -> Result<()> {
self.test_normal().await?;
self.test_stat_root().await?;

Ok(())
}
Expand Down Expand Up @@ -126,6 +127,17 @@ impl BehaviorTest {
Ok(())
}

/// Root should be able to stat and returns DIR.
async fn test_stat_root(&mut self) -> Result<()> {
let meta = self.op.object("").metadata().await?;
assert_eq!(meta.mode(), ObjectMode::DIR);

let meta = self.op.object("/").metadata().await?;
assert_eq!(meta.mode(), ObjectMode::DIR);

Ok(())
}

fn gen_bytes(&mut self) -> (Vec<u8>, usize) {
let size = self.rng.gen_range(1..4 * 1024 * 1024);
let mut content = vec![0; size as usize];
Expand Down

0 comments on commit d8a830b

Please # to comment.