blob: 60bb00ab730933992175248ef6bc84393974107d [file] [log] [blame]
Joe Onoratode5b027d2016-11-23 16:16:12 -08001/*
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
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -070021#include <android-base/unique_fd.h>
Joe Onoratode5b027d2016-11-23 16:16:12 -080022#include <binder/IServiceManager.h>
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -070023#include <binder/ParcelFileDescriptor.h>
Joe Onoratode5b027d2016-11-23 16:16:12 -080024#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
31namespace android {
32namespace os {
33
34using namespace ::com::android::internal::os;
35
36DropBoxManager::Entry::Entry()
37 :mTag(),
38 mTimeMillis(0),
39 mFlags(IS_EMPTY),
40 mData(),
41 mFd()
42{
43 mFlags = IS_EMPTY;
44}
45
46DropBoxManager::Entry::Entry(const String16& tag, int32_t flags)
47 :mTag(tag),
48 mTimeMillis(0),
49 mFlags(flags),
50 mData(),
51 mFd()
52{
53}
54
55DropBoxManager::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
64DropBoxManager::Entry::~Entry()
65{
66}
67
68status_t
69DropBoxManager::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
106status_t
107DropBoxManager::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
Yao Chen482d2722017-09-12 13:25:43 -0700148const vector<uint8_t>&
149DropBoxManager::Entry::getData() const
150{
151 return mData;
152}
153
154const unique_fd&
155DropBoxManager::Entry::getFd() const
156{
157 return mFd;
158}
159
160int32_t
161DropBoxManager::Entry::getFlags() const
162{
163 return mFlags;
164}
165
166int64_t
167DropBoxManager::Entry::getTimestamp() const
168{
169 return mTimeMillis;
170}
Joe Onoratode5b027d2016-11-23 16:16:12 -0800171
172DropBoxManager::DropBoxManager()
173{
174}
175
176DropBoxManager::~DropBoxManager()
177{
178}
179
180Status
181DropBoxManager::addText(const String16& tag, const string& text)
182{
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -0700183 return addData(tag, reinterpret_cast<uint8_t const*>(text.c_str()), text.size(), IS_TEXT);
Joe Onoratode5b027d2016-11-23 16:16:12 -0800184}
185
186Status
187DropBoxManager::addData(const String16& tag, uint8_t const* data,
188 size_t size, int flags)
189{
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -0700190 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);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +0000199 ALOGD("service->add returned %s", status.toString8().c_str());
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -0700200 return status;
Joe Onoratode5b027d2016-11-23 16:16:12 -0800201}
202
203Status
204DropBoxManager::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 }
Sami Kyostila52c866e2018-01-12 18:28:13 +0000213 return addFile(tag, fd, flags);
214}
Joe Onoratode5b027d2016-11-23 16:16:12 -0800215
Sami Kyostila52c866e2018-01-12 18:28:13 +0000216Status
217DropBoxManager::addFile(const String16& tag, int fd, int flags)
218{
Primiano Tucci37293892018-01-26 17:28:58 +0000219 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 }
Joe Onoratode5b027d2016-11-23 16:16:12 -0800224 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 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000229 ALOGD("About to call service->add()");
Jeff Sharkey8eebdbe2021-01-11 12:40:49 -0700230 android::base::unique_fd uniqueFd(fd);
231 android::os::ParcelFileDescriptor parcelFd(std::move(uniqueFd));
232 Status status = service->addFile(tag, parcelFd, flags);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +0000233 ALOGD("service->add returned %s", status.toString8().c_str());
Joe Onorato99598ee2019-02-11 15:55:13 +0000234 return status;
Joe Onoratode5b027d2016-11-23 16:16:12 -0800235}
236
Joe Onoratode5b027d2016-11-23 16:16:12 -0800237}} // namespace android::os