diff --git a/interfaces/kits/rust/include/rust_file.h b/interfaces/kits/rust/include/rust_file.h index 4cc9507cc1a75cac8b2af169c7a50b1618d2d12d..5b6996ba8e2973e5425e45bc82e6bd0f3344c923 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 16304c047157186f7912fa386a3e9434fed57666..4e5f058a0318e42ed0e6e5b0aa698bfce1e597fa 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 6cdc9540abd6200cb769f8984967a648f4b1ff9c..d9211025370d412475915aad59faaa64e38a1b7d 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 24ac15a3af4a566f5ff60177f0fd8603419bedcf..7b3800a8be0d056e7503b540fc5c3047296a46c0 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("