@@ -2087,6 +2087,7 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2087
2087
struct child_process crontab_edit = CHILD_PROCESS_INIT ;
2088
2088
FILE * cron_list , * cron_in ;
2089
2089
struct strbuf line = STRBUF_INIT ;
2090
+ struct tempfile * tmpedit = NULL ;
2090
2091
2091
2092
get_schedule_cmd (& cmd , NULL );
2092
2093
strvec_split (& crontab_list .args , cmd );
@@ -2101,26 +2102,24 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2101
2102
/* Ignore exit code, as an empty crontab will return error. */
2102
2103
finish_command (& crontab_list );
2103
2104
2105
+ tmpedit = mks_tempfile_t (".git_cron_edit_tmpXXXXXX" );
2106
+ if (!tmpedit ) {
2107
+ result = error (_ ("failed to create crontab temporary file" ));
2108
+ goto out ;
2109
+ }
2110
+ cron_in = fdopen_tempfile (tmpedit , "w" );
2111
+ if (!cron_in ) {
2112
+ result = error (_ ("failed to open temporary file" ));
2113
+ goto out ;
2114
+ }
2115
+
2104
2116
/*
2105
2117
* Read from the .lock file, filtering out the old
2106
2118
* schedule while appending the new schedule.
2107
2119
*/
2108
2120
cron_list = fdopen (fd , "r" );
2109
2121
rewind (cron_list );
2110
2122
2111
- strvec_split (& crontab_edit .args , cmd );
2112
- crontab_edit .in = -1 ;
2113
- crontab_edit .git_cmd = 0 ;
2114
-
2115
- if (start_command (& crontab_edit ))
2116
- return error (_ ("failed to run 'crontab'; your system might not support 'cron'" ));
2117
-
2118
- cron_in = fdopen (crontab_edit .in , "w" );
2119
- if (!cron_in ) {
2120
- result = error (_ ("failed to open stdin of 'crontab'" ));
2121
- goto done_editing ;
2122
- }
2123
-
2124
2123
while (!strbuf_getline_lf (& line , cron_list )) {
2125
2124
if (!in_old_region && !strcmp (line .buf , BEGIN_LINE ))
2126
2125
in_old_region = 1 ;
@@ -2154,14 +2153,22 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2154
2153
}
2155
2154
2156
2155
fflush (cron_in );
2157
- fclose (cron_in );
2158
- close (crontab_edit .in );
2159
2156
2160
- done_editing :
2157
+ strvec_split (& crontab_edit .args , cmd );
2158
+ strvec_push (& crontab_edit .args , get_tempfile_path (tmpedit ));
2159
+ crontab_edit .git_cmd = 0 ;
2160
+
2161
+ if (start_command (& crontab_edit )) {
2162
+ result = error (_ ("failed to run 'crontab'; your system might not support 'cron'" ));
2163
+ goto out ;
2164
+ }
2165
+
2161
2166
if (finish_command (& crontab_edit ))
2162
2167
result = error (_ ("'crontab' died" ));
2163
2168
else
2164
2169
fclose (cron_list );
2170
+ out :
2171
+ delete_tempfile (& tmpedit );
2165
2172
return result ;
2166
2173
}
2167
2174
0 commit comments