Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setOwner support #117

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
8 changes: 7 additions & 1 deletion python/python/hdfs_native/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io
from typing import Iterator
from typing import Iterator, Optional
from typing_extensions import Buffer

from ._internal import *
Expand Down Expand Up @@ -113,3 +113,9 @@ def set_times(self, path: str, mtime: int, atime: int) -> None:
Changes the modification time and access time of the file at `path` to `mtime` and `atime`, respectively.
"""
return self.inner.set_times(path, mtime, atime)

def set_owner(self, path: str, owner: Optional[str] = None, group: Optional[str] = None) -> None:
"""
Sets the owner and/or group for the file at `path`
"""
return self.inner.set_owner(path, owner, group)
4 changes: 3 additions & 1 deletion python/python/hdfs_native/_internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ class RawClient:

def delete(self, path: str, recursive: bool) -> bool: ...

def set_times(self, path: str, mtime: int, atime: int) -> None: ...
def set_times(self, path: str, mtime: int, atime: int) -> None: ...

def set_owner(self, path: str, owner: Optional[str], group: Optional[str]) -> None: ...
9 changes: 9 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ impl RawClient {
pub fn set_times(&self, path: &str, mtime: u64, atime: u64) -> PyHdfsResult<()> {
Ok(self.rt.block_on(self.inner.set_times(path, mtime, atime))?)
}

pub fn set_owner(
&self,
path: &str,
owner: Option<&str>,
group: Option<&str>,
) -> PyHdfsResult<()> {
Ok(self.rt.block_on(self.inner.set_owner(path, owner, group))?)
}
}

/// A Python module implemented in Rust.
Expand Down
17 changes: 16 additions & 1 deletion python/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,20 @@ def test_integration(minidfs: str):
assert file_info.modification_time == mtime
assert file_info.access_time == atime

client.delete("/testfile", False)

client.set_owner("/testfile", "testuser", "testgroup")
file_info = client.get_file_info("/testfile")
assert file_info.owner == "testuser"
assert file_info.group == "testgroup"

client.set_owner("/testfile", owner="testuser2")
file_info = client.get_file_info("/testfile")
assert file_info.owner == "testuser2"
assert file_info.group == "testgroup"

client.set_owner("/testfile", group="testgroup2")
file_info = client.get_file_info("/testfile")
assert file_info.owner == "testuser2"
assert file_info.group == "testgroup2"

client.delete("/testfile", False)
14 changes: 14 additions & 0 deletions rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,20 @@ impl Client {
.await?;
Ok(())
}

/// Optionally sets the owner and group for a file.
pub async fn set_owner(
&self,
path: &str,
owner: Option<&str>,
group: Option<&str>,
) -> Result<()> {
let (link, resolved_path) = self.mount_table.resolve(path);
link.protocol
.set_owner(&resolved_path, owner, group)
.await?;
Ok(())
}
}

impl Default for Client {
Expand Down
24 changes: 24 additions & 0 deletions rust/src/hdfs/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,30 @@ impl NamenodeProtocol {
debug!("setTimes response: {:?}", &decoded);
Ok(decoded)
}

pub(crate) async fn set_owner(
&self,
src: &str,
owner: Option<&str>,
group: Option<&str>,
) -> Result<hdfs::SetOwnerResponseProto> {
let message = hdfs::SetOwnerRequestProto {
src: src.to_string(),
username: owner.map(str::to_string),
groupname: group.map(str::to_string),
};

debug!("setOwner request: {:?}", &message);

let response = self
.proxy
.call("setOwner", message.encode_length_delimited_to_vec())
.await?;

let decoded = hdfs::SetOwnerResponseProto::decode_length_delimited(response)?;
debug!("setOwner response: {:?}", &decoded);
Ok(decoded)
}
}

impl Drop for NamenodeProtocol {
Expand Down
33 changes: 33 additions & 0 deletions rust/tests/test_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ mod test {
// We use writing to create files, so do this after
test_recursive_listing(&client).await?;
test_set_times(&client).await?;
test_set_owner(&client).await?;

Ok(())
}
Expand Down Expand Up @@ -348,6 +349,38 @@ mod test {
assert_eq!(file_info.modification_time, mtime);
assert_eq!(file_info.access_time, atime);

client.delete("/test", false).await?;

Ok(())
}

async fn test_set_owner(client: &Client) -> Result<()> {
client
.create("/test", WriteOptions::default())
.await?
.close()
.await?;

client
.set_owner("/test", Some("testuser"), Some("testgroup"))
.await?;
let file_info = client.get_file_info("/test").await?;

assert_eq!(file_info.owner, "testuser");
assert_eq!(file_info.group, "testgroup");

client.set_owner("/test", Some("testuser2"), None).await?;
let file_info = client.get_file_info("/test").await?;

assert_eq!(file_info.owner, "testuser2");
assert_eq!(file_info.group, "testgroup");

client.set_owner("/test", None, Some("testgroup2")).await?;
let file_info = client.get_file_info("/test").await?;

assert_eq!(file_info.owner, "testuser2");
assert_eq!(file_info.group, "testgroup2");

Ok(())
}
}
Loading