sled

Једноставни уређивач текста
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 51a6eeffa32a8a77362affd77ee21d609131a5f9
родитељ 45218d9357bfbc06670eceb437255c9067927ec5
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Mon,  6 Mar 2023 18:19:19 +0100

Add BACKUP_FAIL_OK, which ignores backup failures. Useful with Busybox's crontab -e when EDITOR=sled

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mconfig.def.h | 5+++++
Mconfig.h | 5+++++
Mdraw.h | 3+++
Msled.c | 33+++++++++++++++++++++++++++++++--
измењених датотека: 4, додавања: 44(+), брисања: 2(-)

diff --git a/config.def.h b/config.def.h @@ -2,10 +2,14 @@ /* Comment out to not create backups */ #define CREATE_BACKUPS +/* Comment out to fail with error if creating backups is not possible due to + * file and directory permissions */ +#define BACKUP_FAIL_OK /* Comment out to not wrap lines */ #define ENABLE_WRAP #define WRAP_WIDTH 80 +#ifndef ONLY_DEFS #ifdef SLED_C_GLOBALS const char* backup_suffix = "~"; #endif /* SLED_C_GLOBALS */ @@ -294,3 +298,4 @@ static const struct StatusSegment status_segments[] = { .alignment = RIGHT}, }; /* clang-format on */ +#endif /* ONLY_DEFS */ diff --git a/config.h b/config.h @@ -2,10 +2,14 @@ /* Comment out to not create backups */ #define CREATE_BACKUPS +/* Comment out to fail with error if creating backups is not possible due to + * file and directory permissions */ +#define BACKUP_FAIL_OK /* Comment out to not wrap lines */ #define ENABLE_WRAP #define WRAP_WIDTH 80 +#ifndef ONLY_DEFS #ifdef SLED_C_GLOBALS const char* backup_suffix = "~"; #endif /* SLED_C_GLOBALS */ @@ -294,3 +298,4 @@ static const struct StatusSegment status_segments[] = { .alignment = RIGHT}, }; /* clang-format on */ +#endif /* ONLY_DEFS */ diff --git a/draw.h b/draw.h @@ -85,6 +85,9 @@ struct DrawState { int maxy; struct Coord sel_start; /* buffer coordinate */ struct Coord sel_end; /* buffer coordinate */ +#ifdef CREATE_BACKUPS + int backup_possible; /* is saving backup possible? */ +#endif }; struct Key { diff --git a/sled.c b/sled.c @@ -19,9 +19,12 @@ #define SLED_C_GLOBALS +#define ONLY_DEFS +#include "config.h" #include "version.h" #include "util.h" #include "draw.h" +#undef ONLY_DEFS #include "config.h" #define SEL_NOT_EMPTY(state) \ @@ -786,10 +789,16 @@ save_file(struct DrawState* state) } strcpy(backup_filename, state->filename); strcat(backup_filename, backup_suffix); +#ifdef BACKUP_FAIL_OK + if (state->backup_possible) + rename(rfn_len > 0 ? real_filename : state->filename, + backup_filename); +#else if (rename(rfn_len > 0 ? real_filename : state->filename, backup_filename) == -1) return SAVE_ERR_CANT_MOVE_FILE; +#endif } /* We don't error out here to account for new files */ else new_file = 1; @@ -800,6 +809,21 @@ save_file(struct DrawState* state) return SAVE_ERR_CANT_OPEN_FILE; #ifdef CREATE_BACKUPS +#ifdef BACKUP_FAIL_OK + if (state->backup_possible) + { + if (rfn_len > 0) + { + if (unlink(state->filename) == -1) + return SAVE_ERR_CANT_UNLINK; + if (symlink(real_filename, state->filename) == -1) + return SAVE_ERR_CANT_SYMLINK; + } + if (!new_file) + chmod(rfn_len > 0 ? real_filename : state->filename, + st.st_mode); + } +#else /* !BACKUP_FAIL_OK */ if (rfn_len > 0) { if (unlink(state->filename) == -1) @@ -809,7 +833,8 @@ save_file(struct DrawState* state) } if (!new_file) chmod(rfn_len > 0 ? real_filename : state->filename, st.st_mode); -#endif +#endif /* BACKUP_FAIL_OK */ +#endif /* CREATE_BACKUPS */ if (state->rows_count == 1 && state->buffer[0].length == 0) goto save_file_cleanup; @@ -1895,7 +1920,7 @@ suspend_program(struct DrawState* state) tb_set_cursor(0, 0); tb_present(); - kill(getpid(), SIGTSTP); + kill(getpid(), SIGTSTP); /* --- back from suspend --- */ @@ -2058,11 +2083,15 @@ main(int argc, char** argv) return print_error(EINVAL, "Is not a regular file"); #ifdef CREATE_BACKUPS +#ifdef BACKUP_FAIL_OK + state.backup_possible = dir_access != -1; +#else /* !BACKUP_FAIL_OK */ if (dir_access == -1) { errno = dir_access_errno; goto file_error; } +#endif /* BACKUP_FAIL_OK */ #endif /* CREATE_BACKUPS */ if (access(filename, F_OK | R_OK | W_OK) == -1)