| Ticket UUID: | 9b52204feaf9e1b0d5b081e849bc5136eccbd296 | |||
| Title: | fileutil::find in tcllib 1.17 does not come back when used on /sys/devices | |||
| Type: | Bug | Version: | 1.17 | |
| Submitter: | anonymous | Created on: | 2015-05-11 17:39:15 | |
| Subsystem: | fileutil | Assigned To: | aku | |
| Priority: | 7 High | Severity: | Important | |
| Status: | Closed | Last Modified: | 2015-05-26 23:07:39 | |
| Resolution: | Fixed | Closed By: | aku | |
| Closed on: | 2015-05-26 23:07:39 | |||
| Description: |
Hi,
I'm using the fileutil::find command to search for certain USB devices
under /sys/devices. This still worked fine in tcllib 1.16. However, the
functions hangs (endless loop?) in tcllib 1.17.
Here is a small test program:
--- snip ---
package require fileutil
proc IsIdVendorFile {fileName} {
return [string equal idVendor $fileName]
}
foreach vendorIdFileName [fileutil::find /sys/devices IsIdVendorFile] {
puts $vendorIdFileName
}
--- snip ---
I did not check the find command for other trees.
| |||
| User Comments: |
aku added on 2015-05-26 23:07:39:
Demo test cases done, plus fixed example in the docs. Commit [2cd687d6e6]. Pushed. Closing. aku added on 2015-05-26 22:32:46: Documentation done. Commit [4ae879c0ea]. Pushed. aku added on 2015-05-22 04:41:49: Ok. I now understand the issue. To wit:
- The intentioal change e4cc was to move fileutil::find
(and fileutil::traverse) from a broken regime for
handling symlinks to one which would handle them
correctly, allowing find to folllow all but true cyclic
structures.
After the change find will enumerate all legal paths
from underneath the base directory, which are not
cyclic. Before the use of inodes meant that with
symlinks present some paths would enumerated, their
aliases would not be, and it would be effectively
random wether you would get a path through symlinks, or
without.
- The /sys hierarchy, of which /sys/devices is a part of
is a pathological case for this, due to its extensive
cross-linking. As a small example just consider the
tty's. They exist as hierarchy under
/sys/devices/.../tty/ttyX,
with each device linking over (via subsystem/) to the
second hierarchy under /sys/class/tty, which then again
links back to the tty directories under /sys/devices.
The end result of such a structure is that we have a
number of legal paths which are O(n**2) in the number
of tty's. For the whole of the actual /sys/ tree square
in the numnber of devices, of which we have a few
hundred, giving us millions of paths.
- The original broken regime caused a quick cut-off of
the links, with the majority not followed, bringing
things down do roughly O(n) again, if only by accident.
- Now 'find' is actually traversing the entire millions
of paths. That alone will be slow. Then there is the
memory overhead of the entire fifo of paths waiting for
traversal kept in memory. It actually never shrinks
until all paths have been traversed. The Tcl interp may
well go into thrashing.
I hope that this lets you understand what you were seeing.
Now, solutions ...
- First, no, I will not go back to the old scheme. It was
definitely broken and non-deterministic in what paths
of the hierarchy you would see and not. Very bad. (I
noticed originally with my personal photo collection
which is moderately symlinked for category aliasing
when paths I knew existed were not listed in the output)
- Still, bad news and good news ...
- Bad: "fileutil::find" currently has no wait out, as
you can specify only a basic path filter, but not a
directory filter.
- Good: "fileutil::traverse" (in the package of the
same name) does have a -prefilter option which allows
us to cut all symlinks from traversal. Example code:
===
package require fileutil::traverse
proc IsIdVendorFile {fileName} {
return [string equal idVendor \
[file tail $fileName]]
}
proc NoLinks {fileName} {
if {[string equal [file type $fileName] link]} {
return 0
}
return 1
}
fileutil::traverse T /sys/devices \
-prefilter NoLinks \
-filter IsIdVendorFile
T foreach p {
puts $p
}
T destroy
===
fileutil::traverse is actually better internally as
well, treating the list of pending paths as an actual
stack (find treats it as fifo), reducing memory
overhead. And with "foreach" it also returns any
found paths much quicker (incrementally), whereas
"find" accumulates them before returning all of them
at once.
Because of this last (incremental return) it is
instructive to disable the if command in NoLinks and
see the effect on the set of returned paths.
Actions/Todo:
- Extend the documentation of "find" to note this
pathological case and the effect of the change from
version 1.14.9 to 1.14.10. Refer user to
"fileutil::traverse", and -prefilter.
- Ditto the same for "fileutil::traverse" (v0.4.4 to
v0.4.5), except that it does not need a reference to
itself.
- Add test cases for "find" and "traverse" which
demonstrate the basic cross-link structure
and its effect on traversal and number of paths.
- No code changes needed, although it might be worth to
consider extending the "find" api with options, and
implement a -prefilter.
aku added on 2015-05-21 23:10:36: It is checkin [e4cc97ecc8ec99c4] which caused the change, modifying the handling of symlinks. Rev [83a79da8d2d30a4e] is unrelated (sha), and Rev [4dc136d4389178dc] fixes an issue in e4cc... Main change from e4cc is that symlinks which are not cycles are now followed. Except, in the heavily symlinked devices tree (ex: subsystem/) has cycles , it looks like, which are not detected, it seems. Files seen by find after the change are /sys/devices/virtual/tty/tty15/subsystem/tty14/power /sys/devices/virtual/tty/tty16/subsystem/tty14/power Both represent /sys/devices/virtual/tty/tty14/power The "/sys/devices/virtual/tty/*/subsystem" path is a symlink up to the "/sys/devices/virtual/tty" directory On first glance this should be detected as a cycle to be ignored, and is not. aku added on 2015-05-21 22:43:00: Bisection delivers 2014-11-27 05:44:34 [4dc136d4389178dc] BAD 2014-11-27 01:10:32 [83a79da8d2d30a4e] NEXT 2014-11-27 01:09:11 [e4cc97ecc8ec99c4] CURRENT 2014-11-19 04:29:54 [d9d86cd9ffe11a23] GOOD The revs NEXT and CURRENT exhibit an unrelated bug which breaks the trial script. aku added on 2015-05-11 17:59:39: From the mail conversation >> This "/sys/devices" directory, is that a special filesystem on linux ? >> Or some other platform ? > > Yes, that's a special file system under Linux. It provides direct > hardware access via file I/O (https://en.wikipedia.org/wiki/Sysfs). It > also uses a lot of linking inside between drivers, devices, etc. So my > suspicion is that you get into an endless loop while following some of > these links. aku added on 2015-05-11 17:53:19: Thank you for the report. The platform in question is Linux ? | |||
Home
Blog
Docs
Downloads
Tickets
Timeline
Branches
Tags
Wiki