2016-02-05 21:01:51 +01:00
|
|
|
/* Copyright 2015, 2016, Michele Santullo
|
2015-11-11 02:01:37 +00:00
|
|
|
* This file is part of "dindexer".
|
|
|
|
*
|
|
|
|
* "dindexer" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "dindexer" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dbbackend.hpp"
|
|
|
|
#include "pq/connection.hpp"
|
2015-12-12 17:23:50 +00:00
|
|
|
#include "dindexer-common/settings.hpp"
|
2016-01-05 12:35:46 +00:00
|
|
|
#include "dindexer-machinery/recorddata.hpp"
|
2015-11-11 02:01:37 +00:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2015-11-11 12:28:04 +00:00
|
|
|
#include <utility>
|
2015-11-27 20:41:36 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <exception>
|
2015-12-01 16:35:46 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <boost/utility/string_ref.hpp>
|
2015-12-29 17:32:22 +00:00
|
|
|
#include <chrono>
|
2015-11-11 02:01:37 +00:00
|
|
|
|
|
|
|
namespace din {
|
2015-11-11 12:28:04 +00:00
|
|
|
namespace {
|
|
|
|
} //unnamed namespace
|
|
|
|
|
2016-01-05 12:49:27 +00:00
|
|
|
bool read_from_db (mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const dinlib::SettingsDB& parDB, const mchlib::TigerHash& parHash) {
|
2015-11-27 20:41:36 +00:00
|
|
|
using boost::lexical_cast;
|
|
|
|
|
|
|
|
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
|
|
|
|
conn.connect();
|
|
|
|
|
|
|
|
uint32_t group_id;
|
|
|
|
{
|
2016-01-05 13:30:17 +00:00
|
|
|
auto resultset = conn.query(
|
|
|
|
"SELECT path,level,group_id,is_directory,is_symlink,size FROM files WHERE hash=$1 LIMIT 1;",
|
|
|
|
tiger_to_string(parHash, true)
|
|
|
|
);
|
2015-11-27 20:41:36 +00:00
|
|
|
if (resultset.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto row = resultset[0];
|
2016-01-11 12:46:06 +00:00
|
|
|
parItem.abs_path = row["path"];
|
2015-12-29 17:32:22 +00:00
|
|
|
parItem.hash = parHash;
|
2015-11-27 20:41:36 +00:00
|
|
|
parItem.level = lexical_cast<uint16_t>(row["level"]);
|
|
|
|
parItem.size = lexical_cast<uint64_t>(row["size"]);
|
|
|
|
parItem.is_directory = (row["is_directory"] == "t" ? true : false);
|
|
|
|
parItem.is_symlink = (row["is_symlink"] == "t" ? true : false);
|
|
|
|
group_id = lexical_cast<uint32_t>(row["group_id"]);
|
2016-01-11 12:46:06 +00:00
|
|
|
|
|
|
|
if (parItem.abs_path.size() != 1 or parItem.abs_path != "/") {
|
|
|
|
parItem.abs_path = std::string("/") + parItem.abs_path;
|
|
|
|
}
|
|
|
|
parItem.path = boost::string_ref(parItem.abs_path).substr(1);
|
2015-11-27 20:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-01-05 13:30:17 +00:00
|
|
|
auto resultset = conn.query(
|
|
|
|
"SELECT \"desc\",\"type\",\"disk_number\" FROM sets WHERE \"id\"=$1;",
|
|
|
|
group_id
|
|
|
|
);
|
2015-11-27 20:41:36 +00:00
|
|
|
if (resultset.empty()) {
|
|
|
|
std::ostringstream err_msg;
|
|
|
|
err_msg << "Missing set: found a record with group_id=" << group_id;
|
|
|
|
err_msg << " but there is no such id in table \"sets\"";
|
|
|
|
throw std::length_error(err_msg.str());
|
|
|
|
}
|
|
|
|
auto row = resultset[0];
|
|
|
|
parSet.type = lexical_cast<char>(row["type"]);
|
|
|
|
parSet.name = row["desc"];
|
|
|
|
parSet.disk_number = lexical_cast<uint32_t>(row["disk_number"]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-21 20:30:38 +00:00
|
|
|
void write_to_db (const dinlib::SettingsDB& parDB, const std::vector<mchlib::FileRecordData>& parData, const mchlib::SetRecordData& parSetData, const std::string& parSignature) {
|
2015-12-29 17:32:22 +00:00
|
|
|
using std::chrono::system_clock;
|
2015-12-31 00:38:44 +00:00
|
|
|
using boost::lexical_cast;
|
2015-12-29 17:32:22 +00:00
|
|
|
|
2015-11-11 02:01:37 +00:00
|
|
|
if (parData.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:08:17 +00:00
|
|
|
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
|
2015-11-11 12:28:04 +00:00
|
|
|
conn.connect();
|
|
|
|
|
2015-12-30 10:20:52 +00:00
|
|
|
conn.query("BEGIN;");
|
2015-12-31 00:38:44 +00:00
|
|
|
uint32_t new_group_id;
|
|
|
|
{
|
2016-01-21 20:30:38 +00:00
|
|
|
auto id_res = conn.query("INSERT INTO \"sets\" "
|
2016-02-22 19:44:48 +01:00
|
|
|
"(\"desc\",\"type\",\"app_name\""
|
|
|
|
",\"content_type\") "
|
|
|
|
"VALUES ($1, $2, $3, $4) RETURNING \"id\";",
|
2015-12-31 00:38:44 +00:00
|
|
|
parSetData.name,
|
2016-02-22 19:44:48 +01:00
|
|
|
boost::string_ref(&parSetData.type, 1),
|
|
|
|
parSignature,
|
|
|
|
boost::string_ref(&parSetData.content_type, 1)
|
2015-12-31 00:38:44 +00:00
|
|
|
);
|
|
|
|
assert(id_res.size() == 1);
|
|
|
|
assert(id_res[0].size() == 1);
|
|
|
|
//TODO: make it possible to get the id directly as a string
|
|
|
|
new_group_id = lexical_cast<uint32_t>(id_res[0][0]);
|
|
|
|
}
|
2015-12-29 18:11:15 +00:00
|
|
|
|
2016-01-11 12:46:06 +00:00
|
|
|
//TODO: remove this empty_path part. This is a temporary fix needed to
|
|
|
|
//work around a bug in libpqtypes for which empty paths are inserted
|
|
|
|
//as null values in the db.
|
2016-01-11 14:19:18 +00:00
|
|
|
const char* const empty_path = "/";
|
2016-01-11 12:46:06 +00:00
|
|
|
const auto empty_path_string = boost::string_ref(empty_path);
|
|
|
|
|
2015-12-29 17:32:22 +00:00
|
|
|
for (std::size_t z = 0; z < parData.size(); ++z) {
|
|
|
|
const std::string query = "INSERT INTO \"files\" (path, hash, "
|
|
|
|
"level, group_id, is_directory, is_symlink, size, "
|
2016-01-06 02:18:42 +00:00
|
|
|
"access_time, modify_time, is_hash_valid, unreadable, "
|
|
|
|
"mimetype, charset) VALUES "
|
|
|
|
"($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);";
|
2015-12-29 17:32:22 +00:00
|
|
|
|
|
|
|
const auto& itm = parData[z];
|
2016-01-11 12:46:06 +00:00
|
|
|
assert(itm.path.data());
|
2015-12-30 10:20:52 +00:00
|
|
|
conn.query(query,
|
2016-01-11 12:46:06 +00:00
|
|
|
(itm.path.empty() ? empty_path_string : itm.path),
|
2015-12-29 17:32:22 +00:00
|
|
|
tiger_to_string(itm.hash),
|
|
|
|
itm.level,
|
2015-12-31 00:38:44 +00:00
|
|
|
new_group_id,
|
2015-12-29 17:32:22 +00:00
|
|
|
itm.is_directory,
|
|
|
|
itm.is_symlink,
|
|
|
|
itm.size,
|
|
|
|
system_clock::from_time_t(itm.atime),
|
|
|
|
system_clock::from_time_t(itm.mtime),
|
|
|
|
itm.hash_valid,
|
2016-01-06 02:18:42 +00:00
|
|
|
itm.unreadable,
|
2016-01-11 12:47:17 +00:00
|
|
|
itm.mime_type,
|
|
|
|
itm.mime_charset
|
2015-12-29 17:32:22 +00:00
|
|
|
);
|
2015-11-11 12:35:52 +00:00
|
|
|
}
|
2015-12-30 10:20:52 +00:00
|
|
|
conn.query("COMMIT;");
|
2015-11-11 02:01:37 +00:00
|
|
|
}
|
|
|
|
} //namespace din
|