diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 9334220be9d28e01972e60ed84bec52d044d4e8a..e8fb345c880ba51fb48cdd67eea8043da938bf45 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -692,6 +692,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/ani/rmdir_ani.cpp", "src/mod_fs/properties/ani/truncate_ani.cpp", "src/mod_fs/properties/ani/unlink_ani.cpp", + "src/mod_fs/properties/ani/utimes_ani.cpp", "src/mod_fs/properties/ani/write_ani.cpp", "src/mod_fs/properties/close_core.cpp", "src/mod_fs/properties/copy_file_core.cpp", @@ -705,6 +706,7 @@ ohos_shared_library("ani_fs_class") { "src/mod_fs/properties/stat_core.cpp", "src/mod_fs/properties/truncate_core.cpp", "src/mod_fs/properties/unlink_core.cpp", + "src/mod_fs/properties/utimes_core.cpp", "src/mod_fs/properties/write_core.cpp", ] deps = [ diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 065495c9ade9c4c775ce494fa218d0c382077459..496a93eef1bdb9fa1fb44bff9c8258e08b7aface 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -34,6 +34,7 @@ #include "truncate_ani.h" #include "unlink_ani.h" #include "write_ani.h" +#include "utimes_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; @@ -88,6 +89,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "truncateSync", nullptr, reinterpret_cast(TruncateAni::TruncateSync) }, ani_native_function { "unlinkSync", nullptr, reinterpret_cast(UnlinkAni::UnlinkSync) }, ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, + ani_native_function { "utimes", nullptr, reinterpret_cast(UtimesAni::Utimes) }, }; return BindClass(env, className, methods); } diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 67eb95add30a0cd803f0542ae46c90f870189651..63223f2bb341f744cda5c63670b368b49b36d56b 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -492,6 +492,10 @@ function stat(file: string | number, callback: AsyncCallback): void }); } +function utimes(path: string, mtime: number): void { + return FileIoImpl.utimes(path, mtime); +} + export interface Filter { suffix?: Array; displayName?: Array; @@ -659,4 +663,6 @@ class FileIoImpl { static native writeSync(fd: number, buffer: string | ArrayBuffer, options?: WriteOptions): number; + static native utimes(path: string, mtime: number): void; + } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.cpp new file mode 100644 index 0000000000000000000000000000000000000000..849f10d4d789b07971c92d3f5ac422d073f8642a --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utimes_ani.h" + +#include "error_handler.h" +#include "filemgmt_libhilog.h" +#include "type_converter.h" +#include "utimes_core.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +void UtimesAni::Utimes( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_double mtime) +{ + auto [succPath, newPath] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return; + } + auto ret = UtimesCore::DoUtimes(newPath, static_cast(mtime)); + if (!ret.IsSuccess()) { + HILOGE("Utimes failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return; + } +} +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h new file mode 100644 index 0000000000000000000000000000000000000000..118f7d25ad1cd1fb7cc4442f4e4826af311a36f4 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/ani/utimes_ani.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_UTIMES_ANI_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_UTIMES_ANI_H + +#include + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +namespace ANI { + +class UtimesAni final { +public: + static void Utimes( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_double mtime); +}; +} // namespace ANI +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS + +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_UTIMES_ANI_H \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes_core.cpp b/interfaces/kits/js/src/mod_fs/properties/utimes_core.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b517167bf69d8fbbee4b590ba089d193c6c7746 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes_core.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utimes_core.h" + +#include + +#include "filemgmt_libhilog.h" +#include "fs_utils.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; + +FsResult UtimesCore::DoUtimes(const string &path, const double mtime) +{ + if (mtime < 0) { + HILOGE("Invalid mtime"); + return FsResult::Error(EINVAL); + } + std::unique_ptr statReq = { + new (std::nothrow) uv_fs_t, FsUtils::FsReqCleanup }; + if (!statReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + int ret = uv_fs_stat(nullptr, statReq.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get stat of the file by path"); + return FsResult::Error(ret); + } + + std::unique_ptr utimesReq = { + new uv_fs_t, FsUtils::FsReqCleanup }; + if (!utimesReq) { + HILOGE("Failed to request heap memory."); + return FsResult::Error(ENOMEM); + } + + double atime = static_cast(statReq->statbuf.st_atim.tv_sec) + + static_cast(statReq->statbuf.st_atim.tv_nsec) / NS; + ret = uv_fs_utime(nullptr, utimesReq.get(), path.c_str(), atime, mtime / MS, nullptr); + if (ret < 0) { + HILOGE("Failed to chang mtime of the file for %{public}d", ret); + return FsResult::Error(ret); + } + return FsResult::Success(); +} +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes_core.h b/interfaces/kits/js/src/mod_fs/properties/utimes_core.h new file mode 100644 index 0000000000000000000000000000000000000000..86fe8c4fd83dded66c52ddf571c158e1799330a6 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes_core.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_CORE_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_CORE_H + +#include "filemgmt_libfs.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +class UtimesCore final { +public: + static FsResult DoUtimes(const string &path, const double mtime); +}; +const std::string PROCEDURE_RMDIRENT_NAME = "FileIOUtimes"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_CORE_H \ No newline at end of file