• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SERIALIZE_PROTOBUF_IO_H_
16 #define SERIALIZE_PROTOBUF_IO_H_
17 
18 #include "common/expected.h"
19 #include "serialize/arena_ptr.h"
20 #include "system/iorap/src/serialize/TraceFile.pb.h"
21 
22 #include <string>
23 #include <string_view>
24 
25 namespace iorap {
26 namespace serialize {
27 
28 // XX: either the namespace should be called pb|proto[buf]
29 // or we should hide the protobuf-ness from the names and call this class "IO" , "Reader", etc?
30 // although an obvious name might be the "OpenFactory" or "ProtobufFacade" that reads too much
31 // like a bad joke.
32 
33 // Helpers to read a TraceFile protobuf from a file [descriptor].
34 class ProtobufIO {
35  public:
36   // XX: proto::TraceFile seems annoying, maybe just serialize::TraceFile ?
37 
38   // Open the protobuf associated at the filepath. Returns null on failure.
39   static ArenaPtr<proto::TraceFile> Open(std::string file_path);
40   // Open the protobuf from the file descriptor. Returns null on failure.
41   static ArenaPtr<proto::TraceFile> Open(int fd, const char* file_path = "<unknown>");
42 
43   // Save the protobuf by overwriting the file at file_path.
44   // The file state is indeterminate at failure.
45   // Returns # of bytes written out on success, otherwise the errno value.
46   static iorap::expected<size_t /*bytes written*/, int /*errno*/> WriteFully(
47       const ::google::protobuf::MessageLite& message,
48       std::string_view file_path);
49   // Save the protobuf by truncating the file already open at 'fd'.
50   // The file state is indeterminate at failure.
51   // Returns # of bytes written out on success, otherwise the errno value.
52   static iorap::expected<size_t /*bytes written*/, int /*errno*/> WriteFully(
53       const ::google::protobuf::MessageLite& message,
54       int fd,
55       std::string_view file_path = "<unknown>");
56 
57   ProtobufIO() = delete;
58 };
59 
60 }  // namespace serialize
61 }  // namespace iorap
62 
63 #endif
64 
65