diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 4f420fad0ae5fb513c265717ce38f90ad7962c7a..dd9bb4b82ae25adf5ffba81e137f5c5f3ac5cc2c 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" ] @@ -157,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", @@ -189,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/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 4bcccb9e494c0a9135ba33f412a162fdf5d9285d..a66c422dcadf98b77f2fd294f280ca7b0c9476d3 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 0000000000000000000000000000000000000000..570a95e443b2b959edda9a547ecf889b5a36fad5 --- /dev/null +++ b/interfaces/kits/js/src/mod_fs/properties/utimes.cpp @@ -0,0 +1,82 @@ +/* + * 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" +#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) +{ + 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 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 = 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 = 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 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 0000000000000000000000000000000000000000..60cdb0546befd8e4ad6ced66220504f1cefbe35a --- /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