| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 1 | /* | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 |  | 
| Marco Nelissen | 4f82a25 | 2014-12-22 10:18:15 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 | 
|  | 18 | #define LOG_TAG "FileSource" | 
|  | 19 | #include <utils/Log.h> | 
|  | 20 |  | 
| Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame] | 21 | #include <datasource/FileSource.h> | 
| James Dong | f1d5aa1 | 2012-02-06 23:46:37 -0800 | [diff] [blame] | 22 | #include <media/stagefright/foundation/ADebug.h> | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 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> | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | FileSource::FileSource(const char *filename) | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 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 | } | 
| Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | FileSource::FileSource(int fd, int64_t offset, int64_t length) | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 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 |  | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | FileSource::~FileSource() { | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 93 | if (mFd >= 0) { | 
|  | 94 | ::close(mFd); | 
|  | 95 | mFd = -1; | 
| Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 96 | } | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 97 | } | 
| Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 98 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 99 | status_t FileSource::initCheck() const { | 
|  | 100 | return mFd >= 0 ? OK : NO_INIT; | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 103 | ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) { | 
| James Dong | 674ebd0 | 2010-11-18 20:59:13 -0800 | [diff] [blame] | 104 | if (mFd < 0) { | 
| Andreas Huber | 3d8055a | 2010-05-24 09:18:36 -0700 | [diff] [blame] | 105 | return NO_INIT; | 
|  | 106 | } | 
|  | 107 |  | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 108 | Mutex::Autolock autoLock(mLock); | 
| Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 109 | if (mLength >= 0) { | 
| Marco Nelissen | d91c921 | 2020-01-30 15:52:23 -0800 | [diff] [blame] | 110 | if (offset < 0) { | 
|  | 111 | return UNKNOWN_ERROR; | 
|  | 112 | } | 
| Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 113 | if (offset >= mLength) { | 
|  | 114 | return 0;  // read beyond EOF. | 
|  | 115 | } | 
| Wei Jia | 0eaf820 | 2015-08-19 16:48:47 -0700 | [diff] [blame] | 116 | uint64_t numAvailable = mLength - offset; | 
|  | 117 | if ((uint64_t)size > numAvailable) { | 
| Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 118 | size = numAvailable; | 
|  | 119 | } | 
|  | 120 | } | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 121 | return readAt_l(offset, data, size); | 
| Andreas Huber | 03475f5 | 2009-11-16 15:34:01 -0800 | [diff] [blame] | 122 | } | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 123 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 124 | ssize_t FileSource::readAt_l(off64_t offset, void *data, size_t size) { | 
|  | 125 | off64_t result = lseek64(mFd, offset + mOffset, SEEK_SET); | 
|  | 126 | if (result == -1) { | 
|  | 127 | ALOGE("seek to %lld failed", (long long)(offset + mOffset)); | 
|  | 128 | return UNKNOWN_ERROR; | 
| Gloria Wang | 889b340 | 2011-02-07 11:41:11 -0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 131 | return ::read(mFd, data, size); | 
| Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 134 | status_t FileSource::getSize(off64_t *size) { | 
|  | 135 | Mutex::Autolock autoLock(mLock); | 
|  | 136 |  | 
|  | 137 | if (mFd < 0) { | 
|  | 138 | return NO_INIT; | 
| Gloria Wang | dcd25ef | 2010-06-22 13:55:38 -0700 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 141 | *size = mLength; | 
| Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 142 |  | 
| Dongwon Kang | 9c6f790 | 2019-10-14 11:16:39 -0700 | [diff] [blame] | 143 | return OK; | 
| Andy Hung | d49dbd6 | 2016-07-07 14:20:35 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Andreas Huber | 20111aa | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 146 | }  // namespace android |