• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "lstat_core.h"
17 
18 #include <memory>
19 #include <tuple>
20 
21 #include <securec.h>
22 
23 #include "file_utils.h"
24 #include "filemgmt_libhilog.h"
25 #include "stat_instantiator.h"
26 
27 namespace OHOS::FileManagement::ModuleFileIO {
28 using namespace std;
29 
DoLstat(const string & path)30 FsResult<FsStat *> LstatCore::DoLstat(const string &path)
31 {
32     std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup) *> lstat_req = { new (std::nothrow) uv_fs_t,
33         FsUtils::FsReqCleanup };
34     if (!lstat_req) {
35         HILOGE("Failed to request heap memory.");
36         return FsResult<FsStat *>::Error(ENOMEM);
37     }
38     int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr);
39     if (ret < 0) {
40         HILOGE("Failed to get stat of file, ret: %{public}d", ret);
41         return FsResult<FsStat *>::Error(ret);
42     }
43 
44 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
45     size_t length = path.length() + 1;
46     auto chars = std::make_unique<char[]>(length);
47     ret = strncpy_s(chars.get(), length, path.c_str(), length - 1);
48     if (ret != EOK) {
49         HILOGE("Copy file path failed!");
50         return FsResult<FsStat *>::Error(ret);
51     }
52     struct FileInfo info = { true, move(chars), {} };
53     auto arg = CreateSharedPtr<FileInfo>(move(info));
54     if (arg == nullptr) {
55         HILOGE("Failed to request heap memory.");
56         return FsResult<FsStat *>::Error(ENOMEM);
57     }
58     auto stat = StatInstantiator::InstantiateStat(lstat_req->statbuf, arg);
59 #else
60     auto stat = StatInstantiator::InstantiateStat(lstat_req->statbuf);
61 #endif
62     if (stat == nullptr) {
63         HILOGE("Failed to InstantiateStat.");
64         return FsResult<FsStat *>::Error(ENOMEM);
65     }
66     return FsResult<FsStat *>::Success(stat);
67 }
68 
69 } // namespace OHOS::FileManagement::ModuleFileIO