#! @file find.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `pattern::find`, which walks the file system for the paths a glob #! matches, and `search_error`, which says why it stopped. import system; module std::io::glob; #! Why a search stopped: a directory it had to read couldn't be. pub struct search_error { pub var why: io_error; #! What the system said. pub var at: path; #! The directory. } #! Describes why the search stopped. #! @param this The error. #! @returns A sentence naming the directory and what the system said. pub def search_error::describe(&this): string { return $"`{this.at.span}` could not be read: {this.why}"; } #! Finds every path the glob matches, walking from a directory. #! #! A name written as it is, as `src` in `src/*.sakura`, is joined on rather #! than looked for, so whether it matches another case is the file system's, #! and only the directories a wildcard is matched in are read. `**` reads every #! directory below where it starts, but those reached through a link, so a link #! never leads it around in a circle. A file only reached through a link is #! found by naming the link, as `*` or as written. #! @param this The pattern. #! @param from The directory a glob without a root starts from, as `.`. #! @returns Each path that exists and whose text the pattern `matches`, joined #! onto `from` where the glob has no root, in order by their bytes, as #! `sort` orders them where its locale is `C`, each once, or why a directory couldn't be read. A directory that #! isn't there, or a name that isn't a directory, is passed over, as #! is a link leading around in a circle, and a name no `path` can #! hold, as one with `\` on Linux. pub def pattern::find(&this, from: &path): result, search_error> { var found := vector::::new(); for c in 0..this._choices.length { val each := this._choices[c]; var root := path::parse(each.rooted == 0 ? "." : this._bytes.substr(each.root, each.rooted)) else (_) { continue; }; var walked := this.walk(&each, each.rooted == 0 ? from : &root, each.start, 0, &mut found); if walked is error(&mut why) { return move(why); } } found.sort_by(before); var kept := vector::::new(); for k in 0..found.length { continue if kept.length != 0 && kept.last.equals(&found[k]); kept.append(move(found[k])); } return move(kept); } # Adds each path that the segments of a choice from one on match, below a # directory a number of names were matched to reach. def pattern::walk( &this, each: &choice, dir: &path, s: u64, depth: u64, found: &mut vector ): result { val end := each.start + each.length; if s == end { # A choice of no names but a root, or `.`, matches where it starts, and # `**` alone doesn't. if (depth != 0 || each.length == 0) && is_there(dir, each.directories) { found.append(path::copy_of(dir)); } return; } val seg := this._segments[s]; val last := s + 1 == end; if seg.kind == segment_kind::literal { # A name no path can hold, as one with `\` on Linux, names nothing. var name := path::parse(this._bytes.substr(seg.start, seg.length)) else (_) { return; }; var next := dir.join(&name); if last { if is_there(&next, each.directories) { found.append(move(next)); } return; } return this.walk(each, &next, s + 1, depth + 1, found); } if seg.kind == segment_kind::deep { var walked := this.walk(each, dir, s + 1, depth, found); return move(walked) if walked is error; } var listing := io::directory::open(dir) else (why) { return passed(why, dir); }; while listing.next() is some(entry) { continue if !this.fits_name(&seg, entry.name); var name := path::parse(entry.name) else (_) { continue; }; var next := dir.join(&name); val kind := entry.kind; # `**` takes in a directory, and ends with anything else where it's last. if seg.kind == segment_kind::deep { if kind == file_kind::directory { var walked := this.walk(each, &next, s, depth + 1, found); return move(walked) if walked is error; } else if last && (!each.directories || is_there(&next, true)) { found.append(move(next)); } continue; } if last { if !each.directories || kind == file_kind::directory || (kind == file_kind::link && is_there(&next, true)) { found.append(move(next)); } } else if kind == file_kind::directory || kind == file_kind::link { var walked := this.walk(each, &next, s + 1, depth + 1, found); return move(walked) if walked is error; } } if listing.error is some(why) { return passed(why, dir); } return; } # Whether anything is at a path, a link included wherever it leads, or where # only directories are asked for, whether a directory is, through any links. def is_there(at: &path, directories: bool): bool { if directories { return match io::info(at) { value(&found) => found.kind == file_kind::directory, error(_) => false, }; } return io::info(at, false) is value; } # Passes over a directory that couldn't be read because it isn't one, or isn't # there, or a link leads around in a circle, and stops the search otherwise. def passed(why: io_error, at: &path): result { if why == io_error::not_found || why == io_error::not_a_directory || why == io_error::link_loop { return; } return search_error { why = why, at = path::copy_of(at) }; } # Whether a path comes before another, by their bytes, as `a`, `a-b`, `a/b`, # which keeps a directory's paths together, and orders them as a shell's `*` # and `sort` do. def before(a: &path, b: &path): bool { return a.span.compare(b.span) < 0; }