diff --git a/README.md b/README.md index 775ffca..9f76282 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,15 @@ Edit `~/.config/notewrapper/config.json`. If it does not exist, it will be creat * `backup.interval`: backup frequency (`daily`, `weekly`, `monthly`, or integer) * `backup.rsyncArgs`: arguments passed to `rsync` +Note: +* Directories must end with `/`. +* Elements of `directory` and `backup.directory` must match exactly. +* Directories set in `directory` but not in `backup.directory` won't be backed up. +* Directories set in `backup.directory` but not in `directory` will be ignored. +* `journalRegex` must match if the file name ends with `.md` and if it doesn't. +* It is recommended to keep `-q` or `--quiet` flag in `rsyncArgs` to avoid interference with `ncurses`. +* If `rsync` fails, you will see it inside `ncurses`. + --- ## Vivify configuration diff --git a/src/main.c b/src/main.c index 1fc68ab..b47c59b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,6 @@ -#include "cjson/cJSON.h" #include "ui.h" #include "utils.h" #include "notes.h" -#include -#include int main(int argc, char *argv[]) { int shouldDebug = 0; @@ -205,6 +202,7 @@ arg_next: doesBackup = cJSON_IsTrue(doesBackupJSON) ? 1 : 0; debug("doesBackup is set to %d", doesBackup); if (!doesBackup) { + debug("a backup is not needed. Skipping parsing the rest of the json"); goto backup_config_end; // easier to just go to rather than do a big if statement } @@ -212,15 +210,33 @@ arg_next: cJSON *pathToBackupJSON = cJSON_GetObjectItem(backupJSON, "directory"); debug("%s", cJSON_Print(pathToBackupJSON)); error(pathToBackupJSON == NULL && !(cJSON_IsObject(pathToBackupJSON) || cJSON_IsArray(pathToBackupJSON) || cJSON_IsString(pathToBackupJSON) || cJSON_IsNumber(pathToBackupJSON) || cJSON_IsBool(pathToBackupJSON)), "user", "In %s, incorrect type for \"directory\". It must be a JSON object.", configPath); // for some reason cJSON_IsObject does not work. So we must do it like this. - for (int i = 0; i < numDirectories; i++) { // we iterate over every - cJSON *pathToBackupForIthDirectoryJSON = cJSON_GetObjectItem(pathToBackupJSON, directoriesArray[i]); - if (pathToBackupForIthDirectoryJSON) { - backupDirectoriesArray[i] = strdup(cJSON_GetStringValue(pathToBackupForIthDirectoryJSON)); - } else { // else no path is set. So we won't backup it. + + // we can't just iterate over directoriesArray as we replaced ~ to $HOME + // we must iterate over the json entries + cJSON *directoryEntry = NULL; + int i = 0; + cJSON_ArrayForEach(directoryEntry, dirJson) { // iterate over all the elements of the array _i. e._ over all the dirs + // we don't need to type error handle as we did it before + char *directoryEntryPath = cJSON_GetStringValue(directoryEntry); + cJSON *pathToBackupForIthDirectoryJSON = cJSON_GetObjectItem(pathToBackupJSON, directoryEntryPath); + if (pathToBackupForIthDirectoryJSON) { // if the entry exist + char *textPath = cJSON_GetStringValue(pathToBackupForIthDirectoryJSON); + if (textPath[0] == '~') { + textPath++; //cuts the ~ + backupDirectoriesArray[i] = malloc(PATH_MAX); + error(backupDirectoriesArray[i] == NULL, "program", "malloc failed"); + snprintf(backupDirectoriesArray[i], PATH_MAX, "%s%s", homedir, textPath); + } else { + backupDirectoriesArray[i] = strdup(textPath); + } + debug("%s will be backed up to %s", directoriesArray[i], backupDirectoriesArray[i]); + } else { // we set it to NULL to be sure we won't backup it backupDirectoriesArray[i] = NULL; + debug("%s won't be backed up", directoriesArray[i]); } + i++; } - + // handles the interval of backup cJSON *intervalJSON = cJSON_GetObjectItem(backupJSON, "interval"); if (intervalJSON && cJSON_IsString(intervalJSON)) { @@ -459,6 +475,7 @@ next_arg: isEditorValid(editorToOpen, defaultEditor, shouldDebug); // check if editor is supported and if it is installed. If not, it will throw an error. if (doesBackup) { + debug("Handling backup"); handleBackups(directoriesArray, numDirectories, backupDirectoriesArray, homedir, interval, (const char**)rsyncArgs, rsyncArgsNumber, shouldDebug); } diff --git a/src/notes.c b/src/notes.c index a016d76..770d575 100644 --- a/src/notes.c +++ b/src/notes.c @@ -140,7 +140,7 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber //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"); + altDebug(" is a vault"); vaultsArray = realloc(vaultsArray, sizeof(char *)*(nthVault + 1)); // resize vaultsArray error(vaultsArray == NULL, "program", "realloc failed"); vaultsPerDirectoryNumber[i]++; // it will be used later to know which vaults goes into which directory @@ -148,10 +148,10 @@ char **getVaultsFromDirectories(char **directoryStringArray, int directoryNumber error(vaultsArray[nthVault] == NULL, "program", "strdup failed"); nthVault++; // it is used immediatly to set the vault into directoryStringArray } else { - altDebug(" is not a vault (not a dir.)\n"); + altDebug(" is not a vault (not a dir.)"); } } else { - altDebug(" is not a vault (hidden file/dir)\n"); + altDebug(" is not a vault (hidden file/dir)"); } } // we sort entries for the same vault. We can't sort them all. This would break the order. diff --git a/src/utils.c b/src/utils.c index 18a0fb3..d7f6638 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,5 +1,4 @@ #include "utils.h" -#include "string.h" const char *supportedEditor[] = {"helix", "kakoune", "micro", "nano", "neovim", "vi", "vim"}; const int numEditors = 7; @@ -166,6 +165,7 @@ void handleBackups(char **sourceDirectoryArray, const int sourceNumber, char **d struct stat st; if (stat(cacheFilePATH, &st) != 0) { // file does not exist shouldBackup = 1; + debug("Creating (for the first time) %s", cacheFilePATH); FILE *cacheFile = fopen(cacheFilePATH, "w"); if (!cacheFile) { error(1, "program", "Failed to open cache file for writing: %s", cacheFilePATH); @@ -174,6 +174,7 @@ void handleBackups(char **sourceDirectoryArray, const int sourceNumber, char **d fclose(cacheFile); } else { FILE *cacheFile = fopen(cacheFilePATH, "r"); + debug("reading %s", cacheFilePATH); if (!cacheFile) { error(1, "program", "Failed to open cache file for reading: %s", cacheFilePATH); } @@ -191,6 +192,7 @@ void handleBackups(char **sourceDirectoryArray, const int sourceNumber, char **d } if (difftime(now, lastBackupTime) > interval) { + debug("(difftime) %d is greater than (interval) %d -> backuping...", difftime(now, lastBackupTime), interval); shouldBackup = 1; cacheFile = fopen(cacheFilePATH, "w"); if (!cacheFile) { @@ -198,11 +200,15 @@ void handleBackups(char **sourceDirectoryArray, const int sourceNumber, char **d } fprintf(cacheFile, "%ld\n", (long)now); fclose(cacheFile); + } else { + debug("(difftime) %d is smaller than (interval) %d -> no need to backup.", difftime(now, lastBackupTime), interval); } } if (shouldBackup) { + debug("A backup is needed"); for (int i = 0; i < sourceNumber; i++) { + debug("%s", destinationDirectoryArray[i]); if (destinationDirectoryArray[i]) { // if we don't want to backup it, it was set to NULL copyDir(sourceDirectoryArray[i], destinationDirectoryArray[i], rsyncArguments, rsyncArgumentsNumber, shouldDebug); } @@ -230,7 +236,7 @@ int isEditorValid (char *editorToCheck, int useDefaultEditor, int shouldDebug) { } char *path_env = getenv("PATH"); error(!path_env, "program", "getenv(\"PATH\") failed to get your path. NoteWrapper is unable to check if your desired editor is installed\n"); - debug("Your PATH is %s\n", path_env); + debug("Your PATH is %s", path_env); char *paths = strdup(path_env); // duplicate because strtok modifies the string char *dir = strtok(paths, ":"); while (dir) {