source: trunk/lib/Directory.cc @ 22

Last change on this file since 22 was 22, checked in by Jari Häkkinen, 18 years ago

Fixed check of symbolic links by checking if a node is an explicit directory.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1// $Id: Directory.cc 22 2006-01-02 00:44:17Z jari $
2
3#include "Directory.h"
4#include "File.h"
5#include "Node.h"
6
7#include <algorithm>
8#include <iostream>
9#include <iterator>
10#include <list>
11
12#include <errno.h>  // Needed to check error state below.
13#include <dirent.h>
14#include <sys/stat.h>
15
16namespace theplu{
17namespace svnstat{
18
19  Directory::Directory(const std::string& path)
20    : Node(path)
21  {
22    using namespace std;
23    DIR* directory=opendir(path.c_str());    // C API from dirent.h
24    if (!directory) {
25      // Jari, throw exception
26      cerr << "ERROR: opendir() failed; terminating" << endl;
27      exit(1);
28    }
29    list<string> entries;
30    struct dirent* entry;
31    errno=0;  // Global variable used by C to track errors, from errno.h
32    while ((entry=readdir(directory)))       // C API from dirent.h
33      entries.push_back(string(entry->d_name));
34    if (errno) {
35      // Jari, throw exception
36      cerr << "ERROR: readdir() failure; terminating" << endl;
37      exit(1);
38    }
39    closedir(directory);
40
41    // Jari, change this to some STL algo?
42    for (list<string>::iterator i=entries.begin(); i!=entries.end(); i++)
43      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
44        string fullpath(path_+'/'+(*i));
45        struct stat nodestat;                // C api from sys/stat.h
46        lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
47        if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
48          daughters_.push_back(new Directory(fullpath));
49        else
50          daughters_.push_back(new File(fullpath));
51      }
52  }
53
54
55  Directory::~Directory(void)
56  {
57    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
58      delete *i;
59  }
60
61
62  const Stats& Directory::parse(void)
63  {
64    stats_.reset();
65
66    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); i++)
67      stats_ += (*i)->parse();
68    return stats_;
69  }
70
71
72  void Directory::print(void) {
73    // Jari, temporary using this to print directory tree
74    for_each(daughters_.begin(),daughters_.end(), std::mem_fun(&Node::print));
75  }
76
77
78  void Directory::purge(void) {
79    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); )
80      if (!((*i)->subversion_controlled())) {
81        delete *i;
82        i=daughters_.erase(i);
83      }
84      else {
85        (*i)->purge();
86        i++;
87      }
88  }
89
90}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.