summaryrefslogtreecommitdiff
path: root/brillo/streams/stream_utils.h
blob: 981a6d53252d4d629637c10eb5d5c8b3b550e3f1 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBBRILLO_BRILLO_STREAMS_STREAM_UTILS_H_
#define LIBBRILLO_BRILLO_STREAMS_STREAM_UTILS_H_

#include <base/location.h>
#include <brillo/brillo_export.h>
#include <brillo/streams/stream.h>

namespace brillo {
namespace stream_utils {

// Generates "Stream closed" error and returns false.
BRILLO_EXPORT bool ErrorStreamClosed(
    const tracked_objects::Location& location, ErrorPtr* error);

// Generates "Not supported" error and returns false.
BRILLO_EXPORT bool ErrorOperationNotSupported(
    const tracked_objects::Location& location, ErrorPtr* error);

// Generates "Read past end of stream" error and returns false.
BRILLO_EXPORT bool ErrorReadPastEndOfStream(
    const tracked_objects::Location& location, ErrorPtr* error);

// Generates "Operation time out" error and returns false.
BRILLO_EXPORT bool ErrorOperationTimeout(
    const tracked_objects::Location& location, ErrorPtr* error);

// Checks if |position| + |offset| fit within the constraint of positive
// signed int64_t type. We use uint64_t for absolute stream pointer positions,
// however many implementations, including file-descriptor-based I/O do not
// support the full extent of unsigned 64 bit numbers. So we restrict the file
// positions to what can fit in the signed 64 bit value (that is, we support
// "only" up to 9 exabytes, instead of the possible 18).
// The |location| parameter will be used to report the origin of the error
// if one is generated/triggered.
BRILLO_EXPORT bool CheckInt64Overflow(
    const tracked_objects::Location& location,
    uint64_t position,
    int64_t offset,
    ErrorPtr* error);

// Helper function to calculate the stream position based on the current
// stream position and offset. Returns true and the new calculated stream
// position in |new_position| if successful. In case of invalid stream
// position (negative values or out of range of signed 64 bit values), returns
// false and "invalid_parameter" |error|.
// The |location| parameter will be used to report the origin of the error
// if one is generated/triggered.
BRILLO_EXPORT bool CalculateStreamPosition(
    const tracked_objects::Location& location,
    int64_t offset,
    Stream::Whence whence,
    uint64_t current_position,
    uint64_t stream_size,
    uint64_t* new_position,
    ErrorPtr* error);

// Checks if |mode| allows read access.
inline bool IsReadAccessMode(Stream::AccessMode mode) {
  return mode == Stream::AccessMode::READ ||
         mode == Stream::AccessMode::READ_WRITE;
}

// Checks if |mode| allows write access.
inline bool IsWriteAccessMode(Stream::AccessMode mode) {
  return mode == Stream::AccessMode::WRITE ||
         mode == Stream::AccessMode::READ_WRITE;
}

// Make the access mode based on read/write rights requested.
inline Stream::AccessMode MakeAccessMode(bool read, bool write) {
  CHECK(read || write);  // Either read or write (or both) must be specified.
  if (read && write)
    return Stream::AccessMode::READ_WRITE;
  return write ? Stream::AccessMode::WRITE : Stream::AccessMode::READ;
}

using CopyDataSuccessCallback =
    base::Callback<void(StreamPtr, StreamPtr, uint64_t)>;
using CopyDataErrorCallback =
    base::Callback<void(StreamPtr, StreamPtr, const brillo::Error*)>;

// Asynchronously copies data from input stream to output stream until all the
// data from the input stream is read. The function takes ownership of both
// streams for the duration of the operation and then gives them back when
// either the |success_callback| or |error_callback| is called.
// |success_callback| also provides the number of bytes actually copied.
// This variant of CopyData uses internal buffer of 4 KiB for the operation.
BRILLO_EXPORT void CopyData(StreamPtr in_stream,
                            StreamPtr out_stream,
                            const CopyDataSuccessCallback& success_callback,
                            const CopyDataErrorCallback& error_callback);

// Asynchronously copies data from input stream to output stream until the
// maximum amount of data specified in |max_size_to_copy| is copied or the end
// of the input stream is encountered. The function takes ownership of both
// streams for the duration of the operation and then gives them back when
// either the |success_callback| or |error_callback| is called.
// |success_callback| also provides the number of bytes actually copied.
// |buffer_size| specifies the size of the read buffer to use for the operation.
BRILLO_EXPORT void CopyData(StreamPtr in_stream,
                            StreamPtr out_stream,
                            uint64_t max_size_to_copy,
                            size_t buffer_size,
                            const CopyDataSuccessCallback& success_callback,
                            const CopyDataErrorCallback& error_callback);

}  // namespace stream_utils
}  // namespace brillo

#endif  // LIBBRILLO_BRILLO_STREAMS_STREAM_UTILS_H_