To create a SQLite based program to manage file system metadata in C++, the following features must be implemented:
- Create table structures.
- Implement add file and remove file functions.
- Implement add and remove directory functions.
- Implement functions to get file/directory size.
Firstly, prepare the project by installing the sqlite3 library and include its header:
#include <iostream>
#include <sqlite3.h>
#include <string>
#include <filesystem>
Create table structures for storing files and directories metadata:
std::string create_tables_sql = R"(
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
size INTEGER NOT NULL,
parent_directory INTEGER,
FOREIGN KEY (parent_directory) REFERENCES directories (id)
);
CREATE TABLE IF NOT EXISTS directories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
parent_directory INTEGER,
FOREIGN KEY (parent_directory) REFERENCES directories (id)
);
)";
Define SQLite helper functions to execute SQL statements and initialize the database:
int initialize_database(const std::string& db_path)
{
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
char* err_msg = nullptr;
if (sqlite3_exec(db, create_tables_sql.c_str(), nullptr, nullptr, &err_msg) != SQLITE_OK)
{
std::cerr << "Cannot create tables: " << err_msg << std::endl;
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
Create functions to add and remove files:
int add_file(const std::string& db_path, const std::string& file_path, int parent_directory)
{
int ret = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
namespace fs = std::filesystem;
fs::path path(file_path);
int64_t size;
if (fs::is_regular_file(path))
{
size = fs::file_size(path);
}
else
{
std::cerr << "Path is not a file: " << file_path << std::endl;
sqlite3_close(db);
return 1;
}
std::string name = path.filename().string();
sqlite3_stmt* stmt;
std::string insert_sql = "INSERT INTO files (name, size, parent_directory) VALUES (?, ?, ?);";
sqlite3_prepare_v2(db, insert_sql.c_str(), insert_sql.size(), &stmt, nullptr);
sqlite3_bind_text(stmt, 1, name.c_str(), name.size(), SQLITE_TRANSIENT);
sqlite3_bind_int64(stmt, 2, size);
sqlite3_bind_int(stmt, 3, parent_directory);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
std::cerr << "Error adding file: " << sqlite3_errmsg(db) << std::endl;
ret = 1;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return ret;
}
int remove_file(const std::string& db_path, int file_id)
{
int ret = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
std::string delete_sql = "DELETE FROM files WHERE id = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, delete_sql.c_str(), delete_sql.size(), &stmt, nullptr);
sqlite3_bind_int(stmt, 1, file_id);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
std::cerr << "Error removing file: " << sqlite3_errmsg(db) << std::endl;
ret = 1;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return ret;
}
Create functions to add and remove directories:
int add_directory(const std::string& db_path, const std::string& dir_path, int parent_directory)
{
int ret = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
namespace fs = std::filesystem;
fs::path path(dir_path);
if (!fs::is_directory(path))
{
std::cerr << "Path is not a directory: " << dir_path << std::endl;
sqlite3_close(db);
return 1;
}
std::string name = path.filename().string();
sqlite3_stmt* stmt;
std::string insert_sql = "INSERT INTO directories (name, parent_directory) VALUES (?, ?);";
sqlite3_prepare_v2(db, insert_sql.c_str(), insert_sql.size(), &stmt, nullptr);
sqlite3_bind_text(stmt, 1, name.c_str(), name.size(), SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, parent_directory);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
std::cerr << "Error adding directory: " << sqlite3_errmsg(db) << std::endl;
ret = 1;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return ret;
}
int remove_directory(const std::string& db_path, int dir_id)
{
int ret = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return 1;
}
std::string delete_sql = "DELETE FROM directories WHERE id = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, delete_sql.c_str(), delete_sql.size(), &stmt, nullptr);
sqlite3_bind_int(stmt, 1, dir_id);
if (sqlite3_step(stmt) != SQLITE_DONE)
{
std::cerr << "Error removing directory: " << sqlite3_errmsg(db) << std::endl;
ret = 1;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return ret;
}
Create functions to get file size and directory size:
int64_t get_file_size(const std::string& db_path, int file_id)
{
int64_t size = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return -1;
}
std::string query_sql = "SELECT size FROM files WHERE id = ?;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, query_sql.c_str(), query_sql.size(), &stmt, nullptr);
sqlite3_bind_int(stmt, 1, file_id);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
size = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return size;
}
int get_directory_size_callback(void* data, int argc, char** argv, char** azColName)
{
int64_t* total_size = static_cast<int64_t*>(data);
*total_size += strtoll(argv[0], nullptr, 10);
return 0;
}
int64_t get_directory_size(const std::string& db_path, int dir_id)
{
int64_t total_size = 0;
sqlite3* db;
if (sqlite3_open(db_path.c_str(), &db))
{
std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl;
return -1;
}
std::string query_sql = "WITH RECURSIVE subdir(id) AS ("
"SELECT id FROM directories WHERE id = ? "
"UNION ALL "
"SELECT d.id FROM directories AS d JOIN subdir ON d.parent_directory = subdir.id) "
"SELECT SUM(size) FROM files WHERE parent_directory IN subdir;";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, query_sql.c_str(), -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, dir_id);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
total_size = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return total_size;
}
Finally, use these functions by creating main():
int main()
{
std::string db_path = "metadata.sqlite";
initialize_database(db_path);
// Test add and remove file functions
add_file(db_path, "test.txt", 1);
int64_t file_size = get_file_size(db_path, 1);
std::cout << "File size: " << file_size << std::endl;
remove_file(db_path, 1);
// Test add and remove directory functions
add_directory(db_path, "test", 1);
int64_t dir_size = get_directory_size(db_path, 1);
std::cout << "Directory size: " << dir_size << std::endl;
remove_directory(db_path, 1);
return 0;
}
Compile the program with:
g++ -std=c++17 -o main main.cpp -lsqlite3 -lstdc++fs
This program can be further expanded to retrieve more metadata from the SQLite database, such as finding all files and directories, updating metadata, and more.
Keep in mind that the sample code above assumes that all operations are successful. In a production environment, you should handle errors and edge cases more thoroughly.