Xuanwo commented on code in PR #7061:
URL: https://github.com/apache/opendal/pull/7061#discussion_r2633457703
##########
core/services/fs/src/core.rs:
##########
@@ -205,4 +216,61 @@ impl FsCore {
.map_err(new_std_io_error)?;
Ok(())
}
+
+ /// Get user metadata from extended attributes on Unix systems.
+ ///
+ /// OpenDAL stores user metadata in xattr with the prefix "user.opendal.".
Review Comment:
Please don't add our own prefix. We should be able to read xattr set by
other applications.
##########
core/services/fs/src/core.rs:
##########
@@ -205,4 +216,61 @@ impl FsCore {
.map_err(new_std_io_error)?;
Ok(())
}
+
+ /// Get user metadata from extended attributes on Unix systems.
+ ///
+ /// OpenDAL stores user metadata in xattr with the prefix "user.opendal.".
+ #[cfg(unix)]
+ pub fn get_user_metadata(path: &Path) -> Result<HashMap<String, String>> {
+ let mut user_metadata = HashMap::new();
+
+ let attrs = match xattr::list(path) {
+ Ok(attrs) => attrs,
+ Err(e) => {
+ // xattr may not be supported on some filesystems, ignore the
error
+ if e.kind() == std::io::ErrorKind::Unsupported {
+ return Ok(user_metadata);
+ }
+ // Also ignore "operation not supported" errors
(ENOTSUP/EOPNOTSUPP)
+ // which may occur on filesystems that don't support xattr
+ if let Some(os_error) = e.raw_os_error() {
+ // ENOTSUP (95) and EOPNOTSUPP (95) on Linux
+ if os_error == 95 {
+ return Ok(user_metadata);
+ }
+ }
+ return Err(new_std_io_error(e));
+ }
+ };
+
+ for attr in attrs {
+ let attr_name = attr.to_string_lossy();
+ if let Some(key) = attr_name.strip_prefix(XATTR_PREFIX) {
+ if let Ok(Some(value)) = xattr::get(path, &attr) {
+ if let Ok(v) = String::from_utf8(value) {
+ user_metadata.insert(key.to_string(), v);
+ }
+ }
+ }
+ }
+
+ Ok(user_metadata)
+ }
+
+ /// Set user metadata as extended attributes on Unix systems.
+ ///
+ /// OpenDAL stores user metadata in xattr with the prefix "user.opendal.".
+ #[cfg(unix)]
+ pub fn set_user_metadata(path: &Path, user_metadata: &HashMap<String,
String>) -> Result<()> {
+ for (key, value) in user_metadata {
+ let attr_name = format!("{}{}", XATTR_PREFIX, key);
Review Comment:
The same, please don't prefix for users input.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]