add dirscan
This commit is contained in:
11
dirscan/CMakeLists.txt
Normal file
11
dirscan/CMakeLists.txt
Normal 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
84
dirscan/dirscan.cpp
Normal 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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user