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)
227 _scanned_okay = false;
231 _real_tree = new filename_tree;
233 // check that the top-level is healthy.
234 directory curr(path, "*");
235 if (!curr.good()) return false;
236 // our only exit condition; other directories might not be accessible
237 // underneath, but the top one must be accessible for us to even start
240 traverse(path, pattern, *_real_tree);
241 _scanned_okay = true;;
245 dir_tree_iterator *directory_tree::start_at(filename_tree *start,
246 traversal_types type) const
248 // translate to the lower level traversal enum.
249 tree::traversal_directions dir = tree::prefix;
250 if (type == infix) dir = tree::infix;
251 else if (type == postfix) dir = tree::postfix;
253 return new dir_tree_iterator(start, dir);
256 dir_tree_iterator *directory_tree::start(traversal_types type) const
258 // translate to the lower level traversal enum.
259 tree::traversal_directions dir = tree::prefix;
260 if (type == infix) dir = tree::infix;
261 else if (type == postfix) dir = tree::postfix;
263 return new dir_tree_iterator(_real_tree, dir);
266 bool directory_tree::jump_to(dir_tree_iterator &scanning,
267 const astring &sub_path)
271 filename(sub_path).separate(pieces);
272 for (int i = 0; i < pieces.length(); i++) {
273 filename_tree *curr = dynamic_cast<filename_tree *>(scanning.current());
274 #ifdef DEBUG_DIRECTORY_TREE
275 LOG(astring("at ") + curr->_dirname.raw());
277 string_array sub_pieces = pieces.subarray(i, i);
279 curr_path.join(sub_pieces);
280 curr_path = filename(curr->_dirname.raw() + filename::default_separator()
282 #ifdef DEBUG_DIRECTORY_TREE
283 LOG(astring("made curr path ") + curr_path.raw());
285 if (!curr) return false;
286 bool found_it = false;
287 for (int j = 0; j < curr->branches(); j++) {
288 filename_tree *sub = dynamic_cast<filename_tree *>(curr->branch(j));
289 #ifdef DEBUG_DIRECTORY_TREE
290 LOG(astring("looking at ") + sub->_dirname.raw());
292 if (sub->_dirname.compare_prefix(curr_path)) {
293 // this part matches!
295 #ifdef DEBUG_DIRECTORY_TREE
296 LOG(astring("found at ") + sub->_dirname.raw());
303 #ifdef DEBUG_DIRECTORY_TREE
304 LOG(astring("could not find ") + curr_path.raw());
312 filename_tree *directory_tree::goto_current(dir_tree_iterator &scanning)
314 if (!scanning._current) {
315 // this one hasn't been advanced yet, or it's already over with.
316 scanning._current = (filename_tree *)scanning.next();
318 // now check that we're healthy.
319 if (!scanning._current) return NIL; // all done.
321 // cast the tree to the right type.
322 return dynamic_cast<filename_tree *>(scanning._current);
325 bool directory_tree::current_dir(dir_tree_iterator &scanning,
328 dir_name = astring::empty_string();
329 filename_tree *tof = goto_current(scanning);
330 if (!tof) return false;
331 dir_name = tof->_dirname;
335 bool directory_tree::current(dir_tree_iterator &scanning,
336 filename &dir_name, string_array &to_fill)
338 // clear any existing junk.
339 dir_name = astring::empty_string();
342 filename_tree *tof = goto_current(scanning);
343 if (!tof) return false;
345 // fill in what they wanted.
346 dir_name = tof->_dirname;
347 tof->_files.fill(to_fill);
352 bool directory_tree::current(dir_tree_iterator &scanning,
353 filename &dir_name, filename_list &to_fill)
355 // clear any existing junk.
356 dir_name = astring::empty_string();
359 filename_tree *tof = goto_current(scanning);
360 if (!tof) return false;
362 // fill in what they wanted.
363 dir_name = tof->_dirname;
364 to_fill = tof->_files;
369 filename_list *directory_tree::access(dir_tree_iterator &scanning)
371 filename_tree *tof = goto_current(scanning);
372 if (!tof) return NIL;
376 bool directory_tree::depth(dir_tree_iterator &scanning, int &depth)
378 depth = -1; // invalid as default.
379 filename_tree *tof = goto_current(scanning);
380 if (!tof) return false;
385 bool directory_tree::children(dir_tree_iterator &scanning, int &kids)
387 kids = -1; // invalid as default.
388 filename_tree *tof = goto_current(scanning);
389 if (!tof) return false;
390 kids = tof->branches();
394 bool directory_tree::next(dir_tree_iterator &scanning)
396 scanning._current = (filename_tree *)scanning.next();
397 return !!scanning._current;
400 void directory_tree::throw_out(dir_tree_iterator * &to_whack)
405 filename_tree *directory_tree::seek(const astring &dir_name_in,
406 bool ignore_initial) const
409 array<filename_tree *> examining;
410 // the list of nodes we're currently looking at.
412 #ifdef DEBUG_DIRECTORY_TREE
413 LOG(astring("seeking on root of: ") + *_path);
416 astring dir_name = filename(dir_name_in).raw();
417 // set the search path up to have the proper prefix.
419 dir_name = path() + filename::default_separator()
420 + filename(dir_name_in).raw();
422 #ifdef DEBUG_DIRECTORY_TREE
423 LOG(astring("adding root: ") + _real_tree->_dirname);
425 examining += _real_tree;
427 astring sequel; // holds extra pieces from filename comparisons.
429 // chew on the list of nodes to examine until we run out.
430 while (examining.length()) {
433 // start looking at all the items in the list, even though we might have
434 // to abandon the iteration if we find a match.
435 filename_tree *check = NIL;
436 for (posn = 0; posn < examining.length(); posn++) {
437 check = examining[posn];
438 filename current(check->_dirname);
439 if (!current.is_normal()) {
440 //#ifdef DEBUG_DIRECTORY_TREE
441 LOG(astring("skipping abnormal dir: ") + current);
445 #ifdef DEBUG_DIRECTORY_TREE
446 LOG(astring("looking at ") + current.raw());
448 if (current.compare_prefix(dir_name, sequel)) {
450 #ifdef DEBUG_DIRECTORY_TREE
451 LOG(astring("matched! at ") + current.raw());
455 // whoa! an exact match. we're done now.
456 #ifdef DEBUG_DIRECTORY_TREE
457 LOG(astring("exact match at ") + current.raw() + "! done!!!");
461 #ifdef DEBUG_DIRECTORY_TREE
462 LOG(astring("inexact match because sequel=") + sequel);
468 if (!found) return NIL; // we found nothing comparable.
470 // we found a partial match. that means we should start looking at this
471 // node's children for the exact match.
473 // this is a serious logical error!
474 LOG("serious logical error: tree was not located.");
477 examining.reset(); // clear the existing nodes.
478 for (int i = 0; i < check->branches(); i++)
479 examining += (filename_tree *)check->branch(i);
482 return NIL; // we found nothing that looked like that node.
485 bool directory_tree::calculate(bool just_size)
486 { return calculate(_real_tree, just_size); }
488 bool directory_tree::calculate(filename_tree *start, bool just_size)
490 FUNCDEF("calculate");
491 dir_tree_iterator *ted = start_at(start, directory_tree::postfix);
492 // create our iterator to do a postfix traversal. why postfix? well,
493 // prefix has been used elsewhere and since it doesn't really matter what
494 // order we visit the nodes here, it's good to change up.
496 int depth; // current depth in tree.
497 filename curr; // the current path the iterator is at.
498 filename_list *files; // the filenames held at the iterator.
500 while (directory_tree::current_dir(*ted, curr)) {
501 if (!curr.is_normal()) {
502 //#ifdef DEBUG_DIRECTORY_TREE
503 LOG(astring("skipping abnormal dir: ") + curr);
505 directory_tree::next(*ted);
506 continue; // scary non-simple file type.
508 // we have a good directory to show.
509 #ifdef DEBUG_DIRECTORY_TREE
510 LOG(astring("calcing node ") + curr.raw());
512 files = directory_tree::access(*ted);
513 directory_tree::depth(*ted, depth);
514 for (int i = 0; i < files->elements(); i++) {
515 filename curr_file = curr + "/" + *files->borrow(i);
516 #ifdef DEBUG_DIRECTORY_TREE
517 LOG(astring("got a curr file: ") + curr_file);
519 if (!curr_file.is_normal()) {
520 //#ifdef DEBUG_DIRECTORY_TREE
521 LOG(astring("skipping abnormal file: ") + curr);
525 if (!files->borrow(i)->calculate(curr.raw(), just_size)) {
526 LOG(astring("failure to calculate ") + files->get(i)->text_form());
530 directory_tree::next(*ted);
533 directory_tree::throw_out(ted);
537 bool directory_tree::compare_trees(const directory_tree &source,
538 const directory_tree &target, filename_list &differences,
539 file_info::file_similarity how_to_compare)
541 return compare_trees(source, astring::empty_string(), target,
542 astring::empty_string(), differences, how_to_compare);
545 bool directory_tree::compare_trees(const directory_tree &source,
546 const astring &source_start_in, const directory_tree &target,
547 const astring &target_start_in, filename_list &differences,
548 file_info::file_similarity how_compare)
550 FUNCDEF("compare_trees");
551 differences.reset(); // prepare it for storage.
553 // make sure we get canonical names to work with.
554 filename source_start(source_start_in);
555 filename target_start(target_start_in);
557 dir_tree_iterator *ted = source.start(directory_tree::prefix);
558 // create our iterator to do a prefix traversal.
560 astring real_source_start = source.path();
561 if (source_start.raw().t()) {
562 // move to the right place.
563 real_source_start = real_source_start + filename::default_separator()
564 + source_start.raw();
565 if (!directory_tree::jump_to(*ted, source_start.raw())) {
566 // can't even start comparing.
567 LOG(astring("failed to find source start in tree, given as ")
568 + source_start.raw());
573 filename curr; // the current path the iterator is at.
574 filename_list files; // the filenames held at the iterator.
576 // calculate where our comparison point is on the source.
577 int source_pieces = 0;
580 filename(real_source_start).separate(temp);
581 source_pieces = temp.length();
584 bool seen_zero_pieces = false;
585 while (directory_tree::current(*ted, curr, files)) {
586 // we're in a place in the source tree now. let's compare it with the
587 // target's recollection.
589 #ifdef DEBUG_DIRECTORY_TREE
590 LOG(astring("curr dir in tree: ") + curr.raw());
594 curr.separate(pieces); // get the components of the current location.
595 #ifdef DEBUG_DIRECTORY_TREE
596 LOG(astring("name in pieces:") + pieces.text_form());
598 pieces.zap(0, source_pieces - 1);
599 // snap the root components out of there.
601 filename corresponding_name;
602 corresponding_name.join(pieces);
603 #ifdef DEBUG_DIRECTORY_TREE
604 LOG(astring("computed target name as: ") + corresponding_name);
606 filename original_correspondence(corresponding_name);
608 if (!corresponding_name.raw().t()) {
609 if (seen_zero_pieces) {
610 #ifdef DEBUG_DIRECTORY_TREE
611 LOG(astring("breaking out now due to empty correspondence"));
615 seen_zero_pieces = true;
617 if (target_start.raw().t()) {
618 corresponding_name = filename(target_start.raw()
619 + filename::default_separator() + corresponding_name.raw());
621 #ifdef DEBUG_DIRECTORY_TREE
622 LOG(astring("target with start is: ") + corresponding_name);
625 filename_tree *target_now = target.seek(corresponding_name.raw(), true);
627 // that entire sub-tree is missing. add all of the files here into
629 #ifdef DEBUG_DIRECTORY_TREE
630 LOG(astring("could not find dir in target for ") + curr.raw()
631 + " which we computed corresp as " + corresponding_name.raw());
635 // now scan across all the files that are in our source list.
636 for (int i = 0; i < files.elements(); i++) {
637 if (!target_now // there was no node, so we're adding everything...
638 || !target_now->_files.member_with_state(*files[i], how_compare) ) {
639 // ... or we need to add this file since it's missing.
640 #ifdef DEBUG_DIRECTORY_TREE
641 LOG(astring("adding record: ") + files[i]->text_form());
644 file_info *new_record = new file_info(*files[i]);
645 // record the file time for use later in saving.
646 new_record->calculate(curr, true);
647 astring original = new_record->raw();
648 #ifdef DEBUG_DIRECTORY_TREE
649 LOG(astring("current: ") + new_record->raw());
652 astring actual_name = source_start.raw();
653 #ifdef DEBUG_DIRECTORY_TREE
654 if (actual_name.t()) LOG(astring("sname=") + actual_name);
656 if (actual_name.length()) actual_name += filename::default_separator();
657 actual_name += original_correspondence.raw();
658 if (actual_name.length()) actual_name += filename::default_separator();
659 actual_name += new_record->raw();
660 #ifdef DEBUG_DIRECTORY_TREE
661 if (actual_name.t()) LOG(astring("sname=") + actual_name);
663 (filename &)(*new_record) = filename(actual_name);
665 astring targ_name = corresponding_name.raw();
666 #ifdef DEBUG_DIRECTORY_TREE
667 if (targ_name.t()) LOG(astring("tname=") + targ_name);
669 if (targ_name.length()) targ_name += filename::default_separator();
670 targ_name += original;
671 #ifdef DEBUG_DIRECTORY_TREE
672 if (targ_name.t()) LOG(astring("tname=") + targ_name);
675 new_record->secondary(targ_name);
677 differences += new_record;
678 #ifdef DEBUG_DIRECTORY_TREE
679 LOG(astring("came out as: ") + new_record->text_form());
684 // go to the next place.
685 directory_tree::next(*ted);
688 directory_tree::throw_out(ted);
693 outcome directory_tree::find_common_root(const astring &finding, bool exists,
694 filename_tree * &found, astring &reassembled, string_array &pieces,
697 FUNCDEF("find_common_root");
698 // test the path to find what it is.
699 filename adding(finding);
700 if (exists && !adding.good())
701 return common::BAD_INPUT; // not a good path.
702 int file_subtract = 0; // if it's a file, then we remove last component.
703 if (exists && !adding.is_directory()) file_subtract = 1;
705 // break up the path into pieces.
707 adding.separate(pieces);
709 // break up our root into pieces; we must take off components that are
710 // already in the root.
711 string_array root_pieces;
712 filename temp_file(path());
713 temp_file.separate(root_pieces);
715 // locate the last place where the path we were given touches our tree.
716 // it could be totally new, partially new, or already contained.
717 filename_tree *last_match = _real_tree; // where the common root is located.
718 int list_length = pieces.length() - file_subtract;
721 // we must put all the pieces in that already come from the root.
722 for (int i = 0; i < root_pieces.length() - 1; i++) {
723 bool add_slash = false;
724 if (reassembled.length() && (reassembled[reassembled.end()] != '/') )
726 if (add_slash) reassembled += "/";
727 reassembled += pieces[i];
728 if (reassembled[reassembled.end()] == ':') {
729 #ifdef DEBUG_DIRECTORY_TREE
730 LOG(astring("skipping drive component ") + reassembled);
736 #ifdef DEBUG_DIRECTORY_TREE
737 LOG(astring("after pre-assembly, path is ") + reassembled);
740 outcome to_return = common::NOT_FOUND;
742 for (match_place = root_pieces.length() - 1; match_place < list_length;
744 // add a slash if there's not one present already.
745 bool add_slash = false;
746 if (reassembled.length() && (reassembled[reassembled.end()] != '/') )
748 // add the next component in to our path.
749 if (add_slash) reassembled += "/";
750 reassembled += pieces[match_place];
751 // special case for dos paths.
752 if (reassembled[reassembled.end()] == ':') {
753 #ifdef DEBUG_DIRECTORY_TREE
754 LOG(astring("skipping drive component ") + reassembled);
758 reassembled = filename(reassembled).raw(); // force compliance with OS.
759 #ifdef DEBUG_DIRECTORY_TREE
760 LOG(astring("now seeking ") + reassembled);
762 filename_tree *sought = seek(reassembled, false);
764 #ifdef DEBUG_DIRECTORY_TREE
765 LOG(astring("couldn't find ") + reassembled);
767 if (!exists && (match_place == list_length - 1)) {
768 // see if we can get a match on a file rather than a directory, but
769 // only if we're near the end of the compare.
770 if (last_match->_files.member(pieces[match_place])) {
771 // aha! a file match.
772 to_return = common::OKAY;
780 // record where we last had some success.
781 #ifdef DEBUG_DIRECTORY_TREE
782 LOG(astring("found subtree for ") + reassembled);
787 // this is a success, but our loop structure can put us one past the right
789 if (match_place >= list_length) {
790 match_place = list_length - 1;
791 to_return = common::OKAY;
798 outcome directory_tree::add_path(const astring &new_item, bool just_size)
801 // test the path to find out what it is.
802 filename adding(new_item);
803 if (!adding.good()) {
804 LOG(astring("non-existent new item! ") + new_item);
805 return common::NOT_FOUND; // not an existing path.
807 if (!adding.is_normal()) {
808 //#ifdef DEBUG_DIRECTORY_TREE
809 LOG(astring("abnormal new item: ") + new_item);
811 return common::BAD_INPUT; // not a good path.
813 int file_subtract = 0; // if it's a file, then we remove last component.
814 if (!adding.is_directory()) file_subtract = 1;
815 #ifdef DEBUG_DIRECTORY_TREE
816 if (file_subtract) { LOG(astring("adding a file ") + new_item); }
817 else { LOG(astring("adding a directory ") + new_item); }
820 // find the common root, break up the path into pieces, and tell us where
823 filename_tree *last_match = NIL;
825 astring reassembled; // this will hold the common root.
826 outcome ret = find_common_root(new_item, true, last_match, reassembled,
829 LOG(astring("serious error finding common root for ") + new_item
830 + ", got NIL tree.");
831 return common::FAILURE; // something serious isn't right.
834 if (!file_subtract) {
835 if (ret != common::OKAY) {
836 // if it's a new directory, we add a new node for traverse to work on.
837 #ifdef DEBUG_DIRECTORY_TREE
838 LOG(astring("now adding node for ") + reassembled);
840 filename_tree *new_branch = new filename_tree;
841 new_branch->_depth = last_match->_depth + 1;
842 last_match->attach(new_branch);
843 last_match = new_branch;
845 #ifdef DEBUG_DIRECTORY_TREE
846 LOG(astring("matched properly. reassembled set to ") + reassembled);
852 if (ret != common::OKAY) {
853 #ifdef DEBUG_DIRECTORY_TREE
854 LOG(astring("common gave us posn of: ") + reassembled);
856 // handle the case for files now that we have our proper node.
857 string_array partial_pieces;
858 filename(reassembled).separate(partial_pieces);
859 int levels_missing = pieces.length() - partial_pieces.length();
861 // we loop over all the pieces that were missing in between the last
862 // common root and the file's final location.
863 for (int i = 0; i < levels_missing; i++) {
864 #ifdef DEBUG_DIRECTORY_TREE
865 LOG(astring("adding intermediate directory: ") + reassembled);
867 filename_tree *new_branch = new filename_tree;
868 new_branch->_depth = last_match->_depth + 1;
869 new_branch->_dirname = filename(reassembled).raw();
870 last_match->attach(new_branch);
871 last_match = new_branch;
872 reassembled += astring("/") + pieces[partial_pieces.length() + i];
873 reassembled = filename(reassembled).raw(); // canonicalize.
877 if (!last_match->_files.find(pieces[pieces.last()])) {
878 #ifdef DEBUG_DIRECTORY_TREE
879 LOG(astring("adding new file ") + pieces[pieces.last()]
880 + " at " + reassembled);
882 file_info *to_add = new file_info(pieces[pieces.last()], 0);
883 to_add->calculate(reassembled, just_size);
884 last_match->_files += to_add;
886 #ifdef DEBUG_DIRECTORY_TREE
887 LOG(astring("not adding existing file ") + pieces[pieces.last()]
888 + " at " + reassembled);
892 // handle the case for directories.
893 #ifdef DEBUG_DIRECTORY_TREE
894 LOG(astring("doing traverse in ") + last_match->_dirname
895 + " to add " + reassembled);
897 traverse(reassembled, "*", *last_match);
898 //hmmm: maybe provide pattern capability instead of assuming all files.
899 calculate(last_match, just_size);
905 outcome directory_tree::remove_path(const astring &zap_item)
907 FUNCDEF("remove_path");
908 // find the common root, if one exists. if not, we're not going to do this.
910 filename_tree *last_match = NIL;
913 outcome ret = find_common_root(zap_item, false, last_match, reassembled,
915 if (!last_match) return common::NOT_FOUND;
916 // if we didn't actually finish iterating to the file, then we're not
917 // whacking anything.
918 if (ret != common::OKAY) {
919 #ifdef DEBUG_DIRECTORY_TREE
920 LOG(astring("got error seeking ") + zap_item + " of "
921 + common::outcome_name(ret));
926 if (comp_index == pieces.last()) {
927 // if the names match fully, then we're talking about a directory.
928 #ifdef DEBUG_DIRECTORY_TREE
929 LOG(astring("found directory match for ") + zap_item);
932 #ifdef DEBUG_DIRECTORY_TREE
933 LOG(astring("may have found file match for ") + zap_item);
935 filename to_seek(pieces[pieces.last()]);
936 if (!last_match->_files.member(to_seek)) {
937 // this file is not a member, so we must say it's not found.
938 #ifdef DEBUG_DIRECTORY_TREE
939 LOG(astring("couldn't find file match in common root for ") + zap_item);
941 return common::NOT_FOUND;
943 int indy = last_match->_files.locate(to_seek);
944 #ifdef DEBUG_DIRECTORY_TREE
945 LOG(astring("found match to remove for ") + zap_item);
947 last_match->_files.zap(indy, indy);
948 return common::OKAY; // done!
952 #ifdef DEBUG_DIRECTORY_TREE
953 LOG(astring("going to whack node at: ") + last_match->_dirname.raw());
956 // we're whacking directories, so we need to take out last_match and below.
957 filename_tree *parent = (filename_tree *)last_match->parent();
958 if (!parent || (last_match == _real_tree)) {
959 // this seems to be matching the whole tree. we disallow that.
960 #ifdef DEBUG_DIRECTORY_TREE
961 LOG("there's a problem whacking this node; it's the root.");
963 return common::BAD_INPUT;
965 #ifdef DEBUG_DIRECTORY_TREE
966 LOG(astring("pruning tree at ") + last_match->_dirname.raw());
968 parent->prune(last_match);