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 <binder/IServiceManager.h>
22 #include <com/android/internal/os/IDropBoxManagerService.h>
23 #include <cutils/log.h>
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 
29 namespace android {
30 namespace os {
31 
32 using namespace ::com::android::internal::os;
33 
Entry()34 DropBoxManager::Entry::Entry()
35     :mTag(),
36      mTimeMillis(0),
37      mFlags(IS_EMPTY),
38      mData(),
39      mFd()
40 {
41     mFlags = IS_EMPTY;
42 }
43 
Entry(const String16 & tag,int32_t flags)44 DropBoxManager::Entry::Entry(const String16& tag, int32_t flags)
45     :mTag(tag),
46      mTimeMillis(0),
47      mFlags(flags),
48      mData(),
49      mFd()
50 {
51 }
52 
Entry(const String16 & tag,int32_t flags,int fd)53 DropBoxManager::Entry::Entry(const String16& tag, int32_t flags, int fd)
54     :mTag(tag),
55      mTimeMillis(0),
56      mFlags(flags),
57      mData(),
58      mFd(fd)
59 {
60 }
61 
~Entry()62 DropBoxManager::Entry::~Entry()
63 {
64 }
65 
66 status_t
writeToParcel(Parcel * out) const67 DropBoxManager::Entry::writeToParcel(Parcel* out) const
68 {
69     status_t err;
70 
71     err = out->writeString16(mTag);
72     if (err != NO_ERROR) {
73         return err;
74     }
75 
76     err = out->writeInt64(mTimeMillis);
77     if (err != NO_ERROR) {
78         return err;
79     }
80 
81     if (mFd.get() != -1) {
82         err = out->writeInt32(mFlags & ~HAS_BYTE_ARRAY);  // Clear bit just to be safe
83         if (err != NO_ERROR) {
84             return err;
85         }
86         ALOGD("writing fd %d\n", mFd.get());
87         err = out->writeParcelFileDescriptor(mFd);
88         if (err != NO_ERROR) {
89             return err;
90         }
91     } else {
92         err = out->writeInt32(mFlags | HAS_BYTE_ARRAY);
93         if (err != NO_ERROR) {
94             return err;
95         }
96         err = out->writeByteVector(mData);
97         if (err != NO_ERROR) {
98             return err;
99         }
100     }
101     return NO_ERROR;
102 }
103 
104 status_t
readFromParcel(const Parcel * in)105 DropBoxManager::Entry::readFromParcel(const Parcel* in)
106 {
107     status_t err;
108 
109     err = in->readString16(&mTag);
110     if (err != NO_ERROR) {
111         return err;
112     }
113 
114     err = in->readInt64(&mTimeMillis);
115     if (err != NO_ERROR) {
116         return err;
117     }
118 
119     err = in->readInt32(&mFlags);
120     if (err != NO_ERROR) {
121         return err;
122     }
123 
124     if ((mFlags & HAS_BYTE_ARRAY) != 0) {
125         err = in->readByteVector(&mData);
126         if (err != NO_ERROR) {
127             return err;
128         }
129         mFlags &= ~HAS_BYTE_ARRAY;
130     } else {
131         int fd;
132         fd = in->readParcelFileDescriptor();
133         if (fd == -1) {
134             return EBADF;
135         }
136         fd = dup(fd);
137         if (fd == -1) {
138             return errno;
139         }
140         mFd.reset(fd);
141     }
142 
143     return NO_ERROR;
144 }
145 
146 
DropBoxManager()147 DropBoxManager::DropBoxManager()
148 {
149 }
150 
~DropBoxManager()151 DropBoxManager::~DropBoxManager()
152 {
153 }
154 
155 Status
addText(const String16 & tag,const string & text)156 DropBoxManager::addText(const String16& tag, const string& text)
157 {
158     Entry entry(tag, IS_TEXT);
159     entry.mData.assign(text.c_str(), text.c_str() + text.size());
160     return add(entry);
161 }
162 
163 Status
addData(const String16 & tag,uint8_t const * data,size_t size,int flags)164 DropBoxManager::addData(const String16& tag, uint8_t const* data,
165         size_t size, int flags)
166 {
167     Entry entry(tag, flags);
168     entry.mData.assign(data, data+size);
169     return add(entry);
170 }
171 
172 Status
addFile(const String16 & tag,const string & filename,int flags)173 DropBoxManager::addFile(const String16& tag, const string& filename, int flags)
174 {
175     int fd = open(filename.c_str(), O_RDONLY);
176     if (fd == -1) {
177         string message("addFile can't open file: ");
178         message += filename;
179         ALOGW("DropboxManager: %s", message.c_str());
180         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, message.c_str());
181     }
182 
183     Entry entry(tag, flags, fd);
184     return add(entry);
185 }
186 
187 Status
add(const Entry & entry)188 DropBoxManager::add(const Entry& entry)
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     return service->add(entry);
196 }
197 
198 }} // namespace android::os
199 
200