1 /*****************************************************************************\
3 * Name : directory_tree *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2004-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "directory.h"
16 #include "directory_tree.h"
18 #include "filename_list.h"
19 #include "filename_tree.h"
21 #include <basis/functions.h>
22 #include <basis/contracts.h>
23 #include <loggers/program_wide_logger.h>
24 #include <structures/object_packers.h>
25 #include <structures/string_array.h>
26 #include <textual/parser_bits.h>
27 #include <textual/string_manipulation.h>
31 using namespace basis;
32 using namespace loggers;
33 using namespace nodes;
34 using namespace structures;
35 using namespace textual;
37 #define DEBUG_DIRECTORY_TREE
38 // uncomment for noisier version.
41 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
42 //printf("%s::%s: %s\n", static_class_name(), func, astring(to_print).s())
46 namespace filesystem {
48 class dir_tree_iterator : public filename_tree::iterator
51 filename_tree *_current;
53 dir_tree_iterator(const filename_tree *initial,
54 tree::traversal_directions dir)
55 : filename_tree::iterator(initial, dir), _current(NIL) {}
60 directory_tree::directory_tree()
61 : _scanned_okay(false),
63 _pattern(new astring),
64 _real_tree(new filename_tree),
66 _creator(new fname_tree_creator)
70 directory_tree::directory_tree(const astring &path, const char *pattern,
72 : _scanned_okay(false),
73 _path(new astring(path)),
74 _pattern(new astring(pattern)),
76 _ignore_files(ignore_files),
77 _creator(new fname_tree_creator)
82 directory_tree::~directory_tree()
84 _scanned_okay = false;
91 const astring &directory_tree::path() const { return *_path; }
93 int directory_tree::packed_size() const
95 return 2 * PACKED_SIZE_INT32
96 + _path->packed_size()
97 + _pattern->packed_size()
98 + _real_tree->recursive_packed_size();
101 void directory_tree::pack(byte_array &packed_form) const
103 attach(packed_form, int(_scanned_okay));
104 attach(packed_form, int(_ignore_files));
105 _path->pack(packed_form);
106 _pattern->pack(packed_form);
107 _real_tree->recursive_pack(packed_form);
110 bool directory_tree::unpack(byte_array &packed_form)
113 if (!detach(packed_form, temp)) return false;
114 _scanned_okay = temp;
115 if (!detach(packed_form, temp)) return false;
116 _ignore_files = temp;
117 if (!_path->unpack(packed_form)) return false;
118 if (!_pattern->unpack(packed_form)) return false;
120 _real_tree = (filename_tree *)packable_tree::recursive_unpack
121 (packed_form, *_creator);
123 _real_tree = new filename_tree; // reset it.
129 void directory_tree::text_form(astring &target, bool show_files)
131 dir_tree_iterator *ted = start(directory_tree::prefix);
132 // create our iterator to do a prefix traversal.
134 int depth; // current depth in tree.
135 filename curr; // the current path the iterator is at.
136 string_array files; // the filenames held at the iterator.
138 while (current(*ted, curr, files)) {
139 // we have a good directory to show.
140 directory_tree::depth(*ted, depth);
141 target += string_manipulation::indentation(depth * 2) + astring("[")
142 + curr.raw() + "]" + parser_bits::platform_eol_to_chars();
145 for (int i = 0; i < files.length(); i++) names += files[i] + " ";
146 if (names.length()) {
148 string_manipulation::split_lines(names, split, depth * 2 + 2);
149 target += split + parser_bits::platform_eol_to_chars();
153 // go to the next place.
160 void directory_tree::traverse(const astring &path, const char *pattern,
161 filename_tree &add_to)
164 // prepare the current node.
165 add_to._dirname = filename(path, astring::empty_string());
166 add_to._files.reset();
167 #ifdef DEBUG_DIRECTORY_TREE
168 LOG(astring("working on node ") + add_to._dirname);
171 // open the directory.
172 directory curr(path, "*");
173 if (!curr.good()) return;
175 if (!_ignore_files) {
176 // add all the files to the current node.
177 directory curr_stringent(path, pattern);
178 add_to._files = curr_stringent.files();
181 // now iterate across the directories here and add a sub-node for each one,
182 // and recursively traverse that sub-node also.
183 const string_array &dirs = curr.directories();
184 for (int i = 0; i < dirs.length(); i++) {
185 filename_tree *new_branch = NIL;
186 astring new_path = path + filename::default_separator() + dirs[i];
187 #ifdef DEBUG_DIRECTORY_TREE
188 LOG(astring("seeking path: ") + new_path);
190 if (!filename(new_path).is_normal()) {
191 //#ifdef DEBUG_DIRECTORY_TREE
192 LOG(astring("bailing on weird dir: ") + new_path);
194 continue; // only regular directories please.
196 for (int q = 0; q < add_to.branches(); q++) {
197 filename_tree *curr_kid = (filename_tree *)add_to.branch(q);
198 #ifdef DEBUG_DIRECTORY_TREE
199 LOG(astring("curr kid: ") + curr_kid->_dirname);
201 if (filename(new_path).raw().iequals(filename
202 (curr_kid->_dirname).raw())) {
203 new_branch = curr_kid;
204 #ifdef DEBUG_DIRECTORY_TREE
205 LOG(astring("using existing branch for ") + new_path);
211 #ifdef DEBUG_DIRECTORY_TREE
212 LOG(astring("adding new branch for ") + new_path);
214 new_branch = new filename_tree;
215 add_to.attach(new_branch);
216 new_branch->_depth = add_to._depth + 1;
218 #ifdef DEBUG_DIRECTORY_TREE
219 LOG(astring("traversing sub-node ") + new_path);
221 traverse(new_path, pattern, *new_branch);
225 bool directory_tree::reset(const astring &path, const char *pattern)
228 _scanned_okay = false;
230 // filename temp_path(path);
231 // temp_path.canonicalize();
235 _real_tree = new filename_tree;
237 LOG(astring("dirtree::reset to path: ") + path);
239 // check that the top-level is healthy.
240 directory curr(path, "*");
241 if (!curr.good()) return false;
242 // our only exit condition; other directories might not be accessible
243 // underneath, but the top one must be accessible for us to even start
246 traverse(path, pattern, *_real_tree);
247 _scanned_okay = true;;
251 dir_tree_iterator *directory_tree::start_at(filename_tree *start,
252 traversal_types type) const
254 // translate to the lower level traversal enum.
255 tree::traversal_directions dir = tree::prefix;
256 if (type == infix) dir = tree::infix;
257 else if (type == postfix) dir = tree::postfix;
259 return new dir_tree_iterator(start, dir);
262 dir_tree_iterator *directory_tree::start(traversal_types type) const
264 // translate to the lower level traversal enum.
265 tree::traversal_directions dir = tree::prefix;
266 if (type == infix) dir = tree::infix;
267 else if (type == postfix) dir = tree::postfix;
269 return new dir_tree_iterator(_real_tree, dir);
272 bool directory_tree::jump_to(dir_tree_iterator &scanning,
273 const astring &sub_path)
277 filename(sub_path).separate(pieces);
278 for (int i = 0; i < pieces.length(); i++) {
279 filename_tree *curr = dynamic_cast<filename_tree *>(scanning.current());
280 #ifdef DEBUG_DIRECTORY_TREE
281 LOG(astring("at ") + curr->_dirname.raw());
283 string_array sub_pieces = pieces.subarray(i, i);
285 curr_path.join(sub_pieces);
286 curr_path = filename(curr->_dirname.raw() + filename::default_separator()
288 #ifdef DEBUG_DIRECTORY_TREE
289 LOG(astring("made curr path ") + curr_path.raw());
291 if (!curr) return false;
292 bool found_it = false;
293 for (int j = 0; j < curr->branches(); j++) {
294 filename_tree *sub = dynamic_cast<filename_tree *>(curr->branch(j));
295 #ifdef DEBUG_DIRECTORY_TREE
296 LOG(astring("looking at ") + sub->_dirname.raw());
298 if (sub->_dirname.compare_prefix(curr_path)) {
299 // this part matches!
301 #ifdef DEBUG_DIRECTORY_TREE
302 LOG(astring("found at ") + sub->_dirname.raw());
309 #ifdef DEBUG_DIRECTORY_TREE
310 LOG(astring("could not find ") + curr_path.raw());
318 filename_tree *directory_tree::goto_current(dir_tree_iterator &scanning)
320 if (!scanning._current) {
321 // this one hasn't been advanced yet, or it's already over with.
322 scanning._current = (filename_tree *)scanning.next();
324 // now check that we're healthy.
325 if (!scanning._current) return NIL; // all done.
327 // cast the tree to the right type.
328 return dynamic_cast<filename_tree *>(scanning._current);
331 bool directory_tree::current_dir(dir_tree_iterator &scanning,
334 dir_name = astring::empty_string();
335 filename_tree *tof = goto_current(scanning);
336 if (!tof) return false;
337 dir_name = tof->_dirname;
341 bool directory_tree::current(dir_tree_iterator &scanning,
342 filename &dir_name, string_array &to_fill)
344 // clear any existing junk.
345 dir_name = astring::empty_string();
348 filename_tree *tof = goto_current(scanning);
349 if (!tof) return false;
351 // fill in what they wanted.
352 dir_name = tof->_dirname;
353 tof->_files.fill(to_fill);
358 bool directory_tree::current(dir_tree_iterator &scanning,
359 filename &dir_name, filename_list &to_fill)
361 // clear any existing junk.
362 dir_name = astring::empty_string();
365 filename_tree *tof = goto_current(scanning);
366 if (!tof) return false;
368 // fill in what they wanted.
369 dir_name = tof->_dirname;
370 to_fill = tof->_files;
375 filename_list *directory_tree::access(dir_tree_iterator &scanning)
377 filename_tree *tof = goto_current(scanning);
378 if (!tof) return NIL;
382 bool directory_tree::depth(dir_tree_iterator &scanning, int &depth)
384 depth = -1; // invalid as default.
385 filename_tree *tof = goto_current(scanning);
386 if (!tof) return false;
391 bool directory_tree::children(dir_tree_iterator &scanning, int &kids)
393 kids = -1; // invalid as default.
394 filename_tree *tof = goto_current(scanning);
395 if (!tof) return false;
396 kids = tof->branches();
400 bool directory_tree::next(dir_tree_iterator &scanning)
402 scanning._current = (filename_tree *)scanning.next();
403 return !!scanning._current;
406 void directory_tree::throw_out(dir_tree_iterator * &to_whack)
411 filename_tree *directory_tree::seek(const astring &dir_name_in,
412 bool ignore_initial) const
415 array<filename_tree *> examining;
416 // the list of nodes we're currently looking at.
418 #ifdef DEBUG_DIRECTORY_TREE
419 LOG(astring("seeking on root of: ") + *_path);
422 astring dir_name = filename(dir_name_in).raw();
423 // set the search path up to have the proper prefix.
425 dir_name = path() + filename::default_separator()
426 + filename(dir_name_in).raw();
428 #ifdef DEBUG_DIRECTORY_TREE
429 LOG(astring("adding root: ") + _real_tree->_dirname);
431 examining += _real_tree;
433 astring sequel; // holds extra pieces from filename comparisons.
435 // chew on the list of nodes to examine until we run out.
436 while (examining.length()) {
439 // start looking at all the items in the list, even though we might have
440 // to abandon the iteration if we find a match.
441 filename_tree *check = NIL;
442 for (posn = 0; posn < examining.length(); posn++) {
443 check = examining[posn];
444 filename current(check->_dirname);
445 if (!current.is_normal()) {
446 //#ifdef DEBUG_DIRECTORY_TREE
447 LOG(astring("skipping abnormal dir: \"") + current + "\"");
451 #ifdef DEBUG_DIRECTORY_TREE
452 LOG(astring("looking at ") + current.raw());
454 if (current.compare_prefix(dir_name, sequel)) {
456 #ifdef DEBUG_DIRECTORY_TREE
457 LOG(astring("matched! at ") + current.raw());
461 // whoa! an exact match. we're done now.
462 #ifdef DEBUG_DIRECTORY_TREE
463 LOG(astring("exact match at ") + current.raw() + "! done!!!");
467 #ifdef DEBUG_DIRECTORY_TREE
468 LOG(astring("inexact match because sequel=") + sequel);
474 if (!found) return NIL; // we found nothing comparable.
476 // we found a partial match. that means we should start looking at this
477 // node's children for the exact match.
479 // this is a serious logical error!
480 LOG("serious logical error: tree was not located.");
483 examining.reset(); // clear the existing nodes.
484 for (int i = 0; i < check->branches(); i++)
485 examining += (filename_tree *)check->branch(i);
488 return NIL; // we found nothing that looked like that node.
491 bool directory_tree::calculate(bool just_size)
492 { return calculate(_real_tree, just_size); }
494 bool directory_tree::calculate(filename_tree *start, bool just_size)
496 FUNCDEF("calculate");
497 dir_tree_iterator *ted = start_at(start, directory_tree::postfix);
498 // create our iterator to do a postfix traversal. why postfix? well,
499 // prefix has been used elsewhere and since it doesn't really matter what
500 // order we visit the nodes here, it's good to change up.
502 int depth; // current depth in tree.
503 filename curr; // the current path the iterator is at.
504 filename_list *files; // the filenames held at the iterator.
506 while (directory_tree::current_dir(*ted, curr)) {
507 if (!curr.is_normal()) {
508 //#ifdef DEBUG_DIRECTORY_TREE
509 LOG(astring("skipping abnormal dir: \"") + curr + "\"");
511 directory_tree::next(*ted);
512 continue; // scary non-simple file type.
514 // we have a good directory to show.
515 #ifdef DEBUG_DIRECTORY_TREE
516 LOG(astring("calcing node ") + curr.raw());
518 files = directory_tree::access(*ted);
519 directory_tree::depth(*ted, depth);
520 for (int i = 0; i < files->elements(); i++) {
521 filename curr_file = curr + "/" + *files->borrow(i);
522 #ifdef DEBUG_DIRECTORY_TREE
523 LOG(astring("got a curr file: ") + curr_file);
525 if (!curr_file.is_normal()) {
526 //#ifdef DEBUG_DIRECTORY_TREE
527 LOG(astring("skipping abnormal file: \"") + curr + "\"");
531 if (!files->borrow(i)->calculate(curr.raw(), just_size)) {
532 LOG(astring("failure to calculate ") + files->get(i)->text_form());
536 directory_tree::next(*ted);
539 directory_tree::throw_out(ted);
543 bool directory_tree::compare_trees(const directory_tree &source,
544 const directory_tree &target, filename_list &differences,
545 file_info::file_similarity how_to_compare)
547 return compare_trees(source, astring::empty_string(), target,
548 astring::empty_string(), differences, how_to_compare);
551 bool directory_tree::compare_trees(const directory_tree &source,
552 const astring &source_start_in, const directory_tree &target,
553 const astring &target_start_in, filename_list &differences,
554 file_info::file_similarity how_compare)
556 FUNCDEF("compare_trees");
557 differences.reset(); // prepare it for storage.
559 // make sure we get canonical names to work with.
560 filename source_start(source_start_in);
561 filename target_start(target_start_in);
563 dir_tree_iterator *ted = source.start(directory_tree::prefix);
564 // create our iterator to do a prefix traversal.
566 astring real_source_start = source.path();
567 if (source_start.raw().t()) {
568 // move to the right place.
569 real_source_start = real_source_start + filename::default_separator()
570 + source_start.raw();
571 if (!directory_tree::jump_to(*ted, source_start.raw())) {
572 // can't even start comparing.
573 LOG(astring("failed to find source start in tree, given as ")
574 + source_start.raw());
579 filename curr; // the current path the iterator is at.
580 filename_list files; // the filenames held at the iterator.
582 // calculate where our comparison point is on the source.
583 int source_pieces = 0;
586 filename(real_source_start).separate(temp);
587 source_pieces = temp.length();
590 bool seen_zero_pieces = false;
591 while (directory_tree::current(*ted, curr, files)) {
592 // we're in a place in the source tree now. let's compare it with the
593 // target's recollection.
595 #ifdef DEBUG_DIRECTORY_TREE
596 LOG(astring("curr dir in tree: ") + curr.raw());
600 curr.separate(pieces); // get the components of the current location.
601 #ifdef DEBUG_DIRECTORY_TREE
602 LOG(astring("name in pieces:") + pieces.text_form());
604 pieces.zap(0, source_pieces - 1);
605 // snap the root components out of there.
607 filename corresponding_name;
608 corresponding_name.join(pieces);
609 #ifdef DEBUG_DIRECTORY_TREE
610 LOG(astring("computed target name as: ") + corresponding_name);
612 filename original_correspondence(corresponding_name);
614 if (!corresponding_name.raw().t()) {
615 if (seen_zero_pieces) {
616 #ifdef DEBUG_DIRECTORY_TREE
617 LOG(astring("breaking out now due to empty correspondence"));
621 seen_zero_pieces = true;
623 if (target_start.raw().t()) {
624 corresponding_name = filename(target_start.raw()
625 + filename::default_separator() + corresponding_name.raw());
626 /*doesn't help, not right yet. } else {
627 // if they didn't give us a place to start, we start at the top.
628 corresponding_name = filename(target.path()
629 + filename::default_separator() + corresponding_name.raw());
632 #ifdef DEBUG_DIRECTORY_TREE
633 LOG(astring("target with start is: ") + corresponding_name);
636 filename_tree *target_now = target.seek(corresponding_name.raw(), true);
638 // that entire sub-tree is missing. add all of the files here into
640 #ifdef DEBUG_DIRECTORY_TREE
641 LOG(astring("could not find dir in target for ") + curr.raw()
642 + " which we computed corresp as " + corresponding_name.raw());
646 // now scan across all the files that are in our source list.
647 for (int i = 0; i < files.elements(); i++) {
648 if (!target_now // there was no node, so we're adding everything...
649 || !target_now->_files.member_with_state(*files[i], how_compare) ) {
650 // ... or we need to add this file since it's missing.
651 #ifdef DEBUG_DIRECTORY_TREE
652 LOG(astring("adding record: ") + files[i]->text_form());
655 file_info *new_record = new file_info(*files[i]);
656 // record the file time for use later in saving.
657 new_record->calculate(curr, true);
658 astring original = new_record->raw();
659 #ifdef DEBUG_DIRECTORY_TREE
660 LOG(astring("current: ") + new_record->raw());
663 astring actual_name = source_start.raw();
664 #ifdef DEBUG_DIRECTORY_TREE
665 if (actual_name.t()) LOG(astring("sname=") + actual_name);
667 if (actual_name.length()) actual_name += filename::default_separator();
668 actual_name += original_correspondence.raw();
669 if (actual_name.length()) actual_name += filename::default_separator();
670 actual_name += new_record->raw();
671 #ifdef DEBUG_DIRECTORY_TREE
672 if (actual_name.t()) LOG(astring("sname=") + actual_name);
674 (filename &)(*new_record) = filename(actual_name);
676 astring targ_name = corresponding_name.raw();
677 #ifdef DEBUG_DIRECTORY_TREE
678 if (targ_name.t()) LOG(astring("tname=") + targ_name);
680 if (targ_name.length()) targ_name += filename::default_separator();
681 targ_name += original;
682 #ifdef DEBUG_DIRECTORY_TREE
683 if (targ_name.t()) LOG(astring("tname=") + targ_name);
686 new_record->secondary(targ_name);
688 differences += new_record;
689 #ifdef DEBUG_DIRECTORY_TREE
690 LOG(astring("came out as: ") + new_record->text_form());
695 // go to the next place.
696 directory_tree::next(*ted);
699 directory_tree::throw_out(ted);
704 outcome directory_tree::find_common_root(const astring &finding, bool exists,
705 filename_tree * &found, astring &reassembled, string_array &pieces,
708 FUNCDEF("find_common_root");
709 // test the path to find what it is.
710 filename adding(finding);
711 if (exists && !adding.good())
712 return common::BAD_INPUT; // not a good path.
713 int file_subtract = 0; // if it's a file, then we remove last component.
714 if (exists && !adding.is_directory()) file_subtract = 1;
716 // break up the path into pieces.
718 adding.separate(pieces);
720 // break up our root into pieces; we must take off components that are
721 // already in the root.
722 string_array root_pieces;
723 filename temp_file(path());
724 temp_file.separate(root_pieces);
726 // locate the last place where the path we were given touches our tree.
727 // it could be totally new, partially new, or already contained.
728 filename_tree *last_match = _real_tree; // where the common root is located.
729 int list_length = pieces.length() - file_subtract;
732 // we must put all the pieces in that already come from the root.
733 for (int i = 0; i < root_pieces.length() - 1; i++) {
734 bool add_slash = false;
735 if (reassembled.length() && (reassembled[reassembled.end()] != '/') )
737 if (add_slash) reassembled += "/";
738 reassembled += pieces[i];
739 if (reassembled[reassembled.end()] == ':') {
740 #ifdef DEBUG_DIRECTORY_TREE
741 LOG(astring("skipping drive component ") + reassembled);
747 #ifdef DEBUG_DIRECTORY_TREE
748 LOG(astring("after pre-assembly, path is ") + reassembled);
751 outcome to_return = common::NOT_FOUND;
753 for (match_place = root_pieces.length() - 1; match_place < list_length;
755 // add a slash if there's not one present already.
756 bool add_slash = false;
757 if (reassembled.length() && (reassembled[reassembled.end()] != '/') )
759 // add the next component in to our path.
760 if (add_slash) reassembled += "/";
761 reassembled += pieces[match_place];
762 // special case for dos paths.
763 if (reassembled[reassembled.end()] == ':') {
764 #ifdef DEBUG_DIRECTORY_TREE
765 LOG(astring("skipping drive component ") + reassembled);
769 reassembled = filename(reassembled).raw(); // force compliance with OS.
770 #ifdef DEBUG_DIRECTORY_TREE
771 LOG(astring("now seeking ") + reassembled);
773 filename_tree *sought = seek(reassembled, false);
775 #ifdef DEBUG_DIRECTORY_TREE
776 LOG(astring("couldn't find ") + reassembled);
778 if (!exists && (match_place == list_length - 1)) {
779 // see if we can get a match on a file rather than a directory, but
780 // only if we're near the end of the compare.
781 if (last_match->_files.member(pieces[match_place])) {
782 // aha! a file match.
783 to_return = common::OKAY;
791 // record where we last had some success.
792 #ifdef DEBUG_DIRECTORY_TREE
793 LOG(astring("found subtree for ") + reassembled);
798 // this is a success, but our loop structure can put us one past the right
800 if (match_place >= list_length) {
801 match_place = list_length - 1;
802 to_return = common::OKAY;
809 outcome directory_tree::add_path(const astring &new_item, bool just_size)
812 // test the path to find out what it is.
813 filename adding(new_item);
814 if (!adding.good()) {
815 LOG(astring("non-existent new item! ") + new_item);
816 return common::NOT_FOUND; // not an existing path.
818 if (!adding.is_normal()) {
819 //#ifdef DEBUG_DIRECTORY_TREE
820 LOG(astring("abnormal new item: ") + new_item);
822 return common::BAD_INPUT; // not a good path.
824 int file_subtract = 0; // if it's a file, then we remove last component.
825 if (!adding.is_directory()) file_subtract = 1;
826 #ifdef DEBUG_DIRECTORY_TREE
827 if (file_subtract) { LOG(astring("adding a file ") + new_item); }
828 else { LOG(astring("adding a directory ") + new_item); }
831 // find the common root, break up the path into pieces, and tell us where
834 filename_tree *last_match = NIL;
836 astring reassembled; // this will hold the common root.
837 outcome ret = find_common_root(new_item, true, last_match, reassembled,
840 LOG(astring("serious error finding common root for ") + new_item
841 + ", got NIL tree.");
842 return common::FAILURE; // something serious isn't right.
845 if (!file_subtract) {
846 if (ret != common::OKAY) {
847 // if it's a new directory, we add a new node for traverse to work on.
848 #ifdef DEBUG_DIRECTORY_TREE
849 LOG(astring("now adding node for ") + reassembled);
851 filename_tree *new_branch = new filename_tree;
852 new_branch->_depth = last_match->_depth + 1;
853 last_match->attach(new_branch);
854 last_match = new_branch;
856 #ifdef DEBUG_DIRECTORY_TREE
857 LOG(astring("matched properly. reassembled set to ") + reassembled);
863 if (ret != common::OKAY) {
864 #ifdef DEBUG_DIRECTORY_TREE
865 LOG(astring("common gave us posn of: ") + reassembled);
867 // handle the case for files now that we have our proper node.
868 string_array partial_pieces;
869 filename(reassembled).separate(partial_pieces);
870 int levels_missing = pieces.length() - partial_pieces.length();
872 // we loop over all the pieces that were missing in between the last
873 // common root and the file's final location.
874 for (int i = 0; i < levels_missing; i++) {
875 #ifdef DEBUG_DIRECTORY_TREE
876 LOG(astring("adding intermediate directory: ") + reassembled);
878 filename_tree *new_branch = new filename_tree;
879 new_branch->_depth = last_match->_depth + 1;
880 new_branch->_dirname = filename(reassembled).raw();
881 last_match->attach(new_branch);
882 last_match = new_branch;
883 reassembled += astring("/") + pieces[partial_pieces.length() + i];
884 reassembled = filename(reassembled).raw(); // canonicalize.
888 if (!last_match->_files.find(pieces[pieces.last()])) {
889 #ifdef DEBUG_DIRECTORY_TREE
890 LOG(astring("adding new file ") + pieces[pieces.last()]
891 + " at " + reassembled);
893 file_info *to_add = new file_info(pieces[pieces.last()], 0);
894 to_add->calculate(reassembled, just_size);
895 last_match->_files += to_add;
897 #ifdef DEBUG_DIRECTORY_TREE
898 LOG(astring("not adding existing file ") + pieces[pieces.last()]
899 + " at " + reassembled);
903 // handle the case for directories.
904 #ifdef DEBUG_DIRECTORY_TREE
905 LOG(astring("doing traverse in ") + last_match->_dirname
906 + " to add " + reassembled);
908 traverse(reassembled, "*", *last_match);
909 //hmmm: maybe provide pattern capability instead of assuming all files.
910 calculate(last_match, just_size);
916 outcome directory_tree::remove_path(const astring &zap_item)
918 FUNCDEF("remove_path");
919 // find the common root, if one exists. if not, we're not going to do this.
921 filename_tree *last_match = NIL;
924 outcome ret = find_common_root(zap_item, false, last_match, reassembled,
926 if (!last_match) return common::NOT_FOUND;
927 // if we didn't actually finish iterating to the file, then we're not
928 // whacking anything.
929 if (ret != common::OKAY) {
930 #ifdef DEBUG_DIRECTORY_TREE
931 LOG(astring("got error seeking ") + zap_item + " of "
932 + common::outcome_name(ret));
937 if (comp_index == pieces.last()) {
938 // if the names match fully, then we're talking about a directory.
939 #ifdef DEBUG_DIRECTORY_TREE
940 LOG(astring("found directory match for ") + zap_item);
943 #ifdef DEBUG_DIRECTORY_TREE
944 LOG(astring("may have found file match for ") + zap_item);
946 filename to_seek(pieces[pieces.last()]);
947 if (!last_match->_files.member(to_seek)) {
948 // this file is not a member, so we must say it's not found.
949 #ifdef DEBUG_DIRECTORY_TREE
950 LOG(astring("couldn't find file match in common root for ") + zap_item);
952 return common::NOT_FOUND;
954 int indy = last_match->_files.locate(to_seek);
955 #ifdef DEBUG_DIRECTORY_TREE
956 LOG(astring("found match to remove for ") + zap_item);
958 last_match->_files.zap(indy, indy);
959 return common::OKAY; // done!
963 #ifdef DEBUG_DIRECTORY_TREE
964 LOG(astring("going to whack node at: ") + last_match->_dirname.raw());
967 // we're whacking directories, so we need to take out last_match and below.
968 filename_tree *parent = (filename_tree *)last_match->parent();
969 if (!parent || (last_match == _real_tree)) {
970 // this seems to be matching the whole tree. we disallow that.
971 #ifdef DEBUG_DIRECTORY_TREE
972 LOG("there's a problem whacking this node; it's the root.");
974 return common::BAD_INPUT;
976 #ifdef DEBUG_DIRECTORY_TREE
977 LOG(astring("pruning tree at ") + last_match->_dirname.raw());
979 parent->prune(last_match);