• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #define HST_LOG_TAG "DataSinkFd"
17 
18 #include "data_sink_fd.h"
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "common/log.h"
22 
23 namespace OHOS {
24 namespace Media {
DataSinkFd(int32_t fd)25 DataSinkFd::DataSinkFd(int32_t fd) : fd_(dup(fd)), pos_(0), end_(-1)
26 {
27     MEDIA_LOG_D("dup fd is %{public}d", fd_);
28     end_ = lseek(fd_, 0, SEEK_END);
29     if (lseek(fd_, 0, SEEK_SET) < 0) {
30         MEDIA_LOG_E("failed to construct, fd is %{public}d, error is %{public}s", fd_, strerror(errno));
31     }
32     uint32_t fdPermission = static_cast<uint32_t>(fcntl(fd_, F_GETFL, 0));
33     isCanRead_ = (fdPermission & O_RDWR) == O_RDWR;
34 }
35 
~DataSinkFd()36 DataSinkFd::~DataSinkFd()
37 {
38     if (fd_ > 0) {
39         MEDIA_LOG_D("close fd is %{public}d", fd_);
40         close(fd_);
41         fd_ = -1;
42     }
43 }
44 
Read(uint8_t * buf,int32_t bufSize)45 int32_t DataSinkFd::Read(uint8_t *buf, int32_t bufSize)
46 {
47     FALSE_RETURN_V_MSG_E(fd_ > 0, -1, "failed to read, fd is  %{public}d", fd_);
48     if (pos_ >= end_) {
49         return 0;
50     }
51     FALSE_RETURN_V_MSG_E(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
52     int32_t size = read(fd_, buf, bufSize);
53     FALSE_RETURN_V_MSG_E(size >= 0, -1, "failed to read, %{public}s", strerror(errno));
54     pos_ = pos_ + size;
55     return size;
56 }
57 
Write(const uint8_t * buf,int32_t bufSize)58 int32_t DataSinkFd::Write(const uint8_t *buf, int32_t bufSize)
59 {
60     FALSE_RETURN_V_MSG_E(fd_ > 0, -1, "failed to write, fd is  %{public}d", fd_);
61     FALSE_RETURN_V_MSG_E(lseek(fd_, pos_, SEEK_SET) >= 0, -1, "failed to seek, %{public}s", strerror(errno));
62     int32_t size = write(fd_, buf, bufSize);
63     FALSE_RETURN_V_MSG_E(size == bufSize, -1, "failed to write, %{public}s", strerror(errno));
64     pos_ = pos_ + size;
65     end_ = pos_ > end_ ? pos_ : end_;
66     return size;
67 }
68 
Seek(int64_t offset,int whence)69 int64_t DataSinkFd::Seek(int64_t offset, int whence)
70 {
71     switch (whence) {
72         case SEEK_SET:
73             pos_ = offset;
74             break;
75         case SEEK_CUR:
76             pos_ = pos_ + offset;
77             break;
78         case SEEK_END:
79             pos_ = end_ + offset;
80             break;
81         default:
82             pos_ = offset;
83             break;
84     }
85     return pos_;
86 }
87 
GetCurrentPosition() const88 int64_t DataSinkFd::GetCurrentPosition() const
89 {
90     return pos_;
91 }
92 
CanRead()93 bool DataSinkFd::CanRead()
94 {
95     return isCanRead_;
96 }
97 } // namespace Media
98 } // namespace OHOS