Mercurial > octave-nkf
comparison libinterp/dldfcn/urlwrite.cc @ 17548:92541ff4cc3c
improve implementation of ftp object handles
* urlwrite.cc (curl_handles): Delete class.
(handles): Delete static variable.
(ch_manager): New class.
(make_handle_fraction): New function.
(F__ftp_pwd__, F__ftp_cwd__, F__ftp_dir__, F__ftp_ascii__,
F__ftp_binary__, F__ftp_mode__, F__ftp_delete__, F__ftp_rmdir__,
F__ftp_mkdir__, F__ftp_rename__, F__ftp_mput__, F__ftp_mget__):
Find curl object corresponding to given handle using ch_manager
instead of looking in handles map.
(F__ftp__): Return handle instead of accepting it as argument.
Change all uses.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Oct 2013 23:40:01 -0400 |
parents | 53ae37d37b16 |
children | a646665cd574 |
comparison
equal
deleted
inserted
replaced
17547:e7a9be8fdd21 | 17548:92541ff4cc3c |
---|---|
36 | 36 |
37 #include "dir-ops.h" | 37 #include "dir-ops.h" |
38 #include "file-ops.h" | 38 #include "file-ops.h" |
39 #include "file-stat.h" | 39 #include "file-stat.h" |
40 #include "oct-env.h" | 40 #include "oct-env.h" |
41 #include "oct-handle.h" | |
41 #include "glob-match.h" | 42 #include "glob-match.h" |
43 #include "singleton-cleanup.h" | |
42 | 44 |
43 #include "defun-dld.h" | 45 #include "defun-dld.h" |
44 #include "error.h" | 46 #include "error.h" |
45 #include "oct-obj.h" | 47 #include "oct-obj.h" |
46 #include "ov-cell.h" | 48 #include "ov-cell.h" |
597 } | 599 } |
598 | 600 |
599 #undef setopt | 601 #undef setopt |
600 }; | 602 }; |
601 | 603 |
602 class | 604 typedef octave_handle curl_handle; |
603 curl_handles | 605 |
604 { | 606 class OCTINTERP_API ch_manager |
607 { | |
608 protected: | |
609 | |
610 ch_manager (void) | |
611 : handle_map (), handle_free_list (), | |
612 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) { } | |
613 | |
605 public: | 614 public: |
606 | 615 |
607 typedef std::map<std::string, curl_object>::iterator iterator; | 616 static void create_instance (void); |
608 typedef std::map<std::string, curl_object>::const_iterator const_iterator; | 617 |
609 | 618 static bool instance_ok (void) |
610 curl_handles (void) : map () | 619 { |
611 { | 620 bool retval = true; |
612 curl_global_init (CURL_GLOBAL_DEFAULT); | 621 |
613 } | 622 if (! instance) |
614 | 623 create_instance (); |
615 ~curl_handles (void) | 624 |
616 { | 625 if (! instance) |
617 // Remove the elements of the map explicitly as they should | 626 { |
618 // be deleted before the call to curl_global_cleanup | 627 ::error ("unable to create ch_manager!"); |
619 map.erase (begin (), end ()); | 628 |
620 | 629 retval = false; |
621 curl_global_cleanup (); | 630 } |
622 } | 631 |
623 | 632 return retval; |
624 iterator begin (void) { return iterator (map.begin ()); } | 633 } |
625 const_iterator begin (void) const { return const_iterator (map.begin ()); } | 634 |
626 | 635 static void cleanup_instance (void) { delete instance; instance = 0; } |
627 iterator end (void) { return iterator (map.end ()); } | 636 |
628 const_iterator end (void) const { return const_iterator (map.end ()); } | 637 static curl_handle get_handle (void) |
629 | 638 { |
630 iterator seek (const std::string& k) { return map.find (k); } | 639 return instance_ok () |
631 const_iterator seek (const std::string& k) const { return map.find (k); } | 640 ? instance->do_get_handle () : curl_handle (); |
632 | 641 } |
633 std::string key (const_iterator p) const { return p->first; } | 642 |
634 | 643 static void free (const curl_handle& h) |
635 curl_object& contents (const std::string& k) | 644 { |
636 { | 645 if (instance_ok ()) |
637 return map[k]; | 646 instance->do_free (h); |
638 } | 647 } |
639 | 648 |
640 curl_object contents (const std::string& k) const | 649 static curl_handle lookup (double val) |
641 { | 650 { |
642 const_iterator p = seek (k); | 651 return instance_ok () ? instance->do_lookup (val) : curl_handle (); |
643 return p != end () ? p->second : curl_object (); | 652 } |
644 } | 653 |
645 | 654 static curl_handle lookup (const octave_value& val) |
646 curl_object& contents (iterator p) | 655 { |
647 { return p->second; } | 656 return val.is_real_scalar () |
648 | 657 ? lookup (val.double_value ()) : curl_handle (); |
649 curl_object contents (const_iterator p) const | 658 } |
650 { return p->second; } | 659 |
651 | 660 static curl_object get_object (double val) |
652 void del (const std::string& k) | 661 { |
653 { | 662 return get_object (lookup (val)); |
654 iterator p = map.find (k); | 663 } |
655 | 664 |
656 if (p != map.end ()) | 665 static curl_object get_object (const octave_value& val) |
657 map.erase (p); | 666 { |
658 } | 667 return get_object (lookup (val)); |
668 } | |
669 | |
670 static curl_object get_object (const curl_handle& h) | |
671 { | |
672 return instance_ok () ? instance->do_get_object (h) : curl_object (); | |
673 } | |
674 | |
675 static curl_handle make_curl_handle (const std::string& host, | |
676 const std::string& user, | |
677 const std::string& passwd) | |
678 { | |
679 return instance_ok () | |
680 ? instance->do_make_curl_handle (host, user, passwd) : curl_handle (); | |
681 } | |
682 | |
683 static Matrix handle_list (void) | |
684 { | |
685 return instance_ok () ? instance->do_handle_list () : Matrix (); | |
686 } | |
659 | 687 |
660 private: | 688 private: |
661 std::map<std::string, curl_object> map; | 689 |
690 static ch_manager *instance; | |
691 | |
692 typedef std::map<curl_handle, curl_object>::iterator iterator; | |
693 typedef std::map<curl_handle, curl_object>::const_iterator const_iterator; | |
694 | |
695 typedef std::set<curl_handle>::iterator free_list_iterator; | |
696 typedef std::set<curl_handle>::const_iterator const_free_list_iterator; | |
697 | |
698 // A map of handles to curl objects. | |
699 std::map<curl_handle, curl_object> handle_map; | |
700 | |
701 // The available curl handles. | |
702 std::set<curl_handle> handle_free_list; | |
703 | |
704 // The next handle available if handle_free_list is empty. | |
705 double next_handle; | |
706 | |
707 curl_handle do_get_handle (void); | |
708 | |
709 void do_free (const curl_handle& h); | |
710 | |
711 curl_handle do_lookup (double val) | |
712 { | |
713 iterator p = (xisnan (val) ? handle_map.end () : handle_map.find (val)); | |
714 | |
715 return (p != handle_map.end ()) ? p->first : curl_handle (); | |
716 } | |
717 | |
718 curl_object do_get_object (const curl_handle& h) | |
719 { | |
720 iterator p = (h.ok () ? handle_map.find (h) : handle_map.end ()); | |
721 | |
722 return (p != handle_map.end ()) ? p->second : curl_object (); | |
723 } | |
724 | |
725 curl_handle do_make_curl_handle (const std::string& host, | |
726 const std::string& user, | |
727 const std::string& passwd) | |
728 { | |
729 curl_handle h = get_handle (); | |
730 | |
731 curl_object obj (host, user, passwd); | |
732 | |
733 if (! error_state) | |
734 handle_map[h] = obj; | |
735 else | |
736 { | |
737 do_free (h); | |
738 | |
739 h = curl_handle (); | |
740 } | |
741 | |
742 return h; | |
743 } | |
744 | |
745 Matrix do_handle_list (void) | |
746 { | |
747 Matrix retval (1, handle_map.size ()); | |
748 | |
749 octave_idx_type i = 0; | |
750 for (const_iterator p = handle_map.begin (); p != handle_map.end (); p++) | |
751 { | |
752 curl_handle h = p->first; | |
753 | |
754 retval(i++) = h.value (); | |
755 } | |
756 | |
757 return retval; | |
758 } | |
662 }; | 759 }; |
663 | 760 |
664 static curl_handles handles; | 761 void |
762 ch_manager::create_instance (void) | |
763 { | |
764 instance = new ch_manager (); | |
765 | |
766 if (instance) | |
767 singleton_cleanup_list::add (cleanup_instance); | |
768 } | |
769 | |
770 static double | |
771 make_handle_fraction (void) | |
772 { | |
773 static double maxrand = RAND_MAX + 2.0; | |
774 | |
775 return (rand () + 1.0) / maxrand; | |
776 } | |
777 | |
778 curl_handle | |
779 ch_manager::do_get_handle (void) | |
780 { | |
781 curl_handle retval; | |
782 | |
783 // Curl handles are negative integers plus some random fractional | |
784 // part. To avoid running out of integers, we recycle the integer | |
785 // part but tack on a new random part each time. | |
786 | |
787 free_list_iterator p = handle_free_list.begin (); | |
788 | |
789 if (p != handle_free_list.end ()) | |
790 { | |
791 retval = *p; | |
792 handle_free_list.erase (p); | |
793 } | |
794 else | |
795 { | |
796 retval = curl_handle (next_handle); | |
797 | |
798 next_handle = std::ceil (next_handle) - 1.0 - make_handle_fraction (); | |
799 } | |
800 | |
801 return retval; | |
802 } | |
803 | |
804 void | |
805 ch_manager::do_free (const curl_handle& h) | |
806 { | |
807 if (h.ok ()) | |
808 { | |
809 iterator p = handle_map.find (h); | |
810 | |
811 if (p != handle_map.end ()) | |
812 { | |
813 // Curl handles are negative integers plus some random | |
814 // fractional part. To avoid running out of integers, we | |
815 // recycle the integer part but tack on a new random part | |
816 // each time. | |
817 | |
818 handle_map.erase (p); | |
819 | |
820 if (h.value () < 0) | |
821 handle_free_list.insert (std::ceil (h.value ()) - make_handle_fraction ()); | |
822 } | |
823 else | |
824 error ("ch_manager::free: invalid object %g", h.value ()); | |
825 } | |
826 } | |
827 | |
828 ch_manager *ch_manager::instance = 0; | |
665 | 829 |
666 static void | 830 static void |
667 cleanup_urlwrite (std::string filename) | 831 cleanup_urlwrite (std::string filename) |
668 { | 832 { |
669 octave_unlink (filename); | 833 octave_unlink (filename); |
967 return retval; | 1131 return retval; |
968 } | 1132 } |
969 | 1133 |
970 DEFUN_DLD (__ftp__, args, , | 1134 DEFUN_DLD (__ftp__, args, , |
971 "-*- texinfo -*-\n\ | 1135 "-*- texinfo -*-\n\ |
972 @deftypefn {Loadable Function} {} __ftp__ (@var{handle}, @var{host})\n\ | 1136 @deftypefn {Loadable Function} {@var{handle} =} __ftp__ (@var{host})\n\ |
973 @deftypefnx {Loadable Function} {} __ftp__ (@var{handle}, @var{host}, @var{username}, @var{password})\n\ | 1137 @deftypefnx {Loadable Function} {@var{handle} =} __ftp__ (@var{host}, @var{username}, @var{password})\n\ |
974 Undocumented internal function\n\ | 1138 Undocumented internal function\n\ |
975 @end deftypefn") | 1139 @end deftypefn") |
976 { | 1140 { |
1141 octave_value retval; | |
1142 | |
977 #ifdef HAVE_CURL | 1143 #ifdef HAVE_CURL |
978 int nargin = args.length (); | 1144 int nargin = args.length (); |
979 std::string handle; | |
980 std::string host; | 1145 std::string host; |
981 std::string user = "anonymous"; | 1146 std::string user = "anonymous"; |
982 std::string passwd = ""; | 1147 std::string passwd = ""; |
983 | 1148 |
984 if (nargin < 2 || nargin > 4) | 1149 if (nargin < 1 || nargin > 3) |
985 error ("incorrect number of arguments"); | 1150 { |
1151 print_usage (); | |
1152 return retval; | |
1153 } | |
986 else | 1154 else |
987 { | 1155 { |
988 handle = args(0).string_value (); | 1156 host = args(0).string_value (); |
989 host = args(1).string_value (); | |
990 | 1157 |
991 if (nargin > 1) | 1158 if (nargin > 1) |
992 user = args(2).string_value (); | 1159 user = args(1).string_value (); |
993 | 1160 |
994 if (nargin > 2) | 1161 if (nargin > 2) |
995 passwd = args(3).string_value (); | 1162 passwd = args(2).string_value (); |
996 | 1163 |
997 if (!error_state) | 1164 if (! error_state) |
998 { | 1165 { |
999 handles.contents (handle) = curl_object (host, user, passwd); | 1166 curl_handle ch = ch_manager::make_curl_handle (host, user, passwd); |
1000 | 1167 |
1001 if (error_state) | 1168 if (! error_state) |
1002 handles.del (handle); | 1169 retval = ch.value (); |
1003 } | 1170 } |
1004 } | 1171 } |
1005 #else | 1172 #else |
1006 gripe_disabled_feature ("__ftp__", "FTP"); | 1173 gripe_disabled_feature ("__ftp__", "FTP"); |
1007 #endif | 1174 #endif |
1008 | 1175 |
1009 return octave_value (); | 1176 return retval; |
1010 } | 1177 } |
1011 | 1178 |
1012 DEFUN_DLD (__ftp_pwd__, args, , | 1179 DEFUN_DLD (__ftp_pwd__, args, , |
1013 "-*- texinfo -*-\n\ | 1180 "-*- texinfo -*-\n\ |
1014 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\ | 1181 @deftypefn {Loadable Function} {} __ftp_pwd__ (@var{handle})\n\ |
1021 | 1188 |
1022 if (nargin != 1) | 1189 if (nargin != 1) |
1023 error ("__ftp_pwd__: incorrect number of arguments"); | 1190 error ("__ftp_pwd__: incorrect number of arguments"); |
1024 else | 1191 else |
1025 { | 1192 { |
1026 std::string handle = args(0).string_value (); | 1193 const curl_object curl = ch_manager::get_object (args(0)); |
1027 | 1194 |
1028 if (!error_state) | 1195 if (curl.is_valid ()) |
1029 { | 1196 retval = curl.pwd (); |
1030 const curl_object curl = handles.contents (handle); | 1197 else |
1031 | 1198 error ("__ftp_pwd__: invalid ftp handle"); |
1032 if (curl.is_valid ()) | |
1033 retval = curl.pwd (); | |
1034 else | |
1035 error ("__ftp_pwd__: invalid ftp handle"); | |
1036 } | |
1037 } | 1199 } |
1038 #else | 1200 #else |
1039 gripe_disabled_feature ("__ftp_pwd__", "FTP"); | 1201 gripe_disabled_feature ("__ftp_pwd__", "FTP"); |
1040 #endif | 1202 #endif |
1041 | 1203 |
1053 | 1215 |
1054 if (nargin != 1 && nargin != 2) | 1216 if (nargin != 1 && nargin != 2) |
1055 error ("__ftp_cwd__: incorrect number of arguments"); | 1217 error ("__ftp_cwd__: incorrect number of arguments"); |
1056 else | 1218 else |
1057 { | 1219 { |
1058 std::string handle = args(0).string_value (); | 1220 const curl_object curl = ch_manager::get_object (args(0)); |
1059 std::string path = ""; | 1221 |
1060 | 1222 if (curl.is_valid ()) |
1061 if (nargin > 1) | 1223 { |
1062 path = args(1).string_value (); | 1224 std::string path = ""; |
1063 | 1225 |
1064 if (!error_state) | 1226 if (nargin > 1) |
1065 { | 1227 path = args(1).string_value (); |
1066 const curl_object curl = handles.contents (handle); | 1228 |
1067 | 1229 if (! error_state) |
1068 if (curl.is_valid ()) | |
1069 curl.cwd (path); | 1230 curl.cwd (path); |
1070 else | 1231 else |
1071 error ("__ftp_cwd__: invalid ftp handle"); | 1232 error ("__ftp_cwd__: expecting path as second argument"); |
1072 } | 1233 } |
1234 else | |
1235 error ("__ftp_cwd__: invalid ftp handle"); | |
1073 } | 1236 } |
1074 #else | 1237 #else |
1075 gripe_disabled_feature ("__ftp_cwd__", "FTP"); | 1238 gripe_disabled_feature ("__ftp_cwd__", "FTP"); |
1076 #endif | 1239 #endif |
1077 | 1240 |
1090 | 1253 |
1091 if (nargin != 1) | 1254 if (nargin != 1) |
1092 error ("__ftp_dir__: incorrect number of arguments"); | 1255 error ("__ftp_dir__: incorrect number of arguments"); |
1093 else | 1256 else |
1094 { | 1257 { |
1095 std::string handle = args(0).string_value (); | 1258 const curl_object curl = ch_manager::get_object (args(0)); |
1096 | 1259 |
1097 if (!error_state) | 1260 if (curl.is_valid ()) |
1098 { | 1261 { |
1099 const curl_object curl = handles.contents (handle); | 1262 if (nargout == 0) |
1100 | 1263 curl.dir (); |
1101 if (curl.is_valid ()) | 1264 else |
1102 { | 1265 { |
1103 if (nargout == 0) | 1266 string_vector sv = curl.list (); |
1104 curl.dir (); | 1267 octave_idx_type n = sv.length (); |
1268 | |
1269 if (n == 0) | |
1270 { | |
1271 string_vector flds (5); | |
1272 | |
1273 flds(0) = "name"; | |
1274 flds(1) = "date"; | |
1275 flds(2) = "bytes"; | |
1276 flds(3) = "isdir"; | |
1277 flds(4) = "datenum"; | |
1278 | |
1279 retval = octave_map (flds); | |
1280 } | |
1105 else | 1281 else |
1106 { | 1282 { |
1107 string_vector sv = curl.list (); | 1283 octave_map st; |
1108 octave_idx_type n = sv.length (); | 1284 |
1109 if (n == 0) | 1285 Cell filectime (dim_vector (n, 1)); |
1286 Cell filesize (dim_vector (n, 1)); | |
1287 Cell fileisdir (dim_vector (n, 1)); | |
1288 Cell filedatenum (dim_vector (n, 1)); | |
1289 | |
1290 st.assign ("name", Cell (sv)); | |
1291 | |
1292 for (octave_idx_type i = 0; i < n; i++) | |
1110 { | 1293 { |
1111 string_vector flds (5); | 1294 time_t ftime; |
1112 flds(0) = "name"; | 1295 bool fisdir; |
1113 flds(1) = "date"; | 1296 double fsize; |
1114 flds(2) = "bytes"; | 1297 |
1115 flds(3) = "isdir"; | 1298 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); |
1116 flds(4) = "datenum"; | 1299 |
1117 retval = octave_map (flds); | 1300 fileisdir (i) = fisdir; |
1301 filectime (i) = ctime (&ftime); | |
1302 filesize (i) = fsize; | |
1303 filedatenum (i) = double (ftime); | |
1118 } | 1304 } |
1119 else | 1305 |
1120 { | 1306 st.assign ("date", filectime); |
1121 octave_map st; | 1307 st.assign ("bytes", filesize); |
1122 Cell filectime (dim_vector (n, 1)); | 1308 st.assign ("isdir", fileisdir); |
1123 Cell filesize (dim_vector (n, 1)); | 1309 st.assign ("datenum", filedatenum); |
1124 Cell fileisdir (dim_vector (n, 1)); | 1310 |
1125 Cell filedatenum (dim_vector (n, 1)); | 1311 retval = st; |
1126 | |
1127 st.assign ("name", Cell (sv)); | |
1128 | |
1129 for (octave_idx_type i = 0; i < n; i++) | |
1130 { | |
1131 time_t ftime; | |
1132 bool fisdir; | |
1133 double fsize; | |
1134 | |
1135 curl.get_fileinfo (sv(i), fsize, ftime, fisdir); | |
1136 | |
1137 fileisdir (i) = fisdir; | |
1138 filectime (i) = ctime (&ftime); | |
1139 filesize (i) = fsize; | |
1140 filedatenum (i) = double (ftime); | |
1141 } | |
1142 st.assign ("date", filectime); | |
1143 st.assign ("bytes", filesize); | |
1144 st.assign ("isdir", fileisdir); | |
1145 st.assign ("datenum", filedatenum); | |
1146 retval = st; | |
1147 } | |
1148 } | 1312 } |
1149 } | 1313 } |
1150 else | 1314 } |
1151 error ("__ftp_dir__: invalid ftp handle"); | 1315 else |
1152 } | 1316 error ("__ftp_dir__: invalid ftp handle"); |
1153 } | 1317 } |
1154 #else | 1318 #else |
1155 gripe_disabled_feature ("__ftp_dir__", "FTP"); | 1319 gripe_disabled_feature ("__ftp_dir__", "FTP"); |
1156 #endif | 1320 #endif |
1157 | 1321 |
1169 | 1333 |
1170 if (nargin != 1) | 1334 if (nargin != 1) |
1171 error ("__ftp_ascii__: incorrect number of arguments"); | 1335 error ("__ftp_ascii__: incorrect number of arguments"); |
1172 else | 1336 else |
1173 { | 1337 { |
1174 std::string handle = args(0).string_value (); | 1338 const curl_object curl = ch_manager::get_object (args(0)); |
1175 | 1339 |
1176 if (!error_state) | 1340 if (curl.is_valid ()) |
1177 { | 1341 curl.ascii (); |
1178 const curl_object curl = handles.contents (handle); | 1342 else |
1179 | 1343 error ("__ftp_ascii__: invalid ftp handle"); |
1180 if (curl.is_valid ()) | |
1181 curl.ascii (); | |
1182 else | |
1183 error ("__ftp_ascii__: invalid ftp handle"); | |
1184 } | |
1185 } | 1344 } |
1186 #else | 1345 #else |
1187 gripe_disabled_feature ("__ftp_ascii__", "FTP"); | 1346 gripe_disabled_feature ("__ftp_ascii__", "FTP"); |
1188 #endif | 1347 #endif |
1189 | 1348 |
1201 | 1360 |
1202 if (nargin != 1) | 1361 if (nargin != 1) |
1203 error ("__ftp_binary__: incorrect number of arguments"); | 1362 error ("__ftp_binary__: incorrect number of arguments"); |
1204 else | 1363 else |
1205 { | 1364 { |
1206 std::string handle = args(0).string_value (); | 1365 const curl_object curl = ch_manager::get_object (args(0)); |
1207 | 1366 |
1208 if (!error_state) | 1367 if (curl.is_valid ()) |
1209 { | 1368 curl.binary (); |
1210 const curl_object curl = handles.contents (handle); | 1369 else |
1211 | 1370 error ("__ftp_binary__: invalid ftp handle"); |
1212 if (curl.is_valid ()) | |
1213 curl.binary (); | |
1214 else | |
1215 error ("__ftp_binary__: invalid ftp handle"); | |
1216 } | |
1217 } | 1371 } |
1218 #else | 1372 #else |
1219 gripe_disabled_feature ("__ftp_binary__", "FTP"); | 1373 gripe_disabled_feature ("__ftp_binary__", "FTP"); |
1220 #endif | 1374 #endif |
1221 | 1375 |
1233 | 1387 |
1234 if (nargin != 1) | 1388 if (nargin != 1) |
1235 error ("__ftp_close__: incorrect number of arguments"); | 1389 error ("__ftp_close__: incorrect number of arguments"); |
1236 else | 1390 else |
1237 { | 1391 { |
1238 std::string handle = args(0).string_value (); | 1392 curl_handle h = ch_manager::lookup (args(0)); |
1239 | 1393 |
1240 if (! error_state) | 1394 if (h.ok ()) |
1241 handles.del (handle); | 1395 ch_manager::free (h); |
1396 else | |
1397 error ("__ftp_close__: invalid ftp handle"); | |
1242 } | 1398 } |
1243 #else | 1399 #else |
1244 gripe_disabled_feature ("__ftp_close__", "FTP"); | 1400 gripe_disabled_feature ("__ftp_close__", "FTP"); |
1245 #endif | 1401 #endif |
1246 | 1402 |
1259 | 1415 |
1260 if (nargin != 1) | 1416 if (nargin != 1) |
1261 error ("__ftp_mode__: incorrect number of arguments"); | 1417 error ("__ftp_mode__: incorrect number of arguments"); |
1262 else | 1418 else |
1263 { | 1419 { |
1264 std::string handle = args(0).string_value (); | 1420 const curl_object curl = ch_manager::get_object (args(0)); |
1265 | 1421 |
1266 | 1422 if (curl.is_valid ()) |
1267 if (! error_state) | 1423 retval = (curl.is_ascii () ? "ascii" : "binary"); |
1268 { | 1424 else |
1269 const curl_object curl = handles.contents (handle); | 1425 error ("__ftp_binary__: invalid ftp handle"); |
1270 | |
1271 if (curl.is_valid ()) | |
1272 retval = (curl.is_ascii () ? "ascii" : "binary"); | |
1273 else | |
1274 error ("__ftp_binary__: invalid ftp handle"); | |
1275 } | |
1276 } | 1426 } |
1277 #else | 1427 #else |
1278 gripe_disabled_feature ("__ftp_mode__", "FTP"); | 1428 gripe_disabled_feature ("__ftp_mode__", "FTP"); |
1279 #endif | 1429 #endif |
1280 | 1430 |
1292 | 1442 |
1293 if (nargin != 2) | 1443 if (nargin != 2) |
1294 error ("__ftp_delete__: incorrect number of arguments"); | 1444 error ("__ftp_delete__: incorrect number of arguments"); |
1295 else | 1445 else |
1296 { | 1446 { |
1297 std::string handle = args(0).string_value (); | 1447 const curl_object curl = ch_manager::get_object (args(0)); |
1298 std::string file = args(1).string_value (); | 1448 |
1299 | 1449 if (curl.is_valid ()) |
1300 if (!error_state) | 1450 { |
1301 { | 1451 std::string file = args(1).string_value (); |
1302 const curl_object curl = handles.contents (handle); | 1452 |
1303 | 1453 if (! error_state) |
1304 if (curl.is_valid ()) | |
1305 curl.del (file); | 1454 curl.del (file); |
1306 else | 1455 else |
1307 error ("__ftp_delete__: invalid ftp handle"); | 1456 error ("__ftp_delete__: expecting file name as second argument"); |
1308 } | 1457 } |
1458 else | |
1459 error ("__ftp_delete__: invalid ftp handle"); | |
1309 } | 1460 } |
1310 #else | 1461 #else |
1311 gripe_disabled_feature ("__ftp_delete__", "FTP"); | 1462 gripe_disabled_feature ("__ftp_delete__", "FTP"); |
1312 #endif | 1463 #endif |
1313 | 1464 |
1325 | 1476 |
1326 if (nargin != 2) | 1477 if (nargin != 2) |
1327 error ("__ftp_rmdir__: incorrect number of arguments"); | 1478 error ("__ftp_rmdir__: incorrect number of arguments"); |
1328 else | 1479 else |
1329 { | 1480 { |
1330 std::string handle = args(0).string_value (); | 1481 const curl_object curl = ch_manager::get_object (args(0)); |
1331 std::string dir = args(1).string_value (); | 1482 |
1332 | 1483 if (curl.is_valid ()) |
1333 if (!error_state) | 1484 { |
1334 { | 1485 std::string dir = args(1).string_value (); |
1335 const curl_object curl = handles.contents (handle); | 1486 |
1336 | 1487 if (! error_state) |
1337 if (curl.is_valid ()) | |
1338 curl.rmdir (dir); | 1488 curl.rmdir (dir); |
1339 else | 1489 else |
1340 error ("__ftp_rmdir__: invalid ftp handle"); | 1490 error ("__ftp_rmdir__: expecting directory name as second argument"); |
1341 } | 1491 } |
1492 else | |
1493 error ("__ftp_rmdir__: invalid ftp handle"); | |
1342 } | 1494 } |
1343 #else | 1495 #else |
1344 gripe_disabled_feature ("__ftp_rmdir__", "FTP"); | 1496 gripe_disabled_feature ("__ftp_rmdir__", "FTP"); |
1345 #endif | 1497 #endif |
1346 | 1498 |
1358 | 1510 |
1359 if (nargin != 2) | 1511 if (nargin != 2) |
1360 error ("__ftp_mkdir__: incorrect number of arguments"); | 1512 error ("__ftp_mkdir__: incorrect number of arguments"); |
1361 else | 1513 else |
1362 { | 1514 { |
1363 std::string handle = args(0).string_value (); | 1515 const curl_object curl = ch_manager::get_object (args(0)); |
1364 std::string dir = args(1).string_value (); | 1516 |
1365 | 1517 if (curl.is_valid ()) |
1366 if (!error_state) | 1518 { |
1367 { | 1519 std::string dir = args(1).string_value (); |
1368 const curl_object curl = handles.contents (handle); | 1520 |
1369 | 1521 if (! error_state) |
1370 if (curl.is_valid ()) | |
1371 curl.mkdir (dir); | 1522 curl.mkdir (dir); |
1372 else | 1523 else |
1373 error ("__ftp_mkdir__: invalid ftp handle"); | 1524 error ("__ftp_mkdir__: expecting directory name as second argument"); |
1374 } | 1525 } |
1526 else | |
1527 error ("__ftp_mkdir__: invalid ftp handle"); | |
1375 } | 1528 } |
1376 #else | 1529 #else |
1377 gripe_disabled_feature ("__ftp_mkdir__", "FTP"); | 1530 gripe_disabled_feature ("__ftp_mkdir__", "FTP"); |
1378 #endif | 1531 #endif |
1379 | 1532 |
1391 | 1544 |
1392 if (nargin != 3) | 1545 if (nargin != 3) |
1393 error ("__ftp_rename__: incorrect number of arguments"); | 1546 error ("__ftp_rename__: incorrect number of arguments"); |
1394 else | 1547 else |
1395 { | 1548 { |
1396 std::string handle = args(0).string_value (); | 1549 const curl_object curl = ch_manager::get_object (args(0)); |
1397 std::string oldname = args(1).string_value (); | 1550 |
1398 std::string newname = args(2).string_value (); | 1551 if (curl.is_valid ()) |
1399 | 1552 { |
1400 if (!error_state) | 1553 std::string oldname = args(1).string_value (); |
1401 { | 1554 std::string newname = args(2).string_value (); |
1402 const curl_object curl = handles.contents (handle); | 1555 |
1403 | 1556 if (! error_state) |
1404 if (curl.is_valid ()) | |
1405 curl.rename (oldname, newname); | 1557 curl.rename (oldname, newname); |
1406 else | 1558 else |
1407 error ("__ftp_rename__: invalid ftp handle"); | 1559 error ("__ftp_rename__: expecting file names for second and third arguments"); |
1408 } | 1560 } |
1561 else | |
1562 error ("__ftp_rename__: invalid ftp handle"); | |
1409 } | 1563 } |
1410 #else | 1564 #else |
1411 gripe_disabled_feature ("__ftp_rename__", "FTP"); | 1565 gripe_disabled_feature ("__ftp_rename__", "FTP"); |
1412 #endif | 1566 #endif |
1413 | 1567 |
1513 | 1667 |
1514 if (nargin != 2) | 1668 if (nargin != 2) |
1515 error ("__ftp_mput__: incorrect number of arguments"); | 1669 error ("__ftp_mput__: incorrect number of arguments"); |
1516 else | 1670 else |
1517 { | 1671 { |
1518 std::string handle = args(0).string_value (); | 1672 const curl_object curl = ch_manager::get_object (args(0)); |
1519 std::string pat = args(1).string_value (); | 1673 |
1520 | 1674 if (curl.is_valid ()) |
1521 if (!error_state) | 1675 { |
1522 { | 1676 std::string pat = args(1).string_value (); |
1523 const curl_object curl = handles.contents (handle); | 1677 |
1524 | 1678 if (! error_state) |
1525 if (curl.is_valid ()) | |
1526 { | 1679 { |
1527 glob_match pattern (file_ops::tilde_expand (pat)); | 1680 glob_match pattern (file_ops::tilde_expand (pat)); |
1528 string_vector files = pattern.glob (); | 1681 string_vector files = pattern.glob (); |
1529 | 1682 |
1530 for (octave_idx_type i = 0; i < files.length (); i++) | 1683 for (octave_idx_type i = 0; i < files.length (); i++) |
1567 retval.append (file); | 1720 retval.append (file); |
1568 } | 1721 } |
1569 } | 1722 } |
1570 } | 1723 } |
1571 else | 1724 else |
1572 error ("__ftp_mput__: invalid ftp handle"); | 1725 error ("__ftp_mput__: expecting file name patter as second argument"); |
1573 } | 1726 } |
1727 else | |
1728 error ("__ftp_mput__: invalid ftp handle"); | |
1574 } | 1729 } |
1575 #else | 1730 #else |
1576 gripe_disabled_feature ("__ftp_mput__", "FTP"); | 1731 gripe_disabled_feature ("__ftp_mput__", "FTP"); |
1577 #endif | 1732 #endif |
1578 | 1733 |
1665 | 1820 |
1666 if (nargin != 2 && nargin != 3) | 1821 if (nargin != 2 && nargin != 3) |
1667 error ("__ftp_mget__: incorrect number of arguments"); | 1822 error ("__ftp_mget__: incorrect number of arguments"); |
1668 else | 1823 else |
1669 { | 1824 { |
1670 std::string handle = args(0).string_value (); | 1825 const curl_object curl = ch_manager::get_object (args(0)); |
1671 std::string file = args(1).string_value (); | 1826 |
1672 std::string target; | 1827 if (curl.is_valid ()) |
1673 | 1828 { |
1674 if (nargin == 3) | 1829 std::string file = args(1).string_value (); |
1675 target = args(2).string_value () + file_ops::dir_sep_str (); | 1830 std::string target; |
1676 | 1831 |
1677 if (! error_state) | 1832 if (nargin == 3) |
1678 { | 1833 target = args(2).string_value () + file_ops::dir_sep_str (); |
1679 const curl_object curl = handles.contents (handle); | 1834 |
1680 | 1835 if (! error_state) |
1681 if (curl.is_valid ()) | |
1682 { | 1836 { |
1683 string_vector sv = curl.list (); | 1837 string_vector sv = curl.list (); |
1684 octave_idx_type n = 0; | 1838 octave_idx_type n = 0; |
1685 glob_match pattern (file); | 1839 glob_match pattern (file); |
1686 | 1840 |
1729 } | 1883 } |
1730 } | 1884 } |
1731 if (n == 0) | 1885 if (n == 0) |
1732 error ("__ftp_mget__: file not found"); | 1886 error ("__ftp_mget__: file not found"); |
1733 } | 1887 } |
1734 } | 1888 else |
1889 error ("__ftp_mget__: expecting file name and target as second and third arguments"); | |
1890 } | |
1891 else | |
1892 error ("__ftp_mget__: invalid ftp handle"); | |
1735 } | 1893 } |
1736 #else | 1894 #else |
1737 gripe_disabled_feature ("__ftp_mget__", "FTP"); | 1895 gripe_disabled_feature ("__ftp_mget__", "FTP"); |
1738 #endif | 1896 #endif |
1739 | 1897 |