feat: allow the usage of multiple vault directories

This commit is contained in:
2026-04-26 20:45:42 +02:00
parent e2343dd401
commit 107519507f
7 changed files with 131 additions and 82 deletions
+54 -25
View File
@@ -1,5 +1,23 @@
#include "notes.h"
#include "ui.h"
#include "utils.h"
char *getDirectoryFromVault(char *targetVault, char **vaultsArray, int vaultTotalNumber, int *vaultNumberPerDirectory, char **directoryArray, int directoryNumber, int shouldDebug) {
debug("Searching the vault %s inside all the directories...", targetVault);
int index = 0;
int startIndex = 0;
for (int i = 0; i < directoryNumber; i++) {
while (index - startIndex < vaultNumberPerDirectory[i]) {
if (strcmp(vaultsArray[index], targetVault) == 0) { // we found it
debug("%s found in %s. (This was the %dth step of a maximum of %d)", targetVault, directoryArray[i], index, vaultTotalNumber);
return directoryArray[i];
}
index++;
}
startIndex += vaultNumberPerDirectory[i];
}
error(1, "program", "the vault %s was not found", targetVault);
return "this makes the compiler and clangd happy. Who doesn't want GCC and clangd to be happy? Such person would be a terrible monster... One must imagine GCC and clangd happy.";
}
char **getJournalsFromVault(char *pathToVault, char *vault, char *journalRegex, int *count, int shouldDebug) {
debug("Searching %s for journals", vault);
@@ -90,38 +108,49 @@ char** getNotesFromVault(char *pathToVault, char *vault, char *journalRegex, int
return notesArray;
}
char **getVaultsFromDirectory(char *dirString, int *count, int shouldDebug) {
// this function is inputed a path to a directory (which comes usually from the config file) and outpus all the suitable directories (so not the hidden ones) which will serve as separate vaults for notes
debug("Opening %s ", dirString);
char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber, int *vaultsPerDirectoryNumber, int *count, int shouldDebug) {
// to avoid having to work with a tree-dimensional array. We will use a 2d and vaultsPerDirectoryNumber will indicate the width of the directories.
char **vaultsArray = NULL;
int nthVault = 0; // this is only used internally to set the string into the right place in directoryStringArray.
for (int i = 0; i < directoryNumber; i++) {
debug("Opening %s", directoryStringArray[i]);
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
struct dirent *vaultsDirectoryEntry;
DIR *vaultsDirectory = opendir(dirString);
error(vaultsDirectory==NULL, "program", "Could not open directory %s", dirString);
char **dirsArray = NULL; // will contain all the dirs/vaults
int dirsCount = 0; // we need to count how many dirs there is to always readjust how many memory we alloc
DIR *vaultsDirectory = opendir(directoryStringArray[i]);
error(!vaultsDirectory, "program", "Could not open directory %s", directoryStringArray[i]);
vaultsPerDirectoryNumber[i] = 0;
debug("┌------------------------------\n Detected files and dirs %s:", directoryStringArray[i]);
// Refer https://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
debug("┌------------------------------\n Detected files and dirs from the directory:");
while ((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) {
altDebug("%s\n", vaultsDirectoryEntry->d_name);
if (vaultsDirectoryEntry->d_name[0] != '.') { // if the entry don't start with a dot (so hidden dirs and hidden files)
char fullPathEntry[PATH_MAX]; // creates a string of size of the maximum path lenght
snprintf(fullPathEntry, sizeof(fullPathEntry), "%s/%s", dirString, vaultsDirectoryEntry->d_name); // sets the full absolute path to fullPathEntry
struct stat metadataPathEntry;
if (stat(fullPathEntry, &metadataPathEntry) == 0 && S_ISDIR(metadataPathEntry.st_mode)) { // if this entry is a directory
dirsArray = realloc(dirsArray, (dirsCount + 1)*sizeof(char*)); // resize dirsArray so that
dirsArray[dirsCount] = strdup(vaultsDirectoryEntry->d_name); // copy the dir name into dirsArray
dirsCount++;
while((vaultsDirectoryEntry = readdir(vaultsDirectory)) != NULL) {
char *entryName = vaultsDirectoryEntry->d_name; // gets the entry as a string value
altDebug("%s", entryName);
if (entryName[0] != '.') { // if not hidden file/dir
char tempFullEntryPath[PATH_MAX]; // we recreate the full path to check it's proprieties
snprintf(tempFullEntryPath, PATH_MAX, "%s/%s", directoryStringArray[i], entryName);
//checking the metadata to see if it is a dir
struct stat metadataEntry;
if (stat(tempFullEntryPath, &metadataEntry) == 0 && S_ISDIR(metadataEntry.st_mode)) { // get's the metadata (stat should return 0 if fails) and sees if it is a dir.
altDebug(" is a vault\n");
vaultsArray = realloc(vaultsArray, sizeof(vaultsArray) + sizeof(char *)); // resize vaultsArray
vaultsPerDirectoryNumber[i]++; // it will be used later to know which vaults goes into which directory
directoryStringArray[nthVault] = strdup(entryName); // we use strdup and not strcpy, because memory used with opendir and readdir will be closed.
nthVault++; // it is used immediatly to set the vault into directoryStringArray
} else {
altDebug(" is not a vault (not a dir.)\n");
}
} else {
altDebug(" is not a vault (hidden file/dir)\n");
}
}
altDebug("└------------------------------\n");
// free's some used memory
closedir(vaultsDirectory);
*count = dirsCount;
return dirsArray;
}
// we calculate once the total number of vaults to avoid recalculation every time we use it
count = 0;
for (int i = 0; i < directoryNumber; i++) {
count += vaultsPerDirectoryNumber[i];
}
return vaultsArray;
}
char *updateJournal(char *path, char *journal, char *timeFormat, int *journalWasUpdated, int shouldDebug) {