From ddb0c32a0efe9e545632ec75df5173b74ab404f2 Mon Sep 17 00:00:00 2001 From: sherwinp Date: Mon, 26 May 2025 22:24:37 -0400 Subject: [PATCH] add dirscan --- CMakeLists.txt | 7 +++ db_script.sql | 9 ++++ db_script_create_fs-meta.sql | 5 +++ dirscan/CMakeLists.txt | 11 +++++ dirscan/dirscan.cpp | 84 ++++++++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 db_script.sql create mode 100644 db_script_create_fs-meta.sql create mode 100644 dirscan/CMakeLists.txt create mode 100644 dirscan/dirscan.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..eac9bdb --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +# CMakeList.txt : + +cmake_minimum_required (VERSION 3.16) + +project (xtfs VERSION 1.0 LANGUAGES CXX) + +add_subdirectory(dirscan) \ No newline at end of file diff --git a/db_script.sql b/db_script.sql new file mode 100644 index 0000000..a799956 --- /dev/null +++ b/db_script.sql @@ -0,0 +1,9 @@ + + + create table FSCHILD( + child_dir STRING PRIMARY KEY NOT NULL + ); + + DROP TABLE FSCHILD; + +SELECT child_dir FROM FSCHILD group by soundex(child_dir) diff --git a/db_script_create_fs-meta.sql b/db_script_create_fs-meta.sql new file mode 100644 index 0000000..2f6903e --- /dev/null +++ b/db_script_create_fs-meta.sql @@ -0,0 +1,5 @@ + create table FSCHILD( + child_dir STRING PRIMARY KEY NOT NULL + ); + + diff --git a/dirscan/CMakeLists.txt b/dirscan/CMakeLists.txt new file mode 100644 index 0000000..26d79e9 --- /dev/null +++ b/dirscan/CMakeLists.txt @@ -0,0 +1,11 @@ +# CMakeList.txt : + +add_executable (dirscan "dirscan.cpp" ) + +set_property(TARGET dirscan PROPERTY CXX_STANDARD 23) +set_property(TARGET dirscan PROPERTY CXX_STANDARD_REQUIRED ON) +set_property(TARGET dirscan PROPERTY CXX_EXTENSIONS OFF) + +find_package(SQLite3 REQUIRED) +target_include_directories( dirscan PRIVATE ${SQLite3_INCLUDE_DIRS}) +target_link_libraries( dirscan PRIVATE ${SQLite3_LIBRARIES} ) \ No newline at end of file diff --git a/dirscan/dirscan.cpp b/dirscan/dirscan.cpp new file mode 100644 index 0000000..e84c126 --- /dev/null +++ b/dirscan/dirscan.cpp @@ -0,0 +1,84 @@ + +#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); + +} \ No newline at end of file