@@ -1478,9 +1478,9 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,
1478
1478
1479
1479
static void initialize_maintenance_strategy (void )
1480
1480
{
1481
- char * config_str ;
1481
+ const char * config_str ;
1482
1482
1483
- if (git_config_get_string ("maintenance.strategy" , & config_str ))
1483
+ if (git_config_get_string_tmp ("maintenance.strategy" , & config_str ))
1484
1484
return ;
1485
1485
1486
1486
if (!strcasecmp (config_str , "incremental" )) {
@@ -1782,32 +1782,33 @@ static const char *get_frequency(enum schedule_priority schedule)
1782
1782
* * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1783
1783
* In this case, the *cmd value is read as input.
1784
1784
*
1785
- * * if the input value * cmd is the key of one of the comma-separated list
1786
- * item, then *is_available is set to true and *cmd is modified and becomes
1785
+ * * if the input value cmd is the key of one of the comma-separated list
1786
+ * item, then *is_available is set to true and *out is set to
1787
1787
* the mock command.
1788
1788
*
1789
1789
* * if the input value *cmd isn’t the key of any of the comma-separated list
1790
- * item, then *is_available is set to false.
1790
+ * item, then *is_available is set to false and *out is set to the original
1791
+ * command.
1791
1792
*
1792
1793
* Ex.:
1793
1794
* GIT_TEST_MAINT_SCHEDULER not set
1794
1795
* +-------+-------------------------------------------------+
1795
1796
* | Input | Output |
1796
- * | *cmd | return code | *cmd | *is_available |
1797
+ * | *cmd | return code | *out | *is_available |
1797
1798
* +-------+-------------+-------------------+---------------+
1798
- * | "foo" | false | "foo" (unchanged) | (unchanged) |
1799
+ * | "foo" | false | NULL | (unchanged) |
1799
1800
* +-------+-------------+-------------------+---------------+
1800
1801
*
1801
1802
* GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1802
1803
* +-------+-------------------------------------------------+
1803
1804
* | Input | Output |
1804
- * | *cmd | return code | *cmd | *is_available |
1805
+ * | *cmd | return code | *out | *is_available |
1805
1806
* +-------+-------------+-------------------+---------------+
1806
1807
* | "foo" | true | "./mock.foo.sh" | true |
1807
- * | "qux" | true | "qux" (unchanged ) | false |
1808
+ * | "qux" | true | "qux" (allocated ) | false |
1808
1809
* +-------+-------------+-------------------+---------------+
1809
1810
*/
1810
- static int get_schedule_cmd (const char * * cmd , int * is_available )
1811
+ static int get_schedule_cmd (const char * cmd , int * is_available , char * * out )
1811
1812
{
1812
1813
char * testing = xstrdup_or_null (getenv ("GIT_TEST_MAINT_SCHEDULER" ));
1813
1814
struct string_list_item * item ;
@@ -1826,16 +1827,22 @@ static int get_schedule_cmd(const char **cmd, int *is_available)
1826
1827
if (string_list_split_in_place (& pair , item -> string , ":" , 2 ) != 2 )
1827
1828
continue ;
1828
1829
1829
- if (!strcmp (* cmd , pair .items [0 ].string )) {
1830
- * cmd = pair .items [1 ].string ;
1830
+ if (!strcmp (cmd , pair .items [0 ].string )) {
1831
+ if (out )
1832
+ * out = xstrdup (pair .items [1 ].string );
1831
1833
if (is_available )
1832
1834
* is_available = 1 ;
1833
- string_list_clear (& list , 0 );
1834
- UNLEAK (testing );
1835
- return 1 ;
1835
+ string_list_clear (& pair , 0 );
1836
+ goto out ;
1836
1837
}
1838
+
1839
+ string_list_clear (& pair , 0 );
1837
1840
}
1838
1841
1842
+ if (out )
1843
+ * out = xstrdup (cmd );
1844
+
1845
+ out :
1839
1846
string_list_clear (& list , 0 );
1840
1847
free (testing );
1841
1848
return 1 ;
@@ -1852,9 +1859,8 @@ static int get_random_minute(void)
1852
1859
1853
1860
static int is_launchctl_available (void )
1854
1861
{
1855
- const char * cmd = "launchctl" ;
1856
1862
int is_available ;
1857
- if (get_schedule_cmd (& cmd , & is_available ))
1863
+ if (get_schedule_cmd ("launchctl" , & is_available , NULL ))
1858
1864
return is_available ;
1859
1865
1860
1866
#ifdef __APPLE__
@@ -1892,12 +1898,12 @@ static char *launchctl_get_uid(void)
1892
1898
1893
1899
static int launchctl_boot_plist (int enable , const char * filename )
1894
1900
{
1895
- const char * cmd = "launchctl" ;
1901
+ char * cmd ;
1896
1902
int result ;
1897
1903
struct child_process child = CHILD_PROCESS_INIT ;
1898
1904
char * uid = launchctl_get_uid ();
1899
1905
1900
- get_schedule_cmd (& cmd , NULL );
1906
+ get_schedule_cmd ("launchctl" , NULL , & cmd );
1901
1907
strvec_split (& child .args , cmd );
1902
1908
strvec_pushl (& child .args , enable ? "bootstrap" : "bootout" , uid ,
1903
1909
filename , NULL );
@@ -1910,6 +1916,7 @@ static int launchctl_boot_plist(int enable, const char *filename)
1910
1916
1911
1917
result = finish_command (& child );
1912
1918
1919
+ free (cmd );
1913
1920
free (uid );
1914
1921
return result ;
1915
1922
}
@@ -1961,10 +1968,10 @@ static int launchctl_schedule_plist(const char *exec_path, enum schedule_priorit
1961
1968
static unsigned long lock_file_timeout_ms = ULONG_MAX ;
1962
1969
struct strbuf plist = STRBUF_INIT , plist2 = STRBUF_INIT ;
1963
1970
struct stat st ;
1964
- const char * cmd = "launchctl" ;
1971
+ char * cmd ;
1965
1972
int minute = get_random_minute ();
1966
1973
1967
- get_schedule_cmd (& cmd , NULL );
1974
+ get_schedule_cmd ("launchctl" , NULL , & cmd );
1968
1975
preamble = "<?xml version=\"1.0\"?>\n"
1969
1976
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
1970
1977
"<plist version=\"1.0\">"
@@ -2054,6 +2061,7 @@ static int launchctl_schedule_plist(const char *exec_path, enum schedule_priorit
2054
2061
2055
2062
free (filename );
2056
2063
free (name );
2064
+ free (cmd );
2057
2065
strbuf_release (& plist );
2058
2066
strbuf_release (& plist2 );
2059
2067
return 0 ;
@@ -2078,9 +2086,8 @@ static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
2078
2086
2079
2087
static int is_schtasks_available (void )
2080
2088
{
2081
- const char * cmd = "schtasks" ;
2082
2089
int is_available ;
2083
- if (get_schedule_cmd (& cmd , & is_available ))
2090
+ if (get_schedule_cmd ("schtasks" , & is_available , NULL ))
2084
2091
return is_available ;
2085
2092
2086
2093
#ifdef GIT_WINDOWS_NATIVE
@@ -2099,15 +2106,16 @@ static char *schtasks_task_name(const char *frequency)
2099
2106
2100
2107
static int schtasks_remove_task (enum schedule_priority schedule )
2101
2108
{
2102
- const char * cmd = "schtasks" ;
2109
+ char * cmd ;
2103
2110
struct child_process child = CHILD_PROCESS_INIT ;
2104
2111
const char * frequency = get_frequency (schedule );
2105
2112
char * name = schtasks_task_name (frequency );
2106
2113
2107
- get_schedule_cmd (& cmd , NULL );
2114
+ get_schedule_cmd ("schtasks" , NULL , & cmd );
2108
2115
strvec_split (& child .args , cmd );
2109
2116
strvec_pushl (& child .args , "/delete" , "/tn" , name , "/f" , NULL );
2110
2117
free (name );
2118
+ free (cmd );
2111
2119
2112
2120
return run_command (& child );
2113
2121
}
@@ -2121,7 +2129,7 @@ static int schtasks_remove_tasks(void)
2121
2129
2122
2130
static int schtasks_schedule_task (const char * exec_path , enum schedule_priority schedule )
2123
2131
{
2124
- const char * cmd = "schtasks" ;
2132
+ char * cmd ;
2125
2133
int result ;
2126
2134
struct child_process child = CHILD_PROCESS_INIT ;
2127
2135
const char * xml ;
@@ -2131,7 +2139,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
2131
2139
struct strbuf tfilename = STRBUF_INIT ;
2132
2140
int minute = get_random_minute ();
2133
2141
2134
- get_schedule_cmd (& cmd , NULL );
2142
+ get_schedule_cmd ("schtasks" , NULL , & cmd );
2135
2143
2136
2144
strbuf_addf (& tfilename , "%s/schedule_%s_XXXXXX" ,
2137
2145
repo_get_common_dir (the_repository ), frequency );
@@ -2237,6 +2245,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
2237
2245
2238
2246
delete_tempfile (& tfile );
2239
2247
free (name );
2248
+ free (cmd );
2240
2249
return result ;
2241
2250
}
2242
2251
@@ -2278,29 +2287,36 @@ static int check_crontab_process(const char *cmd)
2278
2287
2279
2288
static int is_crontab_available (void )
2280
2289
{
2281
- const char * cmd = "crontab" ;
2290
+ char * cmd ;
2282
2291
int is_available ;
2292
+ int ret ;
2283
2293
2284
- if (get_schedule_cmd (& cmd , & is_available ))
2285
- return is_available ;
2294
+ if (get_schedule_cmd ("crontab" , & is_available , & cmd )) {
2295
+ ret = is_available ;
2296
+ goto out ;
2297
+ }
2286
2298
2287
2299
#ifdef __APPLE__
2288
2300
/*
2289
2301
* macOS has cron, but it requires special permissions and will
2290
2302
* create a UI alert when attempting to run this command.
2291
2303
*/
2292
- return 0 ;
2304
+ ret = 0 ;
2293
2305
#else
2294
- return check_crontab_process (cmd );
2306
+ ret = check_crontab_process (cmd );
2295
2307
#endif
2308
+
2309
+ out :
2310
+ free (cmd );
2311
+ return ret ;
2296
2312
}
2297
2313
2298
2314
#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2299
2315
#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2300
2316
2301
2317
static int crontab_update_schedule (int run_maintenance , int fd )
2302
2318
{
2303
- const char * cmd = "crontab" ;
2319
+ char * cmd ;
2304
2320
int result = 0 ;
2305
2321
int in_old_region = 0 ;
2306
2322
struct child_process crontab_list = CHILD_PROCESS_INIT ;
@@ -2310,15 +2326,17 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2310
2326
struct tempfile * tmpedit = NULL ;
2311
2327
int minute = get_random_minute ();
2312
2328
2313
- get_schedule_cmd (& cmd , NULL );
2329
+ get_schedule_cmd ("crontab" , NULL , & cmd );
2314
2330
strvec_split (& crontab_list .args , cmd );
2315
2331
strvec_push (& crontab_list .args , "-l" );
2316
2332
crontab_list .in = -1 ;
2317
2333
crontab_list .out = dup (fd );
2318
2334
crontab_list .git_cmd = 0 ;
2319
2335
2320
- if (start_command (& crontab_list ))
2321
- return error (_ ("failed to run 'crontab -l'; your system might not support 'cron'" ));
2336
+ if (start_command (& crontab_list )) {
2337
+ result = error (_ ("failed to run 'crontab -l'; your system might not support 'cron'" ));
2338
+ goto out ;
2339
+ }
2322
2340
2323
2341
/* Ignore exit code, as an empty crontab will return error. */
2324
2342
finish_command (& crontab_list );
@@ -2388,8 +2406,10 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2388
2406
result = error (_ ("'crontab' died" ));
2389
2407
else
2390
2408
fclose (cron_list );
2409
+
2391
2410
out :
2392
2411
delete_tempfile (& tmpedit );
2412
+ free (cmd );
2393
2413
return result ;
2394
2414
}
2395
2415
@@ -2412,10 +2432,9 @@ static int real_is_systemd_timer_available(void)
2412
2432
2413
2433
static int is_systemd_timer_available (void )
2414
2434
{
2415
- const char * cmd = "systemctl" ;
2416
2435
int is_available ;
2417
2436
2418
- if (get_schedule_cmd (& cmd , & is_available ))
2437
+ if (get_schedule_cmd ("systemctl" , & is_available , NULL ))
2419
2438
return is_available ;
2420
2439
2421
2440
return real_is_systemd_timer_available ();
@@ -2596,9 +2615,10 @@ static int systemd_timer_enable_unit(int enable,
2596
2615
enum schedule_priority schedule ,
2597
2616
int minute )
2598
2617
{
2599
- const char * cmd = "systemctl" ;
2618
+ char * cmd = NULL ;
2600
2619
struct child_process child = CHILD_PROCESS_INIT ;
2601
2620
const char * frequency = get_frequency (schedule );
2621
+ int ret ;
2602
2622
2603
2623
/*
2604
2624
* Disabling the systemd unit while it is already disabled makes
@@ -2609,30 +2629,43 @@ static int systemd_timer_enable_unit(int enable,
2609
2629
* On the other hand, enabling a systemd unit which is already enabled
2610
2630
* produces no error.
2611
2631
*/
2612
- if (!enable )
2632
+ if (!enable ) {
2613
2633
child .no_stderr = 1 ;
2614
- else if (systemd_timer_write_timer_file (schedule , minute ))
2615
- return -1 ;
2634
+ } else if (systemd_timer_write_timer_file (schedule , minute )) {
2635
+ ret = -1 ;
2636
+ goto out ;
2637
+ }
2616
2638
2617
- get_schedule_cmd (& cmd , NULL );
2639
+ get_schedule_cmd ("systemctl" , NULL , & cmd );
2618
2640
strvec_split (& child .args , cmd );
2619
2641
strvec_pushl (& child .args , "--user" , enable ? "enable" : "disable" ,
2620
2642
"--now" , NULL );
2621
2643
strvec_pushf (& child .args , SYSTEMD_UNIT_FORMAT , frequency , "timer" );
2622
2644
2623
- if (start_command (& child ))
2624
- return error (_ ("failed to start systemctl" ));
2625
- if (finish_command (& child ))
2645
+ if (start_command (& child )) {
2646
+ ret = error (_ ("failed to start systemctl" ));
2647
+ goto out ;
2648
+ }
2649
+
2650
+ if (finish_command (& child )) {
2626
2651
/*
2627
2652
* Disabling an already disabled systemd unit makes
2628
2653
* systemctl fail.
2629
2654
* Let's ignore this failure.
2630
2655
*
2631
2656
* Enabling an enabled systemd unit doesn't fail.
2632
2657
*/
2633
- if (enable )
2634
- return error (_ ("failed to run systemctl" ));
2635
- return 0 ;
2658
+ if (enable ) {
2659
+ ret = error (_ ("failed to run systemctl" ));
2660
+ goto out ;
2661
+ }
2662
+ }
2663
+
2664
+ ret = 0 ;
2665
+
2666
+ out :
2667
+ free (cmd );
2668
+ return ret ;
2636
2669
}
2637
2670
2638
2671
/*
0 commit comments