summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_cksum_pipe.cc
blob: 0bec8d341af078bbc3eca7f20c90c1126b35c599 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright contributors to the Ceph project
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "rgw_cksum_pipe.h"
#include <memory>
#include <string>
#include <fmt/format.h>
#include <boost/algorithm/string.hpp>
#include "rgw_cksum.h"
#include "rgw_common.h"
#include "common/dout.h"
#include "rgw_client_io.h"

namespace rgw::putobj {

  RGWPutObj_Cksum::RGWPutObj_Cksum(rgw::sal::DataProcessor* next,
				   rgw::cksum::Type _typ,
				   cksum_hdr_t&& _hdr)
    : Pipe(next),
      _type(_typ),
      dv(rgw::cksum::digest_factory(_type)),
      _digest(cksum::get_digest(dv)), cksum_hdr(_hdr)
  {}

  std::unique_ptr<RGWPutObj_Cksum> RGWPutObj_Cksum::Factory(
    rgw::sal::DataProcessor* next, const RGWEnv& env,
    rgw::cksum::Type override_type)
  {
    /* look for matching headers */
    auto algo_header = cksum_algorithm_hdr(env);
    if (algo_header.first) {
      if (algo_header.second) {
	auto cksum_type = cksum::parse_cksum_type(algo_header.second);
	return
	  std::make_unique<RGWPutObj_Cksum>(
				    next, cksum_type, std::move(algo_header));
      }
      /* malformed checksum algorithm header(s) */
      throw rgw::io::Exception(EINVAL, std::system_category());
    }
    /* no checksum header */
    if (override_type != rgw::cksum::Type::none) {
      /* XXXX safe? do we need to fixup env as well? */
      auto algo_header = cksum_algorithm_hdr(override_type);
      return
	std::make_unique<RGWPutObj_Cksum>(
			   next, override_type, std::move(algo_header));
    }
    return std::unique_ptr<RGWPutObj_Cksum>();
  }

  int RGWPutObj_Cksum::process(ceph::buffer::list &&data, uint64_t logical_offset)
  {
    for (const auto& ptr : data.buffers()) {
      _digest->Update(reinterpret_cast<const unsigned char*>(ptr.c_str()),
                      ptr.length());
    }
    return Pipe::process(std::move(data), logical_offset);
  }

} // namespace rgw::putobj