From 1892ab9d0687fad1e92c4a332a598d35924b522c Mon Sep 17 00:00:00 2001 From: thib Date: Sun, 17 Nov 2002 13:13:52 +0000 Subject: [PATCH] moved save_one_file() from conf.c, fileconf.c and convert-fcrontab.c to a unique function in sav --- convert-fcrontab.c | 46 ++++++---------------------------------------- save.c | 46 ++++++++++++++++++++++++++++++++++++++++------ save.h | 5 +++-- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/convert-fcrontab.c b/convert-fcrontab.c index 5813a8a..b777e1a 100644 --- a/convert-fcrontab.c +++ b/convert-fcrontab.c @@ -22,7 +22,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: convert-fcrontab.c,v 1.15 2002-11-01 18:17:39 thib Exp $ */ + /* $Id: convert-fcrontab.c,v 1.16 2002-11-17 13:13:52 thib Exp $ */ #include "global.h" @@ -30,7 +30,7 @@ #include "log.h" #include "subs.h" -char rcs_info[] = "$Id: convert-fcrontab.c,v 1.15 2002-11-01 18:17:39 thib Exp $"; +char rcs_info[] = "$Id: convert-fcrontab.c,v 1.16 2002-11-17 13:13:52 thib Exp $"; void info(void); void usage(void); @@ -46,8 +46,6 @@ char foreground = 1; pid_t daemon_pid = 0; char debug_opt = 0; char dosyslog = 1; -time_t t_save = 0; -struct stat file_stat; void info(void) @@ -137,41 +135,6 @@ delete_file(cf_t *file) } -/* this function is called in save.c */ -int -save_one_file(cf_t *file, char *path) -/* save a given file to disk */ -{ - int fd; - - fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC); - if ( fd == -1 ) { - error_e("Could not open %s", path); - return ERR; - } - - if ( fchown(fd, file_stat.st_uid, file_stat.st_gid) != 0 ) { - error_e("Could not fchown %s", path); - return ERR; - } - - if ( fchmod(fd, file_stat.st_mode) != 0 ) { - error_e("Could not fchmod %s", path); - return ERR; - } - - if ( write_file_to_disk(fd, file, t_save) == ERR ) { - close(fd); - remove(path); - return ERR; - } - - close(fd); - - return OK; - -} - void convert_file(char *file_name) /* this functions is a mix of read_file() from version 1.0.3 and save_file(), @@ -182,6 +145,8 @@ convert_file(char *file_name) env_t *env = NULL; FILE *f = NULL; char buf[LINE_LEN]; + time_t t_save = 0; + struct stat file_stat; explain("Converting %s's fcrontab ...", file_name); @@ -259,7 +224,8 @@ convert_file(char *file_name) if ( rename(buf, file_name) != 0 ) error_e("Could not rename %s to %s", buf, file_name); - save_file_safe(file, file_name, "convert-fcrontab"); + save_file_safe(file, file_name, "convert-fcrontab", file_stat.st_uid, + file_stat.st_gid, t_save); delete_file(file); } diff --git a/save.c b/save.c index 902a978..d753397 100644 --- a/save.c +++ b/save.c @@ -22,7 +22,7 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: save.c,v 1.4 2002-11-01 18:17:44 thib Exp $ */ + /* $Id: save.c,v 1.5 2002-11-17 13:13:55 thib Exp $ */ #include "global.h" #include "save.h" @@ -35,7 +35,8 @@ int save_str(int fd, short int type, char *str, char *write_buf, int *buf_used); int save_strn(int fd, short int type, char *str, short int size, char *write_buf, int *buf_used); int save_lint(int fd, short int type, long int value, char *write_buf, int *buf_used); -extern int save_one_file(cf_t *file, char *filename); +int save_one_file(cf_t *file, char *filename, uid_t own_uid, gid_t own_gid, + time_t save_date); int @@ -291,7 +292,41 @@ write_file_to_disk(int fd, struct cf_t *file, time_t time_date) int -save_file_safe(cf_t *file, char *final_path, char *prog_name) +save_one_file(cf_t *file, char *filename, uid_t own_uid, gid_t own_gid, time_t save_date) +/* save a given file to disk */ +{ + int fd; + + /* open file */ + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR|S_IWUSR); + if ( fd == -1 ) { + error_e("Could not open %s", filename); + return ERR; + } + + if (fchown(fd, own_uid, own_gid) != 0) { + error_e("Could not fchown %s to uid:%d gid:%d", filename, own_uid, own_gid); + close(fd); + remove(filename); + return ERR; + } + + /* save file : */ + if ( write_file_to_disk(fd, file, save_date) == ERR ) { + close(fd); + remove(filename); + return ERR; + } + + close(fd); + + return OK; +} + + +int +save_file_safe(cf_t *file, char *final_path, char *prog_name, uid_t own_uid, + gid_t own_gid, time_t save_date) /* save a file to a temp path, and then rename it (safely) to avoid loss of data * if a system crash, hardware failure, etc happens. */ { @@ -305,15 +340,14 @@ save_file_safe(cf_t *file, char *final_path, char *prog_name) sizeof(temp_path)-sizeof(tmp_str) : final_path_len; strcpy(&temp_path[temp_path_index], ".tmp"); - /* save_one_file() is defined in conf.c for fcron, fileconf.c for fcrondyn */ - if ( save_one_file(file, temp_path) == OK ) { + if ( save_one_file(file, temp_path, own_uid, own_gid, save_date) == OK ) { if ( rename(temp_path, final_path) != 0 ) { error_e("Cannot rename %s to %s", temp_path, final_path); error("%s will try to save the name to its definitive filename " "directly.", prog_name); error("If there is an error, root may consider to replace %s (which is " "a valid copy) by %s manually.", final_path, temp_path); - if ( save_one_file(file, final_path) == ERR ) + if ( save_one_file(file, final_path, own_uid, own_gid, save_date) == ERR ) return ERR; } } diff --git a/save.h b/save.h index f97cd53..31cc492 100644 --- a/save.h +++ b/save.h @@ -21,14 +21,15 @@ * `LICENSE' that comes with the fcron source distribution. */ - /* $Id: save.h,v 1.8 2002-11-01 18:17:59 thib Exp $ */ + /* $Id: save.h,v 1.9 2002-11-17 13:14:02 thib Exp $ */ #ifndef __SAVE_H__ #define __SAVE_H__ /* functions defined by save.c */ extern int write_file_to_disk(int fd, struct cf_t *file, time_t time_date); -extern int save_file_safe(cf_t *file, char *final_path, char *fcron_suffix); +extern int save_file_safe(cf_t *file, char *final_path, char *prog_name, uid_t own_uid, + gid_t own_gid, time_t save_date); /* here is the format fcron(tab) uses to save the fcrontabs : -- 2.50.1