~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/cached_directory.cc

  • Committer: Stewart Smith
  • Date: 2011-03-29 01:30:47 UTC
  • mto: (2257.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2258.
  • Revision ID: stewart@flamingspork.com-20110329013047-5ujzfx6pahmwuko2
have CachedDirectory print out a warning if we can't stat() something in a directory. We should always have access to at least stat() things in directories Drizzle is running in (otherwise there is likely a problem)

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 *   Implementation of CachedDirectory class.
26
26
 */
27
27
 
28
 
#include "config.h"
29
 
 
30
 
#include "drizzled/definitions.h"
31
 
 
 
28
#include <config.h>
 
29
#include <dirent.h>
 
30
#include <drizzled/definitions.h>
 
31
 
 
32
#include <boost/foreach.hpp>
32
33
#include <sys/types.h>
33
34
#include <sys/stat.h>
34
35
#include <unistd.h>
36
37
#include <strings.h>
37
38
#include <limits.h>
38
39
 
39
 
#include "drizzled/cached_directory.h"
 
40
#include <drizzled/cached_directory.h>
 
41
#include <drizzled/util/find_ptr.h>
 
42
#include <drizzled/error_t.h>
 
43
#include <drizzled/error.h>
 
44
#include <drizzled/errmsg_print.h>
40
45
 
41
46
using namespace std;
 
47
using namespace drizzled;
42
48
 
43
 
namespace drizzled
44
 
{
 
49
namespace drizzled {
45
50
 
46
51
CachedDirectory::CachedDirectory() : 
47
52
  error(0)
50
55
 
51
56
 
52
57
CachedDirectory::CachedDirectory(const string &in_path) :
53
 
  error(0)
 
58
  error(0),
 
59
  use_full_path(false)
54
60
{
55
61
  // TODO: Toss future exception
56
62
  (void) open(in_path);
58
64
 
59
65
 
60
66
CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
61
 
  error(0)
 
67
  error(0),
 
68
  use_full_path(false)
62
69
{
63
70
  // TODO: Toss future exception
64
71
  (void) open(in_path, allowed_exts);
65
72
}
66
73
 
67
 
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter) :
68
 
  error(0)
 
74
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter, bool use_full_path_arg) :
 
75
  error(0),
 
76
  use_full_path(use_full_path_arg)
69
77
{
70
78
  set<string> empty;
71
79
  // TODO: Toss future exception
75
83
 
76
84
CachedDirectory::~CachedDirectory()
77
85
{
78
 
  for (Entries::iterator p= entries.begin(); p != entries.end(); ++p)
79
 
  {
80
 
    if (*p)
81
 
      delete *p;
82
 
  }
83
 
  entries.clear();
 
86
        BOOST_FOREACH(Entries::reference iter, entries)
 
87
    delete iter;
84
88
}
85
89
 
86
90
bool CachedDirectory::open(const string &in_path)
87
91
{
88
92
  set<string> empty;
89
 
 
90
93
  return open(in_path, empty);
91
94
}
92
95
 
128
131
         result != NULL)
129
132
  {
130
133
    std::string buffered_fullpath;
131
 
    if (! allowed_exts.empty())
 
134
    if (not allowed_exts.empty())
132
135
    {
133
136
      char *ptr= rindex(result->d_name, '.');
134
 
 
135
 
      if (ptr)
 
137
      if (ptr && allowed_exts.count(ptr))
136
138
      {
137
 
        set<string>::iterator it;
138
 
        it= allowed_exts.find(ptr);
139
 
 
140
 
        if (it != allowed_exts.end())
141
 
        {
142
 
          entries.push_back(new Entry(result->d_name));
143
 
        }
 
139
        entries.push_back(new Entry(result->d_name));
144
140
      }
145
141
    }
146
142
    else
154
150
          if (result->d_name[0] == '.') // We don't pass back anything hidden at the moment.
155
151
            continue;
156
152
 
157
 
          buffered_fullpath.append(in_path);
158
 
          if (buffered_fullpath[buffered_fullpath.length()] != '/')
159
 
            buffered_fullpath.append(1, FN_LIBCHAR);
160
 
 
161
 
          buffered_fullpath.assign(result->d_name);
162
 
 
163
 
          stat(buffered_fullpath.c_str(), &entrystat);
164
 
 
165
 
          if (S_ISDIR(entrystat.st_mode))
 
153
          if (use_full_path)
 
154
          {
 
155
            buffered_fullpath.append(in_path);
 
156
            if (buffered_fullpath[buffered_fullpath.length()] != '/')
 
157
              buffered_fullpath.append(1, FN_LIBCHAR);
 
158
          }
 
159
 
 
160
          buffered_fullpath.append(result->d_name);
 
161
 
 
162
          int err= stat(buffered_fullpath.c_str(), &entrystat);
 
163
 
 
164
          if (err != 0)
 
165
            errmsg_printf(error::WARN, ER(ER_CANT_GET_STAT),
 
166
                          buffered_fullpath.c_str(),
 
167
                          errno);
 
168
 
 
169
          if (err == 0 && S_ISDIR(entrystat.st_mode))
166
170
          {
167
171
            entries.push_back(new Entry(result->d_name));
168
172
          }
200
204
  return error == 0;
201
205
}
202
206
 
 
207
std::ostream& operator<<(std::ostream& output, const CachedDirectory &directory)
 
208
{
 
209
  output << "CachedDirectory:(Path: " << directory.getPath() << ")\n";
 
210
  BOOST_FOREACH(const CachedDirectory::Entry* iter, directory.getEntries())
 
211
    output << "\t(" << iter->filename << ")\n";
 
212
  return output;
 
213
}
 
214
 
203
215
} /* namespace drizzled */