blob: 4e8ff71b377763808c576321aefe6a8b6e373323 [file] [log] [blame]
Joe Onorato99598ee2019-02-11 15:55:13 +00001/*
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
25namespace android {
26namespace os {
27namespace incidentd {
28
29using namespace android::base;
30using namespace android::util;
31using google::protobuf::io::FileOutputStream;
32
33// special section ids
34const int FIELD_ID_INCIDENT_HEADER = 1;
35
Yao Chencbafce92019-04-01 15:56:44 -070036status_t write_header_section(int fd, const uint8_t* buf, size_t bufSize) {
Joe Onorato99598ee2019-02-11 15:55:13 +000037 status_t err;
Joe Onorato99598ee2019-02-11 15:55:13 +000038
Yao Chencbafce92019-04-01 15:56:44 -070039 if (bufSize == 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +000040 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
Yao Chencbafce92019-04-01 15:56:44 -070048 err = WriteFully(fd, buf, bufSize);
Joe Onorato99598ee2019-02-11 15:55:13 +000049 if (err != NO_ERROR) {
50 return err;
51 }
52
53 return NO_ERROR;
54}
55
56status_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
62status_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