Guest User

Untitled

a guest
Jun 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<boost/thread/thread.hpp>
  5. #include<boost/filesystem.hpp>
  6. #include<boost/tokenizer.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. #include "subprocess.h"
  9. #include "Command.h"
  10. namespace fs = boost::filesystem;
  11.  
  12. /*
  13. * Traverse directory till maxdepth
  14. * return path
  15. */
  16. std::vector<std::string> traverse_dir() {
  17. fs::path p("path");
  18. int maxdepth = 3;
  19. std::vector<std::string> traverse_dir;
  20. fs::recursive_directory_iterator end_iter;
  21. boost::system::error_code dir_error;
  22. for(fs::recursive_directory_iterator begin(p, dir_error); begin != end_iter; ++begin) {
  23. try {
  24. if(dir_error.value()) {
  25. std::cout<<"The directory has error"<<dir_error.message()<<std::endl;
  26. continue;
  27. }
  28. if(fs::is_symlink(begin->path())) {
  29. begin.no_push();
  30. }
  31. if(fs::is_directory(begin->status())){
  32. if(begin.level()== maxdepth)
  33. begin.no_push();
  34. traverse_dir.push_back(begin->path().string());
  35. }
  36. }catch(fs::filesystem_error &ex) {
  37. std::cout<<"Boost Filesystem Error"<< ex.what()<<std::endl;
  38. }
  39. catch(const std::exception &ex){
  40. std::cout<<"Standard Exception"<<ex.what()<<std::endl;
  41. }
  42. }
  43. return traverse_dir;
  44. }
  45.  
  46. struct Dirinfo {
  47. uint64_t dir_size;
  48. std::string owner;
  49. std::string dir_name;
  50. std::string dir_path;
  51. std::string f_time;
  52. std::vector<Dirinfo*> list;
  53. Dirinfo *next;
  54. };
  55.  
  56. class Dirmapping {
  57. private:
  58. Dirinfo *root = nullptr;
  59. std::vector<std::string> dirs;
  60. public:
  61. Dirmapping() = default;
  62. Dirmapping(const std::vector<std::string> &dir_name);
  63. void createmap();
  64. void printchild(Dirinfo *child, std::ofstream &outfile, std::string &line);
  65. void printmap(Dirinfo *root);
  66. };
  67.  
  68. Dirmapping::Dirmapping(const std::vector<std::string> &dirs): dirs(dirs) {}
  69.  
  70.  
  71. void Dirmapping::createmap() {
  72. for(auto it = this->dirs.begin(); it != dirs.end(); ++it) {
  73. std::cout<<"processing path "<<*it<<std::endl;
  74. fs::path insert_path(*it);
  75. Dirinfo *dirinfo = new Dirinfo;
  76. dirinfo->dir_path = *it;
  77. dirinfo->dir_name = insert_path.filename();
  78. dirinfo->f_time = std::to_string(fs::last_write_time(insert_path));
  79. dirinfo->next = nullptr;
  80. dirinfo->owner = "";
  81. dirinfo->dir_size = 0;
  82. if (root == nullptr) {
  83. root = dirinfo;
  84. }
  85. else {
  86. Dirinfo *temp = root;
  87. bool found_root = false;
  88. bool found_child_root = false;
  89. while(temp != nullptr) {
  90. fs::path p(temp->dir_path);
  91. fs::path compare_path(*it);
  92. if(fs::equivalent(compare_path.parent_path(), fs::path(temp->dir_path))){
  93. found_root = true;
  94. break;
  95. }
  96. for(auto it_child = temp->list.begin(); it_child != temp->list.end(); ++it_child){
  97. std::string compare_path_string = compare_path.parent_path().string();
  98. if(compare_path_string.compare((*it_child)->dir_path)){
  99. (*it_child)->list.push_back(dirinfo);
  100. found_child_root = true;
  101. std::cout<<"found child node"<<std::endl;
  102. break;
  103. }
  104. compare_path = compare_path.parent_path();
  105. }
  106.  
  107. if(found_root || found_child_root)
  108. break;
  109. if(temp->next == nullptr)
  110. break;
  111. temp= temp->next;
  112. }
  113. if(found_root){
  114. temp->list.push_back(dirinfo);
  115. }
  116. else if(!found_child_root){
  117. temp->next = dirinfo;
  118. }
  119. }
  120. }
  121. printmap(root);
  122. }
  123.  
  124. void Dirmapping::printchild(Dirinfo *temp, std::ofstream &outfile, std::string& line) {
  125. outfile<<temp->dir_path<<" "<<"n";
  126. for(std::vector<Dirinfo*>::iterator it = temp->list.begin(); it != temp->list.end(); ++it){
  127. outfile<<line<<(*it)->dir_path<<"n";
  128. if((*it)->list.size())
  129. line += "====";
  130. printchild(*it, outfile, line);
  131. line = "";
  132. }
  133. }
  134.  
  135. void Dirmapping::printmap(Dirinfo *root) {
  136. Dirinfo *temp = root;
  137. std::ofstream outfile("dir_structure");
  138. std::string line = "=";
  139. while(temp != nullptr){
  140. uint64_t size = 0;
  141. outfile<<"Root:"<<temp->dir_path<<"nn";
  142. if(temp->list.size()){
  143. printchild(temp, outfile, line);
  144. }
  145. temp = temp->next;
  146. }
  147. outfile.close();
  148. }
  149.  
  150. int main() {
  151. std::vector<std::string> dir_names = traverse_dir();
  152. Dirmapping dirmap(dir_names);
  153. dirmap.createmap();
  154. return 0;
  155. }
Add Comment
Please, Sign In to add comment