Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ addons:
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
packages:
- libarchive-dev
- libboost-filesystem-dev
- libboost-locale-dev
- libboost-system-dev
- libtdb-dev
- g++-8
- clang-7
Expand Down
17 changes: 4 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ ENDIF(STATIC_LINKS)

FIND_PACKAGE(Boost 1.44
COMPONENTS
filesystem
system
locale
REQUIRED)

Expand All @@ -39,6 +37,8 @@ FIND_PACKAGE(LibArchive 3 REQUIRED)
INCLUDE_DIRECTORIES(${LibArchive_INCLUDE_DIRS})
LIST(APPEND EXTRA_LIBRARIES ${LibArchive_LIBRARIES})

LIST(APPEND EXTRA_LIBRARIES stdc++fs)

###### PROJECT CONFIGURATION ######

### Names ###
Expand All @@ -57,11 +57,6 @@ SET (VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${VERSION_

GET_GIT_HEAD_REVISION(GIT_REFSPEC GIT_HASH GIT_HASH_SHORT)

IF ("${CMAKE_BUILD_TYPE}" MATCHES "^Debug$")
SET (DEBUG 1)
MESSAGE (STATUS "Compiling with debug symbols")
ENDIF()

### Application Configuration ###

SET (DATABASE_PATH ${CMAKE_INSTALL_PREFIX}/var/lib/${BINARY_NAME})
Expand All @@ -73,11 +68,6 @@ CONFIGURE_FILE (
"${PROJECT_BINARY_DIR}/config.cpp"
)

CONFIGURE_FILE (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)

CONFIGURE_FILE (
"${PROJECT_SOURCE_DIR}/cnf-sync.in"
"${PROJECT_BINARY_DIR}/cnf-sync"
Expand All @@ -100,7 +90,8 @@ INCLUDE_DIRECTORIES ("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}")

###### EXECUTABLES and LIBRARIES######

SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror -std=c++14")
SET(CMAKE_CXX_STANDARD 17)
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror")

OPTION(ENABLE_CLANG_TIDY OFF)
IF(ENABLE_CLANG_TIDY)
Expand Down
2 changes: 0 additions & 2 deletions src/config.h.in → src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

#include <string>

#cmakedefine DEBUG

namespace cnf {

extern const std::string PROGRAM_NAME;
Expand Down
6 changes: 3 additions & 3 deletions src/custom_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
along with command-not-found. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef EXCEPTIONS_H_
#define EXCEPTIONS_H_
#ifndef CUSTOM_EXCEPTIONS_H_
#define CUSTOM_EXCEPTIONS_H_

#include <exception>
#include <string>
Expand Down Expand Up @@ -50,4 +50,4 @@ class DatabaseException : public ErrorCodeException {
};
} // namespace cnf

#endif /* EXCEPTIONS_H_ */
#endif /* CUSTOM_EXCEPTIONS_H_ */
99 changes: 50 additions & 49 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
Expand All @@ -33,49 +34,46 @@
#include "db_tdb.h"
#include "similar.h"

namespace bf = boost::filesystem;
using namespace std;
namespace fs = std::filesystem;
using boost::format;
using boost::locale::translate;

namespace cnf {

const shared_ptr<Database> getDatabase(const string& id,
const bool readonly,
const string& base_path) {
return shared_ptr<Database>(new TdbDatabase(id, readonly, base_path));
std::unique_ptr<Database> getDatabase(const std::string& id,
const bool readonly,
const std::string& base_path) {
return std::make_unique<TdbDatabase>(id, readonly, base_path);
}

void getCatalogs(const string& database_path, vector<string>& result) {
void getCatalogs(const std::string& database_path,
std::vector<std::string>& result) {
TdbDatabase::getCatalogs(database_path, result);
}

void lookup(const string& search_string,
const string& database_path,
void lookup(const std::string& search_string,
const std::string& database_path,
ResultMap& result,
vector<string>* const inexact_matches) {
vector<string> catalogs;
std::vector<std::string>* const inexact_matches) {
std::vector<std::string> catalogs;
getCatalogs(database_path, catalogs);

if (!catalogs.empty()) {
vector<string> terms;
std::vector<std::string> terms;
if (inexact_matches) {
terms = similar_words(search_string);
}

for (const auto& catalog : catalogs) {
vector<Package> packs;
std::vector<Package> packs;

try {
auto d = getDatabase(catalog, true, database_path);
if (inexact_matches == nullptr) {
getDatabase(catalog, true, database_path)
->getPackages(search_string, packs);
d->getPackages(search_string, packs);
} else {
const shared_ptr<Database>& d =
getDatabase(catalog, true, database_path);

for (const auto& term : terms) {
vector<Package> tempPack;
std::vector<Package> tempPack;
d->getPackages(term, tempPack);
if (!tempPack.empty()) {
packs.insert(packs.end(), tempPack.begin(),
Expand All @@ -86,7 +84,7 @@ void lookup(const string& search_string,
}

} catch (const DatabaseException& e) {
cerr << e.what() << endl;
std::cerr << e.what() << '\n';
}

if (!packs.empty()) {
Expand All @@ -95,38 +93,39 @@ void lookup(const string& search_string,
}
}
} else {
cout << format(translate("WARNING: No database for lookup!")) << endl;
std::cout << format(translate("WARNING: No database for lookup!"))
<< '\n';
}
}

void populate_mirror(const bf::path& mirror_path,
const string& database_path,
void populate_mirror(const fs::path& mirror_path,
const std::string& database_path,
const bool truncate,
const uint8_t verbosity) {
using dirIter = bf::directory_iterator;
using dirIter = fs::directory_iterator;

static const string architectures[] = {"i686", "x86_64"};
static const std::string architectures[] = {"i686", "x86_64"};

for (const auto& architecture : architectures) {
vector<string> catalogs;
std::vector<std::string> catalogs;

for (dirIter iter = dirIter(mirror_path); iter != dirIter(); ++iter) {
const string& catalog =
bf::path(*iter).stem().string() + "-" + architecture;
const std::string& catalog =
fs::path(*iter).stem().string() + "-" + architecture;

bool truncated = !truncate;

bool list_catalog = false;

bf::path dir = bf::path(*iter) / "os" / architecture;
if (bf::is_directory(dir)) {
fs::path dir = fs::path(*iter) / "os" / architecture;
if (fs::is_directory(dir)) {
populate(dir, database_path, catalog, !truncated, verbosity);
truncated = true;
list_catalog = true;
}

dir = bf::path(*iter) / "os" / "any";
if (bf::is_directory(dir)) {
dir = fs::path(*iter) / "os" / "any";
if (fs::is_directory(dir)) {
populate(dir, database_path, catalog, !truncated, verbosity);
list_catalog = true;
}
Expand All @@ -135,38 +134,39 @@ void populate_mirror(const bf::path& mirror_path,
}
}

const bf::path& catalogs_file_name =
bf::path(database_path) / ("catalogs-" + architecture + "-tdb");
const fs::path& catalogs_file_name =
fs::path(database_path) / ("catalogs-" + architecture + "-tdb");

ofstream catalogs_file;
std::ofstream catalogs_file;

catalogs_file.open(catalogs_file_name.c_str(), ios::trunc | ios::out);
catalogs_file.open(catalogs_file_name.c_str(),
std::ios::trunc | std::ios::out);

for (const auto& catalog : catalogs) {
catalogs_file << catalog << ".tdb" << endl;
catalogs_file << catalog << ".tdb" << '\n';
}
catalogs_file.close();
}
}

void populate(const bf::path& path,
const string& database_path,
const string& catalog,
void populate(const fs::path& path,
const std::string& database_path,
const std::string& catalog,
const bool truncate,
const uint8_t verbosity) {
shared_ptr<Database> d;
std::unique_ptr<Database> d;
try {
d = getDatabase(catalog, false, database_path);
} catch (const DatabaseException& e) {
cerr << e.what() << endl;
std::cerr << e.what() << '\n';
return;
}

if (truncate) {
d->truncate();
}

using dirIter = bf::directory_iterator;
using dirIter = fs::directory_iterator;

uint32_t count = 0;

Expand All @@ -178,23 +178,24 @@ void populate(const bf::path& path,

for (dirIter iter = dirIter(path); iter != dirIter(); ++iter) {
if (verbosity > 0) {
cout << format(translate("[ %d / %d ] %s...")) % ++current % count %
*iter;
cout.flush();
std::cout << format(translate("[ %d / %d ] %s...")) % ++current %
count % *iter;
std::cout.flush();
}
try {
Package p(*iter, true);
d->storePackage(p);
if (verbosity > 0) {
cout << translate("done") << endl;
std::cout << translate("done") << '\n';
}
} catch (const InvalidArgumentException& e) {
if (verbosity > 0) {
cout << format(translate("skipping (%s)")) % e.what() << endl;
std::cout << format(translate("skipping (%s)")) % e.what()
<< '\n';
}
continue;
} catch (const DatabaseException& e) {
cerr << e.what() << endl;
std::cerr << e.what() << '\n';
return;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define DB_H_

#include <cstdint>
#include <filesystem>
#include <map>
#include <set>
#include <string>
Expand Down Expand Up @@ -61,9 +62,9 @@ class Database {

using ResultMap = std::map<std::string, std::set<Package>>;

const std::shared_ptr<Database> getDatabase(const std::string& id,
bool readonly,
const std::string& base_path);
std::unique_ptr<Database> getDatabase(const std::string& id,
bool readonly,
const std::string& base_path);

void getCatalogs(const std::string& database_path,
std::vector<std::string>& result);
Expand All @@ -73,12 +74,12 @@ void lookup(const std::string& search_string,
ResultMap& result,
std::vector<std::string>* inexact_matches = nullptr);

void populate_mirror(const boost::filesystem::path& path,
void populate_mirror(const std::filesystem::path& path,
const std::string& database_path,
bool truncate,
uint8_t verbosity);

void populate(const boost::filesystem::path& path,
void populate(const std::filesystem::path& path,
const std::string& database_path,
const std::string& catalog,
bool truncate,
Expand Down
Loading