Mercurial > octave
comparison scripts/miscellaneous/copyfile.m @ 28103:1cb3c33f97dc
copyfile.m: throw error if operation fails and nargout == 0 (bug #57830).
* copyfile.m: New internal variable "sts" to hold status of function.
When internal checks fail, check nargout == 0, and throw an error. Otherwise,
set output variables "status", "msg", "msgid" and immediately return from
function. Att end of function, based on nargout variable, either throw an
error if operation failed (sts = 0), or set output "status" to value of "sts"
and return information in outputs.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 18 Feb 2020 17:22:40 -0800 |
parents | bd51beb6205e |
children | 28de41192f3c |
comparison
equal
deleted
inserted
replaced
28102:4d021e0dcfee | 28103:1cb3c33f97dc |
---|---|
51 if (nargin < 2 || nargin > 3) | 51 if (nargin < 2 || nargin > 3) |
52 print_usage (); | 52 print_usage (); |
53 endif | 53 endif |
54 | 54 |
55 max_cmd_line = 1024; | 55 max_cmd_line = 1024; |
56 status = true; | 56 sts = 1; |
57 msg = ""; | 57 msg = ""; |
58 msgid = ""; | 58 msgid = ""; |
59 | 59 |
60 ## FIXME: Maybe use the same method as in ls to allow users control | 60 ## FIXME: Maybe use the same method as in ls to allow users control |
61 ## over the command that is executed. | 61 ## over the command that is executed. |
85 endif | 85 endif |
86 | 86 |
87 ## If f1 has more than 1 element then f2 must be a directory | 87 ## If f1 has more than 1 element then f2 must be a directory |
88 isdir = isfolder (f2); | 88 isdir = isfolder (f2); |
89 if (numel (f1) > 1 && ! isdir) | 89 if (numel (f1) > 1 && ! isdir) |
90 error ("copyfile: when copying multiple files, F2 must be a directory"); | 90 if (nargout == 0) |
91 error ("copyfile: when copying multiple files, F2 must be a directory"); | |
92 else | |
93 status = 0; | |
94 msg = "when copying multiple files, F2 must be a directory"; | |
95 msgid = "copyfile"; | |
96 return; | |
97 endif | |
91 endif | 98 endif |
92 | 99 |
93 ## Protect the filename(s). | 100 ## Protect the filename(s). |
94 f1 = glob (f1); | 101 f1 = glob (f1); |
95 if (isempty (f1)) | 102 if (isempty (f1)) |
96 error ("copyfile: no files to move"); | 103 if (nargout == 0) |
104 error ("copyfile: no files to move"); | |
105 else | |
106 status = 0; | |
107 msg = "no files to move"; | |
108 msgid = "copyfile"; | |
109 return; | |
110 endif | |
97 endif | 111 endif |
98 p1 = sprintf ('"%s" ', f1{:}); | 112 p1 = sprintf ('"%s" ', f1{:}); |
99 p2 = tilde_expand (f2); | 113 p2 = tilde_expand (f2); |
100 | 114 |
101 if (isdir && length (p1) > max_cmd_line) | 115 if (isdir && length (p1) > max_cmd_line) |
116 endif | 130 endif |
117 | 131 |
118 ## Copy the files. | 132 ## Copy the files. |
119 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); | 133 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); |
120 if (err != 0) | 134 if (err != 0) |
121 status = false; | 135 sts = 0; |
122 msgid = "copyfile"; | 136 msgid = "copyfile"; |
123 break; | 137 break; |
124 endif | 138 endif |
125 endwhile | 139 endwhile |
126 else | 140 else |
131 endif | 145 endif |
132 | 146 |
133 ## Copy the files. | 147 ## Copy the files. |
134 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); | 148 [err, msg] = system (sprintf ('%s %s"%s"', cmd, p1, p2)); |
135 if (err != 0) | 149 if (err != 0) |
136 status = false; | 150 sts = 0; |
137 msgid = "copyfile"; | 151 msgid = "copyfile"; |
138 endif | 152 endif |
153 endif | |
154 | |
155 if (nargout == 0) | |
156 if (sts == 0) | |
157 error ("copyfile: operation failed: %s", msg); | |
158 endif | |
159 else | |
160 status = sts; | |
139 endif | 161 endif |
140 | 162 |
141 endfunction | 163 endfunction |
142 | 164 |
143 | 165 |
170 %!error copyfile (1) | 192 %!error copyfile (1) |
171 %!error copyfile (1,2,3,4) | 193 %!error copyfile (1,2,3,4) |
172 %!error <F1 must be a string> copyfile (1, "foobar") | 194 %!error <F1 must be a string> copyfile (1, "foobar") |
173 %!error <F2 must be a string> copyfile ("foobar", 1) | 195 %!error <F2 must be a string> copyfile ("foobar", 1) |
174 %!error <F2 must be a directory> copyfile ({"a", "b"}, "%_NOT_A_DIR_%") | 196 %!error <F2 must be a directory> copyfile ({"a", "b"}, "%_NOT_A_DIR_%") |
197 %!error <no files to move> copyfile ("%_NOT_A_FILENAME1_%", "%_NOT_A_FILENAME2_%") |