Skip to content

Commit

Permalink
also backup the crontab file on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz committed Dec 22, 2022
1 parent 78dd803 commit 3f029a6
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/crontab.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ poke_daemon(void),
check_error(const char *), parse_args(int c, char *v[]),
die(int) ATTRIBUTE_NORETURN;
static int replace_cmd(void), hostset_cmd(void), hostget_cmd(void),
test_cmd(void), check_syntax(FILE *);
test_cmd(void), check_syntax(FILE *), backup_crontab(char[]);
static char *host_specific_filename(const char *prefix,
const char *suffix);
static const char *tmp_path(void);
Expand Down Expand Up @@ -480,7 +480,10 @@ static void delete_cmd(void) {
exit(ERROR_EXIT);
}

backup_crontab(n);
if(backup_crontab(n) == -1){
fprintf(stderr, "no crontab for %s\n", User);
exit(ERROR_EXIT);
}

if (unlink(n) != 0) {
perror(n);
Expand All @@ -489,7 +492,10 @@ static void delete_cmd(void) {
poke_daemon();
}

static void backup_crontab(char crontab_filepath[]) {
/* returns 0 on success
* -1 on non existent crontab file
*/
static int backup_crontab(char crontab_filepath[]) {
const char *env_value;
char backup_filename[MAX_FNAME];

Expand Down Expand Up @@ -518,8 +524,7 @@ static void backup_crontab(char crontab_filepath[]) {
perror(crontab_filepath);
exit(ERROR_EXIT);
}
fprintf(stderr, "no crontab for %s\n", User);
exit(ERROR_EXIT);
return -1;
}

if (swap_uids() == -1) {
Expand All @@ -543,8 +548,7 @@ static void backup_crontab(char crontab_filepath[]) {
fclose(crontab_file);
fclose(backup_file);

printf("Backup of %s's crontab saved to %s\n", User, backup_filename);

printf("Backup of %s's previous crontab saved to %s\n", User, backup_filename);
}

static void check_error(const char *msg) {
Expand Down Expand Up @@ -964,6 +968,9 @@ static int replace_cmd(void) {
error = -2;
goto done;
}

backup_crontab(n);

if (rename(TempFilename, n)) {
fprintf(stderr, "%s: error renaming %s to %s\n",
ProgramName, TempFilename, n);
Expand Down

0 comments on commit 3f029a6

Please sign in to comment.