summaryrefslogtreecommitdiff
path: root/src/serialize/protobuf_io.h
blob: 1092fd7a4a15f8a1af1fb7764df8f556238b4ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SERIALIZE_PROTOBUF_IO_H_
#define SERIALIZE_PROTOBUF_IO_H_

#include "common/expected.h"
#include "serialize/arena_ptr.h"
#include "system/iorap/src/serialize/TraceFile.pb.h"

#include <string>
#include <string_view>

namespace iorap {
namespace serialize {

// XX: either the namespace should be called pb|proto[buf]
// or we should hide the protobuf-ness from the names and call this class "IO" , "Reader", etc?
// although an obvious name might be the "OpenFactory" or "ProtobufFacade" that reads too much
// like a bad joke.

// Helpers to read a TraceFile protobuf from a file [descriptor].
class ProtobufIO {
 public:
  // XX: proto::TraceFile seems annoying, maybe just serialize::TraceFile ?

  // Open the protobuf associated at the filepath. Returns null on failure.
  static ArenaPtr<proto::TraceFile> Open(std::string file_path);
  // Open the protobuf from the file descriptor. Returns null on failure.
  static ArenaPtr<proto::TraceFile> Open(int fd, const char* file_path = "<unknown>");

  // Save the protobuf by overwriting the file at file_path.
  // The file state is indeterminate at failure.
  // Returns # of bytes written out on success, otherwise the errno value.
  static iorap::expected<size_t /*bytes written*/, int /*errno*/> WriteFully(
      const ::google::protobuf::MessageLite& message,
      std::string_view file_path);
  // Save the protobuf by truncating the file already open at 'fd'.
  // The file state is indeterminate at failure.
  // Returns # of bytes written out on success, otherwise the errno value.
  static iorap::expected<size_t /*bytes written*/, int /*errno*/> WriteFully(
      const ::google::protobuf::MessageLite& message,
      int fd,
      std::string_view file_path = "<unknown>");

  ProtobufIO() = delete;
};

}  // namespace serialize
}  // namespace iorap

#endif