rippled
Download.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2021 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/rdb/Download.h>
21 #include <soci/sqlite3/soci-sqlite3.h>
22 
23 namespace ripple {
24 
27  DatabaseCon::Setup const& setup,
28  boost::filesystem::path const& path)
29 {
30  // SOCI requires boost::optional (not std::optional) as the parameter.
31  boost::optional<std::string> pathFromDb;
32  boost::optional<std::uint64_t> size;
33 
34  auto conn = std::make_unique<DatabaseCon>(
35  setup, "Download", DownloaderDBPragma, DatabaseBodyDBInit);
36 
37  auto& session = *conn->checkoutDb();
38 
39  session << "SELECT Path FROM Download WHERE Part=0;",
40  soci::into(pathFromDb);
41 
42  // Try to reuse preexisting
43  // database.
44  if (pathFromDb)
45  {
46  // Can't resuse - database was
47  // from a different file download.
48  if (pathFromDb != path.string())
49  {
50  session << "DROP TABLE Download;";
51  }
52 
53  // Continuing a file download.
54  else
55  {
56  session << "SELECT SUM(LENGTH(Data)) FROM Download;",
57  soci::into(size);
58  }
59  }
60 
61  return {std::move(conn), (size ? *size : std::optional<std::uint64_t>())};
62 }
63 
66  soci::session& session,
67  std::string const& data,
68  std::string const& path,
69  std::uint64_t fileSize,
70  std::uint64_t part,
71  std::uint16_t maxRowSizePad)
72 {
73  std::uint64_t rowSize = 0;
74  soci::indicator rti;
75 
76  std::uint64_t remainingInRow = 0;
77 
78  auto be =
79  dynamic_cast<soci::sqlite3_session_backend*>(session.get_backend());
80  BOOST_ASSERT(be);
81 
82  // This limits how large we can make the blob
83  // in each row. Also subtract a pad value to
84  // account for the other values in the row.
85  auto const blobMaxSize =
86  sqlite_api::sqlite3_limit(be->conn_, SQLITE_LIMIT_LENGTH, -1) -
87  maxRowSizePad;
88 
89  std::string newpath;
90 
91  auto rowInit = [&] {
92  session << "INSERT INTO Download VALUES (:path, zeroblob(0), 0, :part)",
93  soci::use(newpath), soci::use(part);
94 
95  remainingInRow = blobMaxSize;
96  rowSize = 0;
97  };
98 
99  session << "SELECT Path,Size,Part FROM Download ORDER BY Part DESC "
100  "LIMIT 1",
101  soci::into(newpath), soci::into(rowSize), soci::into(part, rti);
102 
103  if (!session.got_data())
104  {
105  newpath = path;
106  rowInit();
107  }
108  else
109  remainingInRow = blobMaxSize - rowSize;
110 
111  auto insert = [&session, &rowSize, &part, &fs = fileSize](
112  auto const& data) {
113  std::uint64_t updatedSize = rowSize + data.size();
114 
115  session << "UPDATE Download SET Data = CAST(Data || :data AS blob), "
116  "Size = :size WHERE Part = :part;",
117  soci::use(data), soci::use(updatedSize), soci::use(part);
118 
119  fs += data.size();
120  };
121 
122  size_t currentBase = 0;
123 
124  while (currentBase + remainingInRow < data.size())
125  {
126  if (remainingInRow)
127  {
128  insert(data.substr(currentBase, remainingInRow));
129  currentBase += remainingInRow;
130  }
131 
132  ++part;
133  rowInit();
134  }
135 
136  insert(data.substr(currentBase));
137 
138  return part;
139 }
140 
141 void
142 databaseBodyFinish(soci::session& session, std::ofstream& fout)
143 {
144  soci::rowset<std::string> rs =
145  (session.prepare << "SELECT Data FROM Download ORDER BY PART ASC;");
146 
147  // iteration through the resultset:
148  for (auto it = rs.begin(); it != rs.end(); ++it)
149  fout.write(it->data(), it->size());
150 }
151 
152 } // namespace ripple
ripple::databaseBodyDoPut
std::uint64_t databaseBodyDoPut(soci::session &session, std::string const &data, std::string const &path, std::uint64_t fileSize, std::uint64_t part, std::uint16_t maxRowSizePad)
databaseBodyDoPut Saves a new fragment of a downloaded file.
Definition: Download.cpp:65
std::string
STL class.
ripple::DatabaseCon::Setup
Definition: DatabaseCon.h:84
std::pair
std::ofstream::write
T write(T... args)
ripple::DownloaderDBPragma
static constexpr std::array< char const *, 2 > DownloaderDBPragma
Definition: DBInit.h:255
std::ofstream
STL class.
std::uint64_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::optional
ripple::openDatabaseBodyDb
std::pair< std::unique_ptr< DatabaseCon >, std::optional< std::uint64_t > > openDatabaseBodyDb(DatabaseCon::Setup const &setup, boost::filesystem::path const &path)
openDatabaseBodyDb Opens a database that will store the contents of a file being downloaded,...
Definition: Download.cpp:26
ripple::databaseBodyFinish
void databaseBodyFinish(soci::session &session, std::ofstream &fout)
databaseBodyFinish Finishes the download process and writes the file to disk.
Definition: Download.cpp:142
ripple::DatabaseBodyDBInit
static constexpr std::array< char const *, 3 > DatabaseBodyDBInit
Definition: DBInit.h:268