• 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 "truncate_core.h"
17 
18 #include <cstring>
19 #include <tuple>
20 #include <unistd.h>
21 
22 #include "file_utils.h"
23 #include "filemgmt_libhilog.h"
24 
25 namespace OHOS::FileManagement::ModuleFileIO {
26 using namespace std;
27 
ValidFileInfo(FileInfo & fileInfo)28 static bool ValidFileInfo(FileInfo &fileInfo)
29 {
30     if (!fileInfo.isPath) {
31         auto fd = fileInfo.fdg->GetFD();
32         if (fd < 0) {
33             HILOGE("Invalid fd");
34             return false;
35         }
36     }
37     return true;
38 }
39 
Truncate(FileInfo & fileInfo,int64_t truncateLen)40 static int Truncate(FileInfo &fileInfo, int64_t truncateLen)
41 {
42     if (fileInfo.isPath) {
43         std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup) *> openReq = { new uv_fs_t, FsUtils::FsReqCleanup };
44         if (!openReq) {
45             HILOGE("Failed to request heap memory.");
46             return ENOMEM;
47         }
48         int ret = uv_fs_open(
49             nullptr, openReq.get(), fileInfo.path.get(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, nullptr);
50         if (ret < 0) {
51             return ret;
52         }
53         std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup) *> ftruncateReq = { new uv_fs_t,
54             FsUtils::FsReqCleanup };
55         if (!ftruncateReq) {
56             HILOGE("Failed to request heap memory.");
57             return ENOMEM;
58         }
59         ret = uv_fs_ftruncate(nullptr, ftruncateReq.get(), ret, truncateLen, nullptr);
60         if (ret < 0) {
61             HILOGE("Failed to truncate file by path");
62             return ret;
63         }
64     } else {
65         std::unique_ptr<uv_fs_t, decltype(FsUtils::FsReqCleanup) *> ftruncateReq = { new uv_fs_t,
66             FsUtils::FsReqCleanup };
67         if (!ftruncateReq) {
68             HILOGE("Failed to request heap memory.");
69             return ENOMEM;
70         }
71         int ret = uv_fs_ftruncate(nullptr, ftruncateReq.get(), fileInfo.fdg->GetFD(), truncateLen, nullptr);
72         if (ret < 0) {
73             HILOGE("Failed to truncate file by fd for libuv error %{public}d", ret);
74             return ret;
75         }
76     }
77 
78     return ERRNO_NOERR;
79 }
80 
DoTruncate(FileInfo & fileInfo,const std::optional<int64_t> & len)81 FsResult<void> TruncateCore::DoTruncate(FileInfo &fileInfo, const std::optional<int64_t> &len)
82 
83 {
84     auto succ = ValidFileInfo(fileInfo);
85     if (!succ) {
86         return FsResult<void>::Error(EINVAL);
87     }
88 
89     int64_t truncateLen = 0;
90     if (len.has_value()) {
91         truncateLen = len.value();
92         if (truncateLen < 0) {
93             HILOGE("Invalid truncate length");
94             return FsResult<void>::Error(EINVAL);
95         }
96     }
97 
98     auto err = Truncate(fileInfo, truncateLen);
99     if (err) {
100         return FsResult<void>::Error(err);
101     }
102 
103     return FsResult<void>::Success();
104 }
105 } // namespace OHOS::FileManagement::ModuleFileIO