Mercurial > octave
annotate scripts/miscellaneous/dir.m @ 30955:cd741166d6fb
maint: merge stable to default.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 21 Apr 2022 12:04:09 -0700 |
parents | 230724ab2977 0acf9363da34 |
children | b6aeea949531 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29590
diff
changeset
|
3 ## Copyright (C) 2004-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
4694 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
4694 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
4694 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
4694 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
4694 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
27 ## @deftypefn {} {} dir |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
28 ## @deftypefnx {} {} dir @var{directory} |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
29 ## @deftypefnx {} {[@var{list}] =} dir (@var{directory}) |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
30 ## Display file listing for directory @var{directory}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
31 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
32 ## If @var{directory} is not specified then list the present working directory. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
33 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
34 ## If a return value is requested, return a structure array with the fields |
4694 | 35 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
36 ## @table @asis |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
37 ## @item name |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17908
diff
changeset
|
38 ## File or directory name. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
39 ## |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
40 ## @item folder |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
41 ## Location of file or directory |
23396
945b53af4655
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
23268
diff
changeset
|
42 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
43 ## @item date |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
44 ## Timestamp of file modification (string value). |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
45 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 ## @item bytes |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
47 ## File size in bytes. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
48 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
49 ## @item isdir |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17908
diff
changeset
|
50 ## True if name is a directory. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
51 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
52 ## @item datenum |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
53 ## Timestamp of file modification as serial date number (double). |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
54 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
55 ## @item statinfo |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
56 ## Information structure returned from @code{stat}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
57 ## @end table |
4694 | 58 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
59 ## If @var{directory} is a filename, rather than a directory, then return |
17908
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
60 ## information about the named file. @var{directory} may also be a list rather |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
61 ## than a single directory or file. |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
62 ## |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 ## @var{directory} is subject to shell expansion if it contains any wildcard |
30952
c09fdabaa5b6
dir.m: Clarify wildcard behavior on Windows in docstring (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
64 ## characters @samp{*}, @samp{?}, @samp{[]}. If these wildcard characters are |
c09fdabaa5b6
dir.m: Clarify wildcard behavior on Windows in docstring (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
65 ## escaped with a backslash @samp{\} (e.g., @samp{\*}) on a POSIX platform, |
30954
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
66 ## then they are not treated as wildcards, but as the corresponding literal |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
67 ## character. On Windows, it is not possible to escape wildcard characters |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
68 ## because backslash @samp{\} is treated as a file separator. On Windows, use |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
69 ## @code{ls} for file or folder names that contain characters that would be |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
70 ## treated as wildcards by @code{dir}. |
6955 | 71 ## |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
72 ## Note that for symbolic links, @code{dir} returns information about the |
30954
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
73 ## file that the symbolic link points to rather than the link itself. However, |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
74 ## if the link points to a nonexistent file, @code{dir} returns information |
0acf9363da34
doc: tweak docstrings for ls, dir (bug #62282).
Rik <rik@octave.org>
parents:
30952
diff
changeset
|
75 ## about the link. |
19198 | 76 ## @seealso{ls, readdir, glob, what, stat, lstat} |
4694 | 77 ## @end deftypefn |
78 | |
19198 | 79 ## FIXME: This is quite slow for large directories. |
80 ## Perhaps it should be converted to C++? | |
4694 | 81 |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
82 function list = dir (directory = ".") |
4694 | 83 |
19198 | 84 if (! ischar (directory)) |
85 error ("dir: DIRECTORY argument must be a string"); | |
86 endif | |
87 | |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
88 ## Prep the list. |
6947 | 89 info = struct (zeros (0, 1), |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
90 {"name", "folder" "date", "bytes", "isdir", "datenum", "statinfo"}); |
5689 | 91 |
19198 | 92 if (strcmp (directory, "*")) |
93 directory = "."; | |
94 endif | |
95 if (strcmp (directory, ".")) | |
96 flst = {"."}; | |
97 nf = 1; | |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
98 dir_has_wildcard = false; |
19198 | 99 else |
22155
289409b2992d
Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21634
diff
changeset
|
100 flst = __wglob__ (directory); |
19198 | 101 nf = numel (flst); |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
102 dir_has_wildcard = any (directory == '*'); # See Bug #58976. |
19198 | 103 endif |
5689 | 104 |
19198 | 105 ## Determine the file list for the case where a single directory is specified. |
106 if (nf == 1) | |
107 fn = flst{1}; | |
108 [st, err, msg] = stat (fn); | |
109 if (err < 0) | |
110 warning ("dir: 'stat (%s)' failed: %s", fn, msg); | |
111 nf = 0; | |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
112 elseif (S_ISDIR (st.mode) && ! dir_has_wildcard) |
19198 | 113 flst = readdir (flst{1}); |
114 nf = numel (flst); | |
23248
529c6d0c6684
dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
115 flst = strcat ([fn filesep], flst); |
5690 | 116 endif |
19198 | 117 endif |
5689 | 118 |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
119 if (nf > 0) |
23248
529c6d0c6684
dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
120 |
23256
99ee4dd83ceb
dir.m: Fix regression in 529c6d0c6684.
Rik <rik@octave.org>
parents:
23249
diff
changeset
|
121 fs = regexptranslate ("escape", filesep ("all")); |
23268
efb139519388
dir.m: Do not include trailing filesep in "folder". Fix BIST. (bug #50504).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23265
diff
changeset
|
122 re = sprintf ('(^.+)[%s]([^%s.]*)([.][^%s]*)?$', fs, fs, fs); |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
123 last_dir = last_absdir = ""; |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
124 info(nf,1).name = ""; # pre-declare size of struct array |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
125 |
19198 | 126 ## Collect results. |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
127 cnt = 0; |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
128 for i = 1:nf |
19198 | 129 fn = flst{i}; |
130 [st, err, msg] = lstat (fn); | |
131 if (err < 0) | |
132 warning ("dir: 'lstat (%s)' failed: %s", fn, msg); | |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
133 continue; |
19198 | 134 else |
135 ## If we are looking at a link that points to something, | |
136 ## return info about the target of the link, otherwise, return | |
137 ## info about the link itself. | |
138 if (S_ISLNK (st.mode)) | |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
139 [xst, err] = stat (fn); |
19198 | 140 if (! err) |
141 st = xst; | |
142 endif | |
143 endif | |
27333
2c4759c8239c
dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents:
27329
diff
changeset
|
144 tmpdir = regexprep (fn, re, '$1'); |
27569
29ccef7e5295
New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents:
27333
diff
changeset
|
145 if (is_same_file (fn, tmpdir)) |
27333
2c4759c8239c
dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents:
27329
diff
changeset
|
146 ## regexrep failed to match, no directory component. |
28009
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
147 no_dir = true; |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
148 else |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
149 no_dir = false; |
27328
29e52bcc72af
dir.m: Fix occasionally returning incorrect folder field (bug #55448).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
150 endif |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
151 fn = regexprep (fn, re, '$2$3'); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
152 info(++cnt).name = fn; |
29187
3a15c18c705f
dir.m: Return folder (not including file) in field "folder" (bug #59689).
Markus Mützel <markus.muetzel@gmx.de>
parents:
28034
diff
changeset
|
153 if (no_dir && ! strcmp (fn, ".")) |
28009
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
154 tmpdir = "."; |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
155 endif |
27569
29ccef7e5295
New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents:
27333
diff
changeset
|
156 if (! is_same_file (last_dir, tmpdir)) |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
157 ## Caching mechanism to speed up function |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
158 last_dir = tmpdir; |
28034
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
159 if (ispc () && strncmp (last_dir, '\\', 2)) |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
160 ## Windows UNC network file name is used as is |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
161 last_absdir = last_dir; |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
162 else |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
163 last_absdir = canonicalize_file_name (last_dir); |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
164 endif |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
165 endif |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
166 info(cnt).folder = last_absdir; |
19198 | 167 lt = localtime (st.mtime); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
168 info(cnt).date = strftime ("%d-%b-%Y %T", lt); |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
169 info(cnt).bytes = st.size; |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
170 info(cnt).isdir = S_ISDIR (st.mode); |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
171 info(cnt).datenum = [lt.year + 1900, lt.mon + 1, lt.mday, ... |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
172 lt.hour, lt.min, lt.sec]; |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
173 info(cnt).statinfo = st; |
19198 | 174 endif |
175 endfor | |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
176 info((cnt+1):end) = []; # remove any unused entries |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
177 ## A lot of gymnastics in order to call datenum just once. 2x speed up. |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
178 dvec = [info.datenum]([[1:6:end]', [2:6:end]', [3:6:end]', ... |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
179 [4:6:end]', [5:6:end]', [6:6:end]']); |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
180 dnum = datenum (dvec); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
181 ctmp = mat2cell (dnum, ones (cnt,1), 1); |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
182 [info.datenum] = ctmp{:}; |
5689 | 183 endif |
184 | |
5690 | 185 ## Return the output arguments. |
5689 | 186 if (nargout > 0) |
5690 | 187 ## Return the requested structure. |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
188 list = info; |
19198 | 189 elseif (numel (info) > 0) |
5690 | 190 ## Print the structure to the screen. |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
191 printf ("%s", list_in_columns ({info.name})); |
5689 | 192 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14327
diff
changeset
|
193 warning ("dir: nonexistent directory '%s'", directory); |
5689 | 194 endif |
195 | |
4694 | 196 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
197 |
19198 | 198 |
199 %!test | |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
200 %! orig_dir = pwd (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
201 %! tmp_dir = tempname (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
202 %! unwind_protect |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
203 %! assert (mkdir (tmp_dir)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
204 %! chdir (tmp_dir); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
205 %! fclose (fopen ("f1", "w")); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
206 %! list = dir (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
207 %! assert (isstruct (list) && ! isempty (list)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
208 %! assert (fieldnames (list), |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
209 %! {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"}); |
19198 | 210 %! |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
211 %! if (isunix ()) |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
212 %! idx = find (strcmp ({list.name}, "."), 1); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
213 %! assert ({list(idx:idx+1).name}, {".", ".."}); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
214 %! assert ([list(idx:idx+1).isdir], [true true]); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
215 %! endif |
19198 | 216 %! |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
217 %! ## test that specifying a filename works the same as using a directory. |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
218 %! found = find (! [list.isdir], 1); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
219 %! if (! isempty (found)) |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
220 %! list2 = dir (fullfile (list(found).folder, list(found).name)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
221 %! assert (list(found), list2); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
222 %! endif |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
223 %! unwind_protect_cleanup |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
224 %! chdir (orig_dir); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
225 %! confirm_recursive_rmdir (false, "local"); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
226 %! if (exist (tmp_dir)) |
28087
9cb04a9e81ec
rmdir: throw an error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
28034
diff
changeset
|
227 %! sts = rmdir (tmp_dir, "s"); |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
228 %! endif |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
229 %! end_unwind_protect |
19198 | 230 |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
231 %!test <*58976> |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
232 %! orig_dir = pwd (); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
233 %! tmp_dir = tempname (); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
234 %! unwind_protect |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
235 %! assert (mkdir (tmp_dir)); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
236 %! assert (mkdir (fullfile (tmp_dir, "dir1"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
237 %! assert (mkdir (fullfile (tmp_dir, "dir2"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
238 %! chdir (fullfile (tmp_dir, "dir1")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
239 %! fclose (fopen ("f1", "w")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
240 %! chdir (tmp_dir); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
241 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
242 %! ## Wildcard with multiple matches lists directories |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
243 %! list = dir (fullfile (tmp_dir, "dir*")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
244 %! assert (numel (list) == 2); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
245 %! assert ({list.name}, {"dir1", "dir2"}); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
246 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
247 %! ## Wildcard with single match lists directories |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
248 %! assert (rmdir (fullfile (tmp_dir, "dir2"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
249 %! list = dir (fullfile (tmp_dir, "dir*")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
250 %! assert (numel (list) == 1); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
251 %! assert ({list.name}, {"dir1"}); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
252 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
253 %! ## No wildcard returns listing of directory contents |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
254 %! list = dir (fullfile (tmp_dir, "dir1")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
255 %! assert (any (strcmp ({list.name}, "f1"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
256 %! unwind_protect_cleanup |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
257 %! chdir (orig_dir); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
258 %! confirm_recursive_rmdir (false, "local"); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
259 %! if (exist (tmp_dir)) |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
260 %! sts = rmdir (tmp_dir, "s"); |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
261 %! endif |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
262 %! end_unwind_protect |
19198 | 263 |
29590
d1a2ea05688b
dir.m: Don't restrict BIST to Unix-like platforms.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29359
diff
changeset
|
264 %!test <*57666> |
28010
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
265 %! orig_dir = pwd (); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
266 %! tmp_dir = tempname (); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
267 %! unwind_protect |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
268 %! assert (mkdir (tmp_dir)); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
269 %! list = dir (tmp_dir); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
270 %! assert (list(1).name, "."); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
271 %! assert (list(1).folder, canonicalize_file_name (tmp_dir)); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
272 %! unwind_protect_cleanup |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
273 %! if (exist (tmp_dir)) |
28087
9cb04a9e81ec
rmdir: throw an error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
28034
diff
changeset
|
274 %! sts = rmdir (tmp_dir); |
28010
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
275 %! endif |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
276 %! end_unwind_protect |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
277 |
19198 | 278 ## Test input validation |
279 %!error <DIRECTORY argument must be a string> dir (1) | |
280 %!warning <nonexistent directory> dir ("_%UNLIKELY_DIR_NAME%_"); |