From 2bc6cdedd494ce403acc39031ff83bdaa76f8cbe Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 27 Sep 2023 23:18:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?utimes=E6=8E=A5=E5=8F=A3=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 3 + .../src/mod_fs/properties/prop_n_exporter.cpp | 2 + .../kits/js/src/mod_fs/properties/utimes.cpp | 103 ++++++++++++++++++ .../kits/js/src/mod_fs/properties/utimes.h | 35 ++++++ 4 files changed, 143 insertions(+) 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 4f420fad0..ec764f9f3 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -114,6 +114,7 @@ ohos_shared_library("fs") { "${src_path}/mod_fs", "${utils_path}/common/include", "//third_party/libuv/include", + "${file_api_path}/interfaces/kits/rust/include", ] sources = [ @@ -133,6 +134,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" ] @@ -140,6 +142,7 @@ ohos_shared_library("fs") { deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", + "${file_api_path}/interfaces/kits/rust:rust_file", ] use_exceptions = true 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 4bcccb9e4..a66c422dc 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" @@ -587,6 +588,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..639d10804 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -0,0 +1,103 @@ +/* + * 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) +{ + HILOGI("Utimes enter"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + HILOGE("Utimes, Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + //1. path - path. + auto [succ1, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ1) { + HILOGE("Utimes, Invalid fd from JS first argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + //2. mtime - last modification time + auto [succ2, mtime] = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble(); + if (!succ2) { + HILOGE("Utimes, Invalid whence from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + char *path = tmp.get(); + HILOGD("Utimes, path:%{public}s, mtime:%{public}f", path, mtime); + 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; + } + + int ret = access(path, F_OK); + if (ret != 0) { + HILOGE("Utimes::Sync, No such file or directory, file %{public}s", path); + NError(ENOENT).ThrowErr(env); + return nullptr; + } + + if (access(path, W_OK) == EROFS) { + HILOGE("Utimes::Sync, Read-only file system"); + NError(EROFS).ThrowErr(env); + return nullptr; + } + + struct stat st; + ret = stat(path, &st); + if (ret != 0) { + HILOGE("Utimes::Sync, Permission denied"); + NError(EACCES).ThrowErr(env); + return nullptr; + } + + double atime = st.st_atime; + ret = uv_fs_utime(nullptr, utimes_req.get(), path, atime, mtime, nullptr); + if (ret < 0) { + HILOGE("Failed to utimes file for %{public}d", ret); + NError(UNKROWN_ERR).ThrowErr(env); + return nullptr; + } + ret = stat(path, &st); + if (ret == 0) { + HILOGD("Utimes finished, mtime:%{public}lld", st.st_mtime); + } else { + HILOGE("Utimes stat failed, ret:%{public}d", ret); + } + + HILOGI("Utimes finished"); + return nullptr; +} + +} // 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..ebe81b4ee --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.h @@ -0,0 +1,35 @@ +/* + * 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 +#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); +}; + +const std::string PROCEDURE_UTIMES_NAME = "FileIOUtimes"; +} // namespace ModuleFileIO +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_H \ No newline at end of file -- Gitee From 30fece437a825bafb22b5efa7604c5983431a652 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Thu, 28 Sep 2023 18:18:12 +0800 Subject: [PATCH 2/2] utimes update Signed-off-by: zhuhongtao666 --- interfaces/kits/js/BUILD.gn | 8 ++- .../kits/js/src/mod_fs/properties/utimes.cpp | 65 +++++++------------ .../kits/js/src/mod_fs/properties/utimes.h | 2 - 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index ec764f9f3..dd9bb4b82 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -114,7 +114,6 @@ ohos_shared_library("fs") { "${src_path}/mod_fs", "${utils_path}/common/include", "//third_party/libuv/include", - "${file_api_path}/interfaces/kits/rust/include", ] sources = [ @@ -142,7 +141,6 @@ ohos_shared_library("fs") { deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", - "${file_api_path}/interfaces/kits/rust:rust_file", ] use_exceptions = true @@ -160,6 +158,7 @@ ohos_shared_library("fs") { ] if (!use_mingw_win && !use_mac) { + include_dirs += [ "${file_api_path}/interfaces/kits/rust/include" ] sources += [ "src/mod_fs/class_randomaccessfile/randomaccessfile_n_exporter.cpp", "src/mod_fs/class_stream/flush.cpp", @@ -192,7 +191,10 @@ ohos_shared_library("fs") { "ipc:ipc_core", "samgr:samgr_proxy", ] - deps += [ "${file_api_path}/interfaces/kits/native:remote_uri_native" ] + deps += [ + "${file_api_path}/interfaces/kits/native:remote_uri_native", + "${file_api_path}/interfaces/kits/rust:rust_file", + ] } } diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp index 639d10804..570a95e44 100644 --- a/interfaces/kits/js/src/mod_fs/properties/utimes.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -13,89 +13,68 @@ * limitations under the License. */ #include "utimes.h" + #include + #include "common_func.h" #include "file_utils.h" #include "filemgmt_libhilog.h" +#include "movedir.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; +const double MS = 1000; napi_value Utimes::Sync(napi_env env, napi_callback_info info) { - HILOGI("Utimes enter"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO)) { - HILOGE("Utimes, Number of arguments unmatched"); + HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; } - //1. path - path. - auto [succ1, tmp, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succ1) { - HILOGE("Utimes, Invalid fd from JS first argument"); + + 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; } - //2. mtime - last modification time - auto [succ2, mtime] = NVal(env, funcArg[NARG_POS::SECOND]).ToDouble(); - if (!succ2) { - HILOGE("Utimes, Invalid whence from JS second argument"); + 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; } - char *path = tmp.get(); - HILOGD("Utimes, path:%{public}s, mtime:%{public}f", path, mtime); 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; - } - - int ret = access(path, F_OK); - if (ret != 0) { - HILOGE("Utimes::Sync, No such file or directory, file %{public}s", path); - NError(ENOENT).ThrowErr(env); - return nullptr; - } - - if (access(path, W_OK) == EROFS) { - HILOGE("Utimes::Sync, Read-only file system"); - NError(EROFS).ThrowErr(env); + NError(ENOMEM).ThrowErr(env); return nullptr; } - struct stat st; - ret = stat(path, &st); - if (ret != 0) { - HILOGE("Utimes::Sync, Permission denied"); - NError(EACCES).ThrowErr(env); + int ret = uv_fs_stat(nullptr, utimes_req.get(), path.get(), nullptr); + if (ret < 0) { + HILOGE("Failed to get stat of the file by path"); return nullptr; } - double atime = st.st_atime; - ret = uv_fs_utime(nullptr, utimes_req.get(), path, atime, mtime, nullptr); + double atime = static_cast(utimes_req->statbuf.st_atim.tv_sec) + + static_cast(utimes_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 utimes file for %{public}d", ret); - NError(UNKROWN_ERR).ThrowErr(env); + HILOGE("Failed to chang mtime of the file for %{public}d", ret); + NError(ret).ThrowErr(env); return nullptr; } - ret = stat(path, &st); - if (ret == 0) { - HILOGD("Utimes finished, mtime:%{public}lld", st.st_mtime); - } else { - HILOGE("Utimes stat failed, ret:%{public}d", ret); - } - HILOGI("Utimes finished"); - return nullptr; + return NVal::CreateUndefined(env).val_; } } // ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/properties/utimes.h b/interfaces/kits/js/src/mod_fs/properties/utimes.h index ebe81b4ee..60cdb0546 100644 --- a/interfaces/kits/js/src/mod_fs/properties/utimes.h +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.h @@ -16,7 +16,6 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_H #define INTERFACES_KITS_JS_SRC_MOD_FS_PROPERTIES_UTIMES_H -#include #include "filemgmt_libn.h" namespace OHOS { @@ -28,7 +27,6 @@ public: static napi_value Sync(napi_env env, napi_callback_info info); }; -const std::string PROCEDURE_UTIMES_NAME = "FileIOUtimes"; } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS -- Gitee