1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "FileSource"
19 #include <utils/Log.h>
20
21 #include <datasource/FileSource.h>
22 #include <media/stagefright/foundation/ADebug.h>
23 #include <media/stagefright/FoundationUtils.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 namespace android {
31
FileSource(const char * filename)32 FileSource::FileSource(const char *filename)
33 : mFd(-1),
34 mOffset(0),
35 mLength(-1),
36 mName("<null>") {
37
38 if (filename) {
39 mName = String8::format("FileSource(%s)", filename);
40 }
41 ALOGV("%s", filename);
42 mFd = open(filename, O_LARGEFILE | O_RDONLY);
43
44 if (mFd >= 0) {
45 mLength = lseek64(mFd, 0, SEEK_END);
46 } else {
47 ALOGE("Failed to open file '%s'. (%s)", filename, strerror(errno));
48 }
49 }
50
FileSource(int fd,int64_t offset,int64_t length)51 FileSource::FileSource(int fd, int64_t offset, int64_t length)
52 : mFd(fd),
53 mOffset(offset),
54 mLength(length),
55 mName("<null>") {
56 ALOGV("fd=%d (%s), offset=%lld, length=%lld",
57 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
58
59 if (mOffset < 0) {
60 mOffset = 0;
61 }
62 if (mLength < 0) {
63 mLength = 0;
64 }
65 if (mLength > INT64_MAX - mOffset) {
66 mLength = INT64_MAX - mOffset;
67 }
68 struct stat s;
69 if (fstat(fd, &s) == 0) {
70 if (mOffset > s.st_size) {
71 mOffset = s.st_size;
72 mLength = 0;
73 }
74 if (mOffset + mLength > s.st_size) {
75 mLength = s.st_size - mOffset;
76 }
77 }
78 if (mOffset != offset || mLength != length) {
79 ALOGW("offset/length adjusted from %lld/%lld to %lld/%lld",
80 (long long) offset, (long long) length,
81 (long long) mOffset, (long long) mLength);
82 }
83
84 mName = String8::format(
85 "FileSource(fd(%s), %lld, %lld)",
86 nameForFd(fd).c_str(),
87 (long long) mOffset,
88 (long long) mLength);
89
90 }
91
~FileSource()92 FileSource::~FileSource() {
93 if (mFd >= 0) {
94 ::close(mFd);
95 mFd = -1;
96 }
97 }
98
initCheck() const99 status_t FileSource::initCheck() const {
100 return mFd >= 0 ? OK : NO_INIT;
101 }
102
readAt(off64_t offset,void * data,size_t size)103 ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
104 if (mFd < 0) {
105 return NO_INIT;
106 }
107
108 Mutex::Autolock autoLock(mLock);
109 if (mLength >= 0) {
110 if (offset >= mLength) {
111 return 0; // read beyond EOF.
112 }
113 uint64_t numAvailable = mLength - offset;
114 if ((uint64_t)size > numAvailable) {
115 size = numAvailable;
116 }
117 }
118 return readAt_l(offset, data, size);
119 }
120
readAt_l(off64_t offset,void * data,size_t size)121 ssize_t FileSource::readAt_l(off64_t offset, void *data, size_t size) {
122 off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET);
123 if (result == -1) {
124 ALOGE("seek to %lld failed", (long long)(offset + mOffset));
125 return UNKNOWN_ERROR;
126 }
127
128 return ::read(mFd, data, size);
129 }
130
getSize(off64_t * size)131 status_t FileSource::getSize(off64_t *size) {
132 Mutex::Autolock autoLock(mLock);
133
134 if (mFd < 0) {
135 return NO_INIT;
136 }
137
138 *size = mLength;
139
140 return OK;
141 }
142
143 } // namespace android
144