@@ -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" )) {
@@ -1818,32 +1818,33 @@ static const char *get_extra_launchctl_strings(void) {
1818
1818
* * If $GIT_TEST_MAINT_SCHEDULER is set, return true.
1819
1819
* In this case, the *cmd value is read as input.
1820
1820
*
1821
- * * if the input value * cmd is the key of one of the comma-separated list
1822
- * item, then *is_available is set to true and *cmd is modified and becomes
1821
+ * * if the input value cmd is the key of one of the comma-separated list
1822
+ * item, then *is_available is set to true and *out is set to
1823
1823
* the mock command.
1824
1824
*
1825
1825
* * if the input value *cmd isn’t the key of any of the comma-separated list
1826
- * item, then *is_available is set to false.
1826
+ * item, then *is_available is set to false and *out is set to the original
1827
+ * command.
1827
1828
*
1828
1829
* Ex.:
1829
1830
* GIT_TEST_MAINT_SCHEDULER not set
1830
1831
* +-------+-------------------------------------------------+
1831
1832
* | Input | Output |
1832
- * | *cmd | return code | *cmd | *is_available |
1833
+ * | *cmd | return code | *out | *is_available |
1833
1834
* +-------+-------------+-------------------+---------------+
1834
- * | "foo" | false | "foo" (unchanged) | (unchanged) |
1835
+ * | "foo" | false | NULL | (unchanged) |
1835
1836
* +-------+-------------+-------------------+---------------+
1836
1837
*
1837
1838
* GIT_TEST_MAINT_SCHEDULER set to “foo:./mock_foo.sh,bar:./mock_bar.sh”
1838
1839
* +-------+-------------------------------------------------+
1839
1840
* | Input | Output |
1840
- * | *cmd | return code | *cmd | *is_available |
1841
+ * | *cmd | return code | *out | *is_available |
1841
1842
* +-------+-------------+-------------------+---------------+
1842
1843
* | "foo" | true | "./mock.foo.sh" | true |
1843
- * | "qux" | true | "qux" (unchanged ) | false |
1844
+ * | "qux" | true | "qux" (allocated ) | false |
1844
1845
* +-------+-------------+-------------------+---------------+
1845
1846
*/
1846
- static int get_schedule_cmd (const char * * cmd , int * is_available )
1847
+ static int get_schedule_cmd (const char * cmd , int * is_available , char * * out )
1847
1848
{
1848
1849
char * testing = xstrdup_or_null (getenv ("GIT_TEST_MAINT_SCHEDULER" ));
1849
1850
struct string_list_item * item ;
@@ -1862,16 +1863,22 @@ static int get_schedule_cmd(const char **cmd, int *is_available)
1862
1863
if (string_list_split_in_place (& pair , item -> string , ":" , 2 ) != 2 )
1863
1864
continue ;
1864
1865
1865
- if (!strcmp (* cmd , pair .items [0 ].string )) {
1866
- * cmd = pair .items [1 ].string ;
1866
+ if (!strcmp (cmd , pair .items [0 ].string )) {
1867
+ if (out )
1868
+ * out = xstrdup (pair .items [1 ].string );
1867
1869
if (is_available )
1868
1870
* is_available = 1 ;
1869
- string_list_clear (& list , 0 );
1870
- UNLEAK (testing );
1871
- return 1 ;
1871
+ string_list_clear (& pair , 0 );
1872
+ goto out ;
1872
1873
}
1874
+
1875
+ string_list_clear (& pair , 0 );
1873
1876
}
1874
1877
1878
+ if (out )
1879
+ * out = xstrdup (cmd );
1880
+
1881
+ out :
1875
1882
string_list_clear (& list , 0 );
1876
1883
free (testing );
1877
1884
return 1 ;
@@ -1888,9 +1895,8 @@ static int get_random_minute(void)
1888
1895
1889
1896
static int is_launchctl_available (void )
1890
1897
{
1891
- const char * cmd = "launchctl" ;
1892
1898
int is_available ;
1893
- if (get_schedule_cmd (& cmd , & is_available ))
1899
+ if (get_schedule_cmd ("launchctl" , & is_available , NULL ))
1894
1900
return is_available ;
1895
1901
1896
1902
#ifdef __APPLE__
@@ -1928,12 +1934,12 @@ static char *launchctl_get_uid(void)
1928
1934
1929
1935
static int launchctl_boot_plist (int enable , const char * filename )
1930
1936
{
1931
- const char * cmd = "launchctl" ;
1937
+ char * cmd ;
1932
1938
int result ;
1933
1939
struct child_process child = CHILD_PROCESS_INIT ;
1934
1940
char * uid = launchctl_get_uid ();
1935
1941
1936
- get_schedule_cmd (& cmd , NULL );
1942
+ get_schedule_cmd ("launchctl" , NULL , & cmd );
1937
1943
strvec_split (& child .args , cmd );
1938
1944
strvec_pushl (& child .args , enable ? "bootstrap" : "bootout" , uid ,
1939
1945
filename , NULL );
@@ -1946,6 +1952,7 @@ static int launchctl_boot_plist(int enable, const char *filename)
1946
1952
1947
1953
result = finish_command (& child );
1948
1954
1955
+ free (cmd );
1949
1956
free (uid );
1950
1957
return result ;
1951
1958
}
@@ -1997,10 +2004,10 @@ static int launchctl_schedule_plist(const char *exec_path, enum schedule_priorit
1997
2004
static unsigned long lock_file_timeout_ms = ULONG_MAX ;
1998
2005
struct strbuf plist = STRBUF_INIT , plist2 = STRBUF_INIT ;
1999
2006
struct stat st ;
2000
- const char * cmd = "launchctl" ;
2007
+ char * cmd ;
2001
2008
int minute = get_random_minute ();
2002
2009
2003
- get_schedule_cmd (& cmd , NULL );
2010
+ get_schedule_cmd ("launchctl" , NULL , & cmd );
2004
2011
preamble = "<?xml version=\"1.0\"?>\n"
2005
2012
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
2006
2013
"<plist version=\"1.0\">"
@@ -2092,6 +2099,7 @@ static int launchctl_schedule_plist(const char *exec_path, enum schedule_priorit
2092
2099
2093
2100
free (filename );
2094
2101
free (name );
2102
+ free (cmd );
2095
2103
strbuf_release (& plist );
2096
2104
strbuf_release (& plist2 );
2097
2105
return 0 ;
@@ -2116,9 +2124,8 @@ static int launchctl_update_schedule(int run_maintenance, int fd UNUSED)
2116
2124
2117
2125
static int is_schtasks_available (void )
2118
2126
{
2119
- const char * cmd = "schtasks" ;
2120
2127
int is_available ;
2121
- if (get_schedule_cmd (& cmd , & is_available ))
2128
+ if (get_schedule_cmd ("schtasks" , & is_available , NULL ))
2122
2129
return is_available ;
2123
2130
2124
2131
#ifdef GIT_WINDOWS_NATIVE
@@ -2137,15 +2144,16 @@ static char *schtasks_task_name(const char *frequency)
2137
2144
2138
2145
static int schtasks_remove_task (enum schedule_priority schedule )
2139
2146
{
2140
- const char * cmd = "schtasks" ;
2147
+ char * cmd ;
2141
2148
struct child_process child = CHILD_PROCESS_INIT ;
2142
2149
const char * frequency = get_frequency (schedule );
2143
2150
char * name = schtasks_task_name (frequency );
2144
2151
2145
- get_schedule_cmd (& cmd , NULL );
2152
+ get_schedule_cmd ("schtasks" , NULL , & cmd );
2146
2153
strvec_split (& child .args , cmd );
2147
2154
strvec_pushl (& child .args , "/delete" , "/tn" , name , "/f" , NULL );
2148
2155
free (name );
2156
+ free (cmd );
2149
2157
2150
2158
return run_command (& child );
2151
2159
}
@@ -2159,7 +2167,7 @@ static int schtasks_remove_tasks(void)
2159
2167
2160
2168
static int schtasks_schedule_task (const char * exec_path , enum schedule_priority schedule )
2161
2169
{
2162
- const char * cmd = "schtasks" ;
2170
+ char * cmd ;
2163
2171
int result ;
2164
2172
struct child_process child = CHILD_PROCESS_INIT ;
2165
2173
const char * xml ;
@@ -2169,7 +2177,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
2169
2177
struct strbuf tfilename = STRBUF_INIT ;
2170
2178
int minute = get_random_minute ();
2171
2179
2172
- get_schedule_cmd (& cmd , NULL );
2180
+ get_schedule_cmd ("schtasks" , NULL , & cmd );
2173
2181
2174
2182
strbuf_addf (& tfilename , "%s/schedule_%s_XXXXXX" ,
2175
2183
repo_get_common_dir (the_repository ), frequency );
@@ -2276,6 +2284,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
2276
2284
2277
2285
delete_tempfile (& tfile );
2278
2286
free (name );
2287
+ free (cmd );
2279
2288
return result ;
2280
2289
}
2281
2290
@@ -2317,29 +2326,36 @@ static int check_crontab_process(const char *cmd)
2317
2326
2318
2327
static int is_crontab_available (void )
2319
2328
{
2320
- const char * cmd = "crontab" ;
2329
+ char * cmd ;
2321
2330
int is_available ;
2331
+ int ret ;
2322
2332
2323
- if (get_schedule_cmd (& cmd , & is_available ))
2324
- return is_available ;
2333
+ if (get_schedule_cmd ("crontab" , & is_available , & cmd )) {
2334
+ ret = is_available ;
2335
+ goto out ;
2336
+ }
2325
2337
2326
2338
#ifdef __APPLE__
2327
2339
/*
2328
2340
* macOS has cron, but it requires special permissions and will
2329
2341
* create a UI alert when attempting to run this command.
2330
2342
*/
2331
- return 0 ;
2343
+ ret = 0 ;
2332
2344
#else
2333
- return check_crontab_process (cmd );
2345
+ ret = check_crontab_process (cmd );
2334
2346
#endif
2347
+
2348
+ out :
2349
+ free (cmd );
2350
+ return ret ;
2335
2351
}
2336
2352
2337
2353
#define BEGIN_LINE "# BEGIN GIT MAINTENANCE SCHEDULE"
2338
2354
#define END_LINE "# END GIT MAINTENANCE SCHEDULE"
2339
2355
2340
2356
static int crontab_update_schedule (int run_maintenance , int fd )
2341
2357
{
2342
- const char * cmd = "crontab" ;
2358
+ char * cmd ;
2343
2359
int result = 0 ;
2344
2360
int in_old_region = 0 ;
2345
2361
struct child_process crontab_list = CHILD_PROCESS_INIT ;
@@ -2349,15 +2365,17 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2349
2365
struct tempfile * tmpedit = NULL ;
2350
2366
int minute = get_random_minute ();
2351
2367
2352
- get_schedule_cmd (& cmd , NULL );
2368
+ get_schedule_cmd ("crontab" , NULL , & cmd );
2353
2369
strvec_split (& crontab_list .args , cmd );
2354
2370
strvec_push (& crontab_list .args , "-l" );
2355
2371
crontab_list .in = -1 ;
2356
2372
crontab_list .out = dup (fd );
2357
2373
crontab_list .git_cmd = 0 ;
2358
2374
2359
- if (start_command (& crontab_list ))
2360
- return error (_ ("failed to run 'crontab -l'; your system might not support 'cron'" ));
2375
+ if (start_command (& crontab_list )) {
2376
+ result = error (_ ("failed to run 'crontab -l'; your system might not support 'cron'" ));
2377
+ goto out ;
2378
+ }
2361
2379
2362
2380
/* Ignore exit code, as an empty crontab will return error. */
2363
2381
finish_command (& crontab_list );
@@ -2427,8 +2445,10 @@ static int crontab_update_schedule(int run_maintenance, int fd)
2427
2445
result = error (_ ("'crontab' died" ));
2428
2446
else
2429
2447
fclose (cron_list );
2448
+
2430
2449
out :
2431
2450
delete_tempfile (& tmpedit );
2451
+ free (cmd );
2432
2452
return result ;
2433
2453
}
2434
2454
@@ -2451,10 +2471,9 @@ static int real_is_systemd_timer_available(void)
2451
2471
2452
2472
static int is_systemd_timer_available (void )
2453
2473
{
2454
- const char * cmd = "systemctl" ;
2455
2474
int is_available ;
2456
2475
2457
- if (get_schedule_cmd (& cmd , & is_available ))
2476
+ if (get_schedule_cmd ("systemctl" , & is_available , NULL ))
2458
2477
return is_available ;
2459
2478
2460
2479
return real_is_systemd_timer_available ();
@@ -2635,9 +2654,10 @@ static int systemd_timer_enable_unit(int enable,
2635
2654
enum schedule_priority schedule ,
2636
2655
int minute )
2637
2656
{
2638
- const char * cmd = "systemctl" ;
2657
+ char * cmd = NULL ;
2639
2658
struct child_process child = CHILD_PROCESS_INIT ;
2640
2659
const char * frequency = get_frequency (schedule );
2660
+ int ret ;
2641
2661
2642
2662
/*
2643
2663
* Disabling the systemd unit while it is already disabled makes
@@ -2648,30 +2668,43 @@ static int systemd_timer_enable_unit(int enable,
2648
2668
* On the other hand, enabling a systemd unit which is already enabled
2649
2669
* produces no error.
2650
2670
*/
2651
- if (!enable )
2671
+ if (!enable ) {
2652
2672
child .no_stderr = 1 ;
2653
- else if (systemd_timer_write_timer_file (schedule , minute ))
2654
- return -1 ;
2673
+ } else if (systemd_timer_write_timer_file (schedule , minute )) {
2674
+ ret = -1 ;
2675
+ goto out ;
2676
+ }
2655
2677
2656
- get_schedule_cmd (& cmd , NULL );
2678
+ get_schedule_cmd ("systemctl" , NULL , & cmd );
2657
2679
strvec_split (& child .args , cmd );
2658
2680
strvec_pushl (& child .args , "--user" , enable ? "enable" : "disable" ,
2659
2681
"--now" , NULL );
2660
2682
strvec_pushf (& child .args , SYSTEMD_UNIT_FORMAT , frequency , "timer" );
2661
2683
2662
- if (start_command (& child ))
2663
- return error (_ ("failed to start systemctl" ));
2664
- if (finish_command (& child ))
2684
+ if (start_command (& child )) {
2685
+ ret = error (_ ("failed to start systemctl" ));
2686
+ goto out ;
2687
+ }
2688
+
2689
+ if (finish_command (& child )) {
2665
2690
/*
2666
2691
* Disabling an already disabled systemd unit makes
2667
2692
* systemctl fail.
2668
2693
* Let's ignore this failure.
2669
2694
*
2670
2695
* Enabling an enabled systemd unit doesn't fail.
2671
2696
*/
2672
- if (enable )
2673
- return error (_ ("failed to run systemctl" ));
2674
- return 0 ;
2697
+ if (enable ) {
2698
+ ret = error (_ ("failed to run systemctl" ));
2699
+ goto out ;
2700
+ }
2701
+ }
2702
+
2703
+ ret = 0 ;
2704
+
2705
+ out :
2706
+ free (cmd );
2707
+ return ret ;
2675
2708
}
2676
2709
2677
2710
/*
0 commit comments