annotate scripts/miscellaneous/dir.m @ 27241:733431da9742

dir.m: Gracefully handle race conditions when file is deleted (bug #56618). * dir.m: Don't enter main body of code if stat() call fails and number of files (nf) is 0. Use new variable cnt to place entries in to pre-declared info array. If lstat call fails on file, skip further processing and "continue" with for loop. At end of for loop over all files, delete any unused elements from info array.
author Rik <rik@octave.org>
date Fri, 12 Jul 2019 09:11:58 -0700
parents 12fe42e16841
children d5e24466835f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
1 ## Copyright (C) 2004-2019 John W. Eaton
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
2 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
4 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23396
diff changeset
5 ## Octave is free software: you can redistribute it and/or modify it
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
6 ## 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
7 ## 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
8 ## (at your option) any later version.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
9 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## GNU General Public License for more details.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
14 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7007
diff changeset
16 ## 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
17 ## <https://www.gnu.org/licenses/>.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
18
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
20 ## @deftypefn {} {} dir
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
21 ## @deftypefnx {} {} dir (@var{directory})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
22 ## @deftypefnx {} {[@var{list}] =} dir (@var{directory})
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
23 ## Display file listing for directory @var{directory}.
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
24 ##
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
25 ## 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
26 ##
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
27 ## If a return value is requested, return a structure array with the fields
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
28 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
29 ## @table @asis
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
30 ## @item name
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17908
diff changeset
31 ## File or directory name.
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
32 ##
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
33 ## @item folder
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
34 ## Location of file or directory
23396
945b53af4655 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 23268
diff changeset
35 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
36 ## @item date
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
37 ## Timestamp of file modification (string value).
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
38 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
39 ## @item bytes
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
40 ## File size in bytes.
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
41 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
42 ## @item isdir
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17908
diff changeset
43 ## True if name is a directory.
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
44 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
45 ## @item datenum
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
46 ## 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
47 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
48 ## @item statinfo
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
49 ## Information structure returned from @code{stat}.
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
50 ## @end table
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
51 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
52 ## 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
53 ## 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
54 ## 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
55 ##
7df8f5372ea8 doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## @var{directory} is subject to shell expansion if it contains any wildcard
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
57 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
58 ## wildcard character the wildcard must be escaped using the backslash operator
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
59 ## @samp{\}.
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
60 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
61 ## Note that for symbolic links, @code{dir} returns information about the
20181
aa36fb998a4d maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents: 20164
diff changeset
62 ## file that the symbolic link points to rather than the link itself.
7007
6304d9ea0a30 [project @ 2007-10-11 16:26:36 by jwe]
jwe
parents: 6955
diff changeset
63 ## However, if the link points to a nonexistent file, @code{dir} returns
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
64 ## information about the link.
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
65 ## @seealso{ls, readdir, glob, what, stat, lstat}
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
66 ## @end deftypefn
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
67
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
68 ## Author: jwe
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
69
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
70 ## FIXME: This is quite slow for large directories.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
71 ## Perhaps it should be converted to C++?
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
72
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
73 function retval = dir (directory)
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
74
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
75 if (nargin == 0)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
76 directory = ".";
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
77 elseif (nargin > 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5775
diff changeset
78 print_usage ();
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
79 endif
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
80
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
81 if (! ischar (directory))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
82 error ("dir: DIRECTORY argument must be a string");
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
83 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
84
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
85 ## Prep the retval.
6947
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
86 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
87 {"name", "folder" "date", "bytes", "isdir", "datenum", "statinfo"});
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
88
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
89 if (strcmp (directory, "*"))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
90 directory = ".";
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
91 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
92 if (strcmp (directory, "."))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
93 flst = {"."};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
94 nf = 1;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
95 else
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21634
diff changeset
96 flst = __wglob__ (directory);
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
97 nf = numel (flst);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
98 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
99
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
100 ## Determine the file list for the case where a single directory is specified.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
101 if (nf == 1)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
102 fn = flst{1};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
103 [st, err, msg] = stat (fn);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
104 if (err < 0)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
105 warning ("dir: 'stat (%s)' failed: %s", fn, msg);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
106 nf = 0;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
107 elseif (S_ISDIR (st.mode))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
108 flst = readdir (flst{1});
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
109 nf = numel (flst);
23248
529c6d0c6684 dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents: 23220
diff changeset
110 flst = strcat ([fn filesep], flst);
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
111 endif
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
112 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
113
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
114 if (nf > 0)
23248
529c6d0c6684 dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents: 23220
diff changeset
115
23256
99ee4dd83ceb dir.m: Fix regression in 529c6d0c6684.
Rik <rik@octave.org>
parents: 23249
diff changeset
116 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
117 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
118 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
119 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
120
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
121 ## Collect results.
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
122 cnt = 0;
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
123 for i = 1:nf
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
124 fn = flst{i};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
125 [st, err, msg] = lstat (fn);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
126 if (err < 0)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
127 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
128 continue;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
129 else
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
130 ## If we are looking at a link that points to something,
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
131 ## return info about the target of the link, otherwise, return
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
132 ## info about the link itself.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
133 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
134 [xst, err] = stat (fn);
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
135 if (! err)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
136 st = xst;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
137 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
138 endif
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
139 tmpdir = regexprep (fn, re, '$1');
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
140 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
141 info(++cnt).name = fn;
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
142 if (! strcmp (last_dir, tmpdir))
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
143 ## 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
144 last_dir = tmpdir;
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
145 last_absdir = canonicalize_file_name (last_dir);
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
146 endif
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
147 info(cnt).folder = last_absdir;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
148 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
149 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
150 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
151 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
152 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
153 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
154 info(cnt).statinfo = st;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
155 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
156 endfor
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
157 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
158 ## 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
159 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
160 [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
161 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
162 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
163 [info.datenum] = ctmp{:};
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
164 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
165
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
166 ## Return the output arguments.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
167 if (nargout > 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
168 ## Return the requested structure.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
169 retval = info;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
170 elseif (numel (info) > 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
171 ## Print the structure to the screen.
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
172 printf ("%s", list_in_columns ({info.name}));
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
173 else
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14327
diff changeset
174 warning ("dir: nonexistent directory '%s'", directory);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
175 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
176
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
177 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 16994
diff changeset
178
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
179
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
180 %!test
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
181 %! orig_dir = pwd ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
182 %! tmp_dir = tempname ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
183 %! unwind_protect
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
184 %! assert (mkdir (tmp_dir));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
185 %! chdir (tmp_dir);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
186 %! fclose (fopen ("f1", "w"));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
187 %! list = dir ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
188 %! assert (isstruct (list) && ! isempty (list));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
189 %! assert (fieldnames (list),
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
190 %! {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"});
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
191 %!
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
192 %! if (isunix ())
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
193 %! idx = find (strcmp ({list.name}, "."), 1);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
194 %! assert ({list(idx:idx+1).name}, {".", ".."});
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
195 %! 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
196 %! endif
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
197 %!
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
198 %! ## 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
199 %! found = find (! [list.isdir], 1);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
200 %! if (! isempty (found))
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
201 %! 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
202 %! assert (list(found), list2);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
203 %! endif
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
204 %! unwind_protect_cleanup
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
205 %! chdir (orig_dir);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
206 %! confirm_recursive_rmdir (false, "local");
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
207 %! if (exist (tmp_dir))
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
208 %! rmdir (tmp_dir, "s");
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
209 %! endif
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
210 %! end_unwind_protect
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
211
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
212 ## Test input validation
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
213 %!error <DIRECTORY argument must be a string> dir (1)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
214 %!warning <nonexistent directory> dir ("_%UNLIKELY_DIR_NAME%_");