comparison libinterp/corefcn/load-path.cc @ 32080:e9fdfebc6db0

Avoid using file_stat in libinterp/corefcn (bug #59711). * dirfnc.cc (F__mkdir__), file-io.cc (do_stream_open), graphics.cc (Fdrawnow), interpreter.cc (interpreter::execute_startup_files), load-path.cc (load_path::find_file, load_path::find_dir, load_path::find_matching_dirs, load_path::find_first_of, load_path::find_all_first_of, load_path::execute_pkg_add_or_del, load_path::add, load_path::find_private_file, load_path::dir_info::get_file_list, load_path::dir_info::get_method_file_map, genpath), load-save.cc (find_file_to_load), ls-mat5.cc (read_mat5_binary_element), urlwrite (Furlwrite), utils.cc (find_data_file_in_load_path, fcn_file_in_path), __ftp__.cc (F__ftp_mput__), __magick_read__.cc (F__magick_write__): Use functions "dir_exists" or "file_exists" instead of "file_stat".
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 06 May 2023 13:08:38 +0200
parents 359de694adce
children 3c608abd55f5
comparison
equal deleted inserted replaced
32079:b0785d76430a 32080:e9fdfebc6db0
31 #include <cctype> 31 #include <cctype>
32 32
33 #include "dir-ops.h" 33 #include "dir-ops.h"
34 #include "file-ops.h" 34 #include "file-ops.h"
35 #include "file-stat.h" 35 #include "file-stat.h"
36 #include "lo-sysdep.h"
36 #include "oct-env.h" 37 #include "oct-env.h"
37 #include "pathsearch.h" 38 #include "pathsearch.h"
38 39
39 #include "defaults.h" 40 #include "defaults.h"
40 #include "defun.h" 41 #include "defun.h"
546 { 547 {
547 std::string retval; 548 std::string retval;
548 549
549 if (sys::env::absolute_pathname (file) 550 if (sys::env::absolute_pathname (file)
550 || sys::env::rooted_relative_pathname (file)) 551 || sys::env::rooted_relative_pathname (file))
551 { 552 return sys::file_exists (file) ? file : retval;
552 sys::file_stat fs (file);
553
554 return fs.exists () ? file : retval;
555 }
556 else 553 else
557 { 554 {
558 std::string tfile = find_private_file (file); 555 std::string tfile = find_private_file (file);
559 556
560 if (! tfile.empty ()) 557 if (! tfile.empty ())
568 // element of the load path in turn. 565 // element of the load path in turn.
569 for (const auto& di : m_dir_info_list) 566 for (const auto& di : m_dir_info_list)
570 { 567 {
571 std::string tfile = sys::file_ops::concat (di.abs_dir_name, file); 568 std::string tfile = sys::file_ops::concat (di.abs_dir_name, file);
572 569
573 sys::file_stat fs (tfile); 570 if (sys::file_exists (tfile))
574
575 if (fs.exists ())
576 return tfile; 571 return tfile;
577 } 572 }
578 } 573 }
579 else 574 else
580 { 575 {
603 598
604 if (dir.find_first_of (sys::file_ops::dir_sep_chars ()) != std::string::npos 599 if (dir.find_first_of (sys::file_ops::dir_sep_chars ()) != std::string::npos
605 && (sys::env::absolute_pathname (dir) 600 && (sys::env::absolute_pathname (dir)
606 || sys::env::rooted_relative_pathname (dir))) 601 || sys::env::rooted_relative_pathname (dir)))
607 { 602 {
608 sys::file_stat fs (dir); 603 if (sys::dir_exists (dir))
609
610 if (fs.exists () && fs.is_dir ())
611 return dir; 604 return dir;
612 } 605 }
613 else 606 else
614 { 607 {
615 std::string canon_dir = maybe_canonicalize (dir); 608 std::string canon_dir = maybe_canonicalize (dir);
628 621
629 std::size_t dir_len = canon_dir.length (); 622 std::size_t dir_len = canon_dir.length ();
630 623
631 if (dname_len > dir_len 624 if (dname_len > dir_len
632 && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1]) 625 && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1])
633 && canon_dir == dname.substr (dname_len - dir_len)) 626 && canon_dir == dname.substr (dname_len - dir_len)
634 { 627 && sys::dir_exists (di.dir_name))
635 sys::file_stat fs (di.dir_name); 628 return di.abs_dir_name;
636
637 if (fs.exists () && fs.is_dir ())
638 return di.abs_dir_name;
639 }
640 } 629 }
641 } 630 }
642 631
643 return retval; 632 return retval;
644 } 633 }
650 639
651 if (dir.find_first_of (sys::file_ops::dir_sep_chars ()) != std::string::npos 640 if (dir.find_first_of (sys::file_ops::dir_sep_chars ()) != std::string::npos
652 && (sys::env::absolute_pathname (dir) 641 && (sys::env::absolute_pathname (dir)
653 || sys::env::rooted_relative_pathname (dir))) 642 || sys::env::rooted_relative_pathname (dir)))
654 { 643 {
655 sys::file_stat fs (dir); 644 if (sys::dir_exists (dir))
656
657 if (fs.exists () && fs.is_dir ())
658 retlist.push_back (dir); 645 retlist.push_back (dir);
659 } 646 }
660 else 647 else
661 { 648 {
662 std::string canon_dir = maybe_canonicalize (dir); 649 std::string canon_dir = maybe_canonicalize (dir);
675 662
676 std::size_t dir_len = canon_dir.length (); 663 std::size_t dir_len = canon_dir.length ();
677 664
678 if (dname_len > dir_len 665 if (dname_len > dir_len
679 && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1]) 666 && sys::file_ops::is_dir_sep (dname[dname_len - dir_len - 1])
680 && canon_dir == dname.substr (dname_len - dir_len)) 667 && canon_dir == dname.substr (dname_len - dir_len)
681 { 668 && sys::dir_exists (di.dir_name))
682 sys::file_stat fs (di.dir_name); 669 retlist.push_back (di.abs_dir_name);
683
684 if (fs.exists () && fs.is_dir ())
685 retlist.push_back (di.abs_dir_name);
686 }
687 } 670 }
688 } 671 }
689 672
690 return retlist; 673 return retlist;
691 } 674 }
711 != std::string::npos) 694 != std::string::npos)
712 { 695 {
713 if (sys::env::absolute_pathname (file) 696 if (sys::env::absolute_pathname (file)
714 || sys::env::rooted_relative_pathname (file)) 697 || sys::env::rooted_relative_pathname (file))
715 { 698 {
716 sys::file_stat fs (file); 699 if (sys::file_exists (file))
717
718 if (fs.exists ())
719 return file; 700 return file;
720 } 701 }
721 else 702 else
722 { 703 {
723 for (const auto& di : m_dir_info_list) 704 for (const auto& di : m_dir_info_list)
724 { 705 {
725 std::string tfile; 706 std::string tfile;
726 tfile = sys::file_ops::concat (di.abs_dir_name, file); 707 tfile = sys::file_ops::concat (di.abs_dir_name, file);
727 708
728 sys::file_stat fs (tfile); 709 if (sys::file_exists (tfile))
729
730 if (fs.exists ())
731 return tfile; 710 return tfile;
732 } 711 }
733 } 712 }
734 } 713 }
735 else 714 else
788 != std::string::npos) 767 != std::string::npos)
789 { 768 {
790 if (sys::env::absolute_pathname (file) 769 if (sys::env::absolute_pathname (file)
791 || sys::env::rooted_relative_pathname (file)) 770 || sys::env::rooted_relative_pathname (file))
792 { 771 {
793 sys::file_stat fs (file); 772 if (sys::file_exists (file))
794
795 if (fs.exists ())
796 retlist.push_back (file); 773 retlist.push_back (file);
797 } 774 }
798 else 775 else
799 { 776 {
800 for (const auto& di : m_dir_info_list) 777 for (const auto& di : m_dir_info_list)
801 { 778 {
802 std::string tfile; 779 std::string tfile;
803 tfile = sys::file_ops::concat (di.abs_dir_name, file); 780 tfile = sys::file_ops::concat (di.abs_dir_name, file);
804 781
805 sys::file_stat fs (tfile); 782 if (sys::file_exists (tfile))
806
807 if (fs.exists ())
808 retlist.push_back (tfile); 783 retlist.push_back (tfile);
809 } 784 }
810 } 785 }
811 } 786 }
812 else 787 else
987 if (! octave_interpreter_ready) 962 if (! octave_interpreter_ready)
988 return; 963 return;
989 964
990 std::string file = sys::file_ops::concat (dir, script_file); 965 std::string file = sys::file_ops::concat (dir, script_file);
991 966
992 sys::file_stat fs (file); 967 if (sys::file_exists (file))
993
994 if (fs.exists ())
995 source_file (file, "base"); 968 source_file (file, "base");
996 } 969 }
997 970
998 // FIXME: maybe we should also maintain a map to speed up this method of 971 // FIXME: maybe we should also maintain a map to speed up this method of
999 // access. 972 // access.
1101 1074
1102 if (i != m_dir_info_list.end ()) 1075 if (i != m_dir_info_list.end ())
1103 move (i, at_end); 1076 move (i, at_end);
1104 else 1077 else
1105 { 1078 {
1106 sys::file_stat fs (dir); 1079 std::string msg;
1107 1080
1108 if (fs) 1081 if (sys::dir_exists (dir, msg))
1109 { 1082 {
1110 if (fs.is_dir ()) 1083 read_dir_config (dir);
1111 { 1084
1112 read_dir_config (dir); 1085 dir_info di (dir);
1113 1086
1114 dir_info di (dir); 1087 if (at_end)
1115 1088 m_dir_info_list.push_back (di);
1116 if (at_end) 1089 else
1117 m_dir_info_list.push_back (di); 1090 m_dir_info_list.push_front (di);
1118 else 1091
1119 m_dir_info_list.push_front (di); 1092 add (di, at_end);
1120 1093
1121 add (di, at_end); 1094 if (m_add_hook)
1122 1095 m_add_hook (dir);
1123 if (m_add_hook) 1096 }
1124 m_add_hook (dir); 1097
1125 } 1098 if (warn && ! msg.empty ())
1126 else if (warn) 1099 warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ());
1127 warning ("addpath: %s: not a directory", dir_arg.c_str ());
1128 }
1129 else if (warn)
1130 {
1131 std::string msg = fs.error ();
1132 warning ("addpath: %s: %s", dir_arg.c_str (), msg.c_str ());
1133 }
1134 } 1100 }
1135 1101
1136 // FIXME: is there a better way to do this? 1102 // FIXME: is there a better way to do this?
1137 1103
1138 i = find_dir_info ("."); 1104 i = find_dir_info (".");
1321 { 1287 {
1322 std::string pfname = dir_name + sys::file_ops::dir_sep_str () 1288 std::string pfname = dir_name + sys::file_ops::dir_sep_str ()
1323 + "private" + sys::file_ops::dir_sep_str () 1289 + "private" + sys::file_ops::dir_sep_str ()
1324 + fname; 1290 + fname;
1325 1291
1326 sys::file_stat fs (pfname); 1292 if (sys::file_exists (pfname, false))
1327
1328 if (fs.exists () && fs.is_reg ())
1329 retval = pfname; 1293 retval = pfname;
1330 } 1294 }
1331 } 1295 }
1332 1296
1333 return retval; 1297 return retval;
1551 { 1515 {
1552 std::string fname = flist[i]; 1516 std::string fname = flist[i];
1553 1517
1554 std::string full_name = sys::file_ops::concat (d, fname); 1518 std::string full_name = sys::file_ops::concat (d, fname);
1555 1519
1556 sys::file_stat fs (full_name); 1520 if (sys::dir_exists (full_name))
1557 1521 {
1558 if (fs) 1522 if (fname == "private")
1559 { 1523 get_private_file_map (full_name);
1560 if (fs.is_dir ()) 1524 else if (fname[0] == '@')
1561 { 1525 get_method_file_map (full_name, fname.substr (1));
1562 if (fname == "private") 1526 else if (fname[0] == '+')
1563 get_private_file_map (full_name); 1527 get_package_dir (full_name, fname.substr (1));
1564 else if (fname[0] == '@') 1528 }
1565 get_method_file_map (full_name, fname.substr (1)); 1529 else if (sys::file_exists (full_name))
1566 else if (fname[0] == '+') 1530 {
1567 get_package_dir (full_name, fname.substr (1)); 1531 all_files[all_files_count++] = fname;
1568 } 1532
1569 else 1533 std::size_t pos = fname.rfind ('.');
1570 { 1534
1571 all_files[all_files_count++] = fname; 1535 if (pos != std::string::npos)
1572 1536 {
1573 std::size_t pos = fname.rfind ('.'); 1537 std::string ext = fname.substr (pos);
1574 1538
1575 if (pos != std::string::npos) 1539 if (ext == ".m" || ext == ".oct" || ext == ".mex")
1576 { 1540 {
1577 std::string ext = fname.substr (pos); 1541 std::string base = fname.substr (0, pos);
1578 1542
1579 if (ext == ".m" || ext == ".oct" || ext == ".mex") 1543 if (valid_identifier (base))
1580 { 1544 fcn_files[fcn_files_count++] = fname;
1581 std::string base = fname.substr (0, pos);
1582
1583 if (valid_identifier (base))
1584 fcn_files[fcn_files_count++] = fname;
1585 }
1586 } 1545 }
1587 } 1546 }
1588 } 1547 }
1589 } 1548 }
1590 1549
1604 { 1563 {
1605 method_file_map[class_name].method_file_map = get_fcn_files (d); 1564 method_file_map[class_name].method_file_map = get_fcn_files (d);
1606 1565
1607 std::string pd = sys::file_ops::concat (d, "private"); 1566 std::string pd = sys::file_ops::concat (d, "private");
1608 1567
1609 sys::file_stat fs (pd); 1568 if (sys::dir_exists (pd))
1610
1611 if (fs && fs.is_dir ())
1612 method_file_map[class_name].private_file_map = get_fcn_files (pd); 1569 method_file_map[class_name].private_file_map = get_fcn_files (pd);
1613 } 1570 }
1614 1571
1615 void 1572 void
1616 load_path::dir_info::get_package_dir (const std::string& d, 1573 load_path::dir_info::get_package_dir (const std::string& d,
2442 2399
2443 if (! skip_p) 2400 if (! skip_p)
2444 { 2401 {
2445 std::string nm = sys::file_ops::concat (dirname, elt); 2402 std::string nm = sys::file_ops::concat (dirname, elt);
2446 2403
2447 sys::file_stat fs (nm); 2404 if (sys::dir_exists (nm))
2448
2449 if (fs && fs.is_dir ())
2450 retval += (directory_path::path_sep_str () 2405 retval += (directory_path::path_sep_str ()
2451 + genpath (nm, skip)); 2406 + genpath (nm, skip));
2452 } 2407 }
2453 } 2408 }
2454 } 2409 }