1 /* 2 * Copyright (C) 2017 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 #include "proto_util.h" 18 19 #include <google/protobuf/io/zero_copy_stream_impl.h> 20 21 #include <android/util/protobuf.h> 22 #include <android/util/ProtoOutputStream.h> 23 #include <android-base/file.h> 24 25 namespace android { 26 namespace os { 27 namespace incidentd { 28 29 using namespace android::base; 30 using namespace android::util; 31 using google::protobuf::io::FileOutputStream; 32 33 // special section ids 34 const int FIELD_ID_INCIDENT_HEADER = 1; 35 36 status_t write_header_section(int fd, const uint8_t* buf, size_t bufSize) { 37 status_t err; 38 39 if (bufSize == 0) { 40 return NO_ERROR; 41 } 42 43 err = write_section_header(fd, FIELD_ID_INCIDENT_HEADER, bufSize); 44 if (err != NO_ERROR) { 45 return err; 46 } 47 48 err = WriteFully(fd, buf, bufSize); 49 if (err != NO_ERROR) { 50 return err; 51 } 52 53 return NO_ERROR; 54 } 55 56 status_t write_section_header(int fd, int sectionId, size_t size) { 57 uint8_t buf[20]; 58 uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size); 59 return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno; 60 } 61 62 status_t write_section(int fd, int sectionId, const MessageLite& message) { 63 status_t err; 64 65 err = write_section_header(fd, sectionId, message.ByteSize()); 66 if (err != NO_ERROR) { 67 return err; 68 } 69 70 FileOutputStream stream(fd); 71 if (!message.SerializeToZeroCopyStream(&stream)) { 72 return stream.GetErrno(); 73 } else { 74 return NO_ERROR; 75 } 76 } 77 78 79 80 } // namespace incidentd 81 } // namespace os 82 } // namespace android 83 84