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 "lseek_core.h"
17
18 #include "filemgmt_libhilog.h"
19
20 namespace OHOS {
21 namespace FileManagement {
22 namespace ModuleFileIO {
23 using namespace std;
24
DoLseek(const int32_t & fd,const int64_t & offset,const optional<SeekPos> & pos)25 FsResult<int64_t> LseekCore::DoLseek(const int32_t &fd, const int64_t &offset,
26 const optional<SeekPos> &pos)
27 {
28 if (fd < 0) {
29 HILOGE("Invalid fd from JS first argument");
30 return FsResult<int64_t>::Error(EINVAL);
31 }
32
33 SeekPos whence = SeekPos::START;
34 if (pos.has_value()) {
35 if (pos.value() < SeekPos::START || pos.value() > SeekPos::END) {
36 HILOGE("Invalid whence from JS third argument");
37 return FsResult<int64_t>::Error(EINVAL);
38 }
39 whence = pos.value();
40 }
41
42 int64_t ret = ::Lseek(fd, offset, whence);
43 if (ret < 0) {
44 HILOGE("Failed to lseek, error:%{public}d", errno);
45 return FsResult<int64_t>::Error(errno);
46 }
47
48 return FsResult<int64_t>::Success(ret);
49 }
50
51 } // ModuleFileIO
52 } // FileManagement
53 } // OHOS