#include #include #include #include "sqlite3.h" using std::cout; using std::endl; using std::filesystem::directory_iterator; using std::filesystem::directory_entry; void directoryList(std::string path){ std::filesystem::directory_iterator Start{path}; for (const auto& entry : Start){ std::cout << entry.path() << std::endl; if( entry.is_directory() ){ for (const auto& subentry : std::filesystem::directory_iterator(entry.path())){ std::cout << subentry.path() << std::endl; } } } } void insertEntry(sqlite3* pDB, std::filesystem::directory_entry direntry){ sqlite3_stmt *pstmt = NULL; int rc = 0; rc = sqlite3_prepare_v3(pDB, "INSERT INTO FSCHILD(child_dir) VALUES(?1);", -1, 0, &pstmt, NULL); if( rc ){ std::cerr << sqlite3_errmsg(pDB) << std::endl; } sqlite3_bind_text(pstmt, 1, direntry.path().generic_string().c_str(), -1, SQLITE_TRANSIENT); rc = sqlite3_step(pstmt); if (rc != SQLITE_DONE){ std::cerr << sqlite3_errmsg(pDB) << std::endl; } sqlite3_finalize(pstmt); } void insertDirectoryList(sqlite3* pDB, std::string path){ std::filesystem::directory_iterator Start{path}; for (const auto& entry : Start){ std::cout << entry.path() << std::endl; if( entry.is_directory() ){ std::filesystem::directory_iterator SubPath{entry.path()}; for (const auto & subentry : SubPath){ insertEntry(pDB, subentry); } } } } static int callback(void* data, int colCount, char** colData, char** colName) { std::string val = colData[0]; if (val == "1") { return true; } return false; } int main() { sqlite3* pDB = NULL; int rc = 0; rc = sqlite3_open("fs-meta.db", &pDB); std::string sql("create table FSCHILD(child_dir STRING PRIMARY KEY NOT NULL );"); std::string data("CALLBACK FUNCTION"); rc = sqlite3_exec(pDB, sql.c_str(), callback, (void *)data.c_str(), NULL); if( rc ){ std::cerr << sqlite3_errmsg(pDB) << std::endl; }else{ insertDirectoryList(pDB, "/var"); } sqlite3_close(pDB); }