/** * FileSystem * * if path exists, initialize FileSystem class * * @param path root node to analyze */ FileSystem::FileSystem(string path) { // Display debug messages debug = true; // Create a boost path object boost::filesystem::path p(path); // Counts number of duplicate directories discovered this->dupDirCount = 0; try { // does path exist if( exists(p) ) { // is path a directory if( is_directory(p) ) { try { DirInfo* node = new DirInfo( p.filename().string(), p.parent_path().string() ); node->isDir(true); debugPrint("root path = "+node->getPath()); // Keeps track of node to deconstruct pointer allNodes.push_back(node); // All directories list (for quick dir operations) allDirs.push_back(node); // Spefiy filesystem root and path root = node; path = node->getPath(); //TODO Might remove... depth = 0; } catch (bad_alloc&) { throw "EXPN_BAD_NODE"; } } else { cout << "Error: Not a valid directory"; throw "EXPN_INVALID_ROOT"; } } else { cout << "Error: " << p << " does not exist." << endl; throw "EXPN_PATH_DNE"; } } catch ( const boost::filesystem::filesystem_error& ex ) { cout << ex.what() << endl; } }
/** * scan * * starts from the given node and iterates through all * child directories and builds vectors of all file * and directory nodes in the FS * * @param pnode pointer to DirInfo node to scan */ bool FileSystem::scan(DirInfo* pnode) { debugPrint("Scanning Node: " + pnode->getPath()); // Check if given path exists if ( !boost::filesystem::exists( pnode->getPath() ) ) { cout << pnode->getPath() << " doesn't exist" << endl; return false; } if (pnode->isHidden()) { return false; } // Create a boost directory iterator boost::filesystem::directory_iterator end_itr; try { for ( boost::filesystem::directory_iterator itr( pnode->getPath() ); itr != end_itr; ++itr ) { // Is iterated object a directory if ( is_directory(itr->status()) ) { DirInfo* dir = new DirInfo(itr->path().filename().string(), pnode); // Keeps track of node to deconstruct pointer allNodes.push_back(dir); // All directories list (for quick dir operations) allDirs.push_back(dir); // Add this node to it's parent children list pnode->addChild(dir); // Create a boost path object to extract fs info boost::filesystem::path p(dir->getPath()); dir->setModifyTime( last_write_time(p) ); dir->isDir(true); // Recursively scan dirs scan(dir); // directory size is only available after all dir and files add their // sizes to their parent dirs to be added here pnode->addSize(dir->getSize()); // If iterated object is not directory, then it must be a file } else { FileInfo* file = new FileInfo(itr->path().filename().string(), pnode); // Keeps track of node to deconstruct pointer allNodes.push_back(file); // All files list (for quick file operations) allFiles.push_back(file); // Add this node to it's parent children list pnode->addChild(file); // Create a boost path object to extract fs info boost::filesystem::path p(file->getPath()); try { file->setSize( file_size(p) ); } catch (boost::filesystem::filesystem_error err) { file->setSize( 0 ); } try { file->setModifyTime( last_write_time(p) ); } catch (boost::filesystem::filesystem_error err) { file->setModifyTime( 0 ); } // Update parent size file->getParent()->addSize(file->getSize()); } } } catch (boost::filesystem::filesystem_error) { cout << "Cannot open file: " << pnode->getPath() << ". Permission denied!" << endl; } return true; }