@@ -1333,22 +1333,31 @@ static int count_commands(struct todo_list *todo_list)
13331333 return count ;
13341334}
13351335
1336+ static ssize_t strbuf_read_file_or_whine (struct strbuf * sb , const char * path )
1337+ {
1338+ int fd ;
1339+ ssize_t len ;
1340+
1341+ fd = open (path , O_RDONLY );
1342+ if (fd < 0 )
1343+ return error_errno (_ ("could not open '%s'" ), path );
1344+ len = strbuf_read (sb , fd , 0 );
1345+ close (fd );
1346+ if (len < 0 )
1347+ return error (_ ("could not read '%s'." ), path );
1348+ return len ;
1349+ }
1350+
13361351static int read_populate_todo (struct todo_list * todo_list ,
13371352 struct replay_opts * opts )
13381353{
13391354 struct stat st ;
13401355 const char * todo_file = get_todo_path (opts );
1341- int fd , res ;
1356+ int res ;
13421357
13431358 strbuf_reset (& todo_list -> buf );
1344- fd = open (todo_file , O_RDONLY );
1345- if (fd < 0 )
1346- return error_errno (_ ("could not open '%s'" ), todo_file );
1347- if (strbuf_read (& todo_list -> buf , fd , 0 ) < 0 ) {
1348- close (fd );
1349- return error (_ ("could not read '%s'." ), todo_file );
1350- }
1351- close (fd );
1359+ if (strbuf_read_file_or_whine (& todo_list -> buf , todo_file ) < 0 )
1360+ return -1 ;
13521361
13531362 res = stat (todo_file , & st );
13541363 if (res )
@@ -2575,20 +2584,13 @@ int check_todo_list(void)
25752584 struct strbuf todo_file = STRBUF_INIT ;
25762585 struct todo_list todo_list = TODO_LIST_INIT ;
25772586 struct strbuf missing = STRBUF_INIT ;
2578- int advise_to_edit_todo = 0 , res = 0 , fd , i ;
2587+ int advise_to_edit_todo = 0 , res = 0 , i ;
25792588
25802589 strbuf_addstr (& todo_file , rebase_path_todo ());
2581- fd = open (todo_file .buf , O_RDONLY );
2582- if (fd < 0 ) {
2583- res = error_errno (_ ("could not open '%s'" ), todo_file .buf );
2584- goto leave_check ;
2585- }
2586- if (strbuf_read (& todo_list .buf , fd , 0 ) < 0 ) {
2587- close (fd );
2588- res = error (_ ("could not read '%s'." ), todo_file .buf );
2590+ if (strbuf_read_file_or_whine (& todo_list .buf , todo_file .buf ) < 0 ) {
2591+ res = -1 ;
25892592 goto leave_check ;
25902593 }
2591- close (fd );
25922594 advise_to_edit_todo = res =
25932595 parse_insn_buffer (todo_list .buf .buf , & todo_list );
25942596
@@ -2604,17 +2606,10 @@ int check_todo_list(void)
26042606
26052607 todo_list_release (& todo_list );
26062608 strbuf_addstr (& todo_file , ".backup" );
2607- fd = open (todo_file .buf , O_RDONLY );
2608- if (fd < 0 ) {
2609- res = error_errno (_ ("could not open '%s'" ), todo_file .buf );
2610- goto leave_check ;
2611- }
2612- if (strbuf_read (& todo_list .buf , fd , 0 ) < 0 ) {
2613- close (fd );
2614- res = error (_ ("could not read '%s'." ), todo_file .buf );
2609+ if (strbuf_read_file_or_whine (& todo_list .buf , todo_file .buf ) < 0 ) {
2610+ res = -1 ;
26152611 goto leave_check ;
26162612 }
2617- close (fd );
26182613 strbuf_release (& todo_file );
26192614 res = !!parse_insn_buffer (todo_list .buf .buf , & todo_list );
26202615
@@ -2682,15 +2677,8 @@ int skip_unnecessary_picks(void)
26822677 }
26832678 strbuf_release (& buf );
26842679
2685- fd = open (todo_file , O_RDONLY );
2686- if (fd < 0 ) {
2687- return error_errno (_ ("could not open '%s'" ), todo_file );
2688- }
2689- if (strbuf_read (& todo_list .buf , fd , 0 ) < 0 ) {
2690- close (fd );
2691- return error (_ ("could not read '%s'." ), todo_file );
2692- }
2693- close (fd );
2680+ if (strbuf_read_file_or_whine (& todo_list .buf , todo_file ) < 0 )
2681+ return -1 ;
26942682 if (parse_insn_buffer (todo_list .buf .buf , & todo_list ) < 0 ) {
26952683 todo_list_release (& todo_list );
26962684 return -1 ;
@@ -2799,17 +2787,11 @@ int rearrange_squash(void)
27992787 const char * todo_file = rebase_path_todo ();
28002788 struct todo_list todo_list = TODO_LIST_INIT ;
28012789 struct hashmap subject2item ;
2802- int res = 0 , rearranged = 0 , * next , * tail , fd , i ;
2790+ int res = 0 , rearranged = 0 , * next , * tail , i ;
28032791 char * * subjects ;
28042792
2805- fd = open (todo_file , O_RDONLY );
2806- if (fd < 0 )
2807- return error_errno (_ ("could not open '%s'" ), todo_file );
2808- if (strbuf_read (& todo_list .buf , fd , 0 ) < 0 ) {
2809- close (fd );
2810- return error (_ ("could not read '%s'." ), todo_file );
2811- }
2812- close (fd );
2793+ if (strbuf_read_file_or_whine (& todo_list .buf , todo_file ) < 0 )
2794+ return -1 ;
28132795 if (parse_insn_buffer (todo_list .buf .buf , & todo_list ) < 0 ) {
28142796 todo_list_release (& todo_list );
28152797 return -1 ;
0 commit comments