fix: multiple small bugs from previous commit

This commit is contained in:
2026-04-26 21:37:20 +02:00
parent 3fb2a32b83
commit d5b5d07e64
3 changed files with 22 additions and 12 deletions
-1
View File
@@ -487,7 +487,6 @@ next_arg:
} }
} }
qsort(vaultsArray, vaultsCount, sizeof(const char *), compareString); // sorts the vaults alphabetically
debug("┌--------------------------------\nAvailable vaults:"); debug("┌--------------------------------\nAvailable vaults:");
if (shouldDebug) { if (shouldDebug) {
for (int i = 0; i < vaultsCount; i++) { for (int i = 0; i < vaultsCount; i++) {
+18 -7
View File
@@ -3,17 +3,24 @@
char *getDirectoryFromVault(char *targetVault, char **vaultsArray, int vaultTotalNumber, int *vaultNumberPerDirectory, char **directoryArray, int directoryNumber, int shouldDebug) { 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); debug("Searching the vault %s inside all the directories...", targetVault);
int index = 0; debug("Here are how many vaults there is per directory:");
int startIndex = 0;
for (int i = 0; i < directoryNumber; i++) { for (int i = 0; i < directoryNumber; i++) {
while (index - startIndex < vaultNumberPerDirectory[i]) { altDebug("%d for %s\n", vaultNumberPerDirectory[i], directoryArray[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); debug("Here are all the vaults in the order they will be searched:");
for (int i = 0; i < vaultTotalNumber; i++) {
altDebug("%s\n", vaultsArray[i]);
}
int index = 0;
for (int i = 0; i < directoryNumber; i++) {
for (int j = 0; j < vaultNumberPerDirectory[i]; j++) {
if (strcmp(vaultsArray[index], targetVault) == 0) {
debug("%s found in %s. (index %d)", targetVault, directoryArray[i], index);
return directoryArray[i]; return directoryArray[i];
} }
index++; index++;
} }
startIndex += vaultNumberPerDirectory[i];
} }
error(1, "program", "the vault %s was not found", targetVault); 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."; 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.";
@@ -112,6 +119,7 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber
// to avoid having to work with a tree-dimensional array. We will use a 2d and vaultsPerDirectoryNumber will indicate the width of the directories. // 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; char **vaultsArray = NULL;
int nthVault = 0; // this is only used internally to set the string into the right place in directoryStringArray. int nthVault = 0; // this is only used internally to set the string into the right place in directoryStringArray.
int previousStartIndex = 0;
for (int i = 0; i < directoryNumber; i++) { for (int i = 0; i < directoryNumber; i++) {
debug("Opening %s", directoryStringArray[i]); debug("Opening %s", directoryStringArray[i]);
// originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/ // originally from https://www.geeksforgeeks.org/c/c-program-list-files-sub-directories-directory/
@@ -146,7 +154,10 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber
altDebug(" is not a vault (hidden file/dir)\n"); altDebug(" is not a vault (hidden file/dir)\n");
} }
} }
// we sort entries for the same vault. We can't sort them all. This would break the order.
qsort(vaultsArray + previousStartIndex, nthVault - previousStartIndex, sizeof(const char *), compareString); // sorts the vaults alphabetically
closedir(vaultsDirectory); closedir(vaultsDirectory);
previousStartIndex = nthVault;
} }
// we calculate once the total number of vaults to avoid recalculation every time we use it // we calculate once the total number of vaults to avoid recalculation every time we use it
*count = 0; *count = 0;
@@ -167,7 +178,7 @@ char *updateJournal(char *path, char *journal, char *timeFormat, int *journalWas
sanitize(dateWithExtension); sanitize(dateWithExtension);
debug("Sanitized date: %s\n(it might be used later for a file name if the journal is divided)", dateWithExtension); debug("Sanitized date: %s\n(it might be used later for a file name if the journal is divided)", dateWithExtension);
struct stat metadata; struct stat metadata;
error(stat(path, &metadata), "program", "stat() failed to get information about %s", path); stat(path, &metadata);
if (S_ISREG(metadata.st_mode)) { if (S_ISREG(metadata.st_mode)) {
debug("%s is a unified journal.", path); debug("%s is a unified journal.", path);
if (!isStringInFile(path, date, shouldDebug)) { // if there is no entry for current date if (!isStringInFile(path, date, shouldDebug)) { // if there is no entry for current date
+1 -1
View File
@@ -112,7 +112,7 @@ char *createNewNote(char dirToVault[PATH_MAX], char *vaultFromDir, int bypass, c
snprintf(fileFullPath, PATH_MAX, "%s/%s/%s/", dirToVault, vaultFromDir, fileName); snprintf(fileFullPath, PATH_MAX, "%s/%s/%s/", dirToVault, vaultFromDir, fileName);
struct stat st = {0}; struct stat st = {0};
if (stat(fileFullPath, &st) == -1) { if (stat(fileFullPath, &st) == -1) {
error(!mkdir(fileFullPath, 0744), "program", "mkdir failed"); error(mkdir(fileFullPath, 0744), "program", "mkdir failed");
} else { } else {
error(1, "program", "%s could not be created", fileFullPath); error(1, "program", "%s could not be created", fileFullPath);
} }