aboutsummaryrefslogtreecommitdiff
path: root/pw_transfer/public/pw_transfer/atomic_file_transfer_handler.h
blob: 22eb4c04d8505219aaaff3c0e4b704af98a21da8 (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
// Copyright 2022 The Pigweed Authors
//
// 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
//
//     https://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.
#pragma once

#include <filesystem>
#include <string>
#include <string_view>
#include <variant>

#include "pw_status/status.h"
#include "pw_stream/std_file_stream.h"
#include "pw_transfer/handler.h"

namespace pw::transfer {

/// `AtomicFileTransferHandler` is intended to be used as a transfer handler for
/// files. It ensures that the target file of the transfer is always in a
/// correct state. In particular, the transfer is first done to a temporary file
/// and once complete, the original targeted file is updated.
class AtomicFileTransferHandler : public ReadWriteHandler {
 public:
  /// @param[in] resource_id An ID for the resource that's being transferred.
  ///
  /// @param[in] file_path The target file to update.
  AtomicFileTransferHandler(uint32_t resource_id, std::string_view file_path)
      : ReadWriteHandler(resource_id), path_(file_path) {}

  AtomicFileTransferHandler(const AtomicFileTransferHandler& rhs) = delete;
  AtomicFileTransferHandler& operator=(const AtomicFileTransferHandler&) =
      delete;
  ~AtomicFileTransferHandler() override = default;

  /// Prepares `AtomicFileTransferHandler` for a read transfer.
  ///
  /// @pre The read transfer has not been initialized before the call to this
  /// method.
  ///
  /// @returns A `pw::Status` object indicating whether
  /// `AtomicFileTransferHandler` is ready for the transfer.
  Status PrepareRead() override;
  /// Handler function that is called by the transfer thread after a read
  /// transfer completes.
  ///
  /// @param[in] Status A `pw::Status` object provided by the transfer thread
  /// indicating whether the transfer succeeded.
  ///
  /// @pre The read transfer is done before the call to this method.
  void FinalizeRead(Status) override;
  /// Prepares `AtomicFileTransferHandler` for a write transfer.
  ///
  /// @pre The write transfer has not been initialized before the call to this
  /// method.
  ///
  /// @returns A `pw::Status` object indicating whether
  /// `AtomicFileTransferHandler` is ready for the transfer.
  Status PrepareWrite() override;
  /// Indicates whether the write transfer was successful.
  ///
  /// @pre The write transfer is done.
  ///
  /// @returns A `pw::Status` object indicating whether the transfer data was
  /// successfully written.
  Status FinalizeWrite(Status) override;

 private:
  std::string path_;
  std::variant<std::monostate, stream::StdFileReader, stream::StdFileWriter>
      stream_{};
};

}  // namespace pw::transfer