From 55eaa9d386729ede815cf71e1ab3a6e1870530db Mon Sep 17 00:00:00 2001 From: fengjq Date: Tue, 5 Dec 2023 16:49:40 +0800 Subject: [PATCH] Add cut_file_name interface for fileio_rust Signed-off-by: fengjq --- interfaces/kits/rust/include/rust_file.h | 13 +++++++++ interfaces/kits/rust/src/adapter.rs | 23 ++++++++++++++++ interfaces/kits/rust/src/ffi.rs | 9 +++++-- .../test/unittest/class_file/rust_test.cpp | 27 +++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/rust/include/rust_file.h b/interfaces/kits/rust/include/rust_file.h index 4cc9507cc..5b6996ba8 100644 --- a/interfaces/kits/rust/include/rust_file.h +++ b/interfaces/kits/rust/include/rust_file.h @@ -125,6 +125,19 @@ int Mkdirs(const char* path, enum MakeDirectionMode mode); */ Str* GetParent(int fd); +/** + * @ingroup rust + * @brief Cut the file name by the specified length. + * @param path file name. + * @param size specified length. + * @retval NULL The file name is empty or error. + * @retval !NULL The result of the file name after cutting. + * @attention + * 1.Errors are stored in errno. + * @li rust_file.h:The file where the interface is located. + */ +Str* CutFileName(const char* path, size_t size); + /** * @ingroup rust * @brief Releases the memory that the `Str` pointer points to. diff --git a/interfaces/kits/rust/src/adapter.rs b/interfaces/kits/rust/src/adapter.rs index 16304c047..4e5f058a0 100644 --- a/interfaces/kits/rust/src/adapter.rs +++ b/interfaces/kits/rust/src/adapter.rs @@ -162,3 +162,26 @@ pub(crate) fn get_parent(fd: i32) -> Result<*mut Str, Error> { } Ok(null_mut()) } + +pub(crate) unsafe fn cut_file_name(path: *const c_char, size: usize) -> *mut Str { + let path_str = match CStr::from_ptr(path).to_str() { + Ok(s) => s, + Err(_) => return std::ptr::null_mut(), + }; + let len = path_str.chars().count(); + let size = size.min(len); + + let mut sliced_str = String::from(path_str); + for _ in 0..size { + let _ = sliced_str.pop(); + } + + let result = match CString::new(sliced_str.clone()) { + Ok(s) => Str { + str: s.into_raw(), + len: sliced_str.as_bytes().len() as c_uint, + }, + Err(_) => return std::ptr::null_mut(), + }; + Box::into_raw(Box::new(result)) +} diff --git a/interfaces/kits/rust/src/ffi.rs b/interfaces/kits/rust/src/ffi.rs index 6cdc9540a..d92110253 100644 --- a/interfaces/kits/rust/src/ffi.rs +++ b/interfaces/kits/rust/src/ffi.rs @@ -14,8 +14,8 @@ */ use crate::adapter::{ - create_dir, error_control, get_parent, next_line, reader_iterator, seek, MakeDirectionMode, - SeekPos, Str, + create_dir, error_control, get_parent, next_line, reader_iterator, seek, cut_file_name, + MakeDirectionMode, SeekPos, Str, }; use libc::{c_char, c_int, c_longlong, c_void}; use std::ffi::CString; @@ -85,3 +85,8 @@ pub unsafe extern "C" fn StrFree(str: *mut Str) { let _ = CString::from_raw(string.str); } } + +#[no_mangle] +pub unsafe extern "C" fn CutFileName(path: *const c_char, size: usize) -> *mut Str { + cut_file_name(path, size) +} diff --git a/interfaces/test/unittest/class_file/rust_test.cpp b/interfaces/test/unittest/class_file/rust_test.cpp index 24ac15a3a..7b3800a8b 100644 --- a/interfaces/test/unittest/class_file/rust_test.cpp +++ b/interfaces/test/unittest/class_file/rust_test.cpp @@ -591,6 +591,33 @@ HWTEST_F(RustTest, RustTest_StrFree_0002, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "RustTest-end RustTest_StrFree_0002"; } +/** +* @tc.name: RustTest_CutFileName_0000 +* @tc.desc: Test function of CutFileName() interface for SUCCESS. +* @tc.size: MEDIUM +* @tc.type: FUNC +* @tc.level Level 1 +* @tc.require: AR000IGDNF +*/ +HWTEST_F(RustTest, RustTest_CutFileName_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RustTest-begin RustTest_CutFileName_0000"; + string myStr = "str; + GTEST_LOG_(INFO) << "RustTest_CutFileName_0000 str1" << str1->len; + EXPECT_TRUE(string(str1->str).compare("str; + GTEST_LOG_(INFO) << "RustTest_CutFileName_0000 str2" << str2->len; + EXPECT_TRUE(string(str2->str).compare("