add dirscan

This commit is contained in:
2025-05-26 22:24:37 -04:00
commit ddb0c32a0e
5 changed files with 116 additions and 0 deletions

7
CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
# CMakeList.txt :
cmake_minimum_required (VERSION 3.16)
project (xtfs VERSION 1.0 LANGUAGES CXX)
add_subdirectory(dirscan)

9
db_script.sql Normal file
View File

@@ -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)

View File

@@ -0,0 +1,5 @@
create table FSCHILD(
child_dir STRING PRIMARY KEY NOT NULL
);

11
dirscan/CMakeLists.txt Normal file
View File

@@ -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} )

84
dirscan/dirscan.cpp Normal file
View File

@@ -0,0 +1,84 @@
#include<iostream>
#include<string>
#include<filesystem>
#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);
}