@@ -1216,6 +1216,88 @@ inline int get_command_index(char cmd_char)
12161216 return -1 ;
12171217}
12181218
1219+ static LINE_BUFFER *batch_readline_init (ulong max_size, const char *path)
1220+ {
1221+ LINE_BUFFER *line_buff;
1222+ File file;
1223+ MY_STAT input_file_stat;
1224+ char buff[FN_REFLEN + 512 ];
1225+
1226+ if (path)
1227+ {
1228+ file= my_open (path, O_RDONLY | O_BINARY, MYF (0 ));
1229+ if (file < 0 && my_errno == ENOENT && script_dir)
1230+ {
1231+ char full_path[FN_REFLEN];
1232+ strxnmov (full_path, sizeof (full_path)-1 , script_dir, " /" , path, NULL );
1233+ file= my_open (full_path, O_RDONLY | O_BINARY, MYF (0 ));
1234+ }
1235+ if (file < 0 )
1236+ {
1237+ #ifdef _WIN32
1238+ if (my_errno == EACCES && my_stat (path, &input_file_stat, MYF (0 )) &&
1239+ MY_S_ISDIR (input_file_stat.st_mode ))
1240+ my_snprintf (buff, sizeof (buff), " Can't read from a directory '%.*s'" ,
1241+ FN_REFLEN, path);
1242+ else
1243+ #endif
1244+ my_snprintf (buff, sizeof (buff), " Failed to open file '%.*s', error: %d" ,
1245+ FN_REFLEN, path, my_errno);
1246+ put_info (buff, INFO_ERROR, 0 );
1247+ return 0 ;
1248+ }
1249+ }
1250+ else
1251+ {
1252+ file= my_fileno (stdin);
1253+ }
1254+
1255+ if (my_fstat (file, &input_file_stat, MYF (0 )))
1256+ {
1257+ my_snprintf (buff, sizeof (buff), " Failed to stat file '%.*s', error: %d" ,
1258+ FN_REFLEN, path ? path : " stdin" , my_errno);
1259+ goto err1;
1260+ }
1261+
1262+ if (MY_S_ISDIR (input_file_stat.st_mode ))
1263+ {
1264+ my_snprintf (buff, sizeof (buff), " Can't read from a directory '%.*s'" ,
1265+ FN_REFLEN, path ? path : " stdin" );
1266+ goto err1;
1267+ }
1268+
1269+ #ifndef _WIN32
1270+ if (MY_S_ISBLK (input_file_stat.st_mode ))
1271+ {
1272+ my_snprintf (buff, sizeof (buff), " Can't read from a block device '%.*s'" ,
1273+ FN_REFLEN, path ? path : " stdin" );
1274+ goto err1;
1275+ }
1276+ #endif
1277+
1278+ if (!(line_buff= (LINE_BUFFER*) my_malloc (PSI_NOT_INSTRUMENTED,
1279+ sizeof (*line_buff),
1280+ MYF (MY_WME | MY_ZEROFILL))))
1281+ {
1282+ goto err;
1283+ }
1284+
1285+ if (init_line_buffer (line_buff, file, IO_SIZE, max_size))
1286+ {
1287+ my_free (line_buff);
1288+ goto err;
1289+ }
1290+
1291+ return line_buff;
1292+
1293+ err1:
1294+ put_info (buff, INFO_ERROR, 0 );
1295+ err:
1296+ if (path)
1297+ my_close (file, MYF (0 ));
1298+ return 0 ;
1299+ }
1300+
12191301static int delimiter_index= -1 ;
12201302static int charset_index= -1 ;
12211303static int sandbox_index= -1 ;
@@ -1290,10 +1372,8 @@ int main(int argc,char *argv[])
12901372 }
12911373
12921374 if (status.batch && !status.line_buff &&
1293- !(status.line_buff = batch_readline_init (MAX_BATCH_BUFFER_SIZE, stdin )))
1375+ !(status.line_buff = batch_readline_init (MAX_BATCH_BUFFER_SIZE, NULL )))
12941376 {
1295- put_info (" Can't initialize batch_readline - may be the input source is "
1296- " a directory or a block device." , INFO_ERROR, 0 );
12971377 free_defaults (defaults_argv);
12981378 my_end (0 );
12991379 exit (1 );
@@ -4742,11 +4822,9 @@ static int com_connect(String *buffer, char *line)
47424822static int com_source (String *, char *line)
47434823{
47444824 char source_name[FN_REFLEN], *end, *param;
4745- char full_path[FN_REFLEN];
47464825 LINE_BUFFER *line_buff;
47474826 int error;
47484827 STATUS old_status;
4749- FILE *sql_file;
47504828 my_bool save_ignore_errors;
47514829
47524830 if (status.sandbox )
@@ -4766,27 +4844,10 @@ static int com_source(String *, char *line)
47664844 end--;
47674845 end[0 ]=0 ;
47684846 unpack_filename (source_name,source_name);
4769- /* open file name */
4770- if (!(sql_file = my_fopen (source_name, O_RDONLY | O_BINARY,MYF (0 ))))
4771- {
4772- if (script_dir)
4773- {
4774- snprintf (full_path, sizeof (full_path), " %s/%s" , script_dir, source_name);
4775- sql_file = my_fopen (full_path, O_RDONLY | O_BINARY, MYF (0 ));
4776- }
4777- }
4778-
4779- if (!sql_file)
4780- {
4781- char buff[FN_REFLEN + 60 ];
4782- sprintf (buff, " Failed to open file '%s', error: %d" , source_name, errno);
4783- return put_info (buff, INFO_ERROR, 0 );
4784- }
47854847
4786- if (!(line_buff= batch_readline_init (MAX_BATCH_BUFFER_SIZE, sql_file )))
4848+ if (!(line_buff= batch_readline_init (MAX_BATCH_BUFFER_SIZE, source_name )))
47874849 {
4788- my_fclose (sql_file,MYF (0 ));
4789- return put_info (" Can't initialize batch_readline" , INFO_ERROR, 0 );
4850+ return ignore_errors ? -1 : 1 ;
47904851 }
47914852
47924853 /* Save old status */
@@ -4805,7 +4866,7 @@ static int com_source(String *, char *line)
48054866 ignore_errors= save_ignore_errors;
48064867 status=old_status; // Continue as before
48074868 in_com_source= aborted= 0 ;
4808- my_fclose (sql_file, MYF (0 ));
4869+ my_close (line_buff-> file , MYF (0 ));
48094870 batch_readline_end (line_buff);
48104871 /*
48114872 If we got an error during source operation, don't abort the client
0 commit comments