1 /*
2 * Copyright (C) 2016 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_TAG "DropBoxManager"
18
19 #include <android/os/DropBoxManager.h>
20
21 #include <android-base/unique_fd.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ParcelFileDescriptor.h>
24 #include <com/android/internal/os/IDropBoxManagerService.h>
25 #include <cutils/log.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30
31 namespace android {
32 namespace os {
33
34 using namespace ::com::android::internal::os;
35
Entry()36 DropBoxManager::Entry::Entry()
37 :mTag(),
38 mTimeMillis(0),
39 mFlags(IS_EMPTY),
40 mData(),
41 mFd()
42 {
43 mFlags = IS_EMPTY;
44 }
45
Entry(const String16 & tag,int32_t flags)46 DropBoxManager::Entry::Entry(const String16& tag, int32_t flags)
47 :mTag(tag),
48 mTimeMillis(0),
49 mFlags(flags),
50 mData(),
51 mFd()
52 {
53 }
54
Entry(const String16 & tag,int32_t flags,int fd)55 DropBoxManager::Entry::Entry(const String16& tag, int32_t flags, int fd)
56 :mTag(tag),
57 mTimeMillis(0),
58 mFlags(flags),
59 mData(),
60 mFd(fd)
61 {
62 }
63
~Entry()64 DropBoxManager::Entry::~Entry()
65 {
66 }
67
68 status_t
writeToParcel(Parcel * out) const69 DropBoxManager::Entry::writeToParcel(Parcel* out) const
70 {
71 status_t err;
72
73 err = out->writeString16(mTag);
74 if (err != NO_ERROR) {
75 return err;
76 }
77
78 err = out->writeInt64(mTimeMillis);
79 if (err != NO_ERROR) {
80 return err;
81 }
82
83 if (mFd.get() != -1) {
84 err = out->writeInt32(mFlags & ~HAS_BYTE_ARRAY); // Clear bit just to be safe
85 if (err != NO_ERROR) {
86 return err;
87 }
88 ALOGD("writing fd %d\n", mFd.get());
89 err = out->writeParcelFileDescriptor(mFd);
90 if (err != NO_ERROR) {
91 return err;
92 }
93 } else {
94 err = out->writeInt32(mFlags | HAS_BYTE_ARRAY);
95 if (err != NO_ERROR) {
96 return err;
97 }
98 err = out->writeByteVector(mData);
99 if (err != NO_ERROR) {
100 return err;
101 }
102 }
103 return NO_ERROR;
104 }
105
106 status_t
readFromParcel(const Parcel * in)107 DropBoxManager::Entry::readFromParcel(const Parcel* in)
108 {
109 status_t err;
110
111 err = in->readString16(&mTag);
112 if (err != NO_ERROR) {
113 return err;
114 }
115
116 err = in->readInt64(&mTimeMillis);
117 if (err != NO_ERROR) {
118 return err;
119 }
120
121 err = in->readInt32(&mFlags);
122 if (err != NO_ERROR) {
123 return err;
124 }
125
126 if ((mFlags & HAS_BYTE_ARRAY) != 0) {
127 err = in->readByteVector(&mData);
128 if (err != NO_ERROR) {
129 return err;
130 }
131 mFlags &= ~HAS_BYTE_ARRAY;
132 } else {
133 int fd;
134 fd = in->readParcelFileDescriptor();
135 if (fd == -1) {
136 return EBADF;
137 }
138 fd = dup(fd);
139 if (fd == -1) {
140 return errno;
141 }
142 mFd.reset(fd);
143 }
144
145 return NO_ERROR;
146 }
147
148 const vector<uint8_t>&
getData() const149 DropBoxManager::Entry::getData() const
150 {
151 return mData;
152 }
153
154 const unique_fd&
getFd() const155 DropBoxManager::Entry::getFd() const
156 {
157 return mFd;
158 }
159
160 int32_t
getFlags() const161 DropBoxManager::Entry::getFlags() const
162 {
163 return mFlags;
164 }
165
166 int64_t
getTimestamp() const167 DropBoxManager::Entry::getTimestamp() const
168 {
169 return mTimeMillis;
170 }
171
DropBoxManager()172 DropBoxManager::DropBoxManager()
173 {
174 }
175
~DropBoxManager()176 DropBoxManager::~DropBoxManager()
177 {
178 }
179
180 Status
addText(const String16 & tag,const string & text)181 DropBoxManager::addText(const String16& tag, const string& text)
182 {
183 return addData(tag, reinterpret_cast<uint8_t const*>(text.c_str()), text.size(), IS_TEXT);
184 }
185
186 Status
addData(const String16 & tag,uint8_t const * data,size_t size,int flags)187 DropBoxManager::addData(const String16& tag, uint8_t const* data,
188 size_t size, int flags)
189 {
190 sp<IDropBoxManagerService> service = interface_cast<IDropBoxManagerService>(
191 defaultServiceManager()->getService(android::String16("dropbox")));
192 if (service == NULL) {
193 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "can't find dropbox service");
194 }
195 ALOGD("About to call service->add()");
196 vector<uint8_t> dataArg;
197 dataArg.assign(data, data + size);
198 Status status = service->addData(tag, dataArg, flags);
199 ALOGD("service->add returned %s", status.toString8().string());
200 return status;
201 }
202
203 Status
addFile(const String16 & tag,const string & filename,int flags)204 DropBoxManager::addFile(const String16& tag, const string& filename, int flags)
205 {
206 int fd = open(filename.c_str(), O_RDONLY);
207 if (fd == -1) {
208 string message("addFile can't open file: ");
209 message += filename;
210 ALOGW("DropboxManager: %s", message.c_str());
211 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, message.c_str());
212 }
213 return addFile(tag, fd, flags);
214 }
215
216 Status
addFile(const String16 & tag,int fd,int flags)217 DropBoxManager::addFile(const String16& tag, int fd, int flags)
218 {
219 if (fd == -1) {
220 string message("invalid fd (-1) passed to to addFile");
221 ALOGW("DropboxManager: %s", message.c_str());
222 return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, message.c_str());
223 }
224 sp<IDropBoxManagerService> service = interface_cast<IDropBoxManagerService>(
225 defaultServiceManager()->getService(android::String16("dropbox")));
226 if (service == NULL) {
227 return Status::fromExceptionCode(Status::EX_NULL_POINTER, "can't find dropbox service");
228 }
229 ALOGD("About to call service->add()");
230 android::base::unique_fd uniqueFd(fd);
231 android::os::ParcelFileDescriptor parcelFd(std::move(uniqueFd));
232 Status status = service->addFile(tag, parcelFd, flags);
233 ALOGD("service->add returned %s", status.toString8().string());
234 return status;
235 }
236
237 }} // namespace android::os
238