#define FUSE_USE_VERSION 31 #include #include #include #include #include #include #include #include #include #include static sqlite3 *db = NULL; static const char *db_path = "/Users/accusys/markbase/data/users/warren.sqlite"; static pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER; // Large buffer for optimized reading #define READ_CHUNK_SIZE 262144 // 256KB chunks typedef struct { char node_id[64]; char file_path[512]; long file_size; } FileCacheEntry; #define CACHE_SIZE 200 static FileCacheEntry file_cache[CACHE_SIZE]; static int cache_count = 0; typedef struct { char node_id[64]; char node_type[20]; long file_size; } NodeCacheEntry; #define NODE_CACHE_SIZE 500 static NodeCacheEntry node_cache[NODE_CACHE_SIZE]; static int node_cache_count = 0; static int init_db() { if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, NULL) != SQLITE_OK) { fprintf(stderr, "Cannot open database\n"); return -1; } printf("Database opened\n"); return 0; } static FileCacheEntry* cache_lookup(const char *node_id) { for (int i = 0; i < cache_count; i++) { if (strcmp(file_cache[i].node_id, node_id) == 0) { return &file_cache[i]; } } return NULL; } static void cache_insert(const char *node_id, const char *file_path, long file_size) { for (int i = 0; i < cache_count; i++) { if (strcmp(file_cache[i].node_id, node_id) == 0) return; } if (cache_count < CACHE_SIZE) { strcpy(file_cache[cache_count].node_id, node_id); strcpy(file_cache[cache_count].file_path, file_path); file_cache[cache_count].file_size = file_size; cache_count++; } } static NodeCacheEntry* node_cache_lookup(const char *node_id) { for (int i = 0; i < node_cache_count; i++) { if (strcmp(node_cache[i].node_id, node_id) == 0) { return &node_cache[i]; } } return NULL; } static void *mb_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { (void) conn; cfg->kernel_cache = 1; init_db(); // Pre-cache largest files const char *sql = "SELECT f.file_uuid, l.location, f.file_size " "FROM file_nodes f JOIN file_locations l ON f.file_uuid = l.file_uuid " "ORDER BY f.file_size DESC LIMIT 200"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { cache_insert( (const char*)sqlite3_column_text(stmt, 0), (const char*)sqlite3_column_text(stmt, 1), sqlite3_column_int64(stmt, 2) ); } sqlite3_finalize(stmt); printf("Pre-cached %d files\n", cache_count); } return NULL; } static void mb_destroy(void *userdata) { (void) userdata; if (db) sqlite3_close(db); } static char* find_node_id(const char *path) { if (strcmp(path, "/") == 0) return NULL; char *path_copy = strdup(path); char *components[20]; int depth = 0; char *token = strtok(path_copy + 1, "/"); while (token && depth < 20) { components[depth++] = strdup(token); token = strtok(NULL, "/"); } free(path_copy); if (depth == 0) return NULL; char *current_parent_id = NULL; for (int level = 0; level < depth; level++) { const char *sql; sqlite3_stmt *stmt; if (level == 0) { sql = "SELECT node_id, node_type, file_size FROM file_nodes WHERE label = ? AND (parent_id IS NULL OR parent_id = '')"; } else { sql = "SELECT node_id, node_type, file_size FROM file_nodes WHERE label = ? AND parent_id = ?"; } if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) { for (int i = 0; i < depth; i++) free(components[i]); if (current_parent_id) free(current_parent_id); return NULL; } sqlite3_bind_text(stmt, 1, components[level], -1, SQLITE_STATIC); if (level > 0 && current_parent_id) { sqlite3_bind_text(stmt, 2, current_parent_id, -1, SQLITE_STATIC); } if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); for (int i = 0; i < depth; i++) free(components[i]); if (current_parent_id) free(current_parent_id); return NULL; } const char *found_node_id = (const char*)sqlite3_column_text(stmt, 0); if (node_cache_count < NODE_CACHE_SIZE) { strcpy(node_cache[node_cache_count].node_id, found_node_id); strcpy(node_cache[node_cache_count].node_type, (const char*)sqlite3_column_text(stmt, 1)); node_cache[node_cache_count].file_size = sqlite3_column_int64(stmt, 2); node_cache_count++; } if (current_parent_id) free(current_parent_id); current_parent_id = strdup(found_node_id); sqlite3_finalize(stmt); } for (int i = 0; i < depth; i++) free(components[i]); return current_parent_id; } static int mb_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) { (void) fi; memset(stbuf, 0, sizeof(struct stat)); if (strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; return 0; } char *node_id = find_node_id(path); if (!node_id) return -ENOENT; NodeCacheEntry *cached_node = node_cache_lookup(node_id); if (cached_node) { if (strcmp(cached_node->node_type, "folder") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else { stbuf->st_mode = S_IFREG | 0444; stbuf->st_nlink = 1; stbuf->st_size = cached_node->file_size; } free(node_id); return 0; } const char *sql = "SELECT node_type, file_size FROM file_nodes WHERE node_id = ?"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, node_id, -1, SQLITE_STATIC); if (sqlite3_step(stmt) == SQLITE_ROW) { const char *node_type = (const char*)sqlite3_column_text(stmt, 0); long file_size = sqlite3_column_int64(stmt, 1); if (strcmp(node_type, "folder") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else { stbuf->st_mode = S_IFREG | 0444; stbuf->st_nlink = 1; stbuf->st_size = file_size; } } sqlite3_finalize(stmt); } free(node_id); return 0; } static int mb_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) { (void) offset; (void) fi; (void) flags; filler(buf, ".", NULL, 0, FUSE_FILL_DIR_DEFAULTS); filler(buf, "..", NULL, 0, FUSE_FILL_DIR_DEFAULTS); if (strcmp(path, "/") == 0) { sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT label FROM file_nodes WHERE parent_id IS NULL OR parent_id = ''", -1, &stmt, NULL) == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { filler(buf, (const char*)sqlite3_column_text(stmt, 0), NULL, 0, FUSE_FILL_DIR_DEFAULTS); } sqlite3_finalize(stmt); } return 0; } char *parent_node_id = find_node_id(path); if (!parent_node_id) return -ENOENT; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT label FROM file_nodes WHERE parent_id = ?", -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, parent_node_id, -1, SQLITE_STATIC); while (sqlite3_step(stmt) == SQLITE_ROW) { filler(buf, (const char*)sqlite3_column_text(stmt, 0), NULL, 0, FUSE_FILL_DIR_DEFAULTS); } sqlite3_finalize(stmt); } free(parent_node_id); return 0; } static int mb_open(const char *path, struct fuse_file_info *fi) { if ((fi->flags & O_ACCMODE) != O_RDONLY) return -EACCES; char *node_id = find_node_id(path); if (!node_id) return -ENOENT; free(node_id); return 0; } static int mb_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { (void) fi; char *node_id = find_node_id(path); if (!node_id) return -ENOENT; FileCacheEntry *cached = cache_lookup(node_id); char *file_path = NULL; if (cached) { file_path = strdup(cached->file_path); } else { sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT location FROM file_locations WHERE file_uuid = ? LIMIT 1", -1, &stmt, NULL) != SQLITE_OK) { free(node_id); return -EIO; } sqlite3_bind_text(stmt, 1, node_id, -1, SQLITE_STATIC); if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); free(node_id); return -ENOENT; } file_path = strdup((const char*)sqlite3_column_text(stmt, 0)); sqlite3_finalize(stmt); cache_insert(node_id, file_path, 0); } free(node_id); FILE *fp = fopen(file_path, "rb"); if (!fp) { free(file_path); return -ENOENT; } fseek(fp, offset, SEEK_SET); size_t total_read = 0; while (total_read < size) { size_t chunk = (size - total_read > READ_CHUNK_SIZE) ? READ_CHUNK_SIZE : size - total_read; size_t bytes = fread(buf + total_read, 1, chunk, fp); total_read += bytes; if (bytes < chunk) break; } fclose(fp); free(file_path); return total_read; } static const struct fuse_operations mb_oper = { .init = mb_init, .destroy = mb_destroy, .getattr = mb_getattr, .readdir = mb_readdir, .open = mb_open, .read = mb_read, }; int main(int argc, char *argv[]) { printf("MarkBase FUSE v12.0 - Final Optimized\n"); printf("====================================\n"); printf("Features:\n"); printf(" - Large read chunks (256KB)\n"); printf(" - Pre-cache 200 files\n"); printf(" - Kernel caching\n"); printf(" - Minimal mutex locking\n"); printf("\n"); return fuse_main(argc, argv, &mb_oper, NULL); }