From be96da30f39f0bc65e46e3a06709bfb9aece482b Mon Sep 17 00:00:00 2001 From: bubble_mao Date: Fri, 20 Oct 2023 11:16:19 +0800 Subject: [PATCH] =?UTF-8?q?fs=E6=96=B0=E5=A2=9Eutimes=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bubble_mao --- interfaces/kits/js/BUILD.gn | 1 + interfaces/kits/js/src/mod_fs/common_func.h | 3 + .../kits/js/src/mod_fs/properties/move.cpp | 4 +- .../kits/js/src/mod_fs/properties/move.h | 1 - .../kits/js/src/mod_fs/properties/movedir.h | 2 - .../src/mod_fs/properties/prop_n_exporter.cpp | 2 + .../kits/js/src/mod_fs/properties/utimes.cpp | 89 +++++++++++++++++++ .../kits/js/src/mod_fs/properties/utimes.h | 33 +++++++ 8 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 interfaces/kits/js/src/mod_fs/properties/utimes.cpp create mode 100644 interfaces/kits/js/src/mod_fs/properties/utimes.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 318932161..d279adaeb 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -133,6 +133,7 @@ ohos_shared_library("fs") { "src/mod_fs/properties/rmdirent.cpp", "src/mod_fs/properties/stat.cpp", "src/mod_fs/properties/truncate.cpp", + "src/mod_fs/properties/utimes.cpp", ] cflags_cc = [ "-std=c++17" ] diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 93de17160..cc7293ddf 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -46,6 +46,9 @@ constexpr unsigned int USR_DIRECTORY = 0200000; constexpr unsigned int USR_NOFOLLOW = 0400000; constexpr unsigned int USR_SYNC = 04010000; +const double NS = 1e9; +const double MS = 1e3; + struct FileInfo { bool isPath = false; std::unique_ptr path = { nullptr }; diff --git a/interfaces/kits/js/src/mod_fs/properties/move.cpp b/interfaces/kits/js/src/mod_fs/properties/move.cpp index e1ed33fe8..38a4812fc 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/move.cpp @@ -90,9 +90,9 @@ static int ChangeTime(const string &path, uv_fs_t *stat_req) return ENOMEM; } double atime = static_cast(stat_req->statbuf.st_atim.tv_sec) + - static_cast(stat_req->statbuf.st_atim.tv_nsec) / NANOSECOND; + static_cast(stat_req->statbuf.st_atim.tv_nsec) / NS; double mtime = static_cast(stat_req->statbuf.st_mtim.tv_sec) + - static_cast(stat_req->statbuf.st_mtim.tv_nsec) / NANOSECOND; + static_cast(stat_req->statbuf.st_mtim.tv_nsec) / NS; int ret = uv_fs_utime(nullptr, utime_req.get(), path.c_str(), atime, mtime, nullptr); if (ret < 0) { HILOGE("Failed to utime dstPath"); diff --git a/interfaces/kits/js/src/mod_fs/properties/move.h b/interfaces/kits/js/src/mod_fs/properties/move.h index 24cc3a734..612494679 100644 --- a/interfaces/kits/js/src/mod_fs/properties/move.h +++ b/interfaces/kits/js/src/mod_fs/properties/move.h @@ -30,7 +30,6 @@ public: }; constexpr int MODE_FORCE_MOVE = 0; constexpr int MODE_THROW_ERR = 1; -const double NANOSECOND = 1e9; const std::string PROCEDURE_MOVE_NAME = "FileIOMove"; } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.h b/interfaces/kits/js/src/mod_fs/properties/movedir.h index 979ebfc49..d1a116d23 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.h +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.h @@ -31,8 +31,6 @@ constexpr int FILE_DISMATCH = 0; constexpr int FILE_MATCH = 1; constexpr int MOVEDIR_DEFAULT_PERM = 0770; -const double NS = 1e9; - enum ModeOfMoveDir { DIRMODE_DIRECTORY_THROW_ERR = 0, DIRMODE_FILE_THROW_ERR, diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 44b4ca8ff..3621bb888 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -39,6 +39,7 @@ #include "rmdirent.h" #include "stat.h" #include "truncate.h" +#include "utimes.h" #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) #include "copy_file.h" @@ -589,6 +590,7 @@ bool PropNExporter::Export() NVal::DeclareNapiFunction("truncateSync", Truncate::Sync), NVal::DeclareNapiFunction("unlink", Unlink), NVal::DeclareNapiFunction("unlinkSync", UnlinkSync), + NVal::DeclareNapiFunction("utimes", Utimes::Sync), NVal::DeclareNapiFunction("write", Write), NVal::DeclareNapiFunction("writeSync", WriteSync), #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp new file mode 100644 index 000000000..b73e1ab70 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023 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.h" + +#include + +#include "common_func.h" +#include "file_utils.h" +#include "filemgmt_libhilog.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { +using namespace std; +using namespace OHOS::FileManagement::LibN; + +napi_value Utimes::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succGetPath) { + HILOGE("Invalid path from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succGetMtime, mtime] = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble();; + if (!succGetMtime || mtime < 0) { + HILOGE("Invalid mtime from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + std::unique_ptr stat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!stat_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + + int ret = uv_fs_stat(nullptr, stat_req.get(), path.get(), nullptr); + if (ret < 0) { + HILOGE("Failed to get stat of the file by path"); + NError(ret).ThrowErr(env); + return nullptr; + } + + std::unique_ptr utimes_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!utimes_req) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + + double atime = static_cast(stat_req->statbuf.st_atim.tv_sec) + + static_cast(stat_req->statbuf.st_atim.tv_nsec) / NS; + ret = uv_fs_utime(nullptr, utimes_req.get(), path.get(), atime, mtime / MS, nullptr); + if (ret < 0) { + HILOGE("Failed to chang mtime of the file for %{public}d", ret); + NError(ret).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +} // ModuleFileIO +} // FileManagement +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes.h b/interfaces/kits/js/src/mod_fs/properties/utimes.h new file mode 100644 index 000000000..60cdb0546 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 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_H +#define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace FileManagement { +namespace ModuleFileIO { + +class Utimes final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); +}; + +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_H \ No newline at end of file -- Gitee